@@ -226,7 +226,7 @@ def list(self, with_valid=True):
226226 return [{'id' : user_model .id , 'username' : user_model .username , 'email' : user_model .email } for user_model in
227227 self .get_query_set ()]
228228
229- def page (self , current_page : int , page_size : int , with_valid = True ):
229+ def page (self , current_page : int , page_size : int , user_id : str , with_valid = True ):
230230 if with_valid :
231231 self .is_valid (raise_exception = True )
232232 result = page_search (current_page , page_size ,
@@ -235,7 +235,7 @@ def page(self, current_page: int, page_size: int, with_valid=True):
235235 role_model = DatabaseModelManage .get_model ("role_model" )
236236 user_role_relation_model = DatabaseModelManage .get_model ("workspace_user_role_mapping" )
237237
238- def _get_user_roles (user_ids ):
238+ def _get_user_roles (user_ids , is_admin = True ):
239239 workspace_model = DatabaseModelManage .get_model ("workspace_model" )
240240 if not (role_model and user_role_relation_model and workspace_model ):
241241 return {}
@@ -261,7 +261,8 @@ def _get_user_roles(user_ids):
261261 user_id = str (relation .user_id )
262262 role_id = relation .role_id
263263 workspace_id = relation .workspace_id
264-
264+ if not is_admin and relation .role .type == RoleConstants .ADMIN .name :
265+ continue
265266 user_role_mapping [user_id ].add (relation .role .role_name )
266267 user_role_setting_mapping [user_id ][role_id ].append (workspace_id )
267268 user_role_workspace_mapping [user_id ][relation .role .role_name ].append (
@@ -285,8 +286,12 @@ def _get_user_roles(user_ids):
285286 return user_role_mapping , result_user_role_setting_mapping , result_user_role_workspace_mapping
286287
287288 if role_model and user_role_relation_model :
289+ # 获取当前用户的所有角色 判断是不是内置的系统管理员
290+ is_admin = user_role_relation_model .objects .filter (user_id = user_id ,
291+ role_id = RoleConstants .ADMIN .name ).exists ()
288292 user_ids = [user ['id' ] for user in result ['records' ]]
289- user_role_mapping , user_role_setting_mapping , user_role_workspace_mapping = _get_user_roles (user_ids )
293+ user_role_mapping , user_role_setting_mapping , user_role_workspace_mapping = _get_user_roles (user_ids ,
294+ is_admin )
290295
291296 # 将角色信息添加回用户数据中
292297 for user in result ['records' ]:
@@ -297,7 +302,7 @@ def _get_user_roles(user_ids):
297302 return result
298303
299304 @transaction .atomic
300- def save (self , instance , with_valid = True ):
305+ def save (self , instance , user_id , with_valid = True ):
301306 if with_valid :
302307 self .UserInstance (data = instance ).is_valid (raise_exception = True )
303308
@@ -312,7 +317,7 @@ def save(self, instance, with_valid=True):
312317 source = "LOCAL" ,
313318 is_active = True
314319 )
315- update_user_role (instance , user )
320+ update_user_role (instance , user , user_id )
316321 user .save ()
317322 return UserInstanceSerializer (user ).data
318323
@@ -419,15 +424,15 @@ def _check_not_admin(self):
419424 if user .role == RoleConstants .ADMIN .name or str (user .id ) == 'f0dd8f71-e4ee-11ee-8c84-a8a1595801ab' :
420425 raise AppApiException (1004 , _ ('Unable to delete administrator' ))
421426
422- def edit (self , instance , with_valid = True ):
427+ def edit (self , instance , user_id , with_valid = True ):
423428 if with_valid :
424429 self .is_valid (raise_exception = True )
425430 UserManageSerializer .UserEditInstance (data = instance ).is_valid (user_id = self .data .get ('id' ),
426431 raise_exception = True )
427432 user = User .objects .filter (id = self .data .get ('id' )).first ()
428433 self ._check_admin_modification (user , instance )
429434 self ._update_user_fields (user , instance )
430- update_user_role (instance , user )
435+ update_user_role (instance , user , user_id )
431436 user .save ()
432437 return UserInstanceSerializer (user ).data
433438
@@ -556,9 +561,11 @@ def get_all_user_list(self):
556561 return list (users )
557562
558563
559- def update_user_role (instance , user ):
564+ def update_user_role (instance , user , user_id = None ):
560565 workspace_user_role_mapping_model = DatabaseModelManage .get_model ("workspace_user_role_mapping" )
561566 if workspace_user_role_mapping_model :
567+ is_admin = workspace_user_role_mapping_model .objects .filter (user_id = user_id ,
568+ role_id = RoleConstants .ADMIN .name ).exists ()
562569 role_setting = instance .get ('role_setting' )
563570 if not role_setting :
564571 return
@@ -587,8 +594,11 @@ def update_user_role(instance, user):
587594 if role_id == str (workspace_manage_role_id ) or role_id == str (RoleConstants .USER .value ):
588595 if default_workspace_id not in workspace_ids :
589596 raise AppApiException (1004 , _ ("Cannot delete built-in role" ))
590-
591- workspace_user_role_mapping_model .objects .filter (user_id = user .id ).delete ()
597+ if is_admin :
598+ workspace_user_role_mapping_model .objects .filter (user_id = user .id ).delete ()
599+ else :
600+ workspace_user_role_mapping_model .objects .filter (user_id = user .id ).exclude (
601+ role_id = RoleConstants .ADMIN .name ).delete ()
592602 relations = set ()
593603 for item in role_setting :
594604 role_id = item ['role_id' ]
0 commit comments