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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions ui/src/permission/knowledge/system-manage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ const systemManage = {
],'OR'
),
// 文档
doc_read: () =>
hasPermission([
RoleConst.ADMIN,
PermissionConst.RESOURCE_KNOWLEDGE_DOCUMENT_READ
],'OR'),
doc_create: () => hasPermission(
[
RoleConst.ADMIN,
Expand Down Expand Up @@ -104,13 +109,23 @@ const systemManage = {
PermissionConst.RESOURCE_KNOWLEDGE_DOCUMENT_DOWNLOAD_SOURCE_FILE
],'OR'
),


knowledge_chat_user_read: () =>
hasPermission([
RoleConst.ADMIN,
PermissionConst.RESOURCE_KNOWLEDGE_CHAT_USER_READ
],'OR'),
knowledge_chat_user_edit: () =>
hasPermission([
RoleConst.ADMIN,
PermissionConst.RESOURCE_KNOWLEDGE_CHAT_USER_EDIT
],'OR'),


problem_read: () =>
hasPermission([
RoleConst.ADMIN,
PermissionConst.RESOURCE_KNOWLEDGE_PROBLEM_READ
],'OR'),
problem_create: () =>
hasPermission([
RoleConst.ADMIN,
Expand Down Expand Up @@ -139,6 +154,11 @@ const systemManage = {
folderCreate: () => false,
folderEdit: () => false,
folderDelete: () => false,
hit_test: () =>
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.

7 changes: 5 additions & 2 deletions ui/src/permission/knowledge/system-share.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ const share = {
'OR'
),

doc_read: () => false,
doc_create: () =>
hasPermission (
[
Expand Down Expand Up @@ -140,15 +141,16 @@ const share = {
],
'OR'
),
knowledge_chat_user_read: () => false,
knowledge_chat_user_edit: () =>
hasPermission(
[
RoleConst.ADMIN,
PermissionConst.SHARED_KNOWLEDGE_CHAT_USER_EDIT
],
'OR'
)
,
),
problem_read: () => false,
problem_relate: () =>
hasPermission (
[
Expand Down Expand Up @@ -176,5 +178,6 @@ const share = {
folderCreate: () => false,
folderEdit: () => false,
folderDelete: () => false,
hit_test: () => false,
}
export default share
4 changes: 4 additions & 0 deletions ui/src/permission/knowledge/workspace-share.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const workspaceShare = {
export: () => false,
delete: () => false,

doc_read: () => false,
doc_create: () => false,
doc_vector: () => false,
doc_generate: () => false,
Expand All @@ -21,8 +22,10 @@ const workspaceShare = {
doc_export: () => false,
doc_download: () => false,

knowledge_chat_user_read: () => false,
knowledge_chat_user_edit: () => false,

problem_read: () => false,
problem_create: () => false,
problem_relate: () => false,
problem_delete: () => false,
Expand All @@ -31,6 +34,7 @@ const workspaceShare = {
folderCreate: () => false,
folderEdit: () => false,
folderDelete: () => false,
hit_test: () => false,
}

export default workspaceShare
6 changes: 5 additions & 1 deletion ui/src/permission/knowledge/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ const workspace = {
],
'OR',
),
doc_read: () => false,
doc_create: (source_id:string) =>
hasPermission(
[
Expand Down Expand Up @@ -200,6 +201,7 @@ const workspace = {
],
'OR',
),
knowledge_chat_user_read: (source_id:string) => false,
knowledge_chat_user_edit: (source_id:string) =>
hasPermission(
[
Expand All @@ -209,7 +211,8 @@ const workspace = {
PermissionConst.KNOWLEDGE_CHAT_USER_EDIT.getWorkspacePermissionWorkspaceManageRole,
]
,'OR'
),
),
problem_read: () => false,
problem_create: (source_id:string) =>
hasPermission(
[
Expand Down Expand Up @@ -250,6 +253,7 @@ const workspace = {
],
'OR',
),
hit_test: () => false,
}

export default workspace
50 changes: 45 additions & 5 deletions ui/src/router/modules/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,15 @@ const DocumentRouter = {
const to: any = get_next_route()
if(to.params.folderId == 'share') {
return RoleConst.USER.getWorkspaceRole() }
}
},
()=>{
const to: any = get_next_route()
if (to.params.folderId == 'resource-management') { return RoleConst.ADMIN}
},
()=>{
const to: any = get_next_route()
if (to.params.folderId == 'resource-management') { return PermissionConst.RESOURCE_KNOWLEDGE_DOCUMENT_READ}
},
],
},
component: () => import('@/views/document/index.vue'),
Expand Down Expand Up @@ -108,7 +116,15 @@ const DocumentRouter = {
const to: any = get_next_route()
if(to.params.folderId == 'share') {
return RoleConst.USER.getWorkspaceRole() }
}
},
()=>{
const to: any = get_next_route()
if (to.params.folderId == 'resource-management') { return RoleConst.ADMIN}
},
()=>{
const to: any = get_next_route()
if (to.params.folderId == 'resource-management') { return PermissionConst.RESOURCE_KNOWLEDGE_PROBLEM_READ}
},
],
},
component: () => import('@/views/problem/index.vue'),
Expand Down Expand Up @@ -154,7 +170,15 @@ const DocumentRouter = {
const to: any = get_next_route()
if(to.params.folderId == 'share') {
return RoleConst.USER.getWorkspaceRole() }
}
},
()=>{
const to: any = get_next_route()
if (to.params.folderId == 'resource-management') { return RoleConst.ADMIN}
},
()=>{
const to: any = get_next_route()
if (to.params.folderId == 'resource-management') { return PermissionConst.RESOURCE_KNOWLEDGE_HIT_TEST}
},
],
},
component: () => import('@/views/hit-test/index.vue'),
Expand Down Expand Up @@ -211,7 +235,15 @@ const DocumentRouter = {
const to: any = get_next_route()
if(to.params.folderId == 'share') {
return RoleConst.USER.getWorkspaceRole() }
}
},
()=>{
const to: any = get_next_route()
if (to.params.folderId == 'resource-management') { return RoleConst.ADMIN}
},
()=>{
const to: any = get_next_route()
if (to.params.folderId == 'resource-management') { return PermissionConst.RESOURCE_KNOWLEDGE_CHAT_USER_READ}
},
]
},
component: () => import('@/views/chat-user/index.vue'),
Expand Down Expand Up @@ -258,7 +290,15 @@ const DocumentRouter = {
const to: any = get_next_route()
if(to.params.folderId == 'share') {
return RoleConst.USER.getWorkspaceRole() }
}
},
()=>{
const to: any = get_next_route()
if (to.params.folderId == 'resource-management') { return RoleConst.ADMIN}
},
()=>{
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.

Expand Down
1 change: 1 addition & 0 deletions ui/src/utils/permission/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ const PermissionConst = {

RESOURCE_KNOWLEDGE_CHAT_USER_READ: new Permission('SYSTEM_RESOURCE_KNOWLEDGE_CHAT_USER:READ'),
RESOURCE_KNOWLEDGE_CHAT_USER_EDIT: new Permission('SYSTEM_RESOURCE_KNOWLEDGE_CHAT_USER:READ+EDIT'),
RESOURCE_KNOWLEDGE_HIT_TEST: new Permission('SYSTEM_RESOURCE_KNOWLEDGE_HIT_TEST:READ'),

RESOURCE_APPLICATION_READ: new Permission('SYSTEM_RESOURCE_APPLICATION:READ'),
RESOURCE_APPLICATION_EDIT: new Permission('SYSTEM_RESOURCE_APPLICATION:READ+EDIT'),
Expand Down
21 changes: 15 additions & 6 deletions ui/src/views/chat-user/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@
? false
: hasPermission(
permissionObj[
route.path.includes('shared')
? 'SHAREDKNOWLEDGE'
: (route.meta?.resourceType as string)
currentPermissionKey
],
'OR',
)
Expand Down Expand Up @@ -96,9 +94,7 @@
? false
: hasPermission(
permissionObj[
route.path.includes('shared')
? 'SHAREDKNOWLEDGE'
: (route.meta?.resourceType as string)
currentPermissionKey
],
'OR',
)
Expand Down Expand Up @@ -207,6 +203,8 @@ const permissionObj = ref<any>({
[],
'OR',
),
APPLICATION_KNOWLEDGE: [RoleConst.ADMIN, PermissionConst.RESOURCE_APPLICATION_CHAT_USER_EDIT],
RESOURCE_KNOWLEDGE: [RoleConst.ADMIN, PermissionConst.RESOURCE_KNOWLEDGE_CHAT_USER_EDIT],
SHAREDKNOWLEDGE: new ComplexPermission(
[RoleConst.ADMIN],
[PermissionConst.SHARED_KNOWLEDGE_CHAT_USER_EDIT],
Expand All @@ -215,6 +213,17 @@ const permissionObj = ref<any>({
),
})

const currentPermissionKey = computed(() => {
if (route.path.includes('shared')) return 'SHAREDKNOWLEDGE'
if (route.path.includes('resource-management')) {
if (route.meta?.resourceType === 'KNOWLEDGE') { return 'RESOURCE_KNOWLEDGE' }
else if (route.meta?.resourceType === 'APPLICATION') { return 'RESOURCE_APPLICATION' }
}
return route.meta?.resourceType as string
})

console.log(currentPermissionKey.value)

const resource = reactive({
resource_id: route.params.id as string,
resource_type: route.meta.resourceType as string,
Expand Down
25 changes: 23 additions & 2 deletions ui/src/views/system-resource-management/KnowledgeResourceIndex.vue
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,9 @@
:content="$t('views.system.resource_management.management')"
placement="top"
>
<span class="mr-8">
<span class="mr-8"
v-if="ManagePermission()"
>
<el-button
type="primary"
text
Expand Down Expand Up @@ -178,7 +180,9 @@
</el-button>
</span>
</el-tooltip>
<el-dropdown trigger="click">
<el-dropdown trigger="click"
v-if="MoreFilledPermission()"
>
<el-button text @click.stop>
<el-icon>
<MoreFilled />
Expand Down Expand Up @@ -264,6 +268,23 @@ const permissionPrecise = computed(() => {
return permissionMap['knowledge']['systemManage']
})

const ManagePermission = () => {
return permissionPrecise.value.doc_read() ||
permissionPrecise.value.problem_read() ||
permissionPrecise.value.edit() ||
permissionPrecise.value.knowledge_chat_user_read() ||
permissionPrecise.value.hit_test()
}
// sync generete edit export delete
const MoreFilledPermission = () => {
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<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.

Expand Down
Loading