mirror of
https://github.com/1Panel-dev/MaxKB.git
synced 2025-12-30 09:42:48 +00:00
refactor: user
This commit is contained in:
parent
598b72fd12
commit
c4642a3595
|
|
@ -198,28 +198,46 @@ class UserManageSerializer(serializers.Serializer):
|
|||
if not (role_model and user_role_relation_model):
|
||||
return {}
|
||||
|
||||
# 获取所有相关角色关系,并预加载角色信息
|
||||
# 获取所有相关角色关系,并预加载角色信息
|
||||
user_role_relations = (
|
||||
user_role_relation_model.objects
|
||||
.filter(user_id__in=user_ids)
|
||||
.distinct('user_id', 'role_id') # 确保每个用户每个角色只返回一次
|
||||
.select_related('role') # 预加载外键数据
|
||||
.select_related('role')
|
||||
.distinct('user_id', 'role_id', 'workspace_id') # 确保组合唯一性
|
||||
)
|
||||
|
||||
# 构建用户ID到角色名称列表的映射
|
||||
user_role_mapping = defaultdict(list)
|
||||
for relation in user_role_relations:
|
||||
user_role_mapping[str(relation.user_id)].append(relation.role.role_name)
|
||||
# 构建用户ID到角色ID与工作空间ID映射
|
||||
user_role_setting_mapping = defaultdict(lambda: defaultdict(list))
|
||||
|
||||
return user_role_mapping
|
||||
for relation in user_role_relations:
|
||||
user_id = str(relation.user_id)
|
||||
role_id = relation.role_id
|
||||
workspace_id = relation.workspace_id
|
||||
|
||||
user_role_mapping[user_id].append(relation.role.role_name)
|
||||
user_role_setting_mapping[user_id][role_id].append(workspace_id)
|
||||
|
||||
# 转换为所需的结构
|
||||
result_user_role_setting_mapping = {
|
||||
user_id: [{"role_id": role_id, "workspace_ids": workspace_ids}
|
||||
for role_id, workspace_ids in roles.items()]
|
||||
for user_id, roles in user_role_setting_mapping.items()
|
||||
}
|
||||
|
||||
return user_role_mapping, result_user_role_setting_mapping
|
||||
|
||||
if role_model and user_role_relation_model:
|
||||
user_ids = [user['id'] for user in result['records']]
|
||||
user_role_mapping = _get_user_roles(user_ids)
|
||||
user_role_mapping, user_role_setting_mapping = _get_user_roles(user_ids)
|
||||
|
||||
# 将角色信息添加回用户数据中
|
||||
for user in result['records']:
|
||||
user['role'] = user_role_mapping.get(str(user['id']), [])
|
||||
user_id = str(user['id'])
|
||||
user['role'] = user_role_mapping.get(user_id, [])
|
||||
user['role_setting'] = user_role_setting_mapping.get(user_id, [])
|
||||
return result
|
||||
|
||||
@valid_license(model=User, count=2,
|
||||
|
|
@ -454,7 +472,7 @@ class UserManageSerializer(serializers.Serializer):
|
|||
'roles': [relation.role.role_name]
|
||||
}
|
||||
else:
|
||||
user_dict[user_id]['roles'].append(relation.role.name)
|
||||
user_dict[user_id]['roles'].append(relation.role.role_name)
|
||||
|
||||
# 将字典值转换为列表形式
|
||||
return list(user_dict.values())
|
||||
|
|
@ -489,6 +507,8 @@ def update_user_role(instance, user):
|
|||
workspace_user_role_mapping_model = DatabaseModelManage.get_model("workspace_user_role_mapping")
|
||||
if workspace_user_role_mapping_model:
|
||||
role_setting = instance.get('role_setting')
|
||||
if not role_setting:
|
||||
return
|
||||
workspace_user_role_mapping_model.objects.filter(user_id=user.id).delete()
|
||||
relations = set()
|
||||
for item in role_setting:
|
||||
|
|
|
|||
Loading…
Reference in New Issue