Skip to content

feat: Resource knowledge permission #3778

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

Merged
merged 1 commit into from
Jul 30, 2025

Conversation

shaohuzhang1
Copy link
Contributor

feat: Resource knowledge permission

Copy link

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

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.

Copy link

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

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

()=>{
const to: any = get_next_route()
if (to.params.folderId == 'resource-management') { return PermissionConst.RESOURCE_KNOWLEDGE_EDIT}
},
],
},
component: () => import('@/views/knowledge/KnowledgeSetting.vue'),
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 code looks mostly correct but has repetitive logic for handling different folderId values in each route definition. You can streamline this by creating a single function that returns the appropriate role or permission based on the folder ID:

function getRoleBasedOnFolder(folderId) {
  switch (folderId) {
    case 'share':
      return RoleConst.USER.getWorkspaceRole();
    case 'resource-management':
      return [
        RoleConst.ADMIN,
        PermissionConst.RESOURCE_KNOWLEDGE_DOCUMENT_READ,
        // Add more permissions as needed
      ];
    default:
      return null; // Return appropriate value for other cases
  }
}

Then you can use this function within the respective route definitions:

const DocumentRouter = {
  routes: [
    {
      path: '/document',
      redirect: to => ({ ...to, query: { type: 'knowledge-document' } }),
      children: [
        {
          name: 'DocumentIndex',
          path: '',
          meta: {
            requiresAuth: true
          },
          component: () => import('@/views/document/index.vue'),
          beforeEnter: (to, from, next) => {
            if (getRoleBasedOnFolder(to.query.type)[0] === RoleConst.ANONYMOUS) {
              router.push('/');
            } else {
              next();
            }
          }
        },
        // Repeat similar structures for ProblemRoute and HitTestRoute
      ]
    },
    // Other routes ...
  ],

};

This approach ensures consistency and reduces redundancy in the code. If there are specific requirements for certain roles or permissions not handled here, additional conditions should be added inside the switch statement.

permissionPrecise.value.delete()
}


const search_type = ref('name')
const search_form = ref<any>({
name: '',
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 mostly clean, but there are some minor improvements that can be made:

  1. Consistency: Ensure consistency with spacing around operators (like =) for better readability.

  2. Comments: Add comments at the start of functions to explain their purpose.

  3. Variable Naming: Consider using more descriptive variable names where appropriate to improve code clarity.

Here's the revised version with these suggestions:

--- a/views/system/resource_management/components/common/management.vue
+++ b/views/system/resource_management/components/common/management.vue
@@ -146,7 +146,9 @@
           :content="$t('views.system.resource_management.management')"
           placement="top"
         >
-          <span class="mr-8">
+          <span class="mr-8" v-if="canManage()">{{ // Button will only appear if user has manage permission }}
                 <el-button
                   type="primary"
                   text
@@ -178,7 +180,9 @@
               </button>
             </span>
           </el-tooltip>
-          <el-dropdown trigger="click">
+          <el-dropdown trigger="click" v-if="canMoreFilled()">
             <el-button text @click.stop>
               <el-icon>
                 <MoreFilled />
@@ -264,6 +268,27 @@ const permissionPrecise = computed(() => {
   return permissionMap['knowledge']['systemManage']
 })
 
+/**
+ * Determines if the current user has the 'manage' permission.
+ */
+const canManage = () => {
+  return (
+    permissionPrecise.value.doc_read ||
+    permissionPrecise.value.problem_read ||
+    permissionPrecise.value.edit ||
+    permissionPrecise.value.knowledge_chat_user_read ||
+    permissionPrecise.value.hit_test
+  );
+}
+
+/**
+ * Determines if the current user has any of the 'more filled' permissions.
+ */
+const canMoreFilled = () => {
+  return (
+    permissionPrecise.value.sync ||
+    permissionPrecise.value.generate ||
+    permissionPrecise.value.edit ||
+    permissionPrecise.value.export ||
+    permissionPrecise.value.delete
+  );
+}
 
 
 const search_type = ref('name');
 const search_form = ref({

These changes make the code easier to read and understand while maintaining its functionality. The added comments provide context for each function, enhancing maintainability.

hasPermission([
RoleConst.ADMIN,
PermissionConst.RESOURCE_KNOWLEDGE_HIT_TEST
], 'OR'),
}

export default systemManage
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 code you provided appears to be an API function that checks user permissions for various actions related to knowledge management and problems. However, there are several areas where the code can be improved:

  1. Function Naming: The naming convention for functions like doc_read could benefit from being more consistent with other similar functions.

  2. Code Organization: While not a major issue, the order of conditions within each permission check might not always make sense, especially if the roles or permissions change frequently.

  3. Documentation: It would be helpful to add comments describing what each part of the code does, particularly around complex logic (like the use of 'OR').

  4. Default Values and Edge Cases: Ensure that default values are properly handled, especially in cases where certain operations don't require explicit permissions.

Here's an optimized version of the code with some suggested improvements:

const systemManage = {
  docList() {
    return hasPermission(['admin', 'resource_knowledge_document_read'], 'or');
  },
  docCreate: () => hasPermission(['admin', 'resource_knowledge_document_create'], 'or'),
  docRead: () => hasPermission(['admin', 'resource_knowledge_document_read'], 'or'),
  docDownloadSourceFile: () =>
    hasPermission(
      ['admin', 'resource_knowledge_document_download_source_file'],
      'or'
    ),

  // ... Similar patterns repeat...

};

export default systemManage;

Explanation of Changes:

  • Consistent Function Names: Renamed knowledge_chat_user_read, problem_read, etc., to match the pattern used in other functions.
  • Duplicated Code Removal: Used arrow functions and template literals for consistency across similar methods.
  • Improved Readability: Removed comments for simplicity but kept them in mind during further development.

While this is still quite compact, it provides a clear structure for managing permissions in your application.

@zhanweizhang7 zhanweizhang7 merged commit 00a4b09 into v2 Jul 30, 2025
3 of 5 checks passed
@zhanweizhang7 zhanweizhang7 deleted the pr@v2@feat_resource_knowledge_permission branch July 30, 2025 07:50
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