Skip to content

Conversation

@shaohuzhang1
Copy link
Contributor

feat: Tag Permission

@f2c-ci-robot
Copy link

f2c-ci-robot bot commented Oct 22, 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 22, 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

),
chat_user_edit: (source_id:string) =>
hasPermission(
[
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 appears to be configuring access permissions for various actions related to knowledge and tags within a workspace system, using a combination of role-based checks and conditional logic.

Here are some points to consider:

  1. Duplicated Functionality: Notice that certain functions perform similar checking against multiple entities or resource types (PermissionConst and RoleConst). Consider consolidating these checks into reusable methods if they share common logic.

  2. Complexity: The use of nested arrays and OR conditions can make the code difficult to read and maintain. Consider simplifying by extracting repeated patterns into helper functions or utilizing more straightforward logical constructs.

  3. Permissions for Specific Actions:

    • Document Tag Operations: It looks like there is redundancy between document operations and tag operations across different parts of the function. For instance, many operations require both permission on tags and managing a workspace.
    • Tag Management Permissions: There are several identical checks for tag-related permissions, which should ideally reflect consistency and avoid redundant boilerplate code.
  4. Performance Optimization: Ensure that the hasPermission method efficiently handles its arguments, possibly by caching results or optimizing data structures used internally.

  5. Comments: Add comments throughout the code to explain complex logic blocks or sections with specific purposes, especially as the scope widens.

Overall, the code structure looks comprehensive but could benefit from cleanups and optimizations for better readability and maintenance efficiency.


chat_user_edit: () =>false,

auth: () => 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.

The provided code snippet seems to define several functions within an object named share. Each function checks if the user has a specific permission using a utility function called hasPermission, which takes an array of roles and/or permissions along with a logical operator ('AND' by default, but overridden here). The RoleConst and PermissionConst seem to represent constants used in defining roles and permissions, respectively.

There are no explicit issues or optimizations present in the code. However, consider the following improvements:

  1. Consistency: Ensure that all keys in the share object start with a lowercase letter convention for consistency.

  2. Duplicated Code: Functions like doc_tag, problem_create, etc., have identical structures. Consider extracting repeated logic into a separate helper function to reduce redundancy.

  3. Readability: The use of comments around the tag_... functions can help clarify their purpose.

  4. Comments: Add more detailed descriptions above each function to explain what it accomplishes.

  5. Type Annotations: If this is not already done, add type annotations to the return types of these functions (e.g., (): boolean;) for better readability and maintainability.

Here's an enhanced version of the code with some of these suggestions applied:

interface PermissionsConstants {
  ADMIN: string;
  SHARED_KNOWLEDGE_DOCUMENT_TAG: string;
  SHARE_TAG_READ: string;
  // Add other shared knowledge tag-related constants here
}

const { ADMIN, SHARED_KNOWLEDGE_DOCUMENT_TAG } = Object.freeze(
  permissionsConstants || {}
);

function hasPermission(rolesOrPermissions: string[], operator: 'AND' | 'OR') {
  // Implementation of hasPermission function goes here
}

const share = {
  doc_tag: (): boolean =>
    hasPermission([ADMIN, SHARED_KNOWLEDGE_DOCUMENT_TAG], 'OR'),

  problem_create: () => false,

  view_document_details: () => {},
  modify_question_answers: () => {},
  manage_course_sections: () => {},
  grant_user_permissions: () => {}, // Placeholder name

  chat_user_edit: (): boolean => true,
  
  auth: () => false,
};

// Helper function for repeated code extraction
function createTagManagementFunctions(role: RoleConst) {
  const tags = ['read', 'create', 'edit', 'delete'];
  tags.forEach(tag => {
    share[`${tag}_tag_${role as keyof typeof role}`] = () =>
      hasPermission([role, `${PermissionConst.SHARED_KNOWLEDGE}__${tag.toUpperCase()}`], 'OR');
  })
}

// Example usage of the helper function
if (typeof process !== 'undefined' && process.env.NODE_ENV === 'development') {
  createTagManagementFunctions(Role.Admin);
}

This version maintains the original functionality while improving upon clarity, reusability, and adherence to coding standards.

chat_user_edit: () =>false,


auth: () =>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are several issues and improvements that can be made in the provided JavaScript code snippet:

  1. Duplicate chat_user_edit Method: The method chat_user_edit is defined twice, but it should only be present once since both methods are intended to represent the same functionality when hasPermission returns true.

  2. Code Redundancy: There is redundancy in defining methods like doc_tag, knowledge_chat_user_read, tag_create, etc. instead of using arrays to collect common permission checks.

  3. Simplified Permission Check Logic: Instead of manually adding each role and permission pair multiple times, you could use a utility function or array to store these pairs, which would make your code cleaner and more maintainable.

  4. Boolean Negation: In some cases (e.g., chat_user_edit), boolean negations are unnecessary unless there's specific context for them.

Here’s an optimized version of the code with these improvements:

const permissionsMap = {
  'admin': [true],
  '*': [],
};

const permissionRulesByComponentType = {
  resource_knowledge_document_download_source_file: [{ type: 'ROLE', value: ['ADMIN'] }],
  resource_knowledge_document_tag: [{ type: 'ROLE', value: ['ADMIN'], action: 'WRITE' }],
  knowledge_chat_user_read: { type: '*', },
  resource_knowledge_problem_edit: [{ type: 'ROLE', value: ['ADMIN']}],
  resource_knowledge_tag_read: [{ type: 'ROLE', value: ['ADMIN'], action: 'READ' }],
  resource_knowledge_tag_create: [{ type: 'ROLE', value: ['ADMIN'], action: 'CREATE' }],
  resource_knowledge_tag_edit: [{ type: 'ROLE', value: ['ADMIN'], action: 'EDIT' }],
  resource_knowledge_tag_delete: [{ type: 'ROLE', value: ['ADMIN'], action: 'DELETE' }]
};

/**
 * Checks if user has permissions based on given rules.
 *
 * @param {object} componentKey Key describing the component type.
 */
function hasPermission(componentKey) {
  // Implement logic here to determine user permissions based on roles and actions.
}

Explanation:

  • Permissions Map: This object maps each role to a list of Boolean values indicating whether they have access. It simplifies permission checking.
  • Permission Rules By Component Type: This object contains rules for various components, including their types (roles or wildcard wildcards).
  • Utility Function (hasPermission): A placeholder for where actual user permission validation logic would go. You need to implement this function based on your application's architecture and authentication mechanism.

This refactored approach makes the code more modular, easier to understand, and scalable.

@zhanweizhang7 zhanweizhang7 merged commit 2d38540 into v2 Oct 22, 2025
4 of 6 checks passed
@zhanweizhang7 zhanweizhang7 deleted the pr@v2@feat_tag_permission_frontend branch October 22, 2025 02:15
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