-
Notifications
You must be signed in to change notification settings - Fork 2.6k
feat: Modify the authorization information of resources for users #3840
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
zhanweizhang7
merged 1 commit into
v2
from
pr@v2@feat_modify_the_authorization_of_resources_for_users
Aug 11, 2025
+393
−5
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ | |
| import os | ||
|
|
||
| from django.core.cache import cache | ||
| from django.db import models | ||
| from django.db.models import QuerySet | ||
| from django.utils.translation import gettext_lazy as _ | ||
| from rest_framework import serializers | ||
|
|
@@ -19,7 +20,7 @@ | |
| from common.constants.permission_constants import get_default_workspace_user_role_mapping_list, RoleConstants, \ | ||
| ResourcePermission, ResourcePermissionRole, ResourceAuthType | ||
| from common.database_model_manage.database_model_manage import DatabaseModelManage | ||
| from common.db.search import native_search | ||
| from common.db.search import native_search, native_page_search, get_dynamics_model | ||
| from common.db.sql_execute import select_list | ||
| from common.exception.app_exception import AppApiException | ||
| from common.utils.common import get_file_content | ||
|
|
@@ -30,6 +31,7 @@ | |
| from models_provider.models import Model | ||
| from system_manage.models import WorkspaceUserResourcePermission, AuthTargetType | ||
| from tools.models import Tool | ||
| from users.models import User | ||
| from users.serializers.user import is_workspace_manage | ||
|
|
||
|
|
||
|
|
@@ -260,3 +262,123 @@ def edit(self, instance, user, with_valid=True): | |
| key = Cache_Version.PERMISSION_LIST.get_key(user_id=user_id) | ||
| cache.delete(key, version=version) | ||
| return True | ||
|
|
||
|
|
||
| class ResourceUserPermissionUserListRequest(serializers.Serializer): | ||
| nick_name = serializers.CharField(required=False, allow_null=True, allow_blank=True, label=_('workspace id')) | ||
| username = serializers.CharField(required=False, allow_null=True, allow_blank=True, label=_('workspace id')) | ||
| permission = serializers.ChoiceField(required=True, choices=['NOT_AUTH', 'MANAGE', 'VIEW', 'ROLE'], | ||
| label=_('permission')) | ||
|
|
||
|
|
||
| class ResourceUserPermissionEditRequest(serializers.Serializer): | ||
| user_id = serializers.CharField(required=True, label=_('workspace id')) | ||
| permission = serializers.ChoiceField(required=True, choices=['NOT_AUTH', 'MANAGE', 'VIEW', 'ROLE'], | ||
| label=_('permission')) | ||
|
|
||
|
|
||
| permission_map = { | ||
| "ROLE": ("ROLE", ["ROLE"]), | ||
| "MANAGE": ("RESOURCE_PERMISSION_GROUP", ["MANAGE", "VIEW"]), | ||
| "VIEW": ("RESOURCE_PERMISSION_GROUP", ["VIEW"]), | ||
| "NOT_AUTH": ("RESOURCE_PERMISSION_GROUP", []), | ||
| } | ||
|
|
||
|
|
||
| class ResourceUserPermissionSerializer(serializers.Serializer): | ||
| workspace_id = serializers.CharField(required=True, label=_('workspace id')) | ||
| target = serializers.CharField(required=True, label=_('resource id')) | ||
| auth_target_type = serializers.CharField(required=True, label=_('resource')) | ||
| users_permission = ResourceUserPermissionEditRequest(required=False, many=True, label=_('users_permission')) | ||
|
|
||
| def get_queryset(self, instance): | ||
|
|
||
| user_query_set = QuerySet(model=get_dynamics_model({ | ||
| 'nick_name': models.CharField(), | ||
| 'username': models.CharField(), | ||
| "permission": models.CharField(), | ||
| })) | ||
| nick_name = instance.get('nick_name') | ||
| username = instance.get('username') | ||
| permission = instance.get('permission') | ||
| workspace_user_resource_permission_query_set = QuerySet(WorkspaceUserResourcePermission).filter( | ||
| workspace_id=self.data.get('workspace_id'), | ||
| auth_target_type=self.data.get('auth_target_type'), | ||
| target=self.data.get('target')) | ||
| if nick_name: | ||
| user_query_set = user_query_set.filter(nick_name__contains=nick_name) | ||
| if username: | ||
| user_query_set = user_query_set.filter(username__contains=username) | ||
| if permission: | ||
| user_query_set = user_query_set.filter( | ||
| permission=None if instance.get('permission') == 'NOT_AUTH' else instance.get('permission')) | ||
|
|
||
| return { | ||
| 'workspace_user_resource_permission_query_set': workspace_user_resource_permission_query_set, | ||
| 'user_query_set': user_query_set | ||
| } | ||
|
|
||
| def list(self, instance, with_valid=True): | ||
| if with_valid: | ||
| self.is_valid(raise_exception=True) | ||
| ResourceUserPermissionUserListRequest(data=instance).is_valid(raise_exception=True) | ||
| # 资源的用户授权列表 | ||
| resource_user_permission_list = native_search(self.get_queryset(instance), get_file_content( | ||
| os.path.join(PROJECT_DIR, "apps", "system_manage", 'sql', 'get_resource_user_permission_detail.sql') | ||
| )) | ||
| return resource_user_permission_list | ||
|
|
||
| def page(self, instance, current_page: int, page_size: int, with_valid=True): | ||
| if with_valid: | ||
| self.is_valid(raise_exception=True) | ||
| ResourceUserPermissionUserListRequest(data=instance).is_valid(raise_exception=True) | ||
| # 分页列表 | ||
| resource_user_permission_page_list = native_page_search(current_page, page_size, self.get_queryset(instance), | ||
| get_file_content( | ||
| os.path.join(PROJECT_DIR, "apps", "system_manage", | ||
| 'sql', | ||
| 'get_resource_user_permission_detail.sql') | ||
| )) | ||
| return resource_user_permission_page_list | ||
|
|
||
| def edit(self, instance, with_valid=True): | ||
| if with_valid: | ||
| self.is_valid(raise_exception=True) | ||
| ResourceUserPermissionEditRequest(data=instance, many=True).is_valid( | ||
| raise_exception=True) | ||
|
|
||
| workspace_id = self.data.get("workspace_id") | ||
| target = self.data.get("target") | ||
| auth_target_type = self.data.get("auth_target_type") | ||
| users_permission = instance | ||
|
|
||
| users_id = [item["user_id"] for item in users_permission] | ||
| # 删除已存在的对应的用户在该资源下的权限 | ||
| QuerySet(WorkspaceUserResourcePermission).filter( | ||
| workspace_id=workspace_id, | ||
| target=target, | ||
| auth_target_type=auth_target_type, | ||
| user_id__in=users_id | ||
| ).delete() | ||
|
|
||
| save_list = [] | ||
| for item in users_permission: | ||
| permission = item['permission'] | ||
| auth_type, permission_list = permission_map[permission] | ||
|
|
||
| save_list.append(WorkspaceUserResourcePermission( | ||
| target=target, | ||
| auth_target_type=auth_target_type, | ||
| workspace_id=workspace_id, | ||
| auth_type=auth_type, | ||
| user_id=item["user_id"], | ||
| permission_list=permission_list | ||
| )) | ||
| if save_list: | ||
| QuerySet(WorkspaceUserResourcePermission).bulk_create(save_list) | ||
|
|
||
| version = Cache_Version.PERMISSION_LIST.get_version() | ||
| for user_id in users_id: | ||
| key = Cache_Version.PERMISSION_LIST.get_key(user_id=user_id) | ||
| cache.delete(key, version=version) | ||
| return True | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. {
"error": "The patch contains multiple issues:"
} |
||
30 changes: 30 additions & 0 deletions
30
apps/system_manage/sql/get_resource_user_permission_detail.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| SELECT | ||
| u.id, | ||
| u.nick_name, | ||
| u.username, | ||
| case | ||
| when | ||
| wurp."permission" is null then 'NOT_AUTH' | ||
| else wurp."permission" | ||
| end | ||
| FROM | ||
| public."user" u | ||
| LEFT JOIN ( | ||
| SELECT | ||
| user_id , | ||
| (case | ||
| when auth_type = 'ROLE' | ||
| and 'ROLE' = any( permission_list) then 'ROLE' | ||
| when auth_type = 'RESOURCE_PERMISSION_GROUP' | ||
| and 'MANAGE'= any(permission_list) then 'MANAGE' | ||
| when auth_type = 'RESOURCE_PERMISSION_GROUP' | ||
| and 'VIEW' = any( permission_list) then 'VIEW' | ||
| else 'NO_AUTH' | ||
| end) as "permission" | ||
| FROM | ||
| workspace_user_resource_permission | ||
| ${workspace_user_resource_permission_query_set} | ||
| ) wurp | ||
| ON | ||
| u.id = wurp.user_id | ||
| ${user_query_set} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The provided code snippet is a series of serializer definitions and API methods related to managing user resource permissions within a Django application. While the structure looks comprehensive, there are a few areas that could be improved for better clarity and functionality:
Translation Usage: The
_function is used as a translation context in theResultSerializer, but it seems unnecessary since these fields (user id,name, etc.) do not actually require translation. Consider removing the translations.Duplicate Parameter Definitions: There might be duplicate parameters across different serializers or classes if they all use similar paths or query strings.
Simplified Query Parameters: For pagination requests, you can simplify the syntax by using generic parameter names like
page_numberinstead ofcurrent_page. This enhances readability and consistency with other systems and libraries.Field Descriptions Alignment: Ensure consistent descriptions for each field to maintain clear understanding across serializers.
Error Handling: Adding error handling logic would improve the robustness and user experience of the APIs.
Code Structure Review: It would be beneficial to review the overall structural organization of the module to ensure logical flow and modularity.
Here's an updated version addressing some of these points:
These changes aim to enhance readability and maintainability while ensuring that the API remains functional.