@@ -124,6 +124,10 @@ pub async fn delete_user(
124124 let caller_userid = get_user_from_request ( & req) ?;
125125 let tenant = tenant_id. as_deref ( ) . unwrap_or ( DEFAULT_TENANT ) ;
126126 let _guard = UPDATE_LOCK . lock ( ) . await ;
127+ // fail this request if the user is protected
128+ if Users . is_protected ( & userid, & tenant_id) == Some ( true ) {
129+ return Err ( RBACError :: ProtectedUser ) ;
130+ }
127131 // fail this request if the user does not exist
128132 if !Users . contains ( & userid, & tenant_id) {
129133 return Err ( RBACError :: UserDoesNotExist ) ;
@@ -335,6 +339,11 @@ pub async fn remove_roles_from_user(
335339 return Err ( RBACError :: UserDoesNotExist ) ;
336340 } ;
337341
342+ // fail this request if the user is protected
343+ if Users . is_protected ( & userid, & tenant_id) == Some ( true ) {
344+ return Err ( RBACError :: ProtectedUser ) ;
345+ }
346+
338347 // find username by userid, for native users, username is userid, for oauth users, we need to look up
339348 let username = if let Some ( users) = users ( ) . get ( tenant)
340349 && let Some ( user) = users. get ( & userid)
@@ -662,6 +671,41 @@ mod role_escalation {
662671 assert ! ( matches!( err, RBACError :: ProtectedUser ) , "{err:?}" ) ;
663672 }
664673
674+ #[ tokio:: test]
675+ async fn delete_user_rejects_protected_user ( ) {
676+ // Other modules' tests watch the RBAC map locks; see `map::TEST_SERIAL`.
677+ let _serial = TEST_SERIAL . lock ( ) . await ;
678+ let ( tenant, _, req) = tenant_request ( ordinary_role ( ) ) ;
679+ let target = insert_protected_user ( & tenant, "admin" ) ;
680+ let Err ( err) = delete_user ( req, web:: Path :: from ( target. clone ( ) ) ) . await else {
681+ panic ! ( "a protected user must be rejected before storage" ) ;
682+ } ;
683+ assert ! ( matches!( err, RBACError :: ProtectedUser ) , "{err:?}" ) ;
684+ assert ! ( Users . contains( & target, & Some ( tenant) ) ) ;
685+ }
686+
687+ #[ tokio:: test]
688+ async fn remove_roles_rejects_protected_user ( ) {
689+ // Other modules' tests watch the RBAC map locks; see `map::TEST_SERIAL`.
690+ let _serial = TEST_SERIAL . lock ( ) . await ;
691+ let ( tenant, _, req) = tenant_request ( ordinary_role ( ) ) ;
692+ let target = insert_protected_user ( & tenant, "admin" ) ;
693+ let Err ( err) = remove_roles_from_user (
694+ req,
695+ web:: Path :: from ( target. clone ( ) ) ,
696+ web:: Json ( HashSet :: from ( [ "admin" . to_owned ( ) ] ) ) ,
697+ )
698+ . await
699+ else {
700+ panic ! ( "a protected user must be rejected before storage" ) ;
701+ } ;
702+ assert ! ( matches!( err, RBACError :: ProtectedUser ) , "{err:?}" ) ;
703+ assert_eq ! (
704+ Users . get_user( & target, & Some ( tenant) ) . unwrap( ) . roles,
705+ HashSet :: from( [ "admin" . to_owned( ) ] )
706+ ) ;
707+ }
708+
665709 #[ tokio:: test]
666710 async fn add_roles_checks_protected_role_before_missing_roles ( ) {
667711 // Other modules' tests watch the RBAC map locks; see `map::TEST_SERIAL`.
0 commit comments