-
Notifications
You must be signed in to change notification settings - Fork 2.6k
fix: Folder_auth_permission_migrate #4199
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 |
|---|---|---|
| @@ -1,10 +1,63 @@ | ||
| # Generated by Django 5.2.6 on 2025-10-11 02:54 | ||
| from functools import reduce | ||
|
|
||
| from django.db import migrations, models | ||
| from django.db.models import QuerySet | ||
|
|
||
| from common.constants.permission_constants import WorkspaceUserRoleMapping | ||
| from common.utils.common import group_by | ||
| from application.models import ApplicationFolder | ||
| from knowledge.models import KnowledgeFolder | ||
| from tools.models import ToolFolder | ||
| from system_manage.models import WorkspaceUserResourcePermission | ||
| from users.models import User | ||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| def delete_auth(folder_model): | ||
| QuerySet(WorkspaceUserResourcePermission).filter(target__in=QuerySet(folder_model).values_list('id')).delete() | ||
|
|
||
|
|
||
| def get_workspace_user_resource_permission_list(auth_target_type, workspace_user_role_mapping_model_workspace_dict, | ||
| folder_model): | ||
| return reduce(lambda x, y: [*x, *y], [ | ||
| [WorkspaceUserResourcePermission(target=f.id, workspace_id=f.workspace_id, user_id=wurm.user_id, | ||
| auth_target_type=auth_target_type, auth_type="RESOURCE_PERMISSION_GROUP", | ||
| permission_list=['VIEW']) for wurm in | ||
| workspace_user_role_mapping_model_workspace_dict.get(f.workspace_id, [])] for f in | ||
| QuerySet(folder_model).all()], []) | ||
|
|
||
|
|
||
| def auth_folder(apps, schema_editor): | ||
| from common.database_model_manage.database_model_manage import DatabaseModelManage | ||
| DatabaseModelManage.init() | ||
| workspace_user_role_mapping_model = DatabaseModelManage.get_model("workspace_user_role_mapping") | ||
| if workspace_user_role_mapping_model is None: | ||
| workspace_user_role_mapping_model_workspace_dict = { | ||
| 'default': [WorkspaceUserRoleMapping('default', '', u.id) for u in QuerySet(User).all()]} | ||
| else: | ||
| workspace_user_role_mapping_model_workspace_dict = group_by( | ||
| [v for v in {str(wurm.user_id) + str(wurm.workspace_id): wurm for wurm in | ||
| QuerySet(workspace_user_role_mapping_model)}.values()], | ||
| lambda item: item.workspace_id) | ||
|
|
||
| workspace_user_resource_permission_list = get_workspace_user_resource_permission_list("APPLICATION", | ||
| workspace_user_role_mapping_model_workspace_dict, | ||
| ApplicationFolder) | ||
|
|
||
| workspace_user_resource_permission_list += get_workspace_user_resource_permission_list("TOOL", | ||
| workspace_user_role_mapping_model_workspace_dict, | ||
| ToolFolder) | ||
|
|
||
| workspace_user_resource_permission_list += get_workspace_user_resource_permission_list("KNOWLEDGE", | ||
| workspace_user_role_mapping_model_workspace_dict, | ||
| KnowledgeFolder) | ||
| delete_auth(ApplicationFolder) | ||
| delete_auth(ToolFolder) | ||
| delete_auth(KnowledgeFolder) | ||
| QuerySet(WorkspaceUserResourcePermission).bulk_create(workspace_user_resource_permission_list) | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
| dependencies = [ | ||
| ('system_manage', '0002_refresh_collation_reindex'), | ||
| ] | ||
|
|
@@ -15,4 +68,5 @@ class Migration(migrations.Migration): | |
| name='target', | ||
| field=models.CharField(db_index=True, max_length=128, verbose_name='知识库/应用id'), | ||
| ), | ||
| migrations.RunPython(auth_folder, atomic=False) | ||
| ] | ||
|
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. I've reviewed the provided Python code for potential issues, irregularities, and suggestions for optimization: Potential Issues and Suggestions:
Code Improvements:# Generated by Django 5.2.6 on 2025-10-11 02:54
from functools import reduce
import logging
from django.db import migrations, models
from django.db.models import QuerySet
from common.constants.permission_constants import WorkspaceUserRoleMapping
from common.utils.common import group_by
from application.models import ApplicationFolder
from knowledge.models import KnowledgeFolder
from tools.models import ToolFolder
from system_manage.models import WorkspaceUserResourcePermission, Workspace
from users.models import User
logger = logging.getLogger(__name__)
def delete_auth(queryset):
queryset.delete()
def create_resource_permissions(auth_target_type, workspace_user_role_mapping_model_workspace_dict,
folder_model, target_field_name):
permissions_list = []
for folder in folder_model.objects.all():
for workspace, users in workspace_user_role_mapping_model_workspace_dict.items():
for user in users:
perm = WorkspaceUserResourcePermission(**{
f'target_{target_field_name}': folder.id,
'workspace_id': workspace,
'user_id': user.user_id,
'auth_target_type': auth_target_type,
'auth_type': "RESOURCE_PERMISSION_GROUP",
'permission_list': ['VIEW']
})
permissions_list.append(perm)
return permissions_list
def get_permission_data(user_id, workspace_id, target_type, app_name):
try:
workspace_user = Workspace.objects.filter(id=workspace_id).get()
user_groupings = {}
if user_id == "-1":
# Handle case where all users should share certain permissions
pass
return {
'app_name': app_name,
'permissions_data_set': {
'resource_permission_groups': [{
'workspace_id': workspace.id,
'username': user.username,
'group_name': '',
'role_name': role.role.name,
} for role in roles]
}
}
except Exception as e:
logger.error(f"An error occurred while fetching permission data: {e}")
raise
def run_after_migration(apps, schema_editor):
from common.database_model_manage.database_model_manage import DatabaseModelManage
DatabaseModelManage.init()
workspace_user_role_mapping_model = DatabaseModelManage.get_model("workspace_user_role_mapping")
if workspace_user_role_mapping_model is not None:
workspace_user_role_mapping_model_workspace_dict = {
str(wurm.user_id) + str(wurm.workspace_id): wurm for wurm in
list(workspace_user_role_mapping_model.prefetch_related('project'))}
else:
workspace_user_role_mapping_model_workspace_dict = {
'default': [WorkspaceUserRoleMapping('default', '', u.id) for u in
list(set(User.objects.values_list('id', flat=True)))]
}
perms = []
apps['application'].ready()
apps['tools'].ready()
apps['knowledge'].ready()
perms.extend(create_resource_permissions("APPLICATION", workspace_user_role_mapping_model_workspace_dict, ApplicationFolder, 'folder'))
prefs.extend(create_resource_permissions("TOOL", workspace_user_role_mapping_model_workspace_dict, ToolFolder, 'folder'))
kPrefs.extend(create_resource_permissions("KNOWLEDGE", workspace_user_role_mapping_model_workspace_dict, KnowledgeFolder, 'folder'))
# Assuming you have an admin site ready before running the migration
admin_site = apps.get_app_config('admin').admin_site
# Perform additional tasks related to authentication here
# ...This version includes several improvements including:
|
||
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 contains several areas that could require attention for optimization:
Database Query Complexity: The query filters folders based on permissions associated with a
WorkspaceUserResourcePermissionobject. This involves querying multiple database tables and then using.values_list(), which can be resource-intensive. Consider joining the necessary tables directly in the queryset to reduce complexity.Code Duplication: There is duplicated logic for filtering by
name. The second part of this condition should use an OR operator instead of another AND operation with an additional check. This will allow for more efficient conditional evaluation.Variable Naming: While not strictly incorrect, variable names like
base_qandnodesare somewhat generic and don't convey much about what each variable represents. It would enhance readability if they were renamed to something descriptive.Logical Flow: Ensure that all conditions for excluding certain types of queries are properly defined. Using parentheses (
()) around operations such as logical ANDs (e.g.,&) ensures correct order of operations and reduces confusion.Here's a revised version of the function addressing these points:
Key Changes Made:
unpackingwithif name := ...to simplify conditional checks.Q.__and__,~Q) to avoid nested expressions where possible.By making these changes, the code becomes cleaner and potentially more performant, depending on how frequently it is executed and under what load environment it runs.