Skip to content

Conversation

@shaohuzhang1
Copy link
Contributor

fix: Folder_auth_permission_migrate

@f2c-ci-robot
Copy link

f2c-ci-robot bot commented Oct 15, 2025

Adding the "do-not-merge/release-note-label-needed" label because no release-note block was detected, please follow our release note process to remove it.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@f2c-ci-robot
Copy link

f2c-ci-robot bot commented Oct 15, 2025

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

field=models.CharField(db_index=True, max_length=128, verbose_name='知识库/应用id'),
),
migrations.RunPython(auth_folder, atomic=False)
]
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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:

  1. Imports:

    • The imports at various points seem to be missing a comma separator between the last import User and other imported modules.
  2. Unused Variables:

    • There are several variables (f, wurm) that are used without being defined later in the function bodies. These might need to be properly initialized or removed to avoid errors.
  3. Logical Flow:

    • The logic around getting permissions seems convoluted. Consider simplifying it while maintaining clarity.
  4. Atomicity of Operations:

    • Using atomic=False should generally be avoided unless absolutely necessary because it can lead to data integrity concerns and unexpected behavior if multiple operations fail half-way through.
  5. Error Handling:

    • There is no error handling specified for database operations, which can lead to silent failures if something goes wrong.
  6. Code Duplication:

    • The logic for creating permission lists for different types of folders appears duplicated across multiple places. Consider abstracting this logic into helper functions.
  7. Performance Optimization:

    • Ensure that database queries are optimized and minimized as much as possible. For example, using .distinct() when needed and avoiding unnecessary filters.
  8. Testing:

    • Since the code involves database modifications, proper unit tests would help verify its correctness and safety during deployments.

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:

  • Proper imports with commas separating them.
  • Simplified variable initialization and usage.
  • Added logical comments and placeholders for further development.
  • Enhanced error handling via exceptions and logging.
  • Optimized duplicate code for creating resource permissions using a more centralized approach.

'target', flat=True)) | Q(id=self.data.get('workspace_id')))

nodes = Folder.objects.filter(base_q).get_cached_trees()

Copy link
Contributor Author

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:

  1. Database Query Complexity: The query filters folders based on permissions associated with a WorkspaceUserResourcePermission object. 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.

  2. 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.

  3. Variable Naming: While not strictly incorrect, variable names like base_q and nodes are somewhat generic and don't convey much about what each variable represents. It would enhance readability if they were renamed to something descriptive.

  4. 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:

from django.db.models import Q

def get_folder_tree(self):
    current_user = self.request.user
    source_type = self.data.get('source')
    workspace_id = self.data.get('workspace_id')

    # Base query for folder retrieval
    base_query = Folder.objects.filter(workspace__id=workspace_id)

    if name := request.query_params.get('name'):
        base_query &= Q(name=name)  # Use unpacking for concise condition checking

    if workspace_manage:
        exclude_queries = Q()
    else:
        # Join WorkspaceUserResourcePermission table to filter by view permissions
        exclude_queries = (
            WorkerUserResourcePermissions.objects
            .filter(
                user_id=current_user.id,
                auth_target_type=self.data.get('source'),
                workflow_group__id=WORKFLOW_GROUP_USER_PERMISSIONS_VIEWS_DEFAULT_ID
            )
            .values('auth_workflow_id')
        )

    if exclude_queries.exists():
        base_query |= ~Q(id__in=exclude_queries)

    return base_query.all().get_cached_trees()

WORKFLOW_GROUP_USER_PERMISSIONS_VIEWS_DEFAULT_ID = "default"  # Define constants or lookups appropriately

Key Changes Made:

  • Used unpacking with if name := ... to simplify conditional checks.
  • Renamed variables for better clarity.
  • Simplified the exclusion logic by utilizing Django’s ORM features (e.g., Q.__and__, ~Q) to avoid nested expressions where possible.
  • Removed duplicate parts from the WHERE clause that are covered by default query construction methods.

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.

@zhanweizhang7 zhanweizhang7 merged commit a6082c9 into v2 Oct 15, 2025
4 of 6 checks passed
@zhanweizhang7 zhanweizhang7 deleted the pr@v2@fix_fodler_migrate branch October 15, 2025 08:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants