feat: add v-access directive
parent
910a3553ac
commit
01e95e029f
|
@ -144,6 +144,7 @@ function handleLockScreen(password: string) {
|
||||||
<template #extra>
|
<template #extra>
|
||||||
<AuthenticationLoginExpiredModal
|
<AuthenticationLoginExpiredModal
|
||||||
v-model:open="openLoginExpiredModal"
|
v-model:open="openLoginExpiredModal"
|
||||||
|
:avatar
|
||||||
:loading="loginLoading"
|
:loading="loginLoading"
|
||||||
password-placeholder="123456"
|
password-placeholder="123456"
|
||||||
username-placeholder="vben"
|
username-placeholder="vben"
|
||||||
|
|
|
@ -14,7 +14,7 @@ const routes: RouteRecordRaw[] = [
|
||||||
},
|
},
|
||||||
name: 'Demos',
|
name: 'Demos',
|
||||||
path: '/demos',
|
path: '/demos',
|
||||||
redirect: '/access',
|
redirect: '/demos/access',
|
||||||
children: [
|
children: [
|
||||||
{
|
{
|
||||||
meta: {
|
meta: {
|
||||||
|
|
|
@ -26,7 +26,7 @@ const accounts: Record<string, LoginAndRegisterParams> = {
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
const { accessMode, hasAuthByCodes } = useAccess();
|
const { accessMode, hasAccessByCodes } = useAccess();
|
||||||
const accessStore = useAccessStore();
|
const accessStore = useAccessStore();
|
||||||
const appStore = useAppStore();
|
const appStore = useAppStore();
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
@ -83,16 +83,16 @@ async function changeAccount(role: string) {
|
||||||
|
|
||||||
<div class="card-box mt-5 p-5 font-semibold">
|
<div class="card-box mt-5 p-5 font-semibold">
|
||||||
<div class="mb-3 text-lg">组件形式控制 - 权限码方式</div>
|
<div class="mb-3 text-lg">组件形式控制 - 权限码方式</div>
|
||||||
<AccessControl :value="['AC_100100']" type="code">
|
<AccessControl :permissions="['AC_100100']" type="code">
|
||||||
<Button class="mr-4"> Super 账号可见 ["AC_1000001"] </Button>
|
<Button class="mr-4"> Super 账号可见 ["AC_1000001"] </Button>
|
||||||
</AccessControl>
|
</AccessControl>
|
||||||
<AccessControl :value="['AC_100030']" type="code">
|
<AccessControl :permissions="['AC_100030']" type="code">
|
||||||
<Button class="mr-4"> Admin 账号可见 ["AC_100010"] </Button>
|
<Button class="mr-4"> Admin 账号可见 ["AC_100010"] </Button>
|
||||||
</AccessControl>
|
</AccessControl>
|
||||||
<AccessControl :value="['AC_1000001']" type="code">
|
<AccessControl :permissions="['AC_1000001']" type="code">
|
||||||
<Button class="mr-4"> User 账号可见 ["AC_1000001"] </Button>
|
<Button class="mr-4"> User 账号可见 ["AC_1000001"] </Button>
|
||||||
</AccessControl>
|
</AccessControl>
|
||||||
<AccessControl :value="['AC_100100', 'AC_100010']" type="code">
|
<AccessControl :permissions="['AC_100100', 'AC_100010']" type="code">
|
||||||
<Button class="mr-4">
|
<Button class="mr-4">
|
||||||
Super & Admin 账号可见 ["AC_100100","AC_1000001"]
|
Super & Admin 账号可见 ["AC_100100","AC_1000001"]
|
||||||
</Button>
|
</Button>
|
||||||
|
@ -104,32 +104,32 @@ async function changeAccount(role: string) {
|
||||||
class="card-box mt-5 p-5 font-semibold"
|
class="card-box mt-5 p-5 font-semibold"
|
||||||
>
|
>
|
||||||
<div class="mb-3 text-lg">组件形式控制 - 用户角色方式</div>
|
<div class="mb-3 text-lg">组件形式控制 - 用户角色方式</div>
|
||||||
<AccessControl :value="['super']">
|
<AccessControl :permissions="['super']">
|
||||||
<Button class="mr-4"> Super 角色可见 </Button>
|
<Button class="mr-4"> Super 角色可见 </Button>
|
||||||
</AccessControl>
|
</AccessControl>
|
||||||
<AccessControl :value="['admin']">
|
<AccessControl :permissions="['admin']">
|
||||||
<Button class="mr-4"> Admin 角色可见 </Button>
|
<Button class="mr-4"> Admin 角色可见 </Button>
|
||||||
</AccessControl>
|
</AccessControl>
|
||||||
<AccessControl :value="['user']">
|
<AccessControl :permissions="['user']">
|
||||||
<Button class="mr-4"> User 角色可见 </Button>
|
<Button class="mr-4"> User 角色可见 </Button>
|
||||||
</AccessControl>
|
</AccessControl>
|
||||||
<AccessControl :value="['super', 'admin']">
|
<AccessControl :permissions="['super', 'admin']">
|
||||||
<Button class="mr-4"> Super & Admin 角色可见 </Button>
|
<Button class="mr-4"> Super & Admin 角色可见 </Button>
|
||||||
</AccessControl>
|
</AccessControl>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="card-box mt-5 p-5 font-semibold">
|
<div class="card-box mt-5 p-5 font-semibold">
|
||||||
<div class="mb-3 text-lg">函数形式控制</div>
|
<div class="mb-3 text-lg">函数形式控制</div>
|
||||||
<Button v-if="hasAuthByCodes(['AC_100100'])" class="mr-4">
|
<Button v-if="hasAccessByCodes(['AC_100100'])" class="mr-4">
|
||||||
Super 账号可见 ["AC_1000001"]
|
Super 账号可见 ["AC_1000001"]
|
||||||
</Button>
|
</Button>
|
||||||
<Button v-if="hasAuthByCodes(['AC_100030'])" class="mr-4">
|
<Button v-if="hasAccessByCodes(['AC_100030'])" class="mr-4">
|
||||||
Admin 账号可见 ["AC_100010"]
|
Admin 账号可见 ["AC_100010"]
|
||||||
</Button>
|
</Button>
|
||||||
<Button v-if="hasAuthByCodes(['AC_1000001'])" class="mr-4">
|
<Button v-if="hasAccessByCodes(['AC_1000001'])" class="mr-4">
|
||||||
User 账号可见 ["AC_1000001"]
|
User 账号可见 ["AC_1000001"]
|
||||||
</Button>
|
</Button>
|
||||||
<Button v-if="hasAuthByCodes(['AC_100100', 'AC_1000001'])" class="mr-4">
|
<Button v-if="hasAccessByCodes(['AC_100100', 'AC_1000001'])" class="mr-4">
|
||||||
Super & Admin 账号可见 ["AC_100100","AC_1000001"]
|
Super & Admin 账号可见 ["AC_100100","AC_1000001"]
|
||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -66,11 +66,12 @@ const defaultPreferences: Preferences = {
|
||||||
expandOnHover: true,
|
expandOnHover: true,
|
||||||
extraCollapse: true,
|
extraCollapse: true,
|
||||||
hidden: false,
|
hidden: false,
|
||||||
width: 240,
|
width: 220,
|
||||||
},
|
},
|
||||||
tabbar: {
|
tabbar: {
|
||||||
dragable: true,
|
dragable: true,
|
||||||
enable: true,
|
enable: true,
|
||||||
|
height: 36,
|
||||||
keepAlive: true,
|
keepAlive: true,
|
||||||
persist: true,
|
persist: true,
|
||||||
showIcon: true,
|
showIcon: true,
|
||||||
|
|
|
@ -140,6 +140,8 @@ interface TabbarPreferences {
|
||||||
dragable: boolean;
|
dragable: boolean;
|
||||||
/** 是否开启多标签页 */
|
/** 是否开启多标签页 */
|
||||||
enable: boolean;
|
enable: boolean;
|
||||||
|
/** 标签页高度 */
|
||||||
|
height: number;
|
||||||
/** 开启标签页缓存功能 */
|
/** 开启标签页缓存功能 */
|
||||||
keepAlive: boolean;
|
keepAlive: boolean;
|
||||||
/** 是否持久化标签 */
|
/** 是否持久化标签 */
|
||||||
|
|
|
@ -44,7 +44,7 @@
|
||||||
"requestTimeout": "请求超时,请稍后再试。",
|
"requestTimeout": "请求超时,请稍后再试。",
|
||||||
"networkError": "网络异常,请检查您的网络连接后重试。",
|
"networkError": "网络异常,请检查您的网络连接后重试。",
|
||||||
"badRequest": "请求错误。请检查您的输入并重试。",
|
"badRequest": "请求错误。请检查您的输入并重试。",
|
||||||
"unauthorized": "未授权。请登录以继续。",
|
"unauthorized": "登录认证过期。请重新登录后继续。",
|
||||||
"forbidden": "禁止访问, 您没有权限访问此资源。",
|
"forbidden": "禁止访问, 您没有权限访问此资源。",
|
||||||
"notFound": "未找到, 请求的资源不存在。",
|
"notFound": "未找到, 请求的资源不存在。",
|
||||||
"internalServerError": "内部服务器错误,请稍后再试。"
|
"internalServerError": "内部服务器错误,请稍后再试。"
|
||||||
|
|
|
@ -45,7 +45,7 @@ const props = withDefaults(defineProps<Props>(), {
|
||||||
sidebarTheme: 'dark',
|
sidebarTheme: 'dark',
|
||||||
sidebarWidth: 180,
|
sidebarWidth: 180,
|
||||||
tabbarEnable: true,
|
tabbarEnable: true,
|
||||||
tabbarHeight: 38,
|
tabbarHeight: 36,
|
||||||
zIndex: 200,
|
zIndex: 200,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -12,7 +12,7 @@ const props = defineProps<{
|
||||||
<div
|
<div
|
||||||
:class="
|
:class="
|
||||||
cn(
|
cn(
|
||||||
'bg-card text-card-foreground border-border rounded-xl border shadow',
|
'bg-card text-card-foreground border-border rounded-xl border shadow-sm',
|
||||||
props.class,
|
props.class,
|
||||||
)
|
)
|
||||||
"
|
"
|
||||||
|
|
|
@ -7,17 +7,17 @@ import { computed } from 'vue';
|
||||||
import { useAccess } from './use-access';
|
import { useAccess } from './use-access';
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
|
/**
|
||||||
|
* Specified codes is visible
|
||||||
|
* @default []
|
||||||
|
*/
|
||||||
|
permissions?: string[];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 通过什么方式来控制组件,如果是 role,则传入角色,如果是 code,则传入权限码
|
* 通过什么方式来控制组件,如果是 role,则传入角色,如果是 code,则传入权限码
|
||||||
* @default 'role'
|
* @default 'role'
|
||||||
*/
|
*/
|
||||||
type?: 'code' | 'role';
|
type?: 'code' | 'role';
|
||||||
|
|
||||||
/**
|
|
||||||
* Specified codes is visible
|
|
||||||
* @default []
|
|
||||||
*/
|
|
||||||
value?: string[];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
defineOptions({
|
defineOptions({
|
||||||
|
@ -25,19 +25,21 @@ defineOptions({
|
||||||
});
|
});
|
||||||
|
|
||||||
const props = withDefaults(defineProps<Props>(), {
|
const props = withDefaults(defineProps<Props>(), {
|
||||||
|
permissions: () => [],
|
||||||
type: 'role',
|
type: 'role',
|
||||||
value: () => [],
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const { hasAuthByCodes, hasAuthByRoles } = useAccess();
|
const { hasAccessByCodes, hasAccessByRoles } = useAccess();
|
||||||
|
|
||||||
const hasAuth = computed(() => {
|
const hasAuth = computed(() => {
|
||||||
const { type, value } = props;
|
const { permissions, type } = props;
|
||||||
return type === 'role' ? hasAuthByRoles(value) : hasAuthByCodes(value);
|
return type === 'role'
|
||||||
|
? hasAccessByRoles(permissions)
|
||||||
|
: hasAccessByCodes(permissions);
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<slot v-if="!value"></slot>
|
<slot v-if="!permissions"></slot>
|
||||||
<slot v-else-if="hasAuth"></slot>
|
<slot v-else-if="hasAuth"></slot>
|
||||||
</template>
|
</template>
|
||||||
|
|
|
@ -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;
|
|
@ -1,3 +1,4 @@
|
||||||
export { default as AccessControl } from './access-control.vue';
|
export { default as AccessControl } from './access-control.vue';
|
||||||
|
export * from './directive';
|
||||||
export * from './generate-accessible';
|
export * from './generate-accessible';
|
||||||
export * from './use-access';
|
export * from './use-access';
|
||||||
|
|
|
@ -14,7 +14,7 @@ function useAccess() {
|
||||||
* @description: Determine whether there is permission,The role is judged by the user's role
|
* @description: Determine whether there is permission,The role is judged by the user's role
|
||||||
* @param roles
|
* @param roles
|
||||||
*/
|
*/
|
||||||
function hasAuthByRoles(roles: string[]) {
|
function hasAccessByRoles(roles: string[]) {
|
||||||
const userRoleSet = new Set(coreAccessStore.userRoles);
|
const userRoleSet = new Set(coreAccessStore.userRoles);
|
||||||
const intersection = roles.filter((item) => userRoleSet.has(item));
|
const intersection = roles.filter((item) => userRoleSet.has(item));
|
||||||
return intersection.length > 0;
|
return intersection.length > 0;
|
||||||
|
@ -25,7 +25,7 @@ function useAccess() {
|
||||||
* @description: Determine whether there is permission,The permission code is judged by the user's permission code
|
* @description: Determine whether there is permission,The permission code is judged by the user's permission code
|
||||||
* @param codes
|
* @param codes
|
||||||
*/
|
*/
|
||||||
function hasAuthByCodes(codes: string[]) {
|
function hasAccessByCodes(codes: string[]) {
|
||||||
const userCodesSet = new Set(coreAccessStore.accessCodes);
|
const userCodesSet = new Set(coreAccessStore.accessCodes);
|
||||||
|
|
||||||
const intersection = codes.filter((item) => userCodesSet.has(item));
|
const intersection = codes.filter((item) => userCodesSet.has(item));
|
||||||
|
@ -43,8 +43,8 @@ function useAccess() {
|
||||||
|
|
||||||
return {
|
return {
|
||||||
accessMode,
|
accessMode,
|
||||||
hasAuthByCodes,
|
hasAccessByCodes,
|
||||||
hasAuthByRoles,
|
hasAccessByRoles,
|
||||||
toggleAccessMode,
|
toggleAccessMode,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,19 +5,24 @@ import {
|
||||||
DialogContent,
|
DialogContent,
|
||||||
DialogDescription,
|
DialogDescription,
|
||||||
DialogTitle,
|
DialogTitle,
|
||||||
|
VbenAvatar,
|
||||||
VisuallyHidden,
|
VisuallyHidden,
|
||||||
} from '@vben-core/shadcn-ui';
|
} from '@vben-core/shadcn-ui';
|
||||||
|
|
||||||
import AuthenticationLogin from './login.vue';
|
import AuthenticationLogin from './login.vue';
|
||||||
import { AuthenticationProps, LoginAndRegisterParams } from './typings';
|
import { AuthenticationProps, LoginAndRegisterParams } from './typings';
|
||||||
|
|
||||||
interface Props extends AuthenticationProps {}
|
interface Props extends AuthenticationProps {
|
||||||
|
avatar?: string;
|
||||||
|
}
|
||||||
|
|
||||||
defineOptions({
|
defineOptions({
|
||||||
name: 'LoginExpiredModal',
|
name: 'LoginExpiredModal',
|
||||||
});
|
});
|
||||||
|
|
||||||
const props = withDefaults(defineProps<Props>(), {});
|
const props = withDefaults(defineProps<Props>(), {
|
||||||
|
avatar: '',
|
||||||
|
});
|
||||||
|
|
||||||
const emit = defineEmits<{
|
const emit = defineEmits<{
|
||||||
submit: [LoginAndRegisterParams];
|
submit: [LoginAndRegisterParams];
|
||||||
|
@ -37,8 +42,10 @@ const forwarded = useForwardPropsEmits(props, emit);
|
||||||
@escape-key-down="(e) => e.preventDefault()"
|
@escape-key-down="(e) => e.preventDefault()"
|
||||||
@interact-outside="(e) => e.preventDefault()"
|
@interact-outside="(e) => e.preventDefault()"
|
||||||
>
|
>
|
||||||
|
<DialogTitle>
|
||||||
|
<VbenAvatar :src="avatar" class="mx-auto size-20" />
|
||||||
|
</DialogTitle>
|
||||||
<VisuallyHidden>
|
<VisuallyHidden>
|
||||||
<DialogTitle />
|
|
||||||
<DialogDescription />
|
<DialogDescription />
|
||||||
</VisuallyHidden>
|
</VisuallyHidden>
|
||||||
<AuthenticationLogin
|
<AuthenticationLogin
|
||||||
|
|
|
@ -135,6 +135,7 @@ function clearPreferencesAndLogout() {
|
||||||
:sidebar-theme="theme"
|
:sidebar-theme="theme"
|
||||||
:sidebar-width="preferences.sidebar.width"
|
:sidebar-width="preferences.sidebar.width"
|
||||||
:tabbar-enable="preferences.tabbar.enable"
|
:tabbar-enable="preferences.tabbar.enable"
|
||||||
|
:tabbar-height="preferences.tabbar.height"
|
||||||
@side-mouse-leave="handleSideMouseLeave"
|
@side-mouse-leave="handleSideMouseLeave"
|
||||||
@toggle-sidebar="toggleSidebar"
|
@toggle-sidebar="toggleSidebar"
|
||||||
@update:sidebar-collapse="
|
@update:sidebar-collapse="
|
||||||
|
|
Loading…
Reference in New Issue