-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat: UI permission instruction #3010
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 |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import type { App } from 'vue' | ||
| import { hasPermission } from '@/utils/permission' | ||
|
|
||
| const display = async (el: any, binding: any) => { | ||
| const has = hasPermission( | ||
| binding.value?.permission || binding.value, | ||
| binding.value?.compare || 'OR', | ||
| ) | ||
| if (!has) { | ||
| el.style.display = 'none' | ||
| } else { | ||
| delete el.style.display | ||
| } | ||
| } | ||
|
|
||
| export default { | ||
| install: (app: App) => { | ||
| app.directive('hasPermission', { | ||
| async created(el: any, binding: any) { | ||
| display(el, binding) | ||
| }, | ||
| async beforeUpdate(el: any, binding: any) { | ||
| display(el, binding) | ||
| }, | ||
| }) | ||
| }, | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import type { App } from 'vue' | ||
|
|
||
| const directives = import.meta.glob('./*.ts', { eager: true }) | ||
| const install = (app: App) => { | ||
| Object.keys(directives) | ||
| .filter((key: string) => { | ||
| return !key.endsWith('index.ts') | ||
| }) | ||
| .forEach((key: string) => { | ||
| const directive: any = directives[key] | ||
| app.use(directive.default) | ||
| }) | ||
| } | ||
| export default { install } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| import { Permission } from '@/utils/permission/type' | ||
| const PermissionConst = { | ||
| USER_READ: new Permission('USER:READ'), | ||
| USER_CREATE: new Permission('USER:CREATE'), | ||
| KNOWLEDGE_READ: new Permission('KNOWLEDGE:READ'), | ||
| } | ||
| export default PermissionConst |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,24 @@ export class Permission { | |
| constructor(permission: string) { | ||
| this.permission = permission | ||
| } | ||
| /** | ||
| * 工作空间权限 | ||
| * @param workspace_id 工作空间id | ||
| * @returns 工作空间权限 | ||
| */ | ||
| getWorkspacePermission(workspace_id: string) { | ||
| return `${this.permission}:/WORKSPACE/${workspace_id}` | ||
| } | ||
| /** | ||
| * 工作空间资源权限 | ||
| * @param workspace_id 工作空间id | ||
| * @param resource 资源 | ||
| * @param resource_id 资源id | ||
| * @returns 工作空间资源权限 | ||
| */ | ||
| getWorkspaceResourcePermission(workspace_id: string, resource: string, resource_id: string) { | ||
| return `${this.permission}:/WORKSPACE/${workspace_id}/${resource}/${resource_id}` | ||
| } | ||
| } | ||
| /** | ||
| * 复杂权限对象 | ||
|
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. There are no obvious syntax errors or logical issues with the provided code snippet. However, here are some potential suggestions for further improvement:
Here's an updated version of the code including some minor adjustments: import { WorkspaceId } from './workspace-id'; // Assuming WorkspaceId is defined somewhere
export interface Permission {
permission: string;
}
export class Permission implements Permission {
private readonly permission: string;
constructor(permission: string) {
this.permission = permission;
}
/**
* 获取工作空间权限
* @param workspace_id 工作空间ID
* @returns 工作空间权限字符串
*/
public getWorkspacePermission(workspace_id: WorkshopId): string {
return `${this.permission}:/WORKSPACE/${workspace_id}`;
}
/**
* 获取工作空间资源权限
* @param workspace_id 工作空间ID
* @param resource 源资源类型(如 'user', 'project' 等)
* @param resourceId 资源ID
* @returns 工作空间资源权限字符串
*/
public getResourceWorkspacePermission(
workspace_id: WorkshopId,
resource: string,
resourceId: number | string
): string {
const resourcePartSuffix = typeof resourceId === 'number'
? `/r${resourceId.toString()}` : `/${resource_id}`;
return `${this.permission}:/WORKSPACE/${workspace_id}/${resource}${resourcePartSuffix}`;
}
}In this revised version, I've introduced a new |
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,21 @@ | ||
| <script setup lang="ts"></script> | ||
| <script setup lang="ts"> | ||
| import PermissionConst from '@/utils/permission/data' | ||
| </script> | ||
|
|
||
| <template>首页</template> | ||
| <template> | ||
| 首页 | ||
| <div v-hasPermission="PermissionConst.USER_READ.getWorkspacePermission('default')"> | ||
| default工作空间用户只读 | ||
| </div> | ||
| <div v-hasPermission="PermissionConst.USER_READ.getWorkspacePermission('default1')"> | ||
| default1工作空间用户只读 | ||
| </div> | ||
| <div v-hasPermission="PermissionConst.USER_READ">用户只读</div> | ||
| <div | ||
| v-hasPermission=" | ||
| PermissionConst.KNOWLEDGE_READ.getWorkspaceResourcePermission('default', 'KNOWLEDGE', 'xxx') | ||
| " | ||
| > | ||
| default工作空间的知识库xxx权限 | ||
| </div> | ||
| </template> | ||
|
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 appears to be correct for the most part, but here are some minor improvements and suggestions:
|
||
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.
The provided code looks mostly correct and functions as intended to conditionally display elements based on user permissions using Vue directives with TypeScript types. However, there are a few minor improvements and optimizations that could be made:
Type Safety: Ensure that
binding.valueis always an object before accessing its properties. This helps prevent runtime errors due to undefined values.Error Handling: Add some basic error handling to ensure that permission logic runs smoothly even if input is unexpected.
Dynamic Permission Check: If you have dynamic or complex permission checks, consider implementing more sophisticated validation within the
displayfunction.Optimization: Since this directive only needs to run during creation and updates, you can use asynchronous methods like
async created()without unnecessary complexity.Here's the revised code:
These changes enhance maintainability and robustness of the code.