@unchainedshop/roles 5.0.0-alpha.1 → 5.0.0-alpha.10

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 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 and register a new role |
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 { IRoleOptionConfig } from '@unchainedshop/roles';
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.READ, () => true);
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<(string | null)[]>;
1
+ export declare const permissions: (userRoles: any, allRoles: any) => Promise<string[]>;
@@ -1,29 +1,30 @@
1
1
  export const permissions = async (userRoles, allRoles) => {
2
- const roleMap = new Map();
2
+ const rolesByName = new Map();
3
3
  for (const r of Object.values(allRoles)) {
4
- roleMap.set(r.name, r);
4
+ if (r?.name)
5
+ rolesByName.set(r.name, r);
5
6
  }
6
- const actions = [];
7
+ const result = [];
8
+ const seen = new Set();
7
9
  for (const role of userRoles || []) {
8
- const foundRole = roleMap.get(role);
10
+ const foundRole = rolesByName.get(role);
9
11
  if (!foundRole)
10
12
  continue;
11
- for (const [roleName, funcs] of Object.entries(foundRole.allowRules || {})) {
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
- actions.push((async () => {
14
- try {
15
- const r = await f(null, null, null);
16
- if (r)
17
- return roleName;
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
- catch {
21
- return null;
22
- }
23
- })());
23
+ }
24
+ catch {
25
+ }
24
26
  }
25
27
  }
26
28
  }
27
- const resolvedActions = await Promise.all(actions);
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
@@ -1,6 +1,9 @@
1
1
  {
2
2
  "name": "@unchainedshop/roles",
3
- "version": "5.0.0-alpha.1",
3
+ "version": "5.0.0-alpha.10",
4
+ "files": [
5
+ "lib"
6
+ ],
4
7
  "description": "Role-based access control system for the Unchained Engine",
5
8
  "main": "lib/roles-index.js",
6
9
  "types": "lib/roles-index.d.ts",
@@ -34,7 +37,7 @@
34
37
  },
35
38
  "homepage": "https://github.com/unchainedshop/unchained#readme",
36
39
  "devDependencies": {
37
- "@types/node": "^25.0.0",
38
- "typescript": "^5.8.3"
40
+ "@types/node": "^26.2.0",
41
+ "typescript": "^6.0.3"
39
42
  }
40
43
  }