@unchainedshop/roles 5.0.0-alpha.5 → 5.0.0-alpha.7
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/README.md +10 -5
- package/lib/utils/permissions.d.ts +1 -1
- package/lib/utils/permissions.js +18 -17
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -20,6 +20,8 @@ import { Roles, Role } from '@unchainedshop/roles';
|
|
|
20
20
|
|
|
21
21
|
// Create a new role
|
|
22
22
|
const editorRole = new Role('editor');
|
|
23
|
+
Roles.addRole(editorRole);
|
|
24
|
+
Roles.registerAction('updateProduct');
|
|
23
25
|
|
|
24
26
|
// Allow actions for the role
|
|
25
27
|
editorRole.allow('updateProduct', async (product, params, context) => {
|
|
@@ -42,6 +44,7 @@ const allowed = await Roles.userHasPermission(
|
|
|
42
44
|
| Method | Description |
|
|
43
45
|
|--------|-------------|
|
|
44
46
|
| `Roles.registerAction` | Register a new action name |
|
|
47
|
+
| `Roles.addRole` | Register a role instance |
|
|
45
48
|
| `Roles.registerHelper` | Register a new helper name |
|
|
46
49
|
| `Roles.getUserRoles` | Get user roles including special roles |
|
|
47
50
|
| `Roles.allow` | Check if any role allows an action |
|
|
@@ -51,12 +54,14 @@ const allowed = await Roles.userHasPermission(
|
|
|
51
54
|
|
|
52
55
|
| Method | Description |
|
|
53
56
|
|--------|-------------|
|
|
54
|
-
| `new Role(name)` | Create
|
|
57
|
+
| `new Role(name)` | Create a role; register it with `roles.addRole()` |
|
|
55
58
|
| `role.allow(action, fn)` | Add allow rule for an action |
|
|
56
59
|
| `role.helper(name, fn)` | Add helper function to role |
|
|
57
60
|
|
|
58
61
|
### Built-in Roles
|
|
59
62
|
|
|
63
|
+
`createRoles()` returns an empty role registry. Call `initDefaultRoles(registry)` to add the following roles, or use the API layer's `roles.configureRoles()` to configure the platform's permission rules.
|
|
64
|
+
|
|
60
65
|
| Role | Description |
|
|
61
66
|
|------|-------------|
|
|
62
67
|
| `Roles.adminRole` | Admin role with elevated privileges |
|
|
@@ -70,7 +75,7 @@ const allowed = await Roles.userHasPermission(
|
|
|
70
75
|
| `__all__` | Automatically assigned to everyone |
|
|
71
76
|
| `__loggedIn__` | Automatically assigned to authenticated users |
|
|
72
77
|
| `__notLoggedIn__` | Automatically assigned to anonymous users |
|
|
73
|
-
| `__notAdmin__` | Automatically assigned to non-admin users |
|
|
78
|
+
| `__notAdmin__` | Automatically assigned to authenticated non-admin users |
|
|
74
79
|
|
|
75
80
|
### Utility Functions
|
|
76
81
|
|
|
@@ -93,13 +98,13 @@ const allowed = await Roles.userHasPermission(
|
|
|
93
98
|
## Configuration
|
|
94
99
|
|
|
95
100
|
```typescript
|
|
96
|
-
import type
|
|
101
|
+
import { Role, type IRoleOptionConfig } from '@unchainedshop/roles';
|
|
97
102
|
|
|
98
103
|
const config: IRoleOptionConfig = {
|
|
99
104
|
additionalRoles: {
|
|
100
105
|
customRole: (roles, actions) => {
|
|
101
|
-
const role = new Role('customRole');
|
|
102
|
-
role.allow(actions.
|
|
106
|
+
const role = roles.addRole(new Role('customRole'));
|
|
107
|
+
role.allow(actions.CUSTOM_ACTION, async () => true);
|
|
103
108
|
},
|
|
104
109
|
},
|
|
105
110
|
additionalActions: ['CUSTOM_ACTION'],
|
|
@@ -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,29 +1,30 @@
|
|
|
1
1
|
export const permissions = async (userRoles, allRoles) => {
|
|
2
|
-
const
|
|
2
|
+
const rolesByName = new Map();
|
|
3
3
|
for (const r of Object.values(allRoles)) {
|
|
4
|
-
|
|
4
|
+
if (r?.name)
|
|
5
|
+
rolesByName.set(r.name, r);
|
|
5
6
|
}
|
|
6
|
-
const
|
|
7
|
+
const result = [];
|
|
8
|
+
const seen = new Set();
|
|
7
9
|
for (const role of userRoles || []) {
|
|
8
|
-
const foundRole =
|
|
10
|
+
const foundRole = rolesByName.get(role);
|
|
9
11
|
if (!foundRole)
|
|
10
12
|
continue;
|
|
11
|
-
for (const [
|
|
13
|
+
for (const [actionName, funcs] of Object.entries(foundRole.allowRules || {})) {
|
|
14
|
+
if (seen.has(actionName))
|
|
15
|
+
continue;
|
|
12
16
|
for (const f of funcs) {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
return null;
|
|
17
|
+
try {
|
|
18
|
+
if (await f(null, null, null)) {
|
|
19
|
+
seen.add(actionName);
|
|
20
|
+
result.push(actionName);
|
|
21
|
+
break;
|
|
19
22
|
}
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
})());
|
|
23
|
+
}
|
|
24
|
+
catch {
|
|
25
|
+
}
|
|
24
26
|
}
|
|
25
27
|
}
|
|
26
28
|
}
|
|
27
|
-
|
|
28
|
-
return [...new Set(resolvedActions)].filter(Boolean).toSorted((a, b) => a.localeCompare(b));
|
|
29
|
+
return result.sort((a, b) => a.localeCompare(b));
|
|
29
30
|
};
|
package/package.json
CHANGED