@anievo-id/rbac-evolution 0.0.0-stage → 0.1.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 +21 -0
- package/README.md +240 -2
- package/dist/adapter-BBs9C5pT.d.cts +130 -0
- package/dist/adapter-BBs9C5pT.d.ts +130 -0
- package/dist/chunk-JQ53Q7UP.js +734 -0
- package/dist/chunk-JQ53Q7UP.js.map +1 -0
- package/dist/index.cjs +867 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +402 -0
- package/dist/index.d.ts +402 -0
- package/dist/index.js +121 -0
- package/dist/index.js.map +1 -0
- package/dist/testing.cjs +913 -0
- package/dist/testing.cjs.map +1 -0
- package/dist/testing.d.cts +13 -0
- package/dist/testing.d.ts +13 -0
- package/dist/testing.js +178 -0
- package/dist/testing.js.map +1 -0
- package/package.json +70 -3
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,867 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/cache.ts
|
|
4
|
+
var MemoryCache = class {
|
|
5
|
+
store = /* @__PURE__ */ new Map();
|
|
6
|
+
get(key2) {
|
|
7
|
+
const entry = this.store.get(key2);
|
|
8
|
+
if (!entry) return void 0;
|
|
9
|
+
if (entry.expiresAt <= Date.now()) {
|
|
10
|
+
this.store.delete(key2);
|
|
11
|
+
return void 0;
|
|
12
|
+
}
|
|
13
|
+
return entry.value;
|
|
14
|
+
}
|
|
15
|
+
set(key2, value, ttlMs) {
|
|
16
|
+
this.store.set(key2, { value, expiresAt: Date.now() + ttlMs });
|
|
17
|
+
}
|
|
18
|
+
clear() {
|
|
19
|
+
this.store.clear();
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
// src/errors.ts
|
|
24
|
+
var RbacError = class extends Error {
|
|
25
|
+
code;
|
|
26
|
+
constructor(code, message) {
|
|
27
|
+
super(message);
|
|
28
|
+
this.name = new.target.name;
|
|
29
|
+
this.code = code;
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
var PermissionDoesNotExist = class extends RbacError {
|
|
33
|
+
constructor(name, guardName) {
|
|
34
|
+
super("PERMISSION_DOES_NOT_EXIST", `There is no permission named \`${name}\` for guard \`${guardName}\`.`);
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
var PermissionAlreadyExists = class extends RbacError {
|
|
38
|
+
constructor(name, guardName) {
|
|
39
|
+
super("PERMISSION_ALREADY_EXISTS", `A permission \`${name}\` already exists for guard \`${guardName}\`.`);
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
var RoleDoesNotExist = class extends RbacError {
|
|
43
|
+
constructor(name, guardName) {
|
|
44
|
+
super("ROLE_DOES_NOT_EXIST", `There is no role named \`${name}\` for guard \`${guardName}\`.`);
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
var RoleAlreadyExists = class extends RbacError {
|
|
48
|
+
constructor(name, guardName) {
|
|
49
|
+
super("ROLE_ALREADY_EXISTS", `A role \`${name}\` already exists for guard \`${guardName}\`.`);
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
var GuardDoesNotMatch = class extends RbacError {
|
|
53
|
+
constructor(given, expected) {
|
|
54
|
+
super("GUARD_DOES_NOT_MATCH", `The given role or permission uses guard \`${given}\` instead of \`${expected}\`.`);
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
var InvalidWildcardPermission = class extends RbacError {
|
|
58
|
+
constructor(permission) {
|
|
59
|
+
super("INVALID_WILDCARD_PERMISSION", `Wildcard permission \`${permission}\` is not valid.`);
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
// src/events.ts
|
|
64
|
+
var RbacEmitter = class {
|
|
65
|
+
#listeners = /* @__PURE__ */ new Map();
|
|
66
|
+
on(event, listener) {
|
|
67
|
+
let set = this.#listeners.get(event);
|
|
68
|
+
if (!set) {
|
|
69
|
+
set = /* @__PURE__ */ new Set();
|
|
70
|
+
this.#listeners.set(event, set);
|
|
71
|
+
}
|
|
72
|
+
set.add(listener);
|
|
73
|
+
return () => this.off(event, listener);
|
|
74
|
+
}
|
|
75
|
+
off(event, listener) {
|
|
76
|
+
this.#listeners.get(event)?.delete(listener);
|
|
77
|
+
}
|
|
78
|
+
has(event) {
|
|
79
|
+
return (this.#listeners.get(event)?.size ?? 0) > 0;
|
|
80
|
+
}
|
|
81
|
+
/** Calls the listeners one by one, in registration order, awaiting each. */
|
|
82
|
+
async emit(payload) {
|
|
83
|
+
const set = this.#listeners.get(payload.event);
|
|
84
|
+
if (!set || set.size === 0) return;
|
|
85
|
+
for (const listener of [...set]) {
|
|
86
|
+
await listener(payload);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
// src/wildcard.ts
|
|
92
|
+
var WILDCARD_TOKEN = "*";
|
|
93
|
+
var PART_DELIMITER = ".";
|
|
94
|
+
var SUBPART_DELIMITER = ",";
|
|
95
|
+
function parseWildcardPermission(permission) {
|
|
96
|
+
const trimmed = permission.trim();
|
|
97
|
+
if (trimmed === "") throw new InvalidWildcardPermission(permission);
|
|
98
|
+
return trimmed.split(PART_DELIMITER).map((part) => {
|
|
99
|
+
const subparts = part.split(SUBPART_DELIMITER).map((s) => s.trim());
|
|
100
|
+
if (subparts.some((s) => s === "")) throw new InvalidWildcardPermission(permission);
|
|
101
|
+
return subparts;
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
function wildcardImplies(granted, required) {
|
|
105
|
+
const grantedParts = parseWildcardPermission(granted);
|
|
106
|
+
const requiredParts = parseWildcardPermission(required);
|
|
107
|
+
for (let i = 0; i < grantedParts.length; i++) {
|
|
108
|
+
const grantedPart = grantedParts[i];
|
|
109
|
+
const requiredPart = requiredParts[i];
|
|
110
|
+
if (requiredPart === void 0) {
|
|
111
|
+
return grantedParts.slice(i).every((part) => part.includes(WILDCARD_TOKEN));
|
|
112
|
+
}
|
|
113
|
+
if (grantedPart.includes(WILDCARD_TOKEN)) continue;
|
|
114
|
+
if (requiredPart.includes(WILDCARD_TOKEN)) return false;
|
|
115
|
+
if (!requiredPart.every((sub) => grantedPart.includes(sub))) return false;
|
|
116
|
+
}
|
|
117
|
+
return true;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// src/rbac.ts
|
|
121
|
+
var DAY_MS = 24 * 60 * 60 * 1e3;
|
|
122
|
+
var idKey = (id) => id === null ? "\0null" : String(id);
|
|
123
|
+
function flatten(items) {
|
|
124
|
+
const out = [];
|
|
125
|
+
for (const item of items) {
|
|
126
|
+
if (Array.isArray(item)) out.push(...item);
|
|
127
|
+
else out.push(item);
|
|
128
|
+
}
|
|
129
|
+
return out;
|
|
130
|
+
}
|
|
131
|
+
function uniqueById(items) {
|
|
132
|
+
const seen = /* @__PURE__ */ new Set();
|
|
133
|
+
return items.filter((item) => {
|
|
134
|
+
const k = idKey(item.id);
|
|
135
|
+
if (seen.has(k)) return false;
|
|
136
|
+
seen.add(k);
|
|
137
|
+
return true;
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
function uniqueIds(ids) {
|
|
141
|
+
const seen = /* @__PURE__ */ new Set();
|
|
142
|
+
return ids.filter((id) => {
|
|
143
|
+
const k = idKey(id);
|
|
144
|
+
if (seen.has(k)) return false;
|
|
145
|
+
seen.add(k);
|
|
146
|
+
return true;
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
function filterByIds(items, ids, keep) {
|
|
150
|
+
const set = new Set(ids.map(idKey));
|
|
151
|
+
return items.filter((item) => set.has(idKey(item.id)) === keep);
|
|
152
|
+
}
|
|
153
|
+
function normalizeName(name) {
|
|
154
|
+
const trimmed = name.trim();
|
|
155
|
+
if (trimmed === "") throw new RbacError("INVALID_NAME", "Role and permission names must not be empty.");
|
|
156
|
+
return trimmed;
|
|
157
|
+
}
|
|
158
|
+
var Rbac = class _Rbac {
|
|
159
|
+
adapter;
|
|
160
|
+
defaultGuard;
|
|
161
|
+
wildcard;
|
|
162
|
+
teams;
|
|
163
|
+
#cache;
|
|
164
|
+
#cacheTtl;
|
|
165
|
+
#before;
|
|
166
|
+
#superAdminRoles;
|
|
167
|
+
#events = new RbacEmitter();
|
|
168
|
+
constructor(options) {
|
|
169
|
+
this.adapter = options.adapter;
|
|
170
|
+
this.defaultGuard = options.defaultGuard ?? "web";
|
|
171
|
+
this.wildcard = options.wildcard ?? false;
|
|
172
|
+
this.teams = options.teams ?? false;
|
|
173
|
+
this.#cache = options.cache === false ? null : options.cache ?? new MemoryCache();
|
|
174
|
+
this.#cacheTtl = options.cacheTtl ?? DAY_MS;
|
|
175
|
+
this.#before = options.before;
|
|
176
|
+
this.#superAdminRoles = options.superAdminRoles ?? [];
|
|
177
|
+
}
|
|
178
|
+
/** Work with the roles & permissions of a subject (usually a user). */
|
|
179
|
+
for(subject, options = {}) {
|
|
180
|
+
return new SubjectRbac(this, subject, this.resolveGuard(options.guard), this.resolveTeam(options.team));
|
|
181
|
+
}
|
|
182
|
+
/** Work with the permissions of a role. */
|
|
183
|
+
role(role, options = {}) {
|
|
184
|
+
if (typeof role !== "string") return new RoleRbac(this, role, role.guardName, role.teamId);
|
|
185
|
+
return new RoleRbac(this, role, this.resolveGuard(options.guard), this.resolveTeam(options.team));
|
|
186
|
+
}
|
|
187
|
+
// ----------------------------------------------------------------------- events
|
|
188
|
+
/**
|
|
189
|
+
* Listens to an event. Listeners run in registration order and are awaited one by one,
|
|
190
|
+
* after the change has been written by the adapter. Returns an unsubscribe function.
|
|
191
|
+
*/
|
|
192
|
+
on(event, listener) {
|
|
193
|
+
return this.#events.on(event, listener);
|
|
194
|
+
}
|
|
195
|
+
/** Removes a listener added with {@link on}. */
|
|
196
|
+
off(event, listener) {
|
|
197
|
+
this.#events.off(event, listener);
|
|
198
|
+
}
|
|
199
|
+
/** @internal */
|
|
200
|
+
get events() {
|
|
201
|
+
return this.#events;
|
|
202
|
+
}
|
|
203
|
+
// ------------------------------------------------------------------ permissions
|
|
204
|
+
async createPermission(name, options = {}) {
|
|
205
|
+
const guardName = this.resolveGuard(options.guard);
|
|
206
|
+
const permissionName = normalizeName(name);
|
|
207
|
+
if (this.wildcard) parseWildcardPermission(permissionName);
|
|
208
|
+
if (await this.findPermission(permissionName, { guard: guardName })) {
|
|
209
|
+
throw new PermissionAlreadyExists(permissionName, guardName);
|
|
210
|
+
}
|
|
211
|
+
const permission = await this.adapter.createPermission({ name: permissionName, guardName });
|
|
212
|
+
await this.forgetCachedPermissions();
|
|
213
|
+
await this.#events.emit({ event: "permission.created", permission });
|
|
214
|
+
return permission;
|
|
215
|
+
}
|
|
216
|
+
async findPermission(name, options = {}) {
|
|
217
|
+
const [permission] = await this.adapter.findPermissions({
|
|
218
|
+
names: [normalizeName(name)],
|
|
219
|
+
guardName: this.resolveGuard(options.guard)
|
|
220
|
+
});
|
|
221
|
+
return permission ?? null;
|
|
222
|
+
}
|
|
223
|
+
async findPermissionOrFail(name, options = {}) {
|
|
224
|
+
const permission = await this.findPermission(name, options);
|
|
225
|
+
if (!permission) throw new PermissionDoesNotExist(normalizeName(name), this.resolveGuard(options.guard));
|
|
226
|
+
return permission;
|
|
227
|
+
}
|
|
228
|
+
async findOrCreatePermission(name, options = {}) {
|
|
229
|
+
return await this.findPermission(name, options) ?? this.createPermission(name, options);
|
|
230
|
+
}
|
|
231
|
+
async getPermissions(options = {}) {
|
|
232
|
+
return this.adapter.findPermissions({ guardName: this.resolveGuard(options.guard) });
|
|
233
|
+
}
|
|
234
|
+
async deletePermission(permission, options = {}) {
|
|
235
|
+
const guardName = typeof permission === "string" ? this.resolveGuard(options.guard) : permission.guardName;
|
|
236
|
+
const [resolved] = await this.resolvePermissions([permission], guardName);
|
|
237
|
+
await this.adapter.deletePermission(resolved.id);
|
|
238
|
+
await this.forgetCachedPermissions();
|
|
239
|
+
await this.#events.emit({ event: "permission.deleted", permission: resolved });
|
|
240
|
+
}
|
|
241
|
+
// ------------------------------------------------------------------------ roles
|
|
242
|
+
async createRole(name, options = {}) {
|
|
243
|
+
const guardName = this.resolveGuard(options.guard);
|
|
244
|
+
const teamId = this.resolveTeam(options.team);
|
|
245
|
+
const roleName = normalizeName(name);
|
|
246
|
+
const existing = await this.adapter.findRoles({ names: [roleName], guardName, teamIds: [teamId] });
|
|
247
|
+
if (existing.length > 0) throw new RoleAlreadyExists(roleName, guardName);
|
|
248
|
+
const role = await this.adapter.createRole({ name: roleName, guardName, teamId });
|
|
249
|
+
await this.forgetCachedPermissions();
|
|
250
|
+
await this.#events.emit({ event: "role.created", role, teamId: role.teamId });
|
|
251
|
+
return role;
|
|
252
|
+
}
|
|
253
|
+
/** Finds a role. With a team, a team-specific role wins over a global role of the same name. */
|
|
254
|
+
async findRole(name, options = {}) {
|
|
255
|
+
const guardName = this.resolveGuard(options.guard);
|
|
256
|
+
const teamId = this.resolveTeam(options.team);
|
|
257
|
+
const roleName = normalizeName(name);
|
|
258
|
+
const roles = await this.adapter.findRoles({ names: [roleName], guardName, teamIds: this.visibleTeams(teamId) });
|
|
259
|
+
return _Rbac.pickRole(roles, roleName, teamId) ?? null;
|
|
260
|
+
}
|
|
261
|
+
async findRoleOrFail(name, options = {}) {
|
|
262
|
+
const role = await this.findRole(name, options);
|
|
263
|
+
if (!role) throw new RoleDoesNotExist(normalizeName(name), this.resolveGuard(options.guard));
|
|
264
|
+
return role;
|
|
265
|
+
}
|
|
266
|
+
async findOrCreateRole(name, options = {}) {
|
|
267
|
+
return await this.findRole(name, options) ?? this.createRole(name, options);
|
|
268
|
+
}
|
|
269
|
+
/** Lists roles visible in the given scope (global roles + roles of the given team). */
|
|
270
|
+
async getRoles(options = {}) {
|
|
271
|
+
const teamId = this.resolveTeam(options.team);
|
|
272
|
+
return this.adapter.findRoles({ guardName: this.resolveGuard(options.guard), teamIds: this.visibleTeams(teamId) });
|
|
273
|
+
}
|
|
274
|
+
async deleteRole(role, options = {}) {
|
|
275
|
+
const resolved = await this.role(role, options).get();
|
|
276
|
+
await this.adapter.deleteRole(resolved.id);
|
|
277
|
+
await this.forgetCachedPermissions();
|
|
278
|
+
await this.#events.emit({ event: "role.deleted", role: resolved, teamId: resolved.teamId });
|
|
279
|
+
}
|
|
280
|
+
// ---------------------------------------------------------------------- queries
|
|
281
|
+
/**
|
|
282
|
+
* Ids of subjects of `subjectType` that have ANY of the given roles in the given scope
|
|
283
|
+
* (spatie: `User::role('writer')->get()`). Throws `RoleDoesNotExist` for unknown roles.
|
|
284
|
+
*/
|
|
285
|
+
async subjectsWithRole(subjectType, roles, options = {}) {
|
|
286
|
+
const guardName = this.resolveGuard(options.guard);
|
|
287
|
+
const teamId = this.resolveTeam(options.team);
|
|
288
|
+
const resolved = await this.resolveRoles(flatten([roles]), guardName, teamId);
|
|
289
|
+
if (resolved.length === 0) return [];
|
|
290
|
+
return this.adapter.getSubjectIdsWithRoles(
|
|
291
|
+
subjectType,
|
|
292
|
+
resolved.map((r) => r.id),
|
|
293
|
+
teamId
|
|
294
|
+
);
|
|
295
|
+
}
|
|
296
|
+
/**
|
|
297
|
+
* Ids of subjects of `subjectType` that have ANY of the given permissions in the given scope —
|
|
298
|
+
* directly or via one of their roles (spatie: `User::permission('edit')->get()`).
|
|
299
|
+
* Throws `PermissionDoesNotExist` for unknown permissions. Wildcards are not expanded.
|
|
300
|
+
*/
|
|
301
|
+
async subjectsWithPermission(subjectType, permissions, options = {}) {
|
|
302
|
+
const guardName = this.resolveGuard(options.guard);
|
|
303
|
+
const teamId = this.resolveTeam(options.team);
|
|
304
|
+
const resolved = await this.resolvePermissions(flatten([permissions]), guardName);
|
|
305
|
+
if (resolved.length === 0) return [];
|
|
306
|
+
const permissionIds = resolved.map((p) => p.id);
|
|
307
|
+
const wanted = new Set(permissionIds.map(idKey));
|
|
308
|
+
const roles = await this.adapter.findRoles({ guardName, teamIds: this.visibleTeams(teamId) });
|
|
309
|
+
const links = roles.length > 0 ? await this.adapter.getRolePermissionLinks(roles.map((r) => r.id)) : [];
|
|
310
|
+
const roleIds = uniqueIds(links.filter((l) => wanted.has(idKey(l.permissionId))).map((l) => l.roleId));
|
|
311
|
+
const viaRoles = roleIds.length > 0 ? await this.adapter.getSubjectIdsWithRoles(subjectType, roleIds, teamId) : [];
|
|
312
|
+
const direct = await this.adapter.getSubjectIdsWithPermissions(subjectType, permissionIds, teamId);
|
|
313
|
+
return uniqueIds([...viaRoles, ...direct]);
|
|
314
|
+
}
|
|
315
|
+
// ------------------------------------------------------------------------ cache
|
|
316
|
+
/** Clears every cached role/permission lookup. Called automatically after each change. */
|
|
317
|
+
async forgetCachedPermissions() {
|
|
318
|
+
await this.#cache?.clear();
|
|
319
|
+
}
|
|
320
|
+
/** @internal */
|
|
321
|
+
async remember(key2, compute) {
|
|
322
|
+
if (!this.#cache) return compute();
|
|
323
|
+
const hit = await this.#cache.get(key2);
|
|
324
|
+
if (hit !== void 0) return hit;
|
|
325
|
+
const value = await compute();
|
|
326
|
+
await this.#cache.set(key2, value, this.#cacheTtl);
|
|
327
|
+
return value;
|
|
328
|
+
}
|
|
329
|
+
// ---------------------------------------------------------------------- internal
|
|
330
|
+
/** @internal Runs the `before` hook and the super-admin check. `undefined` = no decision. */
|
|
331
|
+
async gate(subject, permission) {
|
|
332
|
+
if (this.#before) {
|
|
333
|
+
const decision = await this.#before({ subject, permission });
|
|
334
|
+
if (typeof decision === "boolean") return decision;
|
|
335
|
+
}
|
|
336
|
+
if (this.#superAdminRoles.length > 0 && await subject.hasAnyRole(this.#superAdminRoles)) {
|
|
337
|
+
return true;
|
|
338
|
+
}
|
|
339
|
+
return void 0;
|
|
340
|
+
}
|
|
341
|
+
/** @internal */
|
|
342
|
+
resolveGuard(guard) {
|
|
343
|
+
return guard ?? this.defaultGuard;
|
|
344
|
+
}
|
|
345
|
+
/** @internal */
|
|
346
|
+
resolveTeam(team) {
|
|
347
|
+
if (team === void 0 || team === null) return null;
|
|
348
|
+
if (!this.teams) {
|
|
349
|
+
throw new RbacError("TEAMS_NOT_ENABLED", "A team was given but teams are not enabled. Use `createRbac({ teams: true })`.");
|
|
350
|
+
}
|
|
351
|
+
return team;
|
|
352
|
+
}
|
|
353
|
+
/** @internal */
|
|
354
|
+
visibleTeams(teamId) {
|
|
355
|
+
return teamId === null ? [null] : [teamId, null];
|
|
356
|
+
}
|
|
357
|
+
/** @internal Resolves names/objects to permissions, validating existence and guard. */
|
|
358
|
+
async resolvePermissions(inputs, guardName) {
|
|
359
|
+
const names = [];
|
|
360
|
+
const objects = [];
|
|
361
|
+
for (const input of inputs) {
|
|
362
|
+
if (typeof input === "string") {
|
|
363
|
+
names.push(normalizeName(input));
|
|
364
|
+
} else {
|
|
365
|
+
if (input.guardName !== guardName) throw new GuardDoesNotMatch(input.guardName, guardName);
|
|
366
|
+
objects.push(input);
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
const found = names.length > 0 ? await this.adapter.findPermissions({ names, guardName }) : [];
|
|
370
|
+
for (const name of names) {
|
|
371
|
+
if (!found.some((p) => p.name === name)) throw new PermissionDoesNotExist(name, guardName);
|
|
372
|
+
}
|
|
373
|
+
return uniqueById([...found, ...objects]);
|
|
374
|
+
}
|
|
375
|
+
/** @internal Resolves names/objects to roles, validating existence, guard and team. */
|
|
376
|
+
async resolveRoles(inputs, guardName, teamId) {
|
|
377
|
+
const names = [];
|
|
378
|
+
const objects = [];
|
|
379
|
+
for (const input of inputs) {
|
|
380
|
+
if (typeof input === "string") {
|
|
381
|
+
names.push(normalizeName(input));
|
|
382
|
+
} else {
|
|
383
|
+
if (input.guardName !== guardName) throw new GuardDoesNotMatch(input.guardName, guardName);
|
|
384
|
+
if (input.teamId !== null && idKey(input.teamId) !== idKey(teamId)) {
|
|
385
|
+
throw new RbacError("TEAM_DOES_NOT_MATCH", `Role \`${input.name}\` belongs to another team.`);
|
|
386
|
+
}
|
|
387
|
+
objects.push(input);
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
const found = names.length > 0 ? await this.adapter.findRoles({ names, guardName, teamIds: this.visibleTeams(teamId) }) : [];
|
|
391
|
+
const picked = names.map((name) => {
|
|
392
|
+
const role = _Rbac.pickRole(found, name, teamId);
|
|
393
|
+
if (!role) throw new RoleDoesNotExist(name, guardName);
|
|
394
|
+
return role;
|
|
395
|
+
});
|
|
396
|
+
return uniqueById([...picked, ...objects]);
|
|
397
|
+
}
|
|
398
|
+
/** @internal */
|
|
399
|
+
permissionMatches(granted, required) {
|
|
400
|
+
if (!this.wildcard) return granted.includes(required);
|
|
401
|
+
return granted.some((pattern) => {
|
|
402
|
+
try {
|
|
403
|
+
return wildcardImplies(pattern, required);
|
|
404
|
+
} catch {
|
|
405
|
+
return false;
|
|
406
|
+
}
|
|
407
|
+
});
|
|
408
|
+
}
|
|
409
|
+
static pickRole(roles, name, teamId) {
|
|
410
|
+
const candidates = roles.filter((r) => r.name === name);
|
|
411
|
+
return candidates.find((r) => idKey(r.teamId) === idKey(teamId)) ?? candidates.find((r) => r.teamId === null);
|
|
412
|
+
}
|
|
413
|
+
};
|
|
414
|
+
var RoleRbac = class {
|
|
415
|
+
constructor(rbac, input, guardName, teamId) {
|
|
416
|
+
this.rbac = rbac;
|
|
417
|
+
this.input = input;
|
|
418
|
+
this.guardName = guardName;
|
|
419
|
+
this.teamId = teamId;
|
|
420
|
+
}
|
|
421
|
+
rbac;
|
|
422
|
+
input;
|
|
423
|
+
guardName;
|
|
424
|
+
teamId;
|
|
425
|
+
/** Returns the role, throwing `RoleDoesNotExist` if it can't be found. */
|
|
426
|
+
async get() {
|
|
427
|
+
const [role] = await this.rbac.resolveRoles([this.input], this.guardName, this.teamId);
|
|
428
|
+
return role;
|
|
429
|
+
}
|
|
430
|
+
async givePermissionTo(...permissions) {
|
|
431
|
+
const role = await this.get();
|
|
432
|
+
const resolved = await this.rbac.resolvePermissions(flatten(permissions), role.guardName);
|
|
433
|
+
const track = this.rbac.events.has("permission.attached");
|
|
434
|
+
const before = track ? await this.currentPermissionIds(role) : [];
|
|
435
|
+
await this.rbac.adapter.attachRolePermissions(role.id, resolved.map((p) => p.id));
|
|
436
|
+
await this.rbac.forgetCachedPermissions();
|
|
437
|
+
if (track) await this.emit("permission.attached", role, filterByIds(resolved, before, false));
|
|
438
|
+
return this;
|
|
439
|
+
}
|
|
440
|
+
async revokePermissionTo(...permissions) {
|
|
441
|
+
const role = await this.get();
|
|
442
|
+
const resolved = await this.rbac.resolvePermissions(flatten(permissions), role.guardName);
|
|
443
|
+
const track = this.rbac.events.has("permission.detached");
|
|
444
|
+
const before = track ? await this.currentPermissionIds(role) : [];
|
|
445
|
+
await this.rbac.adapter.detachRolePermissions(role.id, resolved.map((p) => p.id));
|
|
446
|
+
await this.rbac.forgetCachedPermissions();
|
|
447
|
+
if (track) await this.emit("permission.detached", role, filterByIds(resolved, before, true));
|
|
448
|
+
return this;
|
|
449
|
+
}
|
|
450
|
+
/** Replaces all permissions of the role with the given ones. */
|
|
451
|
+
async syncPermissions(...permissions) {
|
|
452
|
+
const role = await this.get();
|
|
453
|
+
const resolved = await this.rbac.resolvePermissions(flatten(permissions), role.guardName);
|
|
454
|
+
const track = this.rbac.events.has("permission.attached") || this.rbac.events.has("permission.detached");
|
|
455
|
+
const current = track ? await this.getPermissions() : [];
|
|
456
|
+
await this.rbac.adapter.detachRolePermissions(role.id);
|
|
457
|
+
await this.rbac.adapter.attachRolePermissions(role.id, resolved.map((p) => p.id));
|
|
458
|
+
await this.rbac.forgetCachedPermissions();
|
|
459
|
+
if (track) {
|
|
460
|
+
const newIds = resolved.map((p) => p.id);
|
|
461
|
+
await this.emit("permission.detached", role, filterByIds(current, newIds, false));
|
|
462
|
+
await this.emit("permission.attached", role, filterByIds(resolved, current.map((p) => p.id), false));
|
|
463
|
+
}
|
|
464
|
+
return this;
|
|
465
|
+
}
|
|
466
|
+
async getPermissions() {
|
|
467
|
+
const role = await this.get();
|
|
468
|
+
const links = await this.rbac.adapter.getRolePermissionLinks([role.id]);
|
|
469
|
+
if (links.length === 0) return [];
|
|
470
|
+
return this.rbac.adapter.findPermissions({ ids: links.map((l) => l.permissionId), guardName: role.guardName });
|
|
471
|
+
}
|
|
472
|
+
async getPermissionNames() {
|
|
473
|
+
return (await this.getPermissions()).map((p) => p.name);
|
|
474
|
+
}
|
|
475
|
+
async hasPermissionTo(permission) {
|
|
476
|
+
const name = typeof permission === "string" ? normalizeName(permission) : permission.name;
|
|
477
|
+
return this.rbac.permissionMatches(await this.getPermissionNames(), name);
|
|
478
|
+
}
|
|
479
|
+
async currentPermissionIds(role) {
|
|
480
|
+
return (await this.rbac.adapter.getRolePermissionLinks([role.id])).map((l) => l.permissionId);
|
|
481
|
+
}
|
|
482
|
+
async emit(event, role, permissions) {
|
|
483
|
+
if (permissions.length === 0) return;
|
|
484
|
+
await this.rbac.events.emit({
|
|
485
|
+
event,
|
|
486
|
+
target: "role",
|
|
487
|
+
role,
|
|
488
|
+
permissions,
|
|
489
|
+
guardName: role.guardName,
|
|
490
|
+
teamId: role.teamId
|
|
491
|
+
});
|
|
492
|
+
}
|
|
493
|
+
};
|
|
494
|
+
var SubjectRbac = class _SubjectRbac {
|
|
495
|
+
constructor(rbac, subject, guardName, teamId) {
|
|
496
|
+
this.rbac = rbac;
|
|
497
|
+
this.subject = subject;
|
|
498
|
+
this.guardName = guardName;
|
|
499
|
+
this.teamId = teamId;
|
|
500
|
+
}
|
|
501
|
+
rbac;
|
|
502
|
+
subject;
|
|
503
|
+
guardName;
|
|
504
|
+
teamId;
|
|
505
|
+
// ------------------------------------------------------------------------ roles
|
|
506
|
+
async assignRole(...roles) {
|
|
507
|
+
const { adapter, events } = this.rbac;
|
|
508
|
+
const resolved = await this.rbac.resolveRoles(flatten(roles), this.guardName, this.teamId);
|
|
509
|
+
const track = events.has("role.attached");
|
|
510
|
+
const before = track ? await adapter.getSubjectRoleIds(this.subject, this.teamId) : [];
|
|
511
|
+
await adapter.attachSubjectRoles(this.subject, resolved.map((r) => r.id), this.teamId);
|
|
512
|
+
await this.rbac.forgetCachedPermissions();
|
|
513
|
+
if (track) await this.emitRoles("role.attached", filterByIds(resolved, before, false));
|
|
514
|
+
return this;
|
|
515
|
+
}
|
|
516
|
+
async removeRole(...roles) {
|
|
517
|
+
const { adapter, events } = this.rbac;
|
|
518
|
+
const resolved = await this.rbac.resolveRoles(flatten(roles), this.guardName, this.teamId);
|
|
519
|
+
const track = events.has("role.detached");
|
|
520
|
+
const before = track ? await adapter.getSubjectRoleIds(this.subject, this.teamId) : [];
|
|
521
|
+
await adapter.detachSubjectRoles(this.subject, resolved.map((r) => r.id), this.teamId);
|
|
522
|
+
await this.rbac.forgetCachedPermissions();
|
|
523
|
+
if (track) await this.emitRoles("role.detached", filterByIds(resolved, before, true));
|
|
524
|
+
return this;
|
|
525
|
+
}
|
|
526
|
+
/** Replaces all roles (in the current guard & team) with the given ones. */
|
|
527
|
+
async syncRoles(...roles) {
|
|
528
|
+
const resolved = await this.rbac.resolveRoles(flatten(roles), this.guardName, this.teamId);
|
|
529
|
+
const current = (await this.state()).roles;
|
|
530
|
+
await this.rbac.adapter.detachSubjectRoles(this.subject, current.map((r) => r.id), this.teamId);
|
|
531
|
+
await this.rbac.adapter.attachSubjectRoles(this.subject, resolved.map((r) => r.id), this.teamId);
|
|
532
|
+
await this.rbac.forgetCachedPermissions();
|
|
533
|
+
await this.emitRoles("role.detached", filterByIds(current, resolved.map((r) => r.id), false));
|
|
534
|
+
await this.emitRoles("role.attached", filterByIds(resolved, current.map((r) => r.id), false));
|
|
535
|
+
return this;
|
|
536
|
+
}
|
|
537
|
+
async getRoles() {
|
|
538
|
+
return [...(await this.state()).roles];
|
|
539
|
+
}
|
|
540
|
+
async getRoleNames() {
|
|
541
|
+
return (await this.state()).roles.map((r) => r.name);
|
|
542
|
+
}
|
|
543
|
+
/** `true` if the subject has the role — or ANY of the roles when given an array. */
|
|
544
|
+
async hasRole(roles) {
|
|
545
|
+
return this.hasAnyRole(roles);
|
|
546
|
+
}
|
|
547
|
+
async hasAnyRole(...roles) {
|
|
548
|
+
const mine = (await this.state()).roles;
|
|
549
|
+
return flatten(roles).some((wanted) => mine.some((r) => _SubjectRbac.roleMatches(r, wanted)));
|
|
550
|
+
}
|
|
551
|
+
async hasAllRoles(...roles) {
|
|
552
|
+
const mine = (await this.state()).roles;
|
|
553
|
+
return flatten(roles).every((wanted) => mine.some((r) => _SubjectRbac.roleMatches(r, wanted)));
|
|
554
|
+
}
|
|
555
|
+
/** `true` if the subject has ALL the given roles and no other role (in the current guard & team). */
|
|
556
|
+
async hasExactRoles(...roles) {
|
|
557
|
+
const mine = uniqueById((await this.state()).roles);
|
|
558
|
+
const matched = /* @__PURE__ */ new Set();
|
|
559
|
+
for (const wanted of flatten(roles)) {
|
|
560
|
+
const role = mine.find((r) => _SubjectRbac.roleMatches(r, wanted));
|
|
561
|
+
if (!role) return false;
|
|
562
|
+
matched.add(idKey(role.id));
|
|
563
|
+
}
|
|
564
|
+
return matched.size === mine.length;
|
|
565
|
+
}
|
|
566
|
+
// ------------------------------------------------------------ direct permissions
|
|
567
|
+
async givePermissionTo(...permissions) {
|
|
568
|
+
const { adapter, events } = this.rbac;
|
|
569
|
+
const resolved = await this.rbac.resolvePermissions(flatten(permissions), this.guardName);
|
|
570
|
+
const track = events.has("permission.attached");
|
|
571
|
+
const before = track ? await adapter.getSubjectPermissionIds(this.subject, this.teamId) : [];
|
|
572
|
+
await adapter.attachSubjectPermissions(this.subject, resolved.map((p) => p.id), this.teamId);
|
|
573
|
+
await this.rbac.forgetCachedPermissions();
|
|
574
|
+
if (track) await this.emitPermissions("permission.attached", filterByIds(resolved, before, false));
|
|
575
|
+
return this;
|
|
576
|
+
}
|
|
577
|
+
async revokePermissionTo(...permissions) {
|
|
578
|
+
const { adapter, events } = this.rbac;
|
|
579
|
+
const resolved = await this.rbac.resolvePermissions(flatten(permissions), this.guardName);
|
|
580
|
+
const track = events.has("permission.detached");
|
|
581
|
+
const before = track ? await adapter.getSubjectPermissionIds(this.subject, this.teamId) : [];
|
|
582
|
+
await adapter.detachSubjectPermissions(this.subject, resolved.map((p) => p.id), this.teamId);
|
|
583
|
+
await this.rbac.forgetCachedPermissions();
|
|
584
|
+
if (track) await this.emitPermissions("permission.detached", filterByIds(resolved, before, true));
|
|
585
|
+
return this;
|
|
586
|
+
}
|
|
587
|
+
/** Replaces all direct permissions (in the current guard & team) with the given ones. */
|
|
588
|
+
async syncPermissions(...permissions) {
|
|
589
|
+
const resolved = await this.rbac.resolvePermissions(flatten(permissions), this.guardName);
|
|
590
|
+
const current = (await this.state()).direct;
|
|
591
|
+
await this.rbac.adapter.detachSubjectPermissions(this.subject, current.map((p) => p.id), this.teamId);
|
|
592
|
+
await this.rbac.adapter.attachSubjectPermissions(this.subject, resolved.map((p) => p.id), this.teamId);
|
|
593
|
+
await this.rbac.forgetCachedPermissions();
|
|
594
|
+
await this.emitPermissions("permission.detached", filterByIds(current, resolved.map((p) => p.id), false));
|
|
595
|
+
await this.emitPermissions("permission.attached", filterByIds(resolved, current.map((p) => p.id), false));
|
|
596
|
+
return this;
|
|
597
|
+
}
|
|
598
|
+
// ------------------------------------------------------------------- queries
|
|
599
|
+
async getDirectPermissions() {
|
|
600
|
+
return [...(await this.state()).direct];
|
|
601
|
+
}
|
|
602
|
+
async getPermissionsViaRoles() {
|
|
603
|
+
return [...(await this.state()).viaRoles];
|
|
604
|
+
}
|
|
605
|
+
async getAllPermissions() {
|
|
606
|
+
const { direct, viaRoles } = await this.state();
|
|
607
|
+
return uniqueById([...direct, ...viaRoles]);
|
|
608
|
+
}
|
|
609
|
+
async getPermissionNames() {
|
|
610
|
+
return (await this.getAllPermissions()).map((p) => p.name);
|
|
611
|
+
}
|
|
612
|
+
/**
|
|
613
|
+
* Checks a permission (direct or via roles). Pure data check: the `before` hook and
|
|
614
|
+
* `superAdminRoles` are NOT applied (like spatie, where only `can()` goes through the Gate).
|
|
615
|
+
* Throws `PermissionDoesNotExist` if the permission is unknown (when wildcard is off).
|
|
616
|
+
* Use {@link can} for a check that never throws.
|
|
617
|
+
*/
|
|
618
|
+
async hasPermissionTo(permission) {
|
|
619
|
+
const name = await this.requirePermissionName(permission);
|
|
620
|
+
return this.rbac.permissionMatches(await this.getPermissionNames(), name);
|
|
621
|
+
}
|
|
622
|
+
/** Checks a direct permission only (ignores roles). */
|
|
623
|
+
async hasDirectPermission(permission) {
|
|
624
|
+
const name = await this.requirePermissionName(permission);
|
|
625
|
+
const direct = (await this.state()).direct.map((p) => p.name);
|
|
626
|
+
return this.rbac.permissionMatches(direct, name);
|
|
627
|
+
}
|
|
628
|
+
/**
|
|
629
|
+
* Authorization check: runs the `before` hook and `superAdminRoles` first, then falls back
|
|
630
|
+
* to {@link hasPermissionTo}, returning `false` instead of throwing.
|
|
631
|
+
*/
|
|
632
|
+
async can(permission) {
|
|
633
|
+
const name = typeof permission === "string" ? permission.trim() : permission.name;
|
|
634
|
+
const decision = await this.rbac.gate(this, name);
|
|
635
|
+
if (decision !== void 0) return decision;
|
|
636
|
+
return this.check(permission);
|
|
637
|
+
}
|
|
638
|
+
async cannot(permission) {
|
|
639
|
+
return !await this.can(permission);
|
|
640
|
+
}
|
|
641
|
+
async canAny(...permissions) {
|
|
642
|
+
for (const permission of flatten(permissions)) {
|
|
643
|
+
if (await this.can(permission)) return true;
|
|
644
|
+
}
|
|
645
|
+
return false;
|
|
646
|
+
}
|
|
647
|
+
async canAll(...permissions) {
|
|
648
|
+
for (const permission of flatten(permissions)) {
|
|
649
|
+
if (!await this.can(permission)) return false;
|
|
650
|
+
}
|
|
651
|
+
return true;
|
|
652
|
+
}
|
|
653
|
+
/** `true` if the subject has ANY of the permissions. Pure data check (no `before` / super-admin), never throws. */
|
|
654
|
+
async hasAnyPermission(...permissions) {
|
|
655
|
+
for (const permission of flatten(permissions)) {
|
|
656
|
+
if (await this.check(permission)) return true;
|
|
657
|
+
}
|
|
658
|
+
return false;
|
|
659
|
+
}
|
|
660
|
+
/** `true` if the subject has ALL the permissions. Pure data check (no `before` / super-admin), never throws. */
|
|
661
|
+
async hasAllPermissions(...permissions) {
|
|
662
|
+
for (const permission of flatten(permissions)) {
|
|
663
|
+
if (!await this.check(permission)) return false;
|
|
664
|
+
}
|
|
665
|
+
return true;
|
|
666
|
+
}
|
|
667
|
+
/** Serializable `{ roles, permissions }` — e.g. to hydrate a React/Vue client. */
|
|
668
|
+
async snapshot() {
|
|
669
|
+
return { roles: await this.getRoleNames(), permissions: await this.getPermissionNames() };
|
|
670
|
+
}
|
|
671
|
+
// ------------------------------------------------------------------- internal
|
|
672
|
+
/** {@link hasPermissionTo} that returns `false` instead of throwing. */
|
|
673
|
+
async check(permission) {
|
|
674
|
+
try {
|
|
675
|
+
return await this.hasPermissionTo(permission);
|
|
676
|
+
} catch (error) {
|
|
677
|
+
if (error instanceof RbacError) return false;
|
|
678
|
+
throw error;
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
async requirePermissionName(permission) {
|
|
682
|
+
if (typeof permission !== "string") {
|
|
683
|
+
if (permission.guardName !== this.guardName) throw new GuardDoesNotMatch(permission.guardName, this.guardName);
|
|
684
|
+
return permission.name;
|
|
685
|
+
}
|
|
686
|
+
const name = normalizeName(permission);
|
|
687
|
+
if (!this.rbac.wildcard && !await this.rbac.findPermission(name, { guard: this.guardName })) {
|
|
688
|
+
throw new PermissionDoesNotExist(name, this.guardName);
|
|
689
|
+
}
|
|
690
|
+
return name;
|
|
691
|
+
}
|
|
692
|
+
state() {
|
|
693
|
+
const key2 = `rbac:${JSON.stringify([this.guardName, this.teamId, this.subject.type, String(this.subject.id)])}`;
|
|
694
|
+
return this.rbac.remember(key2, async () => {
|
|
695
|
+
const { adapter } = this.rbac;
|
|
696
|
+
const roleIds = await adapter.getSubjectRoleIds(this.subject, this.teamId);
|
|
697
|
+
const roles = roleIds.length > 0 ? await adapter.findRoles({ ids: roleIds, guardName: this.guardName }) : [];
|
|
698
|
+
const directIds = await adapter.getSubjectPermissionIds(this.subject, this.teamId);
|
|
699
|
+
const direct = directIds.length > 0 ? await adapter.findPermissions({ ids: directIds, guardName: this.guardName }) : [];
|
|
700
|
+
const links = roles.length > 0 ? await adapter.getRolePermissionLinks(roles.map((r) => r.id)) : [];
|
|
701
|
+
const viaRoles = links.length > 0 ? await adapter.findPermissions({ ids: links.map((l) => l.permissionId), guardName: this.guardName }) : [];
|
|
702
|
+
return { roles, direct, viaRoles };
|
|
703
|
+
});
|
|
704
|
+
}
|
|
705
|
+
async emitRoles(event, roles) {
|
|
706
|
+
if (roles.length === 0) return;
|
|
707
|
+
await this.rbac.events.emit({
|
|
708
|
+
event,
|
|
709
|
+
subject: this.subject,
|
|
710
|
+
roles,
|
|
711
|
+
guardName: this.guardName,
|
|
712
|
+
teamId: this.teamId
|
|
713
|
+
});
|
|
714
|
+
}
|
|
715
|
+
async emitPermissions(event, permissions) {
|
|
716
|
+
if (permissions.length === 0) return;
|
|
717
|
+
await this.rbac.events.emit({
|
|
718
|
+
event,
|
|
719
|
+
target: "subject",
|
|
720
|
+
subject: this.subject,
|
|
721
|
+
permissions,
|
|
722
|
+
guardName: this.guardName,
|
|
723
|
+
teamId: this.teamId
|
|
724
|
+
});
|
|
725
|
+
}
|
|
726
|
+
static roleMatches(role, wanted) {
|
|
727
|
+
return typeof wanted === "string" ? role.name === wanted.trim() : idKey(role.id) === idKey(wanted.id);
|
|
728
|
+
}
|
|
729
|
+
};
|
|
730
|
+
function createRbac(options) {
|
|
731
|
+
return new Rbac(options);
|
|
732
|
+
}
|
|
733
|
+
|
|
734
|
+
// src/adapters/memory.ts
|
|
735
|
+
var key = (id) => id === null ? "\0null" : String(id);
|
|
736
|
+
var same = (a, b) => key(a) === key(b);
|
|
737
|
+
var inIds = (ids, id) => ids.some((x) => same(x, id));
|
|
738
|
+
var isSubject = (l, s) => l.subjectType === s.type && same(l.subjectId, s.id);
|
|
739
|
+
var MemoryAdapter = class _MemoryAdapter {
|
|
740
|
+
nextId = 1;
|
|
741
|
+
permissions = [];
|
|
742
|
+
roles = [];
|
|
743
|
+
rolePermissions = [];
|
|
744
|
+
subjectRoles = [];
|
|
745
|
+
subjectPermissions = [];
|
|
746
|
+
// ---------------------------------------------------------------- permissions
|
|
747
|
+
async createPermission(data) {
|
|
748
|
+
const permission = { id: this.nextId++, name: data.name, guardName: data.guardName };
|
|
749
|
+
this.permissions.push(permission);
|
|
750
|
+
return { ...permission };
|
|
751
|
+
}
|
|
752
|
+
async findPermissions(query = {}) {
|
|
753
|
+
return this.permissions.filter(
|
|
754
|
+
(p) => (query.ids === void 0 || inIds(query.ids, p.id)) && (query.names === void 0 || query.names.includes(p.name)) && (query.guardName === void 0 || p.guardName === query.guardName)
|
|
755
|
+
).map((p) => ({ ...p }));
|
|
756
|
+
}
|
|
757
|
+
async deletePermission(id) {
|
|
758
|
+
this.permissions = this.permissions.filter((p) => !same(p.id, id));
|
|
759
|
+
this.rolePermissions = this.rolePermissions.filter((l) => !same(l.permissionId, id));
|
|
760
|
+
this.subjectPermissions = this.subjectPermissions.filter((l) => !same(l.targetId, id));
|
|
761
|
+
}
|
|
762
|
+
// ---------------------------------------------------------------------- roles
|
|
763
|
+
async createRole(data) {
|
|
764
|
+
const role = { id: this.nextId++, name: data.name, guardName: data.guardName, teamId: data.teamId };
|
|
765
|
+
this.roles.push(role);
|
|
766
|
+
return { ...role };
|
|
767
|
+
}
|
|
768
|
+
async findRoles(query = {}) {
|
|
769
|
+
return this.roles.filter(
|
|
770
|
+
(r) => (query.ids === void 0 || inIds(query.ids, r.id)) && (query.names === void 0 || query.names.includes(r.name)) && (query.guardName === void 0 || r.guardName === query.guardName) && (query.teamIds === void 0 || inIds(query.teamIds, r.teamId))
|
|
771
|
+
).map((r) => ({ ...r }));
|
|
772
|
+
}
|
|
773
|
+
async deleteRole(id) {
|
|
774
|
+
this.roles = this.roles.filter((r) => !same(r.id, id));
|
|
775
|
+
this.rolePermissions = this.rolePermissions.filter((l) => !same(l.roleId, id));
|
|
776
|
+
this.subjectRoles = this.subjectRoles.filter((l) => !same(l.targetId, id));
|
|
777
|
+
}
|
|
778
|
+
// ---------------------------------------------------------- role ↔ permission
|
|
779
|
+
async getRolePermissionLinks(roleIds) {
|
|
780
|
+
return this.rolePermissions.filter((l) => inIds(roleIds, l.roleId)).map((l) => ({ ...l }));
|
|
781
|
+
}
|
|
782
|
+
async attachRolePermissions(roleId, permissionIds) {
|
|
783
|
+
for (const permissionId of permissionIds) {
|
|
784
|
+
const exists = this.rolePermissions.some((l) => same(l.roleId, roleId) && same(l.permissionId, permissionId));
|
|
785
|
+
if (!exists) this.rolePermissions.push({ roleId, permissionId });
|
|
786
|
+
}
|
|
787
|
+
}
|
|
788
|
+
async detachRolePermissions(roleId, permissionIds) {
|
|
789
|
+
this.rolePermissions = this.rolePermissions.filter(
|
|
790
|
+
(l) => !(same(l.roleId, roleId) && (permissionIds === void 0 || inIds(permissionIds, l.permissionId)))
|
|
791
|
+
);
|
|
792
|
+
}
|
|
793
|
+
// ------------------------------------------------------------- subject links
|
|
794
|
+
async getSubjectRoleIds(subject, teamId) {
|
|
795
|
+
return _MemoryAdapter.getLinks(this.subjectRoles, subject, teamId);
|
|
796
|
+
}
|
|
797
|
+
async attachSubjectRoles(subject, roleIds, teamId) {
|
|
798
|
+
_MemoryAdapter.attach(this.subjectRoles, subject, roleIds, teamId);
|
|
799
|
+
}
|
|
800
|
+
async detachSubjectRoles(subject, roleIds, teamId) {
|
|
801
|
+
this.subjectRoles = _MemoryAdapter.detach(this.subjectRoles, subject, roleIds, teamId);
|
|
802
|
+
}
|
|
803
|
+
async getSubjectPermissionIds(subject, teamId) {
|
|
804
|
+
return _MemoryAdapter.getLinks(this.subjectPermissions, subject, teamId);
|
|
805
|
+
}
|
|
806
|
+
async attachSubjectPermissions(subject, permissionIds, teamId) {
|
|
807
|
+
_MemoryAdapter.attach(this.subjectPermissions, subject, permissionIds, teamId);
|
|
808
|
+
}
|
|
809
|
+
async detachSubjectPermissions(subject, permissionIds, teamId) {
|
|
810
|
+
this.subjectPermissions = _MemoryAdapter.detach(this.subjectPermissions, subject, permissionIds, teamId);
|
|
811
|
+
}
|
|
812
|
+
async getSubjectIdsWithRoles(subjectType, roleIds, teamId) {
|
|
813
|
+
return _MemoryAdapter.reverse(this.subjectRoles, subjectType, roleIds, teamId);
|
|
814
|
+
}
|
|
815
|
+
async getSubjectIdsWithPermissions(subjectType, permissionIds, teamId) {
|
|
816
|
+
return _MemoryAdapter.reverse(this.subjectPermissions, subjectType, permissionIds, teamId);
|
|
817
|
+
}
|
|
818
|
+
// -------------------------------------------------------------------- helpers
|
|
819
|
+
static getLinks(links, subject, teamId) {
|
|
820
|
+
return links.filter((l) => isSubject(l, subject) && same(l.teamId, teamId)).map((l) => l.targetId);
|
|
821
|
+
}
|
|
822
|
+
static attach(links, subject, ids, teamId) {
|
|
823
|
+
for (const targetId of ids) {
|
|
824
|
+
const exists = links.some((l) => isSubject(l, subject) && same(l.targetId, targetId) && same(l.teamId, teamId));
|
|
825
|
+
if (!exists) links.push({ subjectType: subject.type, subjectId: subject.id, targetId, teamId });
|
|
826
|
+
}
|
|
827
|
+
}
|
|
828
|
+
static detach(links, subject, ids, teamId) {
|
|
829
|
+
return links.filter(
|
|
830
|
+
(l) => !(isSubject(l, subject) && same(l.teamId, teamId) && (ids === void 0 || inIds(ids, l.targetId)))
|
|
831
|
+
);
|
|
832
|
+
}
|
|
833
|
+
static reverse(links, subjectType, ids, teamId) {
|
|
834
|
+
const seen = /* @__PURE__ */ new Set();
|
|
835
|
+
const out = [];
|
|
836
|
+
for (const l of links) {
|
|
837
|
+
if (l.subjectType !== subjectType || !same(l.teamId, teamId) || !inIds(ids, l.targetId)) continue;
|
|
838
|
+
const k = key(l.subjectId);
|
|
839
|
+
if (seen.has(k)) continue;
|
|
840
|
+
seen.add(k);
|
|
841
|
+
out.push(l.subjectId);
|
|
842
|
+
}
|
|
843
|
+
return out;
|
|
844
|
+
}
|
|
845
|
+
};
|
|
846
|
+
|
|
847
|
+
// src/index.ts
|
|
848
|
+
var VERSION = "0.1.0";
|
|
849
|
+
|
|
850
|
+
exports.GuardDoesNotMatch = GuardDoesNotMatch;
|
|
851
|
+
exports.InvalidWildcardPermission = InvalidWildcardPermission;
|
|
852
|
+
exports.MemoryAdapter = MemoryAdapter;
|
|
853
|
+
exports.MemoryCache = MemoryCache;
|
|
854
|
+
exports.PermissionAlreadyExists = PermissionAlreadyExists;
|
|
855
|
+
exports.PermissionDoesNotExist = PermissionDoesNotExist;
|
|
856
|
+
exports.Rbac = Rbac;
|
|
857
|
+
exports.RbacError = RbacError;
|
|
858
|
+
exports.RoleAlreadyExists = RoleAlreadyExists;
|
|
859
|
+
exports.RoleDoesNotExist = RoleDoesNotExist;
|
|
860
|
+
exports.RoleRbac = RoleRbac;
|
|
861
|
+
exports.SubjectRbac = SubjectRbac;
|
|
862
|
+
exports.VERSION = VERSION;
|
|
863
|
+
exports.createRbac = createRbac;
|
|
864
|
+
exports.parseWildcardPermission = parseWildcardPermission;
|
|
865
|
+
exports.wildcardImplies = wildcardImplies;
|
|
866
|
+
//# sourceMappingURL=index.cjs.map
|
|
867
|
+
//# sourceMappingURL=index.cjs.map
|