feat: add v-access directive
This commit is contained in:
38
packages/effects/access/src/directive.ts
Normal file
38
packages/effects/access/src/directive.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
/**
|
||||
* Global authority directive
|
||||
* Used for fine-grained control of component permissions
|
||||
* @Example v-auth="RoleEnum.TEST"
|
||||
*/
|
||||
import type { App, Directive, DirectiveBinding } from 'vue';
|
||||
|
||||
import { useAccess } from './use-access';
|
||||
|
||||
function isAccessible(el: Element, binding: any) {
|
||||
const { accessMode, hasAccessByCodes, hasAccessByRoles } = useAccess();
|
||||
|
||||
const value = binding.value;
|
||||
|
||||
if (!value) {
|
||||
return;
|
||||
}
|
||||
const authMethod =
|
||||
accessMode.value === 'frontend' ? hasAccessByRoles : hasAccessByCodes;
|
||||
|
||||
if (!authMethod(value)) {
|
||||
el?.remove();
|
||||
}
|
||||
}
|
||||
|
||||
const mounted = (el: Element, binding: DirectiveBinding<string | string[]>) => {
|
||||
isAccessible(el, binding);
|
||||
};
|
||||
|
||||
const authDirective: Directive = {
|
||||
mounted,
|
||||
};
|
||||
|
||||
export function installAccessDirective(app: App) {
|
||||
app.directive('access', authDirective);
|
||||
}
|
||||
|
||||
export default authDirective;
|
Reference in New Issue
Block a user