@@ -22,12 +22,11 @@ use actix_web::{
22
22
HttpResponse , Responder ,
23
23
} ;
24
24
use http:: StatusCode ;
25
- use itertools:: Itertools ;
26
25
27
26
use crate :: {
28
27
parseable:: PARSEABLE ,
29
28
rbac:: {
30
- map:: { mut_roles, read_user_groups , write_user_groups , DEFAULT_ROLE } ,
29
+ map:: { mut_roles, DEFAULT_ROLE } ,
31
30
role:: model:: DefaultPrivilege ,
32
31
} ,
33
32
storage:: { self , ObjectStorageError , StorageMetadata } ,
@@ -78,46 +77,22 @@ pub async fn list_roles() -> Result<impl Responder, RoleError> {
78
77
// Delete existing role
79
78
pub async fn delete ( name : web:: Path < String > ) -> Result < impl Responder , RoleError > {
80
79
let name = name. into_inner ( ) ;
80
+ // check if the role is being used by any user or group
81
81
let mut metadata = get_metadata ( ) . await ?;
82
82
if metadata. users . iter ( ) . any ( |user| user. roles . contains ( & name) ) {
83
83
return Err ( RoleError :: RoleInUse ) ;
84
84
}
85
+ if metadata
86
+ . user_groups
87
+ . iter ( )
88
+ . any ( |user_group| user_group. roles . contains ( & name) )
89
+ {
90
+ return Err ( RoleError :: RoleInUse ) ;
91
+ }
85
92
metadata. roles . remove ( & name) ;
86
93
put_metadata ( & metadata) . await ?;
87
94
mut_roles ( ) . remove ( & name) ;
88
95
89
- // also delete from user groups
90
- let groups = read_user_groups ( ) . keys ( ) . cloned ( ) . collect_vec ( ) ;
91
- let mut group_names = Vec :: new ( ) ;
92
-
93
- for user_group in groups {
94
- if let Some ( ug) = read_user_groups ( ) . get ( & user_group) {
95
- if ug. roles . contains ( & name) {
96
- return Err ( RoleError :: RoleInUse ) ;
97
- }
98
- group_names. push ( ug. name . clone ( ) ) ;
99
- } else {
100
- continue ;
101
- } ;
102
- }
103
-
104
- // remove role from all user groups that have it
105
- let mut groups_to_update = Vec :: new ( ) ;
106
- for group in write_user_groups ( ) . values_mut ( ) {
107
- if group. roles . remove ( & name) {
108
- groups_to_update. push ( group. clone ( ) ) ;
109
- }
110
- }
111
-
112
- // update metadata only if there are changes
113
- if !groups_to_update. is_empty ( ) {
114
- metadata
115
- . user_groups
116
- . retain ( |x| !groups_to_update. contains ( x) ) ;
117
- metadata. user_groups . extend ( groups_to_update) ;
118
- }
119
- put_metadata ( & metadata) . await ?;
120
-
121
96
Ok ( HttpResponse :: Ok ( ) . finish ( ) )
122
97
}
123
98
0 commit comments