@@ -115,17 +115,17 @@ pub async fn post_user(
115115 if user_roles. is_empty ( ) {
116116 return Err ( RBACError :: RoleValidationError ) ;
117117 } else {
118- let mut non_existant_roles = Vec :: new ( ) ;
118+ let mut non_existent_roles = Vec :: new ( ) ;
119119 user_roles
120120 . iter ( )
121121 . map ( |r| {
122122 if !roles ( ) . contains_key ( r) {
123- non_existant_roles . push ( r. clone ( ) ) ;
123+ non_existent_roles . push ( r. clone ( ) ) ;
124124 }
125125 } )
126126 . for_each ( drop) ;
127- if !non_existant_roles . is_empty ( ) {
128- return Err ( RBACError :: RolesDoNotExist ( non_existant_roles ) ) ;
127+ if !non_existent_roles . is_empty ( ) {
128+ return Err ( RBACError :: RolesDoNotExist ( non_existent_roles ) ) ;
129129 }
130130 }
131131 let _ = UPDATE_LOCK . lock ( ) . await ;
@@ -237,24 +237,23 @@ pub async fn delete_user(username: web::Path<String>) -> Result<impl Responder,
237237 let mut metadata = get_metadata ( ) . await ?;
238238 metadata. users . retain ( |user| user. username ( ) != username) ;
239239
240- // also delete from user groups
240+ // Remove user from all groups
241241 let user_groups = Users . get_user_groups ( & username) ;
242- let mut groups_to_update = Vec :: new ( ) ;
243- for user_group in user_groups {
244- if let Some ( ug) = write_user_groups ( ) . get_mut ( & user_group) {
245- ug. remove_users ( HashSet :: from_iter ( [ username. clone ( ) ] ) ) ?;
246- groups_to_update. push ( ug. clone ( ) ) ;
247- // ug.update_in_metadata().await?;
248- } else {
249- continue ;
250- } ;
242+ {
243+ let mut groups = write_user_groups ( ) ;
244+ for group_name in & user_groups {
245+ if let Some ( group) = groups. get_mut ( group_name) {
246+ group. remove_users ( HashSet :: from_iter ( [ username. clone ( ) ] ) ) ?;
247+ }
248+ }
251249 }
252250
253- // update in metadata user groups
254- metadata
255- . user_groups
256- . retain ( |x| !groups_to_update. contains ( x) ) ;
257- metadata. user_groups . extend ( groups_to_update) ;
251+ // Update metadata with modified groups
252+ for group in metadata. user_groups . iter_mut ( ) {
253+ if user_groups. contains ( & group. name ) {
254+ group. users . retain ( |u| u != & username) ;
255+ }
256+ }
258257 put_metadata ( & metadata) . await ?;
259258
260259 // update in mem table
@@ -274,17 +273,17 @@ pub async fn add_roles_to_user(
274273 return Err ( RBACError :: UserDoesNotExist ) ;
275274 } ;
276275
277- let mut non_existant_roles = Vec :: new ( ) ;
276+ let mut non_existent_roles = Vec :: new ( ) ;
278277
279278 // check if the role exists
280279 roles_to_add. iter ( ) . for_each ( |r| {
281280 if roles ( ) . get ( r) . is_none ( ) {
282- non_existant_roles . push ( r. clone ( ) ) ;
281+ non_existent_roles . push ( r. clone ( ) ) ;
283282 }
284283 } ) ;
285284
286- if !non_existant_roles . is_empty ( ) {
287- return Err ( RBACError :: RolesDoNotExist ( non_existant_roles ) ) ;
285+ if !non_existent_roles . is_empty ( ) {
286+ return Err ( RBACError :: RolesDoNotExist ( non_existent_roles ) ) ;
288287 }
289288
290289 // update parseable.json first
@@ -319,17 +318,17 @@ pub async fn remove_roles_from_user(
319318 return Err ( RBACError :: UserDoesNotExist ) ;
320319 } ;
321320
322- let mut non_existant_roles = Vec :: new ( ) ;
321+ let mut non_existent_roles = Vec :: new ( ) ;
323322
324323 // check if the role exists
325324 roles_to_remove. iter ( ) . for_each ( |r| {
326325 if roles ( ) . get ( r) . is_none ( ) {
327- non_existant_roles . push ( r. clone ( ) ) ;
326+ non_existent_roles . push ( r. clone ( ) ) ;
328327 }
329328 } ) ;
330329
331- if !non_existant_roles . is_empty ( ) {
332- return Err ( RBACError :: RolesDoNotExist ( non_existant_roles ) ) ;
330+ if !non_existent_roles . is_empty ( ) {
331+ return Err ( RBACError :: RolesDoNotExist ( non_existent_roles ) ) ;
333332 }
334333
335334 // check for role not present with user
@@ -368,31 +367,13 @@ pub async fn remove_roles_from_user(
368367#[ serde( rename = "camelCase" ) ]
369368pub struct InvalidUserGroupError {
370369 pub valid_name : bool ,
371- pub non_existant_roles : Vec < String > ,
372- pub non_existant_users : Vec < String > ,
370+ pub non_existent_roles : Vec < String > ,
371+ pub non_existent_users : Vec < String > ,
373372 pub roles_not_in_group : Vec < String > ,
374373 pub users_not_in_group : Vec < String > ,
375374 pub comments : String ,
376375}
377376
378- // impl Display for InvalidUserGroupRequestStruct {
379- // fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
380- // if !self.invalid_name {
381- // write!(
382- // f,
383- // "Invalid user group request- {{invalidName: {}\nnonExistantRoles: {:?}\nnonExistantUsers: {:?}\nThe name should follow this regex- ^[A-Za-z0-9_-]+$}}",
384- // self.invalid_name, self.non_existant_roles, self.non_existant_users
385- // )
386- // } else {
387- // write!(
388- // f,
389- // "Invalid user group request- {{nonExistantRoles: {:?}\nnonExistantUsers: {:?}}}",
390- // self.non_existant_roles, self.non_existant_users
391- // )
392- // }
393- // }
394- // }
395-
396377#[ derive( Debug , thiserror:: Error ) ]
397378pub enum RBACError {
398379 #[ error( "User exists already" ) ]
@@ -461,7 +442,7 @@ impl actix_web::ResponseError for RBACError {
461442 RBACError :: RolesDoNotExist ( obj) => actix_web:: HttpResponse :: build ( self . status_code ( ) )
462443 . insert_header ( ContentType :: plaintext ( ) )
463444 . json ( json ! ( {
464- "non_existant_roles " : obj
445+ "non_existent_roles " : obj
465446 } ) ) ,
466447 RBACError :: InvalidUserGroupRequest ( obj) => {
467448 actix_web:: HttpResponse :: build ( self . status_code ( ) )
0 commit comments