@anievo-id/rbac-evolution 0.6.6 → 0.7.1
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 +1 -1
- package/dist/{adapter-XWQmXahX.d.cts → adapter-BAYcKRIJ.d.cts} +9 -9
- package/dist/{adapter-XWQmXahX.d.ts → adapter-BAYcKRIJ.d.ts} +9 -9
- package/dist/{chunk-T7W6K4SM.js → chunk-4IVKUA7F.js} +97 -81
- package/dist/chunk-4IVKUA7F.js.map +1 -0
- package/dist/index.cjs +96 -80
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +21 -54
- package/dist/index.d.ts +21 -54
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/dist/testing.cjs +178 -79
- package/dist/testing.cjs.map +1 -1
- package/dist/testing.d.cts +2 -2
- package/dist/testing.d.ts +2 -2
- package/dist/testing.js +84 -1
- package/dist/testing.js.map +1 -1
- package/package.json +3 -2
- package/dist/chunk-T7W6K4SM.js.map +0 -1
package/dist/testing.cjs
CHANGED
|
@@ -145,7 +145,7 @@ function cleanName(value, where) {
|
|
|
145
145
|
function pushUnique(list, name) {
|
|
146
146
|
if (!list.includes(name)) list.push(name);
|
|
147
147
|
}
|
|
148
|
-
function normalize(
|
|
148
|
+
function normalize(core, definition, options) {
|
|
149
149
|
const list = Array.isArray(definition) ? definition : [definition];
|
|
150
150
|
const blocks = [];
|
|
151
151
|
list.forEach((raw, index) => {
|
|
@@ -154,12 +154,12 @@ function normalize(rbac, definition, options) {
|
|
|
154
154
|
const def = raw;
|
|
155
155
|
const guardInput = def["guard"] ?? options.guard;
|
|
156
156
|
if (guardInput !== void 0 && typeof guardInput !== "string") throw invalid(`${at}.guard must be a string.`);
|
|
157
|
-
const guard =
|
|
157
|
+
const guard = core.resolveGuard(guardInput === void 0 ? void 0 : cleanName(guardInput, `${at}.guard`));
|
|
158
158
|
const teamInput = def["team"] !== void 0 ? def["team"] : options.team;
|
|
159
159
|
if (teamInput !== void 0 && teamInput !== null && typeof teamInput !== "string" && typeof teamInput !== "number") {
|
|
160
160
|
throw invalid(`${at}.team must be a string, a number or null.`);
|
|
161
161
|
}
|
|
162
|
-
const team =
|
|
162
|
+
const team = core.resolveTeam(teamInput);
|
|
163
163
|
let block = blocks.find((b) => b.guard === guard && idKey(b.team) === idKey(team));
|
|
164
164
|
if (!block) {
|
|
165
165
|
block = { guard, team, permissions: [], roles: /* @__PURE__ */ new Map() };
|
|
@@ -195,8 +195,8 @@ function normalize(rbac, definition, options) {
|
|
|
195
195
|
});
|
|
196
196
|
return blocks;
|
|
197
197
|
}
|
|
198
|
-
async function syncDefinition(rbac, definition, options = {}) {
|
|
199
|
-
const blocks = normalize(
|
|
198
|
+
async function syncDefinition(rbac, core, definition, options = {}) {
|
|
199
|
+
const blocks = normalize(core, definition, options);
|
|
200
200
|
const prune = options.prune ?? false;
|
|
201
201
|
const dryRun = options.dryRun ?? false;
|
|
202
202
|
const result = {
|
|
@@ -328,6 +328,7 @@ function normalizeName(name) {
|
|
|
328
328
|
if (trimmed === "") throw new RbacError("INVALID_NAME", "Role and permission names must not be empty.");
|
|
329
329
|
return trimmed;
|
|
330
330
|
}
|
|
331
|
+
var internalsOf;
|
|
331
332
|
var Rbac = class _Rbac {
|
|
332
333
|
adapter;
|
|
333
334
|
defaultGuard;
|
|
@@ -340,6 +341,10 @@ var Rbac = class _Rbac {
|
|
|
340
341
|
#events = new RbacEmitter();
|
|
341
342
|
/** Bumped on every change, so a lookup that started before the change never caches its stale result. */
|
|
342
343
|
#generation = 0;
|
|
344
|
+
#internals;
|
|
345
|
+
static {
|
|
346
|
+
internalsOf = (rbac) => rbac.#internals;
|
|
347
|
+
}
|
|
343
348
|
constructor(options) {
|
|
344
349
|
this.adapter = options.adapter;
|
|
345
350
|
this.defaultGuard = options.defaultGuard ?? "web";
|
|
@@ -349,15 +354,27 @@ var Rbac = class _Rbac {
|
|
|
349
354
|
this.#cacheTtl = options.cacheTtl ?? DAY_MS;
|
|
350
355
|
this.#before = options.before;
|
|
351
356
|
this.#superAdminRoles = options.superAdminRoles ?? [];
|
|
357
|
+
this.#internals = {
|
|
358
|
+
events: this.#events,
|
|
359
|
+
remember: (key, compute) => this.#remember(key, compute),
|
|
360
|
+
permissionExists: (name, guardName) => this.#permissionExists(name, guardName),
|
|
361
|
+
gate: (subject, permission) => this.#gate(subject, permission),
|
|
362
|
+
resolveGuard: (guard) => this.#resolveGuard(guard),
|
|
363
|
+
resolveTeam: (team) => this.#resolveTeam(team),
|
|
364
|
+
visibleTeams: (teamId) => this.#visibleTeams(teamId),
|
|
365
|
+
resolvePermissions: (inputs, guardName) => this.#resolvePermissions(inputs, guardName),
|
|
366
|
+
resolveRoles: (inputs, guardName, teamId) => this.#resolveRoles(inputs, guardName, teamId),
|
|
367
|
+
permissionMatches: (granted, required) => this.#permissionMatches(granted, required)
|
|
368
|
+
};
|
|
352
369
|
}
|
|
353
370
|
/** Work with the roles & permissions of a subject (usually a user). */
|
|
354
371
|
for(subject, options = {}) {
|
|
355
|
-
return new SubjectRbac(this, subject, this
|
|
372
|
+
return new SubjectRbac(this, subject, this.#resolveGuard(options.guard), this.#resolveTeam(options.team));
|
|
356
373
|
}
|
|
357
374
|
/** Work with the permissions of a role. */
|
|
358
375
|
role(role, options = {}) {
|
|
359
376
|
if (typeof role !== "string") return new RoleRbac(this, role, role.guardName, role.teamId);
|
|
360
|
-
return new RoleRbac(this, role, this
|
|
377
|
+
return new RoleRbac(this, role, this.#resolveGuard(options.guard), this.#resolveTeam(options.team));
|
|
361
378
|
}
|
|
362
379
|
// ----------------------------------------------------------------------- events
|
|
363
380
|
/**
|
|
@@ -371,13 +388,9 @@ var Rbac = class _Rbac {
|
|
|
371
388
|
off(event, listener) {
|
|
372
389
|
this.#events.off(event, listener);
|
|
373
390
|
}
|
|
374
|
-
/** @internal */
|
|
375
|
-
get events() {
|
|
376
|
-
return this.#events;
|
|
377
|
-
}
|
|
378
391
|
// ------------------------------------------------------------------ permissions
|
|
379
392
|
async createPermission(name, options = {}) {
|
|
380
|
-
const guardName = this
|
|
393
|
+
const guardName = this.#resolveGuard(options.guard);
|
|
381
394
|
const permissionName = normalizeName(name);
|
|
382
395
|
if (this.wildcard) parseWildcardPermission(permissionName);
|
|
383
396
|
if (await this.findPermission(permissionName, { guard: guardName })) {
|
|
@@ -391,32 +404,32 @@ var Rbac = class _Rbac {
|
|
|
391
404
|
async findPermission(name, options = {}) {
|
|
392
405
|
const [permission] = await this.adapter.findPermissions({
|
|
393
406
|
names: [normalizeName(name)],
|
|
394
|
-
guardName: this
|
|
407
|
+
guardName: this.#resolveGuard(options.guard)
|
|
395
408
|
});
|
|
396
409
|
return permission ?? null;
|
|
397
410
|
}
|
|
398
411
|
async findPermissionOrFail(name, options = {}) {
|
|
399
412
|
const permission = await this.findPermission(name, options);
|
|
400
|
-
if (!permission) throw new PermissionDoesNotExist(normalizeName(name), this
|
|
413
|
+
if (!permission) throw new PermissionDoesNotExist(normalizeName(name), this.#resolveGuard(options.guard));
|
|
401
414
|
return permission;
|
|
402
415
|
}
|
|
403
416
|
async findOrCreatePermission(name, options = {}) {
|
|
404
417
|
return await this.findPermission(name, options) ?? this.createPermission(name, options);
|
|
405
418
|
}
|
|
406
419
|
async getPermissions(options = {}) {
|
|
407
|
-
return this.adapter.findPermissions({ guardName: this
|
|
420
|
+
return this.adapter.findPermissions({ guardName: this.#resolveGuard(options.guard) });
|
|
408
421
|
}
|
|
409
422
|
async deletePermission(permission, options = {}) {
|
|
410
|
-
const guardName = typeof permission === "string" ? this
|
|
411
|
-
const [resolved] = await this
|
|
423
|
+
const guardName = typeof permission === "string" ? this.#resolveGuard(options.guard) : permission.guardName;
|
|
424
|
+
const [resolved] = await this.#resolvePermissions([permission], guardName);
|
|
412
425
|
await this.adapter.deletePermission(resolved.id);
|
|
413
426
|
await this.forgetCachedPermissions();
|
|
414
427
|
await this.#events.emit({ event: "permission.deleted", permission: resolved });
|
|
415
428
|
}
|
|
416
429
|
// ------------------------------------------------------------------------ roles
|
|
417
430
|
async createRole(name, options = {}) {
|
|
418
|
-
const guardName = this
|
|
419
|
-
const teamId = this
|
|
431
|
+
const guardName = this.#resolveGuard(options.guard);
|
|
432
|
+
const teamId = this.#resolveTeam(options.team);
|
|
420
433
|
const roleName = normalizeName(name);
|
|
421
434
|
const existing = await this.adapter.findRoles({ names: [roleName], guardName, teamIds: [teamId] });
|
|
422
435
|
if (existing.length > 0) throw new RoleAlreadyExists(roleName, guardName);
|
|
@@ -427,15 +440,15 @@ var Rbac = class _Rbac {
|
|
|
427
440
|
}
|
|
428
441
|
/** Finds a role. With a team, a team-specific role wins over a global role of the same name. */
|
|
429
442
|
async findRole(name, options = {}) {
|
|
430
|
-
const guardName = this
|
|
431
|
-
const teamId = this
|
|
443
|
+
const guardName = this.#resolveGuard(options.guard);
|
|
444
|
+
const teamId = this.#resolveTeam(options.team);
|
|
432
445
|
const roleName = normalizeName(name);
|
|
433
|
-
const roles = await this.adapter.findRoles({ names: [roleName], guardName, teamIds: this
|
|
446
|
+
const roles = await this.adapter.findRoles({ names: [roleName], guardName, teamIds: this.#visibleTeams(teamId) });
|
|
434
447
|
return _Rbac.pickRole(roles, roleName, teamId) ?? null;
|
|
435
448
|
}
|
|
436
449
|
async findRoleOrFail(name, options = {}) {
|
|
437
450
|
const role = await this.findRole(name, options);
|
|
438
|
-
if (!role) throw new RoleDoesNotExist(normalizeName(name), this
|
|
451
|
+
if (!role) throw new RoleDoesNotExist(normalizeName(name), this.#resolveGuard(options.guard));
|
|
439
452
|
return role;
|
|
440
453
|
}
|
|
441
454
|
async findOrCreateRole(name, options = {}) {
|
|
@@ -443,8 +456,8 @@ var Rbac = class _Rbac {
|
|
|
443
456
|
}
|
|
444
457
|
/** Lists roles visible in the given scope (global roles + roles of the given team). */
|
|
445
458
|
async getRoles(options = {}) {
|
|
446
|
-
const teamId = this
|
|
447
|
-
return this.adapter.findRoles({ guardName: this
|
|
459
|
+
const teamId = this.#resolveTeam(options.team);
|
|
460
|
+
return this.adapter.findRoles({ guardName: this.#resolveGuard(options.guard), teamIds: this.#visibleTeams(teamId) });
|
|
448
461
|
}
|
|
449
462
|
async deleteRole(role, options = {}) {
|
|
450
463
|
const resolved = await this.role(role, options).get();
|
|
@@ -474,7 +487,7 @@ var Rbac = class _Rbac {
|
|
|
474
487
|
* Not atomic: adapters have no transactions, so a failure midway leaves the earlier changes.
|
|
475
488
|
*/
|
|
476
489
|
async sync(definition, options = {}) {
|
|
477
|
-
return syncDefinition(this, definition, options);
|
|
490
|
+
return syncDefinition(this, this.#internals, definition, options);
|
|
478
491
|
}
|
|
479
492
|
// ---------------------------------------------------------------------- queries
|
|
480
493
|
/**
|
|
@@ -482,9 +495,9 @@ var Rbac = class _Rbac {
|
|
|
482
495
|
* (spatie: `User::role('writer')->get()`). Throws `RoleDoesNotExist` for unknown roles.
|
|
483
496
|
*/
|
|
484
497
|
async subjectsWithRole(subjectType, roles, options = {}) {
|
|
485
|
-
const guardName = this
|
|
486
|
-
const teamId = this
|
|
487
|
-
const resolved = await this
|
|
498
|
+
const guardName = this.#resolveGuard(options.guard);
|
|
499
|
+
const teamId = this.#resolveTeam(options.team);
|
|
500
|
+
const resolved = await this.#resolveRoles(flatten([roles]), guardName, teamId);
|
|
488
501
|
if (resolved.length === 0) return [];
|
|
489
502
|
return this.adapter.getSubjectIdsWithRoles(
|
|
490
503
|
subjectType,
|
|
@@ -498,13 +511,13 @@ var Rbac = class _Rbac {
|
|
|
498
511
|
* Throws `PermissionDoesNotExist` for unknown permissions. Wildcards are not expanded.
|
|
499
512
|
*/
|
|
500
513
|
async subjectsWithPermission(subjectType, permissions, options = {}) {
|
|
501
|
-
const guardName = this
|
|
502
|
-
const teamId = this
|
|
503
|
-
const resolved = await this
|
|
514
|
+
const guardName = this.#resolveGuard(options.guard);
|
|
515
|
+
const teamId = this.#resolveTeam(options.team);
|
|
516
|
+
const resolved = await this.#resolvePermissions(flatten([permissions]), guardName);
|
|
504
517
|
if (resolved.length === 0) return [];
|
|
505
518
|
const permissionIds = resolved.map((p) => p.id);
|
|
506
519
|
const wanted = new Set(permissionIds.map(idKey2));
|
|
507
|
-
const roles = await this.adapter.findRoles({ guardName, teamIds: this
|
|
520
|
+
const roles = await this.adapter.findRoles({ guardName, teamIds: this.#visibleTeams(teamId) });
|
|
508
521
|
const links = roles.length > 0 ? await this.adapter.getRolePermissionLinks(roles.map((r) => r.id)) : [];
|
|
509
522
|
const roleIds = uniqueIds(links.filter((l) => wanted.has(idKey2(l.permissionId))).map((l) => l.roleId));
|
|
510
523
|
const viaRoles = roleIds.length > 0 ? await this.adapter.getSubjectIdsWithRoles(subjectType, roleIds, teamId) : [];
|
|
@@ -517,8 +530,7 @@ var Rbac = class _Rbac {
|
|
|
517
530
|
this.#generation++;
|
|
518
531
|
await this.#cache?.clear();
|
|
519
532
|
}
|
|
520
|
-
|
|
521
|
-
async remember(key, compute) {
|
|
533
|
+
async #remember(key, compute) {
|
|
522
534
|
if (!this.#cache) return compute();
|
|
523
535
|
const hit = await this.#cache.get(key);
|
|
524
536
|
if (hit !== void 0) return hit;
|
|
@@ -528,24 +540,24 @@ var Rbac = class _Rbac {
|
|
|
528
540
|
return value;
|
|
529
541
|
}
|
|
530
542
|
/**
|
|
531
|
-
*
|
|
543
|
+
* Whether a permission with this name exists in the guard.
|
|
532
544
|
* With caching on, the guard's permission names are cached (cleared on every change),
|
|
533
545
|
* so repeated `can()` checks don't hit the database.
|
|
534
546
|
*/
|
|
535
|
-
async permissionExists(name, guardName) {
|
|
547
|
+
async #permissionExists(name, guardName) {
|
|
536
548
|
if (!this.#cache) {
|
|
537
549
|
const [permission] = await this.adapter.findPermissions({ names: [name], guardName });
|
|
538
550
|
return permission !== void 0;
|
|
539
551
|
}
|
|
540
|
-
const names = await this
|
|
552
|
+
const names = await this.#remember(
|
|
541
553
|
`rbac:permissions:${JSON.stringify([guardName])}`,
|
|
542
554
|
async () => (await this.adapter.findPermissions({ guardName })).map((p) => p.name)
|
|
543
555
|
);
|
|
544
556
|
return names.includes(name);
|
|
545
557
|
}
|
|
546
558
|
// ---------------------------------------------------------------------- internal
|
|
547
|
-
/**
|
|
548
|
-
async gate(subject, permission) {
|
|
559
|
+
/** Runs the `before` hook and the super-admin check. `undefined` = no decision. */
|
|
560
|
+
async #gate(subject, permission) {
|
|
549
561
|
if (this.#before) {
|
|
550
562
|
const decision = await this.#before({ subject, permission });
|
|
551
563
|
if (typeof decision === "boolean") return decision;
|
|
@@ -555,24 +567,21 @@ var Rbac = class _Rbac {
|
|
|
555
567
|
}
|
|
556
568
|
return void 0;
|
|
557
569
|
}
|
|
558
|
-
|
|
559
|
-
resolveGuard(guard) {
|
|
570
|
+
#resolveGuard(guard) {
|
|
560
571
|
return guard ?? this.defaultGuard;
|
|
561
572
|
}
|
|
562
|
-
|
|
563
|
-
resolveTeam(team) {
|
|
573
|
+
#resolveTeam(team) {
|
|
564
574
|
if (team === void 0 || team === null) return null;
|
|
565
575
|
if (!this.teams) {
|
|
566
576
|
throw new RbacError("TEAMS_NOT_ENABLED", "A team was given but teams are not enabled. Use `createRbac({ teams: true })`.");
|
|
567
577
|
}
|
|
568
578
|
return team;
|
|
569
579
|
}
|
|
570
|
-
|
|
571
|
-
visibleTeams(teamId) {
|
|
580
|
+
#visibleTeams(teamId) {
|
|
572
581
|
return teamId === null ? [null] : [teamId, null];
|
|
573
582
|
}
|
|
574
|
-
/**
|
|
575
|
-
async resolvePermissions(inputs, guardName) {
|
|
583
|
+
/** Resolves names/objects to permissions, validating existence and guard. */
|
|
584
|
+
async #resolvePermissions(inputs, guardName) {
|
|
576
585
|
const names = [];
|
|
577
586
|
const objects = [];
|
|
578
587
|
for (const input of inputs) {
|
|
@@ -589,8 +598,8 @@ var Rbac = class _Rbac {
|
|
|
589
598
|
}
|
|
590
599
|
return uniqueById([...found, ...objects]);
|
|
591
600
|
}
|
|
592
|
-
/**
|
|
593
|
-
async resolveRoles(inputs, guardName, teamId) {
|
|
601
|
+
/** Resolves names/objects to roles, validating existence, guard and team. */
|
|
602
|
+
async #resolveRoles(inputs, guardName, teamId) {
|
|
594
603
|
const names = [];
|
|
595
604
|
const objects = [];
|
|
596
605
|
for (const input of inputs) {
|
|
@@ -604,7 +613,7 @@ var Rbac = class _Rbac {
|
|
|
604
613
|
objects.push(input);
|
|
605
614
|
}
|
|
606
615
|
}
|
|
607
|
-
const found = names.length > 0 ? await this.adapter.findRoles({ names, guardName, teamIds: this
|
|
616
|
+
const found = names.length > 0 ? await this.adapter.findRoles({ names, guardName, teamIds: this.#visibleTeams(teamId) }) : [];
|
|
608
617
|
const picked = names.map((name) => {
|
|
609
618
|
const role = _Rbac.pickRole(found, name, teamId);
|
|
610
619
|
if (!role) throw new RoleDoesNotExist(name, guardName);
|
|
@@ -612,8 +621,7 @@ var Rbac = class _Rbac {
|
|
|
612
621
|
});
|
|
613
622
|
return uniqueById([...picked, ...objects]);
|
|
614
623
|
}
|
|
615
|
-
|
|
616
|
-
permissionMatches(granted, required) {
|
|
624
|
+
#permissionMatches(granted, required) {
|
|
617
625
|
if (!this.wildcard) return granted.includes(required);
|
|
618
626
|
return granted.some((pattern) => {
|
|
619
627
|
try {
|
|
@@ -634,20 +642,22 @@ var RoleRbac = class {
|
|
|
634
642
|
this.input = input;
|
|
635
643
|
this.guardName = guardName;
|
|
636
644
|
this.teamId = teamId;
|
|
645
|
+
this.#core = internalsOf(rbac);
|
|
637
646
|
}
|
|
638
647
|
rbac;
|
|
639
648
|
input;
|
|
640
649
|
guardName;
|
|
641
650
|
teamId;
|
|
651
|
+
#core;
|
|
642
652
|
/** Returns the role, throwing `RoleDoesNotExist` if it can't be found. */
|
|
643
653
|
async get() {
|
|
644
|
-
const [role] = await this.
|
|
654
|
+
const [role] = await this.#core.resolveRoles([this.input], this.guardName, this.teamId);
|
|
645
655
|
return role;
|
|
646
656
|
}
|
|
647
657
|
async givePermissionTo(...permissions) {
|
|
648
658
|
const role = await this.get();
|
|
649
|
-
const resolved = await this.
|
|
650
|
-
const track = this.
|
|
659
|
+
const resolved = await this.#core.resolvePermissions(flatten(permissions), role.guardName);
|
|
660
|
+
const track = this.#core.events.has("permission.attached");
|
|
651
661
|
const before = track ? await this.currentPermissionIds(role) : [];
|
|
652
662
|
await this.rbac.adapter.attachRolePermissions(role.id, resolved.map((p) => p.id));
|
|
653
663
|
await this.rbac.forgetCachedPermissions();
|
|
@@ -656,8 +666,8 @@ var RoleRbac = class {
|
|
|
656
666
|
}
|
|
657
667
|
async revokePermissionTo(...permissions) {
|
|
658
668
|
const role = await this.get();
|
|
659
|
-
const resolved = await this.
|
|
660
|
-
const track = this.
|
|
669
|
+
const resolved = await this.#core.resolvePermissions(flatten(permissions), role.guardName);
|
|
670
|
+
const track = this.#core.events.has("permission.detached");
|
|
661
671
|
const before = track ? await this.currentPermissionIds(role) : [];
|
|
662
672
|
await this.rbac.adapter.detachRolePermissions(role.id, resolved.map((p) => p.id));
|
|
663
673
|
await this.rbac.forgetCachedPermissions();
|
|
@@ -667,8 +677,8 @@ var RoleRbac = class {
|
|
|
667
677
|
/** Replaces all permissions of the role with the given ones. */
|
|
668
678
|
async syncPermissions(...permissions) {
|
|
669
679
|
const role = await this.get();
|
|
670
|
-
const resolved = await this.
|
|
671
|
-
const track = this.
|
|
680
|
+
const resolved = await this.#core.resolvePermissions(flatten(permissions), role.guardName);
|
|
681
|
+
const track = this.#core.events.has("permission.attached") || this.#core.events.has("permission.detached");
|
|
672
682
|
const current = track ? await this.getPermissions() : [];
|
|
673
683
|
const newIds = resolved.map((p) => p.id);
|
|
674
684
|
await this.rbac.adapter.attachRolePermissions(role.id, newIds);
|
|
@@ -710,14 +720,14 @@ var RoleRbac = class {
|
|
|
710
720
|
}
|
|
711
721
|
async hasPermissionTo(permission) {
|
|
712
722
|
const name = typeof permission === "string" ? normalizeName(permission) : permission.name;
|
|
713
|
-
return this.
|
|
723
|
+
return this.#core.permissionMatches(await this.getPermissionNames(), name);
|
|
714
724
|
}
|
|
715
725
|
async currentPermissionIds(role) {
|
|
716
726
|
return (await this.rbac.adapter.getRolePermissionLinks([role.id])).map((l) => l.permissionId);
|
|
717
727
|
}
|
|
718
728
|
async emit(event, role, permissions) {
|
|
719
729
|
if (permissions.length === 0) return;
|
|
720
|
-
await this.
|
|
730
|
+
await this.#core.events.emit({
|
|
721
731
|
event,
|
|
722
732
|
target: "role",
|
|
723
733
|
role,
|
|
@@ -733,15 +743,18 @@ var SubjectRbac = class _SubjectRbac {
|
|
|
733
743
|
this.subject = subject;
|
|
734
744
|
this.guardName = guardName;
|
|
735
745
|
this.teamId = teamId;
|
|
746
|
+
this.#core = internalsOf(rbac);
|
|
736
747
|
}
|
|
737
748
|
rbac;
|
|
738
749
|
subject;
|
|
739
750
|
guardName;
|
|
740
751
|
teamId;
|
|
752
|
+
#core;
|
|
741
753
|
// ------------------------------------------------------------------------ roles
|
|
742
754
|
async assignRole(...roles) {
|
|
743
|
-
const { adapter
|
|
744
|
-
const
|
|
755
|
+
const { adapter } = this.rbac;
|
|
756
|
+
const { events } = this.#core;
|
|
757
|
+
const resolved = await this.#core.resolveRoles(flatten(roles), this.guardName, this.teamId);
|
|
745
758
|
const track = events.has("role.attached");
|
|
746
759
|
const before = track ? await adapter.getSubjectRoleIds(this.subject, this.teamId) : [];
|
|
747
760
|
await adapter.attachSubjectRoles(this.subject, resolved.map((r) => r.id), this.teamId);
|
|
@@ -750,8 +763,9 @@ var SubjectRbac = class _SubjectRbac {
|
|
|
750
763
|
return this;
|
|
751
764
|
}
|
|
752
765
|
async removeRole(...roles) {
|
|
753
|
-
const { adapter
|
|
754
|
-
const
|
|
766
|
+
const { adapter } = this.rbac;
|
|
767
|
+
const { events } = this.#core;
|
|
768
|
+
const resolved = await this.#core.resolveRoles(flatten(roles), this.guardName, this.teamId);
|
|
755
769
|
const track = events.has("role.detached");
|
|
756
770
|
const before = track ? await adapter.getSubjectRoleIds(this.subject, this.teamId) : [];
|
|
757
771
|
await adapter.detachSubjectRoles(this.subject, resolved.map((r) => r.id), this.teamId);
|
|
@@ -761,7 +775,7 @@ var SubjectRbac = class _SubjectRbac {
|
|
|
761
775
|
}
|
|
762
776
|
/** Replaces all roles (in the current guard & team) with the given ones. */
|
|
763
777
|
async syncRoles(...roles) {
|
|
764
|
-
const resolved = await this.
|
|
778
|
+
const resolved = await this.#core.resolveRoles(flatten(roles), this.guardName, this.teamId);
|
|
765
779
|
const current = (await this.state()).roles;
|
|
766
780
|
const newIds = resolved.map((r) => r.id);
|
|
767
781
|
await this.rbac.adapter.attachSubjectRoles(this.subject, newIds, this.teamId);
|
|
@@ -803,8 +817,9 @@ var SubjectRbac = class _SubjectRbac {
|
|
|
803
817
|
}
|
|
804
818
|
// ------------------------------------------------------------ direct permissions
|
|
805
819
|
async givePermissionTo(...permissions) {
|
|
806
|
-
const { adapter
|
|
807
|
-
const
|
|
820
|
+
const { adapter } = this.rbac;
|
|
821
|
+
const { events } = this.#core;
|
|
822
|
+
const resolved = await this.#core.resolvePermissions(flatten(permissions), this.guardName);
|
|
808
823
|
const track = events.has("permission.attached");
|
|
809
824
|
const before = track ? await adapter.getSubjectPermissionIds(this.subject, this.teamId) : [];
|
|
810
825
|
await adapter.attachSubjectPermissions(this.subject, resolved.map((p) => p.id), this.teamId);
|
|
@@ -813,8 +828,9 @@ var SubjectRbac = class _SubjectRbac {
|
|
|
813
828
|
return this;
|
|
814
829
|
}
|
|
815
830
|
async revokePermissionTo(...permissions) {
|
|
816
|
-
const { adapter
|
|
817
|
-
const
|
|
831
|
+
const { adapter } = this.rbac;
|
|
832
|
+
const { events } = this.#core;
|
|
833
|
+
const resolved = await this.#core.resolvePermissions(flatten(permissions), this.guardName);
|
|
818
834
|
const track = events.has("permission.detached");
|
|
819
835
|
const before = track ? await adapter.getSubjectPermissionIds(this.subject, this.teamId) : [];
|
|
820
836
|
await adapter.detachSubjectPermissions(this.subject, resolved.map((p) => p.id), this.teamId);
|
|
@@ -824,7 +840,7 @@ var SubjectRbac = class _SubjectRbac {
|
|
|
824
840
|
}
|
|
825
841
|
/** Replaces all direct permissions (in the current guard & team) with the given ones. */
|
|
826
842
|
async syncPermissions(...permissions) {
|
|
827
|
-
const resolved = await this.
|
|
843
|
+
const resolved = await this.#core.resolvePermissions(flatten(permissions), this.guardName);
|
|
828
844
|
const current = (await this.state()).direct;
|
|
829
845
|
const newIds = resolved.map((p) => p.id);
|
|
830
846
|
await this.rbac.adapter.attachSubjectPermissions(this.subject, newIds, this.teamId);
|
|
@@ -859,13 +875,13 @@ var SubjectRbac = class _SubjectRbac {
|
|
|
859
875
|
*/
|
|
860
876
|
async hasPermissionTo(permission) {
|
|
861
877
|
const name = await this.requirePermissionName(permission);
|
|
862
|
-
return this.
|
|
878
|
+
return this.#core.permissionMatches(await this.getPermissionNames(), name);
|
|
863
879
|
}
|
|
864
880
|
/** Checks a direct permission only (ignores roles). */
|
|
865
881
|
async hasDirectPermission(permission) {
|
|
866
882
|
const name = await this.requirePermissionName(permission);
|
|
867
883
|
const direct = (await this.state()).direct.map((p) => p.name);
|
|
868
|
-
return this.
|
|
884
|
+
return this.#core.permissionMatches(direct, name);
|
|
869
885
|
}
|
|
870
886
|
/**
|
|
871
887
|
* Authorization check: runs the `before` hook and `superAdminRoles` first, then falls back
|
|
@@ -873,7 +889,7 @@ var SubjectRbac = class _SubjectRbac {
|
|
|
873
889
|
*/
|
|
874
890
|
async can(permission) {
|
|
875
891
|
const name = typeof permission === "string" ? permission.trim() : permission.name;
|
|
876
|
-
const decision = await this.
|
|
892
|
+
const decision = await this.#core.gate(this, name);
|
|
877
893
|
if (decision !== void 0) return decision;
|
|
878
894
|
return this.check(permission);
|
|
879
895
|
}
|
|
@@ -926,14 +942,14 @@ var SubjectRbac = class _SubjectRbac {
|
|
|
926
942
|
return permission.name;
|
|
927
943
|
}
|
|
928
944
|
const name = normalizeName(permission);
|
|
929
|
-
if (!this.rbac.wildcard && !await this.
|
|
945
|
+
if (!this.rbac.wildcard && !await this.#core.permissionExists(name, this.guardName)) {
|
|
930
946
|
throw new PermissionDoesNotExist(name, this.guardName);
|
|
931
947
|
}
|
|
932
948
|
return name;
|
|
933
949
|
}
|
|
934
950
|
state() {
|
|
935
951
|
const key = `rbac:${JSON.stringify([this.guardName, this.teamId, this.subject.type, String(this.subject.id)])}`;
|
|
936
|
-
return this.
|
|
952
|
+
return this.#core.remember(key, async () => {
|
|
937
953
|
const { adapter } = this.rbac;
|
|
938
954
|
const roleIds = await adapter.getSubjectRoleIds(this.subject, this.teamId);
|
|
939
955
|
const roles = roleIds.length > 0 ? await adapter.findRoles({ ids: roleIds, guardName: this.guardName }) : [];
|
|
@@ -946,7 +962,7 @@ var SubjectRbac = class _SubjectRbac {
|
|
|
946
962
|
}
|
|
947
963
|
async emitRoles(event, roles) {
|
|
948
964
|
if (roles.length === 0) return;
|
|
949
|
-
await this.
|
|
965
|
+
await this.#core.events.emit({
|
|
950
966
|
event,
|
|
951
967
|
subject: this.subject,
|
|
952
968
|
roles,
|
|
@@ -956,7 +972,7 @@ var SubjectRbac = class _SubjectRbac {
|
|
|
956
972
|
}
|
|
957
973
|
async emitPermissions(event, permissions) {
|
|
958
974
|
if (permissions.length === 0) return;
|
|
959
|
-
await this.
|
|
975
|
+
await this.#core.events.emit({
|
|
960
976
|
event,
|
|
961
977
|
target: "subject",
|
|
962
978
|
subject: this.subject,
|
|
@@ -1154,6 +1170,89 @@ function runAdapterTests(name, factory, options = {}) {
|
|
|
1154
1170
|
});
|
|
1155
1171
|
}
|
|
1156
1172
|
});
|
|
1173
|
+
vitest.describe("exact matching", () => {
|
|
1174
|
+
const ACCENTS = { a: "\xE0", e: "\xE9", i: "\xED", o: "\xF3", u: "\xFA" };
|
|
1175
|
+
const lookalikes = (value) => [
|
|
1176
|
+
value.toUpperCase(),
|
|
1177
|
+
`${value} `,
|
|
1178
|
+
value.replace(/[aeiou]/, (vowel) => ACCENTS[vowel])
|
|
1179
|
+
];
|
|
1180
|
+
vitest.it("never returns a role or permission for a lookalike name or guard", async () => {
|
|
1181
|
+
const permission = await adapter.createPermission({ name: "edit", guardName: "web" });
|
|
1182
|
+
const role = await adapter.createRole({ name: "admin", guardName: "web", teamId: null });
|
|
1183
|
+
for (const name2 of lookalikes("edit")) {
|
|
1184
|
+
await Promise.resolve(adapter.createPermission({ name: name2, guardName: "web" })).catch(() => void 0);
|
|
1185
|
+
}
|
|
1186
|
+
for (const name2 of lookalikes("admin")) {
|
|
1187
|
+
await Promise.resolve(adapter.createRole({ name: name2, guardName: "web", teamId: null })).catch(() => void 0);
|
|
1188
|
+
}
|
|
1189
|
+
const permissions = await adapter.findPermissions({ names: ["edit"] });
|
|
1190
|
+
vitest.expect(permissions.map((p) => [String(p.id), p.name])).toEqual([[String(permission.id), "edit"]]);
|
|
1191
|
+
const roles = await adapter.findRoles({ names: ["admin"] });
|
|
1192
|
+
vitest.expect(roles.map((r) => [String(r.id), r.name])).toEqual([[String(role.id), "admin"]]);
|
|
1193
|
+
for (const name2 of lookalikes("edit")) {
|
|
1194
|
+
for (const p of await adapter.findPermissions({ names: [name2] })) vitest.expect(p.name, name2).toBe(name2);
|
|
1195
|
+
}
|
|
1196
|
+
for (const name2 of lookalikes("admin")) {
|
|
1197
|
+
for (const r of await adapter.findRoles({ names: [name2] })) vitest.expect(r.name, name2).toBe(name2);
|
|
1198
|
+
}
|
|
1199
|
+
vitest.expect(await adapter.findPermissions({ names: ["edit"], guardName: "WEB" })).toEqual([]);
|
|
1200
|
+
vitest.expect(await adapter.findRoles({ names: ["admin"], guardName: "web " })).toEqual([]);
|
|
1201
|
+
});
|
|
1202
|
+
vitest.it("never gives a subject the links of a lookalike subject id or type", async () => {
|
|
1203
|
+
const r = await adapter.createRole({ name: "admin", guardName: "web", teamId: null });
|
|
1204
|
+
const p = await adapter.createPermission({ name: "edit", guardName: "web" });
|
|
1205
|
+
const admin = { type: "user", id: "admin" };
|
|
1206
|
+
await adapter.attachSubjectRoles(admin, [r.id], null);
|
|
1207
|
+
await adapter.attachSubjectPermissions(admin, [p.id], null);
|
|
1208
|
+
const others = [
|
|
1209
|
+
...lookalikes("admin").map((id) => ({ type: "user", id })),
|
|
1210
|
+
{ type: "USER", id: "admin" },
|
|
1211
|
+
{ type: "user ", id: "admin" }
|
|
1212
|
+
];
|
|
1213
|
+
for (const other of others) {
|
|
1214
|
+
vitest.expect(await adapter.getSubjectRoleIds(other, null), JSON.stringify(other)).toEqual([]);
|
|
1215
|
+
vitest.expect(await adapter.getSubjectPermissionIds(other, null), JSON.stringify(other)).toEqual([]);
|
|
1216
|
+
}
|
|
1217
|
+
vitest.expect(s(await adapter.getSubjectRoleIds(admin, null))).toEqual(s([r.id]));
|
|
1218
|
+
vitest.expect(s(await adapter.getSubjectIdsWithRoles("user", [r.id], null))).toEqual(["admin"]);
|
|
1219
|
+
vitest.expect(await adapter.getSubjectIdsWithRoles("USER", [r.id], null)).toEqual([]);
|
|
1220
|
+
vitest.expect(await adapter.getSubjectIdsWithPermissions("USER", [p.id], null)).toEqual([]);
|
|
1221
|
+
for (const other of others) {
|
|
1222
|
+
await adapter.detachSubjectRoles(other, void 0, null);
|
|
1223
|
+
await adapter.detachSubjectPermissions(other, void 0, null);
|
|
1224
|
+
}
|
|
1225
|
+
vitest.expect(s(await adapter.getSubjectRoleIds(admin, null))).toEqual(s([r.id]));
|
|
1226
|
+
vitest.expect(s(await adapter.getSubjectPermissionIds(admin, null))).toEqual(s([p.id]));
|
|
1227
|
+
});
|
|
1228
|
+
if (teams) {
|
|
1229
|
+
vitest.it("never mixes up lookalike team ids", async () => {
|
|
1230
|
+
const r = await adapter.createRole({ name: "admin", guardName: "web", teamId: "acme" });
|
|
1231
|
+
const p = await adapter.createPermission({ name: "edit", guardName: "web" });
|
|
1232
|
+
await adapter.attachSubjectRoles(alice, [r.id], "acme");
|
|
1233
|
+
await adapter.attachSubjectPermissions(alice, [p.id], "acme");
|
|
1234
|
+
for (const team of lookalikes("acme")) {
|
|
1235
|
+
vitest.expect(await adapter.findRoles({ teamIds: [team] }), team).toEqual([]);
|
|
1236
|
+
vitest.expect(await adapter.getSubjectRoleIds(alice, team), team).toEqual([]);
|
|
1237
|
+
vitest.expect(await adapter.getSubjectPermissionIds(alice, team), team).toEqual([]);
|
|
1238
|
+
vitest.expect(await adapter.getSubjectIdsWithRoles("user", [r.id], team), team).toEqual([]);
|
|
1239
|
+
}
|
|
1240
|
+
vitest.expect(await adapter.findRoles({ teamIds: ["acme"] })).toHaveLength(1);
|
|
1241
|
+
});
|
|
1242
|
+
}
|
|
1243
|
+
vitest.it("end to end: a lookalike user is denied", async () => {
|
|
1244
|
+
const rbac = createRbac({ adapter, cache: false });
|
|
1245
|
+
await rbac.createPermission("delete everything");
|
|
1246
|
+
await rbac.createRole("admin");
|
|
1247
|
+
await rbac.role("admin").givePermissionTo("delete everything");
|
|
1248
|
+
await rbac.for({ type: "user", id: "admin" }).assignRole("admin");
|
|
1249
|
+
vitest.expect(await rbac.for({ type: "user", id: "admin" }).can("delete everything")).toBe(true);
|
|
1250
|
+
for (const id of lookalikes("admin")) {
|
|
1251
|
+
vitest.expect(await rbac.for({ type: "user", id }).can("delete everything"), id).toBe(false);
|
|
1252
|
+
vitest.expect(await rbac.for({ type: "user", id }).hasRole("admin"), id).toBe(false);
|
|
1253
|
+
}
|
|
1254
|
+
});
|
|
1255
|
+
});
|
|
1157
1256
|
vitest.describe("engine integration", () => {
|
|
1158
1257
|
vitest.it("runs a full flow through createRbac", async () => {
|
|
1159
1258
|
const rbac = createRbac({ adapter, teams, cache: false });
|