Skip to content

feat: Resource tool permission#3765

Merged
zhanweizhang7 merged 1 commit intov2from
pr@v2@feat_resource_tool_permission
Jul 29, 2025
Merged

feat: Resource tool permission#3765
zhanweizhang7 merged 1 commit intov2from
pr@v2@feat_resource_tool_permission

Conversation

@shaohuzhang1
Copy link
Copy Markdown
Contributor

feat: Resource tool permission

@f2c-ci-robot
Copy link
Copy Markdown

f2c-ci-robot bot commented Jul 29, 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
Copy Markdown

f2c-ci-robot bot commented Jul 29, 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

@zhanweizhang7 zhanweizhang7 merged commit 53f83d3 into v2 Jul 29, 2025
3 of 5 checks passed
@zhanweizhang7 zhanweizhang7 deleted the pr@v2@feat_resource_tool_permission branch July 29, 2025 06:50
Copy link
Copy Markdown
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 defines permission functions for various operations related to system management. It utilizes a utility function hasPermission that checks if a user has any of the specified permissions based on an OR condition. However, there are a few areas where improvements can be made:

  1. Duplicate Methods: There are duplicate methods named folderCreate, folderEdit, delete, folderDelete, etc., with almost identical implementation. Instead, these could be consolidated into one generic method that accepts different action types.

  2. Consistent Return Values: The return values should be consistent across all actions. If all permissions allow access, returning true makes more sense than not returning anything (which is ambiguous).

  3. Comments: Adding comments to describe what each method does would improve readability and maintainability.

Here's a revised version with optimizations:

import { hasPermission } from '@/utils/permission/index';
import { ComplexPermission } from '@/utils/permission/type';
import { EditionConst, PermissionConst, RoleConst } from '@/utils/permission/data';

const systemManage = {
    // Generic method for handling CRUD operations
    handleOperation(actionType) {
        const requiredPermissions = {
            create: [RoleConst.ADMIN],
            edit: [],
            folderCreate: [RoleConst.ADMIN],
            folderEdit: [],
            delete: [],
            folderDelete: [],
            overview_embed: [RoleConst.ADMIN],
            overview_access: [RoleConst.ADMIN],
            overview_display: [RoleConst.ADMIN],
            overview_api_key: [RoleConst.ADMIN],
            access_edit: [RoleConst.ADMIN],
            application_chat_user_edit: [RoleConst.ADMIN],
            chat_log_clear: [RoleConst.ADMIN],
            chat_log_export: [RoleConst.ADMIN],
            chat_log_add_knowledge: []
        };

        const permissionKeys = Object.keys(requiredPermissions).filter(key => key === actionType || `${actionType}Access`).map(key => RequiredPermissions[key]);

        return hasPermission(permissionKeys, 'OR');
    },

    // Specific action implementations using the generic method
    create() {
        return this.handleOperation('create');
    },
    edit() {
        return this.handleOperation('edit');
    },
    folderCreate() {
        return this.handleOperation('folderCreate');
    },
    folderEdit() {
        return this.handleOperation('folderEdit');
    },
    delete() {
        return this.handleOperation('delete');
    },
    folderDelete() {
        return this.handleOperation('folderDelete');
    },
    overview_embed() {
        return this.handleOperation('overviewEmbed');
    },
    overview_access() {
        return this.handleOperation('overviewAccess');
    },
    overview_display() {
        return this.handleOperation('overviewDisplay');
    },
    overview_api_key() {
        return this.handleOperation('overviewApiKey');
    },
    access_edit() {
        return this.handleOperation('accessEdit');
    },
    application_chat_user_edit() {
        return this.handleOperation('applicationChatUserEdit');
    },
    chat_log_clear() {
        return this.handleOperation('chatLogClear');
    },
    chat_log_export() {
        return this.handleOperation('chatLogExport');
    },
    chat_log_add_knowledge() {
        return this.handleOperation('chatLogAddKnowledge');
    }
};

Key Changes:

  • Generic Method (handleOperation): This function allows setting up common logic for handling permissions based on operation type.
  • Reduced Duplicates: Methods have been unified under a single handler.
  • Improved Comments: Additions explain the purpose of each section and action method.


function exportTool(row: any) {
ToolResourceApi.exportTool(row.id, row.name, loading).catch((e: any) => {
if (e.response.status !== 403) {
Copy link
Copy Markdown
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 seems mostly aligned with good practices. Here are some minor suggestions and improvements:

  1. Code Consistency: The comment formatting is consistent, which is good practice.

  2. Function Naming: Ensure that all function names follow a standardized naming convention, e.g., use camelCase instead of PascalCase for clarity.

  3. Conditional Rendering: Using v-if on an element to conditionally render it based on data is appropriate here.

  4. String Concatenation: Consider using template literals or string interpolation for better readability when constructing strings like comments.

Here's a slightly improved version with these considerations:

// Import statements should be at the top
import { ref, computed } from 'vue';
import { ElTooltip, MoreFilled, ToolResourceApi } from '@/plugins'; // Adjust module paths accordingly

export default {
  components: {
    ElTooltip,
    MoreFilled,
  },
  setup() {
    const moreFilledPermission = (row) => permissionPrecise.value.export() || 
                                    permissionPrecise.value.delete() ||
                                    (row.initFieldList.length > 0 && permissionPrecise.value.edit());

    const exportTool = async (row) => {
      try {
        await ToolResourceApi.exportTool(row.id, row.name);
      } catch (e) {
        if (e.response.status !== 403) {
          console.error('Error exporting tool:', e.message);
        }
      } finally {
        // Clear loading state if needed
        loading.value = false;
      }
    };

    return {
      exportTool,
      MoreFilledPermission,
    };
  },
};

Key Changes:

  • Module Imports: Added type annotations for easier reference in TypeScript projects.
  • Function Naming: Used camelCase for moreFilledPermission.
  • Export Function: Wrapped asynchronous operations in a try-catch-finally block for error handling within the finally block to ensure cleanups or retries can occur if necessary.

These changes make the code cleaner, more readable, and maintainable.

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.

2 participants