@unchainedshop/roles 4.8.2 → 4.8.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/utils/permissions.d.ts +1 -1
- package/lib/utils/permissions.js +23 -15
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const permissions: (userRoles: any, allRoles: any) => Promise<
|
|
1
|
+
export declare const permissions: (userRoles: any, allRoles: any) => Promise<string[]>;
|
package/lib/utils/permissions.js
CHANGED
|
@@ -1,22 +1,30 @@
|
|
|
1
1
|
export const permissions = async (userRoles, allRoles) => {
|
|
2
|
-
const
|
|
3
|
-
|
|
2
|
+
const rolesByName = new Map();
|
|
3
|
+
for (const r of Object.values(allRoles)) {
|
|
4
|
+
if (r?.name)
|
|
5
|
+
rolesByName.set(r.name, r);
|
|
6
|
+
}
|
|
7
|
+
const result = [];
|
|
8
|
+
const seen = new Set();
|
|
9
|
+
for (const role of userRoles || []) {
|
|
10
|
+
const foundRole = rolesByName.get(role);
|
|
4
11
|
if (!foundRole)
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
12
|
+
continue;
|
|
13
|
+
for (const [actionName, funcs] of Object.entries(foundRole.allowRules || {})) {
|
|
14
|
+
if (seen.has(actionName))
|
|
15
|
+
continue;
|
|
16
|
+
for (const f of funcs) {
|
|
8
17
|
try {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
18
|
+
if (await f(null, null, null)) {
|
|
19
|
+
seen.add(actionName);
|
|
20
|
+
result.push(actionName);
|
|
21
|
+
break;
|
|
22
|
+
}
|
|
13
23
|
}
|
|
14
24
|
catch {
|
|
15
|
-
return null;
|
|
16
25
|
}
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
return [...new Set(resolvedActions)].filter(Boolean).toSorted((a, b) => a.localeCompare(b));
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return result.sort((a, b) => a.localeCompare(b));
|
|
22
30
|
};
|