-
Notifications
You must be signed in to change notification settings - Fork 2.6k
feat: Resource permission in system-manage #3768
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,29 +13,128 @@ const systemManage = { | |
| 'OR', | ||
| ), | ||
| create: () => false, | ||
| sync: () => false, | ||
| vector: () => false, | ||
| generate: () => false, | ||
| edit: () => false, | ||
| export: () => false, | ||
| delete: () => false, | ||
| sync: () => hasPermission( | ||
| [ | ||
| RoleConst.ADMIN, | ||
| PermissionConst.RESOURCE_KNOWLEDGE_SYNC | ||
| ],'OR' | ||
| ), | ||
| vector: () => hasPermission( | ||
| [ | ||
| RoleConst.ADMIN, | ||
| PermissionConst.RESOURCE_KNOWLEDGE_VECTOR | ||
| ],'OR' | ||
| ), | ||
| generate: () => hasPermission( | ||
| [ | ||
| RoleConst.ADMIN, | ||
| PermissionConst.RESOURCE_KNOWLEDGE_GENERATE | ||
| ],'OR' | ||
| ), | ||
| edit: () => hasPermission( | ||
| [ | ||
| RoleConst.ADMIN, | ||
| PermissionConst.RESOURCE_KNOWLEDGE_EDIT | ||
| ],'OR' | ||
| ), | ||
| export: () => hasPermission( | ||
| [ | ||
| RoleConst.ADMIN, | ||
| PermissionConst.RESOURCE_KNOWLEDGE_EXPORT | ||
| ],'OR' | ||
| ), | ||
| delete: () => hasPermission( | ||
| [ | ||
| RoleConst.ADMIN, | ||
| PermissionConst.RESOURCE_KNOWLEDGE_DELETE | ||
| ],'OR' | ||
| ), | ||
| // 文档 | ||
| doc_create: () => hasPermission( | ||
| [ | ||
| RoleConst.ADMIN, | ||
| PermissionConst.RESOURCE_KNOWLEDGE_DOCUMENT_CREATE | ||
| ],'OR' | ||
| ), | ||
| doc_vector: () => hasPermission( | ||
| [ | ||
| RoleConst.ADMIN, | ||
| PermissionConst.RESOURCE_KNOWLEDGE_DOCUMENT_VECTOR | ||
| ],'OR' | ||
| ), | ||
| doc_generate: () => hasPermission( | ||
| [ | ||
| RoleConst.ADMIN, | ||
| PermissionConst.RESOURCE_KNOWLEDGE_DOCUMENT_GENERATE | ||
| ],'OR' | ||
| ), | ||
| doc_migrate: () => hasPermission( | ||
| [ | ||
| RoleConst.ADMIN, | ||
| PermissionConst.RESOURCE_KNOWLEDGE_DOCUMENT_MIGRATE | ||
| ],'OR' | ||
| ), | ||
| doc_edit: () => hasPermission( | ||
| [ | ||
| RoleConst.ADMIN, | ||
| PermissionConst.RESOURCE_KNOWLEDGE_DOCUMENT_EDIT | ||
| ],'OR' | ||
| ), | ||
| doc_sync: () => hasPermission( | ||
| [ | ||
| RoleConst.ADMIN, | ||
| PermissionConst.RESOURCE_KNOWLEDGE_DOCUMENT_SYNC | ||
| ],'OR' | ||
| ), | ||
| doc_delete: () => hasPermission( | ||
| [ | ||
| RoleConst.ADMIN, | ||
| PermissionConst.RESOURCE_KNOWLEDGE_DOCUMENT_DELETE | ||
| ],'OR' | ||
| ), | ||
| doc_export: () => hasPermission( | ||
| [ | ||
| RoleConst.ADMIN, | ||
| PermissionConst.RESOURCE_KNOWLEDGE_DOCUMENT_EXPORT | ||
| ],'OR' | ||
| ), | ||
| doc_download: () => hasPermission( | ||
| [ | ||
| RoleConst.ADMIN, | ||
| PermissionConst.RESOURCE_KNOWLEDGE_DOCUMENT_DOWNLOAD_SOURCE_FILE | ||
| ],'OR' | ||
| ), | ||
|
|
||
| doc_create: () => false, | ||
| doc_vector: () => false, | ||
| doc_generate: () => false, | ||
| doc_migrate: () => false, | ||
| doc_edit: () => false, | ||
| doc_sync: () => false, | ||
| doc_delete: () => false, | ||
| doc_export: () => false, | ||
| doc_download: () => false, | ||
| knowledge_chat_user_edit: () => | ||
| hasPermission([ | ||
| RoleConst.ADMIN, | ||
| PermissionConst.RESOURCE_KNOWLEDGE_CHAT_USER_EDIT | ||
| ],'OR'), | ||
|
|
||
| knowledge_chat_user_edit: () => false, | ||
|
|
||
| problem_create: () => false, | ||
| problem_relate: () => false, | ||
| problem_delete: () => false, | ||
| problem_edit: () => false, | ||
| problem_create: () => | ||
| hasPermission([ | ||
| RoleConst.ADMIN, | ||
| PermissionConst.RESOURCE_KNOWLEDGE_PROBLEM_CREATE | ||
| ],'OR' | ||
| ), | ||
| problem_relate: () => | ||
| hasPermission([ | ||
| RoleConst.ADMIN, | ||
| PermissionConst.RESOURCE_KNOWLEDGE_PROBLEM_RELATE | ||
| ],'OR' | ||
| ), | ||
| problem_delete: () => | ||
| hasPermission([ | ||
| RoleConst.ADMIN, | ||
| PermissionConst.RESOURCE_KNOWLEDGE_PROBLEM_DELETE | ||
| ],'OR' | ||
| ), | ||
| problem_edit: () => | ||
| hasPermission([ | ||
| RoleConst.ADMIN, | ||
| PermissionConst.RESOURCE_KNOWLEDGE_PROBLEM_EDIT | ||
| ],'OR' | ||
| ), | ||
|
|
||
| folderCreate: () => false, | ||
| folderEdit: () => false, | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The code looks well-designed overall but can be optimized in several ways:
Here’s an example of how you might refactor some of this: const systemManage = {
OR: (...args) => args.every(Boolean),
};
function hasPermission(neededRoles, joinTypeOrArray = 'OR') {
let result;
switch (joinTypeOrArray.toLowerCase()) {
case 'and':
result = neededRoles.every((role) => roleExists(role));
break;
default:
result = neededRoles.some((role) => roleExists(role));
break;
}
return result;
}
// Helper function to check if a role exists
function roleExists(roleName) {
// Implementation depends on where roles are defined or loaded
// Here we just assume it's available globally
try {
return global.roles.includes(roleName);
} catch (err) {
console.error("Error checking role existence:", err);
return false;
}
}
function initializePermissionsMap() {
const permissionsMap = {};
// Initialize specific permissions based on their types
const permissionTypes = ['sync', 'vector', 'generate', 'edit', 'export', 'delete'];
permissionTypes.forEach(type => {
const key = `${type}_create`;
permissionsMap[key] = (role) =>
hasPermission([RoleConst.ADMIN, `RESOURCE_KNOWLEDGE_${key.split('_')[0].toUpperCase()}`]);
});
// Additional document-specific permissions...
permissionsMap.doc_create = (role) =>
hasPermission([RoleConst.ADMIN, PermissionConst.RESOURCE_KNOWLEDGE_DOCUMENT_CREATE]);
permissionsMap.knowledge_chat_user_edit = (role) =>
hasPermission([RoleConst.ADMIN, PermissionConst.RESOURCE_KNOWLEDGE_CHAT_USER_EDIT]);
// Problem-related permissions...
permissionsMap.problem_create = (role) =>
hasPermission([RoleConst.ADMIN, PermissionConst.RESOURCE_KNOWLEDGE_PROBLEM_CREATE]);
// Document-migration etc...
// Return full map or individual objects as needed
return permissionsMap;
}This approach separates functionality into reusable components, which makes maintenance easier, and ensures that all similar permissions are handled similarly throughout the application. |
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,9 +13,21 @@ const systemManage = { | |
| 'OR', | ||
| ), | ||
| create: () => false, | ||
| modify: () => false, | ||
| paramSetting: () => false, | ||
| delete: () => false, | ||
| modify: () => | ||
| hasPermission([ | ||
| RoleConst.ADMIN, | ||
| PermissionConst.RESOURCE_MODEL_EDIT | ||
| ],'OR'), | ||
| paramSetting: () => | ||
| hasPermission([ | ||
| RoleConst.ADMIN, | ||
| PermissionConst.RESOURCE_MODEL_EDIT | ||
| ],'OR'), | ||
| delete: () => | ||
| hasPermission([ | ||
| RoleConst.ADMIN, | ||
| PermissionConst.RESOURCE_MODEL_DELETE | ||
| ],'OR'), | ||
|
|
||
| folderCreate: () => false, | ||
| folderEdit: () => false, | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The provided JavaScript function
Here's the improved version of the code: const roleConst = {
ADMIN: 'ADMIN',
};
const permissionConst = {
RESOURCE_MODEL_EDIT: 'RESOURCE_MODEL_EDIT',
RESOURCE_MODEL_DELETE: 'RESOURCE_MODEL DELETE',
};
function hasPermission(requiredRoles, operator) {
if (operator === 'AND') {
return requiredRoles.every(role => user.hasRole(role));
} else if (operator === 'OR') {
return requiredRoles.some(role => user.hasRole(role)); // Adjust depending on actual data structure
}
}
const systemManage = {
folderCreate: () => false,
folderEdit: () => false,
create: () =>
hasPermission([roleConst.ADMIN], 'AND'),
modify: () =>
hasPermission([roleConst.ADMIN, permissionConst.RESOURCE_MODEL_EDIT], 'OR'),
paramSetting: () =>
hasPermission([roleConst.ADMIN, permissionConst.RESOURCE_MODEL_EDIT], 'OR'),
delete: () =>
hasPermission([roleConst.ADMIN, permissionConst.RESOURCE_MODEL_DELETE], 'OR'),
};Key Improvements:
Ensure you have access to the necessary utility functions such as |
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Your code has several issues that need to be addressed:
Removed Import Statement: The
ComplexPermissiontype is required for permission checks but is removed from the import statement at line 3.To fix: Add back the
import { ComplexPermission } from '@/utils/permission/type';Unused Function Call: There's a commented-out function call
create => falsewithin thesystemManageobject literal at lines 4-5. Since there aren't any other calls to this, you might want to remove it if not needed.Code Structure: While minor, consider organizing related functions or constants together rather than scattering them throughout the file.
Here’s the corrected version of your code with these suggested changes:
This should address the specified issues and improve the readability and functionality of your code.