idmgr helper fns

This commit is contained in:
ModulatingForce 2024-02-18 19:23:50 -05:00
parent cbaad60557
commit b41c34c392

View file

@ -1104,6 +1104,43 @@ impl IdentityManager {
} }
} }
async fn add_role(&self, trgchatter:String,trg_role:UserRole)
{
let mut srulock = self.special_roles_users.write().await;
let mut usrrolelock = srulock
.get_mut(&trgchatter)
.expect("Error retrieving roles")
.write().await;
usrrolelock.push(trg_role);
}
async fn remove_role(&self, trgchatter:String,trg_role:UserRole)
{
let mut srulock = self.special_roles_users.write().await;
let mut usrrolelock = srulock
.get_mut(&trgchatter)
.expect("Error retrieving roles")
.write().await;
if let Some(indx) = usrrolelock.iter().position(|value| *value == trg_role){
usrrolelock.swap_remove(indx);
//return ChangeResult::Success("Demoted successfully".to_string())
}
}
async fn affirm_chatter_in_db(&self, trgchatter:String)
{
let mut srulock = self.special_roles_users.write().await;
srulock.entry(trgchatter.clone()).or_insert(Arc::new(RwLock::new(vec![])));
botinstance::botlog::trace(&format!("Ensuring User in Roles {:?}",srulock.entry(trgchatter.clone())),
Some("IdentityManager > affirm_chatter_in_db()".to_string()), None);
Log::flush();
}
// [ ] Maybe I should create a can_user_run version that simply takes PrvMsg, but then calls can_user_run directly // [ ] Maybe I should create a can_user_run version that simply takes PrvMsg, but then calls can_user_run directly
// pub fn can_user_run_PRVMSG(self,msg:&PrivmsgMessage,cmdreqroles:Vec<UserRole>) -> Result<Permissible,Box<dyn Error>> // pub fn can_user_run_PRVMSG(self,msg:&PrivmsgMessage,cmdreqroles:Vec<UserRole>) -> Result<Permissible,Box<dyn Error>>
@ -1298,15 +1335,16 @@ impl IdentityManager {
botinstance::botlog::trace("Read lock on : Special_Roles_User", botinstance::botlog::trace("Read lock on : Special_Roles_User",
Some("identity.rs > can_user_run()".to_string()), None); Some("identity.rs > can_user_run()".to_string()), None);
{ // {
// If target user doesn't exist in special_roles_users , add with blank vector roles // // If target user doesn't exist in special_roles_users , add with blank vector roles
let mut srulock = self.special_roles_users.write().await; // let mut srulock = self.special_roles_users.write().await;
srulock.entry(usr.clone()).or_insert(Arc::new(RwLock::new(vec![]))); // srulock.entry(usr.clone()).or_insert(Arc::new(RwLock::new(vec![])));
botinstance::botlog::trace(&format!("Ensuring Chatter in Roles {:?}",srulock.entry(usr.clone())), // botinstance::botlog::trace(&format!("Ensuring Chatter in Roles {:?}",srulock.entry(usr.clone())),
Some("identity.rs > promote()".to_string()), None); // Some("identity.rs > promote()".to_string()), None);
Log::flush(); // Log::flush();
} // }
self.affirm_chatter_in_db(usr.clone()).await;
let mut roleslock = roleslock.write().await; let mut roleslock = roleslock.write().await;
match (*roleslock).get(&usr.to_lowercase()) { match (*roleslock).get(&usr.to_lowercase()) {
@ -1482,15 +1520,18 @@ impl IdentityManager {
// (*authusrroles_mut).push(UserRole::Mod(channel.clone())); // (*authusrroles_mut).push(UserRole::Mod(channel.clone()));
authusrroles.push(UserRole::Mod(channel.clone())); authusrroles.push(UserRole::Mod(channel.clone()));
let mut srulock = self.special_roles_users.write().await; // let mut srulock = self.special_roles_users.write().await;
srulock // srulock
.get_mut(&trgchatter) // .get_mut(&trgchatter)
.expect("Error getting roles") // .expect("Error getting roles")
// !! [ ] Unsure what happens if promoting a chatter that doesn't exist at // // !! [ ] Unsure what happens if promoting a chatter that doesn't exist at
.write().await // .write().await
.push(UserRole::Mod(channel.clone())); // .push(UserRole::Mod(channel.clone()));
self.add_role(trgchatter.clone(), UserRole::Mod(channel.clone())).await;
} }
_ => (), _ => (),
} }
} // mut block } // mut block
@ -1531,28 +1572,31 @@ impl IdentityManager {
return ChangeResult::NoChange("Already has the role".to_string()); return ChangeResult::NoChange("Already has the role".to_string());
} else { } else {
{ // {
let mut srulock = self.special_roles_users.write().await; // let mut srulock = self.special_roles_users.write().await;
srulock.entry(trgchatter.clone()).or_insert(Arc::new(RwLock::new(vec![]))); // srulock.entry(trgchatter.clone()).or_insert(Arc::new(RwLock::new(vec![])));
botinstance::botlog::trace(&format!("Ensuring Target Chatter in Roles > {:?}",srulock.entry(trgchatter.clone())), // botinstance::botlog::trace(&format!("Ensuring Target Chatter in Roles > {:?}",srulock.entry(trgchatter.clone())),
Some("identity.rs > promote()".to_string()), None); // Some("identity.rs > promote()".to_string()), None);
Log::flush(); // Log::flush();
} // }
self.affirm_chatter_in_db(trgchatter.clone()).await;
{ // {
let mut srulock = self.special_roles_users.write().await; // let mut srulock = self.special_roles_users.write().await;
// srulock
// .get_mut(&trgchatter)
// .expect("Error getting roles for the user")
// .write().await
// .push(UserRole::BotAdmin); // <-- Adds the specific role
// botinstance::botlog::trace(&format!("Inserting Role > {:?}",srulock.entry(trgchatter.clone())),
// Some("identity.rs > promote()".to_string()), None);
// Log::flush();
// }
self.add_role(trgchatter.clone(), UserRole::BotAdmin).await;
srulock
.get_mut(&trgchatter)
.expect("Error getting roles for the user")
.write().await
.push(UserRole::BotAdmin); // <-- Adds the specific role
botinstance::botlog::trace(&format!("Inserting Role > {:?}",srulock.entry(trgchatter.clone())),
Some("identity.rs > promote()".to_string()), None);
Log::flush();
}
return ChangeResult::Success("Promotion Successful".to_string()); return ChangeResult::Success("Promotion Successful".to_string());
@ -1598,27 +1642,30 @@ impl IdentityManager {
|| authusrroles.contains(&UserRole::BotAdmin) || authusrroles.contains(&UserRole::BotAdmin)
{ {
{ // {
// If target user doesn't exist in special_roles_users , add with blank vector roles // // If target user doesn't exist in special_roles_users , add with blank vector roles
let mut srulock = self.special_roles_users.write().await; // let mut srulock = self.special_roles_users.write().await;
srulock.entry(trgchatter.clone()).or_insert(Arc::new(RwLock::new(vec![]))); // srulock.entry(trgchatter.clone()).or_insert(Arc::new(RwLock::new(vec![])));
botinstance::botlog::trace(&format!("Ensuring Chatter in Roles {:?}",srulock.entry(trgchatter.clone())), // botinstance::botlog::trace(&format!("Ensuring Chatter in Roles {:?}",srulock.entry(trgchatter.clone())),
Some("identity.rs > promote()".to_string()), None); // Some("identity.rs > promote()".to_string()), None);
Log::flush(); // Log::flush();
} // }
{ self.affirm_chatter_in_db(trgchatter.clone()).await;
// promote target after
let mut srulock = self.special_roles_users.write().await; // {
srulock // // promote target after
.get_mut(&trgchatter) // let mut srulock = self.special_roles_users.write().await;
.expect("Error getting roles") // srulock
.write().await // .get_mut(&trgchatter)
.push(UserRole::Mod(trg_chnl.clone())); // Role to Add // .expect("Error getting roles")
botinstance::botlog::trace(&format!("Adding Roles to Chatter {:?}",srulock.entry(trgchatter.clone())), // .write().await
Some("identity.rs > promote()".to_string()), None); // .push(UserRole::Mod(trg_chnl.clone())); // Role to Add
Log::flush(); // botinstance::botlog::trace(&format!("Adding Roles to Chatter {:?}",srulock.entry(trgchatter.clone())),
} // Some("identity.rs > promote()".to_string()), None);
// Log::flush();
// }
self.add_role(trgchatter.clone(), UserRole::Mod(trg_chnl.clone())).await;
return ChangeResult::Success(String::from("Promotion Successful")); return ChangeResult::Success(String::from("Promotion Successful"));
@ -1645,39 +1692,47 @@ impl IdentityManager {
|| authusrroles.contains(&UserRole::BotAdmin) || authusrroles.contains(&UserRole::BotAdmin)
{ {
{ // Inserts user if doesn't exist // { // Inserts user if doesn't exist
let mut srulock = self.special_roles_users.write().await; // let mut srulock = self.special_roles_users.write().await;
srulock.entry(trgchatter.clone()).or_insert(Arc::new(RwLock::new(vec![]))); // srulock.entry(trgchatter.clone()).or_insert(Arc::new(RwLock::new(vec![])));
botinstance::botlog::trace(&format!("Ensuring User in Roles {:?}",srulock.entry(trgchatter.clone())), // botinstance::botlog::trace(&format!("Ensuring User in Roles {:?}",srulock.entry(trgchatter.clone())),
Some("identity.rs > promote()".to_string()), None); // Some("identity.rs > promote()".to_string()), None);
Log::flush(); // Log::flush();
} // }
{ // Adds the requested role for the user self.affirm_chatter_in_db(trgchatter.clone()).await;
let mut srulock = self.special_roles_users.write().await;
srulock
.get_mut(&trgchatter)
.expect("Error getting roles")
// !! [ ] Unsure what happens if promoting a chatter that doesn't exist at
.write().await
.push(UserRole::SupMod(trg_chnl.clone()));
botinstance::botlog::trace(&format!("Adding Required Role > {:?}",srulock.entry(trgchatter.clone())),
Some("identity.rs > promote()".to_string()), None);
Log::flush();
}
{ // Removes the lower role (mod) from the user
let mut srulock = self.special_roles_users.write().await;
let mut uroleslock = srulock
.get_mut(&trgchatter)
.expect("Error getting roles")
.write().await;
if let Some(indx) = uroleslock.iter().position(|value| *value == UserRole::Mod(trg_chnl.clone())){
uroleslock.swap_remove(indx);
}
botinstance::botlog::trace(&format!("Removing lower role > {:?}",uroleslock), // { // Adds the requested role for the user
Some("identity.rs > promote()".to_string()), None); // let mut srulock = self.special_roles_users.write().await;
Log::flush(); // srulock
} // .get_mut(&trgchatter)
// .expect("Error getting roles")
// // !! [ ] Unsure what happens if promoting a chatter that doesn't exist at
// .write().await
// .push(UserRole::SupMod(trg_chnl.clone()));
// botinstance::botlog::trace(&format!("Adding Required Role > {:?}",srulock.entry(trgchatter.clone())),
// Some("identity.rs > promote()".to_string()), None);
// Log::flush();
// }
self.add_role(trgchatter.clone(), UserRole::SupMod(trg_chnl.clone())).await;
// { // Removes the lower role (mod) from the user
// let mut srulock = self.special_roles_users.write().await;
// let mut uroleslock = srulock
// .get_mut(&trgchatter)
// .expect("Error getting roles")
// .write().await;
// if let Some(indx) = uroleslock.iter().position(|value| *value == UserRole::Mod(trg_chnl.clone())){
// uroleslock.swap_remove(indx);
// }
// botinstance::botlog::trace(&format!("Removing lower role > {:?}",uroleslock),
// Some("identity.rs > promote()".to_string()), None);
// Log::flush();
// }
self.remove_role(trgchatter, UserRole::Mod(trg_chnl.clone())).await;
return ChangeResult::Success(String::from("Promotion Successful")); return ChangeResult::Success(String::from("Promotion Successful"));
@ -2097,13 +2152,16 @@ impl IdentityManager {
// (*authusrroles_mut).push(UserRole::Mod(channel.clone())); // (*authusrroles_mut).push(UserRole::Mod(channel.clone()));
authusrroles.push(UserRole::Mod(channel.clone())); authusrroles.push(UserRole::Mod(channel.clone()));
let mut srulock = self.special_roles_users.write().await; // [ ] below pushes mod to authorizer
srulock // let mut srulock = self.special_roles_users.write().await;
.get_mut(&trgchatter) // srulock
.expect("Error getting roles") // .get_mut(&trgchatter)
// !! [ ] Unsure what happens if promoting a chatter that doesn't exist at // .expect("Error getting roles")
.write().await // // !! [ ] Unsure what happens if promoting a chatter that doesn't exist at
.push(UserRole::Mod(channel.clone())); // .write().await
// .push(UserRole::Mod(channel.clone()));
self.add_role(authorizer.clone(), UserRole::Mod(channel.clone())).await;
} }
_ => (), _ => (),
@ -2129,34 +2187,45 @@ impl IdentityManager {
authusrroles.contains(&UserRole::SupMod(channel.clone())) ) && authusrroles.contains(&UserRole::SupMod(channel.clone())) ) &&
trgusrroles.contains(&UserRole::Mod(channel.clone())) trgusrroles.contains(&UserRole::Mod(channel.clone()))
{ {
let mut srulock = self.special_roles_users.write().await; // // [ ] Below removes Mod from trgchatter
let mut usrrolelock = srulock // let mut srulock = self.special_roles_users.write().await;
.get_mut(&trgchatter) // let mut usrrolelock = srulock
.expect("Error getting roles") // .get_mut(&trgchatter)
.write().await; // .expect("Error getting roles")
if let Some(indx) = usrrolelock.iter().position(|value| *value == UserRole::Mod(channel.clone())){ // .write().await;
usrrolelock.swap_remove(indx); // if let Some(indx) = usrrolelock.iter().position(|value| *value == UserRole::Mod(channel.clone())){
// usrrolelock.swap_remove(indx);
// return ChangeResult::Success("Demoted successfully".to_string())
// }
self.remove_role(trgchatter.clone(), UserRole::Mod(channel.clone())).await;
return ChangeResult::Success("Demoted successfully".to_string()) return ChangeResult::Success("Demoted successfully".to_string())
} }
}
// [x] 4b. Authorizers who are BotAdmin, Broadcaster can demote a SupMod // [x] 4b. Authorizers who are BotAdmin, Broadcaster can demote a SupMod
else if ( authusrroles.contains(&UserRole::BotAdmin) || else if ( authusrroles.contains(&UserRole::BotAdmin) ||
authusrroles.contains(&UserRole::Broadcaster) ) && authusrroles.contains(&UserRole::Broadcaster) ) &&
trgusrroles.contains(&UserRole::SupMod(channel.clone())) trgusrroles.contains(&UserRole::SupMod(channel.clone()))
{ {
let mut srulock = self.special_roles_users.write().await; // [ ] For Trgchatter, below pushes Mod UserRole and removes SupMod
let mut usrrolelock = srulock // let mut srulock = self.special_roles_users.write().await;
.get_mut(&trgchatter) // let mut usrrolelock = srulock
.expect("Error getting roles") // .get_mut(&trgchatter)
.write().await; // .expect("Error getting roles")
usrrolelock.push(UserRole::Mod(channel.clone())); // pushes Mod , and removes SupMod // .write().await;
if let Some(indx) = usrrolelock.iter().position(|value| *value == UserRole::SupMod(channel.clone())){ // usrrolelock.push(UserRole::Mod(channel.clone())); // pushes Mod , and removes SupMod
usrrolelock.swap_remove(indx); // if let Some(indx) = usrrolelock.iter().position(|value| *value == UserRole::SupMod(channel.clone())){
return ChangeResult::Success("Demoted successfully".to_string()) // usrrolelock.swap_remove(indx);
} // return ChangeResult::Success("Demoted successfully".to_string())
// }
self.add_role(trgchatter.clone(), UserRole::Mod(channel.clone())).await;
self.remove_role(trgchatter.clone(), UserRole::SupMod(channel.clone())).await;
return ChangeResult::Success("Demoted successfully".to_string());
} }
// [x] 4c. When Target chatter isnt a Mod or SupMod to demote // [x] 4c. When Target chatter isnt a Mod or SupMod to demote
else if !trgusrroles.contains(&UserRole::Mod(channel.clone())) && else if !trgusrroles.contains(&UserRole::Mod(channel.clone())) &&
!trgusrroles.contains(&UserRole::SupMod(channel.clone())) { !trgusrroles.contains(&UserRole::SupMod(channel.clone())) {