acl-next 1.0.0
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/LICENSE +23 -0
- package/README.md +174 -0
- package/dist/index.cjs +679 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +346 -0
- package/dist/index.d.ts +346 -0
- package/dist/index.js +667 -0
- package/dist/index.js.map +1 -0
- package/package.json +90 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2011-2013 Manuel Astudillo <manuel@optimalbits.com> (original node_acl)
|
|
4
|
+
Copyright (c) 2026 Nordin Zahari <vipnordin@gmail.com> (acl-next)
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
7
|
+
a copy of this software and associated documentation files (the
|
|
8
|
+
'Software'), to deal in the Software without restriction, including
|
|
9
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
10
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
11
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
12
|
+
the following conditions:
|
|
13
|
+
|
|
14
|
+
The above copyright notice and this permission notice shall be
|
|
15
|
+
included in all copies or substantial portions of the Software.
|
|
16
|
+
|
|
17
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
|
18
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
19
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
20
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
21
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
22
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
23
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
# acl-next
|
|
2
|
+
|
|
3
|
+
> Modern TypeScript Access Control Lists (ACL / RBAC) for Node.js — with Redis, MongoDB and in-memory backends, and Express middleware.
|
|
4
|
+
|
|
5
|
+
A maintained, modernized fork of [optimalbits/node_acl](https://github.com/optimalbits/node_acl) (MIT). Same proven model — users → roles → resources → permissions, with role hierarchies — rebuilt as **TypeScript**, **promise-native**, and **zero runtime dependencies**.
|
|
6
|
+
|
|
7
|
+
## What changed from `node_acl`
|
|
8
|
+
|
|
9
|
+
- **TypeScript**, with full type declarations.
|
|
10
|
+
- **Promise-only** API — the legacy callback signatures are gone (use `await`).
|
|
11
|
+
- **No runtime dependencies.** `bluebird`, `lodash` and `async` are removed. `redis` and `mongodb` are now **optional peer dependencies** — install only the driver you use.
|
|
12
|
+
- Modern drivers: **redis v4+**, **mongodb v4+**.
|
|
13
|
+
- IDs are normalized to **strings** in stored/returned values.
|
|
14
|
+
- Dual **ESM + CommonJS** build.
|
|
15
|
+
|
|
16
|
+
See the [Migration guide](#migration-from-node_acl) below.
|
|
17
|
+
|
|
18
|
+
## Install
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
npm install acl-next
|
|
22
|
+
# plus the backend driver you use (optional peer deps):
|
|
23
|
+
npm install redis # for RedisBackend
|
|
24
|
+
npm install mongodb # for MongoDBBackend
|
|
25
|
+
# MemoryBackend needs nothing
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Quick start
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
import { Acl, MemoryBackend } from "acl-next";
|
|
32
|
+
|
|
33
|
+
const acl = new Acl(new MemoryBackend());
|
|
34
|
+
|
|
35
|
+
// Roles get permissions over resources (roles/resources created implicitly):
|
|
36
|
+
await acl.allow("guest", "blogs", "view");
|
|
37
|
+
await acl.allow("member", "blogs", ["edit", "view", "delete"]);
|
|
38
|
+
|
|
39
|
+
// Users get roles (users created implicitly):
|
|
40
|
+
await acl.addUserRoles("joed", "guest");
|
|
41
|
+
|
|
42
|
+
// Query:
|
|
43
|
+
await acl.isAllowed("joed", "blogs", "view"); // => true
|
|
44
|
+
await acl.isAllowed("joed", "blogs", "edit"); // => false
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### Role hierarchies
|
|
48
|
+
|
|
49
|
+
```ts
|
|
50
|
+
await acl.addRoleParents("baz", ["foo", "bar"]); // baz inherits foo + bar
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### Bulk permissions
|
|
54
|
+
|
|
55
|
+
```ts
|
|
56
|
+
await acl.allow([
|
|
57
|
+
{
|
|
58
|
+
roles: ["guest", "member"],
|
|
59
|
+
allows: [
|
|
60
|
+
{ resources: "blogs", permissions: "get" },
|
|
61
|
+
{ resources: ["forums", "news"], permissions: ["get", "put", "delete"] },
|
|
62
|
+
],
|
|
63
|
+
},
|
|
64
|
+
]);
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### Wildcard
|
|
68
|
+
|
|
69
|
+
```ts
|
|
70
|
+
await acl.allow("admin", ["blogs", "forums"], "*"); // all permissions
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## Backends
|
|
74
|
+
|
|
75
|
+
```ts
|
|
76
|
+
import { Acl, RedisBackend, MongoDBBackend, MemoryBackend } from "acl-next";
|
|
77
|
+
|
|
78
|
+
// In-memory (no deps) — great for tests / single process:
|
|
79
|
+
new Acl(new MemoryBackend());
|
|
80
|
+
|
|
81
|
+
// Redis (node-redis v4+):
|
|
82
|
+
import { createClient } from "redis";
|
|
83
|
+
const redis = createClient();
|
|
84
|
+
await redis.connect();
|
|
85
|
+
new Acl(new RedisBackend(redis, "acl" /* key prefix */));
|
|
86
|
+
|
|
87
|
+
// MongoDB (mongodb v4+):
|
|
88
|
+
import { MongoClient } from "mongodb";
|
|
89
|
+
const client = new MongoClient("mongodb://localhost:27017");
|
|
90
|
+
await client.connect();
|
|
91
|
+
new Acl(new MongoDBBackend(client.db("mydb"), { prefix: "acl_", useSingle: false }));
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
Bring your own backend by implementing the `Backend<T>` interface (see [`src/types.ts`](src/types.ts)).
|
|
95
|
+
|
|
96
|
+
## Express middleware
|
|
97
|
+
|
|
98
|
+
```ts
|
|
99
|
+
import { aclErrorHandler } from "acl-next";
|
|
100
|
+
|
|
101
|
+
// Protect a route — resource defaults to req.url, permission to req.method:
|
|
102
|
+
app.put("/blogs/:id", acl.middleware(), handler);
|
|
103
|
+
|
|
104
|
+
// Only the first N path components form the resource name:
|
|
105
|
+
app.put("/blogs/:id/comments/:commentId", acl.middleware(3), handler);
|
|
106
|
+
|
|
107
|
+
// Custom userId (value or resolver) and explicit permission:
|
|
108
|
+
app.put("/blogs/:id", acl.middleware(3, (req) => req.user.id, "post"), handler);
|
|
109
|
+
|
|
110
|
+
// Render the 401/403 errors it raises:
|
|
111
|
+
app.use(aclErrorHandler("json")); // or "html", or omit for plain text
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
The middleware resolves the user from (in order): the `userId` argument, `req.session.userId`, then `req.user.id`.
|
|
115
|
+
|
|
116
|
+
## API
|
|
117
|
+
|
|
118
|
+
All methods return Promises.
|
|
119
|
+
|
|
120
|
+
| Method | Description |
|
|
121
|
+
| --- | --- |
|
|
122
|
+
| `addUserRoles(userId, roles)` | Assign role(s) to a user |
|
|
123
|
+
| `removeUserRoles(userId, roles)` | Remove role(s) from a user |
|
|
124
|
+
| `userRoles(userId)` | Roles assigned to a user |
|
|
125
|
+
| `roleUsers(role)` | Users that have a role |
|
|
126
|
+
| `hasRole(userId, role)` | Whether a user has a role |
|
|
127
|
+
| `addRoleParents(role, parents)` | Add parent role(s) (inheritance) |
|
|
128
|
+
| `removeRoleParents(role, parents?)` | Remove parent role(s) (all if omitted) |
|
|
129
|
+
| `removeRole(role)` | Remove a role and its permissions |
|
|
130
|
+
| `removeResource(resource)` | Remove a resource |
|
|
131
|
+
| `allow(roles, resources, permissions)` / `allow(rules[])` | Grant permissions |
|
|
132
|
+
| `removeAllow(role, resources, permissions?)` | Revoke permissions |
|
|
133
|
+
| `allowedPermissions(userId, resources)` | Map of resource → permissions for a user |
|
|
134
|
+
| `isAllowed(userId, resource, permissions)` | Whether a user has all permissions |
|
|
135
|
+
| `areAnyRolesAllowed(roles, resource, permissions)` | Whether any role qualifies |
|
|
136
|
+
| `whatResources(roles)` / `whatResources(roles, permissions)` | Resources a role can access |
|
|
137
|
+
| `middleware(numPathComponents?, userId?, actions?)` | Express middleware factory |
|
|
138
|
+
|
|
139
|
+
## Migration from `node_acl`
|
|
140
|
+
|
|
141
|
+
1. **Rename the import:** `acl` → `acl-next`.
|
|
142
|
+
2. **Drop callbacks, use `await`:**
|
|
143
|
+
|
|
144
|
+
```ts
|
|
145
|
+
// before
|
|
146
|
+
acl.isAllowed("joed", "blogs", "view", (err, allowed) => { ... });
|
|
147
|
+
// after
|
|
148
|
+
const allowed = await acl.isAllowed("joed", "blogs", "view");
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
3. **Constructor uses imported backends** (no more `new acl.redisBackend(...)`):
|
|
152
|
+
|
|
153
|
+
```ts
|
|
154
|
+
import { Acl, RedisBackend } from "acl-next";
|
|
155
|
+
const acl = new Acl(new RedisBackend(redisClient));
|
|
156
|
+
```
|
|
157
|
+
4. **MongoDB options are an object:** `new MongoDBBackend(db, { prefix, useSingle })` instead of positional args.
|
|
158
|
+
5. **Upgrade drivers** to `redis@4+` / `mongodb@4+`.
|
|
159
|
+
6. **Numeric IDs come back as strings** (e.g. `roleUsers` returns `["3"]`, not `[3]`).
|
|
160
|
+
|
|
161
|
+
## Development
|
|
162
|
+
|
|
163
|
+
```bash
|
|
164
|
+
npm run build # ESM + CJS + .d.ts via tsup
|
|
165
|
+
npm run typecheck # tsc --noEmit
|
|
166
|
+
npm run lint # biome
|
|
167
|
+
npm test # vitest (Redis/Mongo suites use testcontainers → Docker required)
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
The Redis/MongoDB suites spin up real databases with [testcontainers](https://testcontainers.com/) (needs Docker). The Memory and unit suites run without Docker.
|
|
171
|
+
|
|
172
|
+
## License
|
|
173
|
+
|
|
174
|
+
[MIT](LICENSE). Original work © 2011-2013 Manuel Astudillo; modernization © 2026 Nordin Zahari.
|