blob: 2b39e48c71285992bfc03c99903eaa09214c7e5f [file] [log] [blame]
Jiarenxiang38dcb052025-03-13 16:40:09 +08001// /**
2// * 字符权限校验
3// * @param {Array} value 校验值
4// * @returns {Boolean}
5
6// import Role = API.System.Role;
7
8export function matchPerms(permissions: string[], value: string[]) {
9 if (value && value instanceof Array && value.length > 0) {
10 const permissionDatas = value;
11 const all_permission = '*:*:*';
12 const hasPermission = permissions.some((permission) => {
13 return all_permission === permission || permissionDatas.includes(permission);
14 });
15 if (!hasPermission) {
16 return false;
17 }
18 return true;
19 }
20 console.error(`need roles! Like checkPermi="['system:user:add','system:user:edit']"`);
21 return false;
22}
23
24export function matchPerm(permissions: string[], value: string) {
25 if (value && value.length > 0) {
26 const permissionDatas = value;
27 const all_permission = '*:*:*';
28 const hasPermission = permissions.some((permission) => {
29 return all_permission === permission || permissionDatas === permission;
30 });
31 if (!hasPermission) {
32 return false;
33 }
34 return true;
35 }
36 console.error(`need roles! Like checkPermi="['system:user:add','system:user:edit']"`);
37 return false;
38}
39
40export function matchPermission(permissions: string[] | undefined, value: any): boolean {
41 if (permissions === undefined) return false;
42 const type = typeof value;
43 if (type === 'string') {
44 return matchPerm(permissions, value);
45 }
46 return matchPerms(permissions, value);
47}
48
49/**
50 * 角色权限校验
51 * @param roles
52 * @param {Array} value 校验值
53 * @returns {Boolean}
54 */
55export function checkRole(roles: Role[] | undefined, value: string[]) {
56 if (roles && value && value.length > 0) {
57 for (let i = 0; i < roles?.length; i++) {
58 for (let j = 0; j < value?.length; j++) {
59 if (value[j] === roles[i].roleKey) {
60 return true;
61 }
62 }
63 }
64 }
65 console.error(`need roles! Like checkRole="['admin','editor']"`);
66 return false;
67}