-
Notifications
You must be signed in to change notification settings - Fork 2.6k
feat: The folder creator manages permissions and the root directory displays all resources #4191
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,7 @@ | |
| from knowledge.serializers.knowledge import KnowledgeSerializer | ||
| from knowledge.serializers.knowledge_folder import KnowledgeFolderTreeSerializer | ||
| from system_manage.models import WorkspaceUserResourcePermission | ||
| from system_manage.serializers.user_resource_permission import UserResourcePermissionSerializer | ||
| from tools.models import ToolFolder, Tool | ||
| from tools.serializers.tool import ToolSerializer | ||
| from tools.serializers.tool_folder import ToolFolderTreeSerializer | ||
|
|
@@ -139,6 +140,13 @@ def insert(self, instance, with_valid=True): | |
| parent_id=parent_id | ||
| ) | ||
| folder.save() | ||
|
|
||
| UserResourcePermissionSerializer(data={ | ||
| 'workspace_id': self.data.get('workspace_id'), | ||
| 'user_id': self.data.get('user_id'), | ||
| 'auth_target_type': self.data.get('source') | ||
| }).auth_resource(str(folder.id), is_folder=True) | ||
|
|
||
| return FolderSerializer(folder).data | ||
|
|
||
| class Operate(serializers.Serializer): | ||
|
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. The provided code snippet contains several enhancements and modifications to improve performance, maintainability, and functionality:
Overall, these changes generally enhance the robustness and usability of the code. Always test thoroughly after making such substantial modifications to ensure all functionalities remain intact and perform as expected. |
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -73,7 +73,8 @@ def is_valid(self, *, auth_target_type=None, workspace_id=None, raise_exception= | |
| illegal_target_id_list = select_list( | ||
| get_file_content( | ||
| os.path.join(PROJECT_DIR, "apps", "system_manage", 'sql', 'check_member_permission_target_exists.sql')), | ||
| [json.dumps(user_resource_permission_list), workspace_id, workspace_id, workspace_id, workspace_id,workspace_id,workspace_id,workspace_id]) | ||
| [json.dumps(user_resource_permission_list), workspace_id, workspace_id, workspace_id, workspace_id, | ||
| workspace_id, workspace_id, workspace_id]) | ||
| if illegal_target_id_list is not None and len(illegal_target_id_list) > 0: | ||
| raise AppApiException(500, | ||
| _('Non-existent id[') + str(illegal_target_id_list) + ']') | ||
|
|
@@ -192,7 +193,7 @@ def auth_resource_batch(self, resource_id_list: list): | |
| cache.delete(key, version=version) | ||
| return True | ||
|
|
||
| def auth_resource(self, resource_id: str): | ||
| def auth_resource(self, resource_id: str, is_folder=False): | ||
| self.is_valid(raise_exception=True) | ||
| auth_target_type = self.data.get('auth_target_type') | ||
| workspace_id = self.data.get('workspace_id') | ||
|
|
@@ -206,11 +207,12 @@ def auth_resource(self, resource_id: str): | |
| target=resource_id, | ||
| auth_target_type=auth_target_type, | ||
| permission_list=[ResourcePermission.VIEW, | ||
| ResourcePermission.MANAGE] if auth_type == ResourceAuthType.RESOURCE_PERMISSION_GROUP else [ | ||
| ResourcePermission.MANAGE] if ( | ||
| auth_type == ResourceAuthType.RESOURCE_PERMISSION_GROUP or is_folder) else [ | ||
| ResourcePermissionRole.ROLE], | ||
| workspace_id=workspace_id, | ||
| user_id=user_id, | ||
| auth_type=auth_type | ||
| auth_type=ResourceAuthType.RESOURCE_PERMISSION_GROUP if is_folder else auth_type | ||
| ).save() | ||
| # 刷新缓存 | ||
| version = Cache_Version.PERMISSION_LIST.get_version() | ||
|
|
@@ -358,7 +360,7 @@ def get_queryset(self, instance): | |
| permission__in=query_p_list) | ||
| workspace_user_role_mapping_model = DatabaseModelManage.get_model("workspace_user_role_mapping") | ||
| if workspace_user_role_mapping_model: | ||
| user_query_set=user_query_set.filter( | ||
| user_query_set = user_query_set.filter( | ||
| id__in=QuerySet(workspace_user_role_mapping_model).filter( | ||
| workspace_id=self.data.get('workspace_id')).values("user_id")) | ||
|
|
||
|
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. The provided code has several potential issues and areas for improvement:
Here’s an optimized version of the code addressing these points: import os
from django.core.cache import cache
from apps.system_manage.models import DatabaseModelManage, UserResourcePermissions
def get_auth_targets(auth_type):
# Placeholder logic to determine authorization targets based on auth_type
return ['view', 'manage'] if auth_type == ResourceAuthType.RESOURCE_PERMISSION_GROUP else ['role']
class YourClass:
def __init__(self, data):
self.data = data
@staticmethod
def select_list(query_sql_path, params):
# Securely execute SQL query
# Example implementation using Django ORM
pass
def is_valid(self, *, raise_exception=False):
user_resource_permission_list = self.data.get('user_resource_permission_list')
workspace_id = self.data.get('workspace_id')
if isinstance(user_resource_permission_list, str):
illegal_target_id_list = self.select_list(os.path.join(PROJECT_DIR, "apps", "system_manage", 'sql', 'check_member_permission_target_exists.sql'), json.dumps([user_resource_permission_list], ensure_ascii=False))
elif isinstance(user_resource_permission_list, list):
illegal_target_id_list = self.select_list(os.path.join(PROJECT_DIR, "apps", "system_manage", 'sql', 'check_member_permission_target_exists_aliased_groups.sql').ensure_ascii(False))
else:
illegal_target_id_list = []
if illegal_target_id_list is not None and len(illegal_target_id_list) > 0:
raise AppApiException(500, _('Non-existent id[') + str(illegal_target_id_list) + ']')
def auth_resource_batch(self, resource_id_list: list):
# Optimized batch authentication logic
try:
for resource_id in resource_id_list:
self.auth_resource(resource_id)
cache.delete_many(["resource_permissions_" + str(workspace_id) for workspace_id in set(self.data["workspace_ids"])])
except Exception as e:
return False
return True
def auth_resource(self, resource_id: str, is_folder: bool = False):
self.is_valid(True)
auth_target_type = self.data.get('auth_target_type')
permission_list = get_auth_targets(auth_target_type)
user_id = self.data.get('user_id')
permissions_to_save = UserResourcePermissions.objects.create(
target=resource_id,
auth_target_type=auth_target_type,
permissions=permission_list,
workspace_id=self.data.get('workspace_id'),
user_id=user_id,
is_group=is_folder
)
# Refresh cached permissions
Cache_Version.PERMISSION_LIST.refresh()
def get_queryset(self, instance):
... # Existing queryset logic without significant issuesKey Changes:
|
||
|
|
||
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.
There are no specific irregularities or issues with the provided code snippet. However, I suggest making a small optimization:
The
distinct()method is added to thefolder_query_setfilter operation when filtering byparent, which can increase efficiency slightly.This change ensures that only unique entries from the
folder_query_setfiltered byparentare included in the results. If multiple sub-folders have the same parent at different levels of nesting, this might prevent duplicates. But please note that using distinct in a query may negatively impact performance on very large datasets due to increased database scanning. Always balance between readability and performance based on practical use cases.