Skip to content

Commit 73f00e3

Browse files
authored
fix(vue3): 添加配置authService的功能以支持严格权限判断 (#353)
1 parent 89f62fd commit 73f00e3

3 files changed

Lines changed: 58 additions & 9 deletions

File tree

packages/basic/src/init/auth/authService.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export interface IService {
4848
resourceType: string;
4949
}>,
5050
) => void;
51+
_getResourceMap?: () => Map<string, any>;
5152
}
5253

5354
let _map;
@@ -299,6 +300,10 @@ const Service: IService = {
299300
return (_map && _map.has(authPath)) || false;
300301
},
301302

303+
_getResourceMap() {
304+
return _map;
305+
},
306+
302307
_setCustomResources(list = []) {
303308
_map = new Map();
304309
list.forEach((resource) =>

packages/basic/src/init/auth/index.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,44 @@
1-
import authService from "./authService";
2-
import type { NASLUserInfo } from "./authService";
3-
import Global from "../../global";
4-
import Config from "../../config";
1+
import authService from './authService';
2+
import type { NASLUserInfo } from './authService';
3+
import Global from '../../global';
4+
import Config from '../../config';
55

66
function initAuth(
77
options: {
88
allowList?: string[];
99
router?: any;
1010
base?: string;
11+
configureAuthService?: (service: typeof authService) => void;
1112
} = {},
1213
) {
14+
// 留一个配置,可以改写authService的实现
15+
if (typeof options.configureAuthService === 'function') {
16+
options.configureAuthService(authService);
17+
}
18+
1319
authService.start();
1420
options.allowList = options.allowList || [];
1521
const router = options.router;
16-
const base = (options.base || "").replace(/\/$/, "");
22+
const base = (options.base || '').replace(/\/$/, '');
1723
/**
1824
* 是否有当前路由下的子权限
1925
* 该方法只能在 Global 中调用
2026
* @param {*} subPath 子权限路径,如 /createButton/enabled
2127
*/
2228
authService.hasSub = function (subPath) {
2329
const currentPath = base + router.currentRoute.path;
24-
if (subPath[0] !== "/") subPath = "/" + subPath;
30+
if (subPath[0] !== '/') subPath = '/' + subPath;
2531
return this.has(currentPath + subPath);
2632
};
2733
authService.hasFullPath = function (path) {
28-
if (path[0] !== "/") path = "/" + path;
34+
if (path[0] !== '/') path = '/' + path;
2935
return this.has(base + path);
3036
};
3137

3238
/**
3339
* 账号与权限中心
3440
*/
35-
Config.globalProperties.set("$auth", authService);
41+
Config.globalProperties.set('$auth', authService);
3642

3743
return {
3844
auth: authService,

packages/vue3/source/src/common/plugins/auth/index.js

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,45 @@ import { initAuth, authService } from '@lcap/basic-template';
22

33
export default {
44
install(vm, options = {}) {
5-
initAuth(options);
5+
initAuth({
6+
...options,
7+
configureAuthService(service) {
8+
/**
9+
* 严格的权限判断,父级权限未配置时,子权限即使配置了也无效
10+
* 例如:当只有 /dashboard/entity/list 配置了权限,而没有 /dashboard/entity 时,访问 /dashboard/entity/list 的权限会失效,并在控制台警告缺少 /dashboard/entity 权限
11+
* 反之,当 /dashboard/entity 和 /dashboard/entity/list 都配置了权限时,访问 /dashboard/entity/list 的权限才会生效
12+
*/
13+
service.has = function(authPath) {
14+
const _map = service._getResourceMap();
15+
16+
if (!_map) {
17+
console.warn('权限资源未获取到,请检查权限资源接口');
18+
return false;
19+
}
20+
21+
let hasPermission = true;
22+
23+
const authPathSegments = authPath.split('/').filter(Boolean);
24+
const parentAuthPaths = authPathSegments.reduce((acc, segment) => {
25+
const lastPath = acc.length > 0 ? acc[acc.length - 1] : '';
26+
const newPath = `${lastPath}/${segment}`;
27+
acc.push(newPath);
28+
return acc;
29+
}, []);
30+
31+
while (parentAuthPaths.length > 0) {
32+
const path = parentAuthPaths.shift();
33+
if (!_map.has(path)) {
34+
hasPermission = false;
35+
console.warn(`权限资源:缺少权限 ${path},请确认是否已配置该权限项`);
36+
break;
37+
}
38+
}
39+
40+
return hasPermission;
41+
}
42+
}
43+
});
644

745
const base = (options.base || '').replace(/\/$/, '');
846
/**

0 commit comments

Comments
 (0)