@anievo-id/rbac-evolution 0.6.5 → 0.7.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/README.md +1 -1
- package/dist/{chunk-T7W6K4SM.js → chunk-N2QVWHD7.js} +97 -81
- package/dist/chunk-N2QVWHD7.js.map +1 -0
- package/dist/index.cjs +96 -80
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -35
- package/dist/index.d.ts +2 -35
- 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.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/index.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
// package.json
|
|
4
|
-
var version = "0.
|
|
4
|
+
var version = "0.7.0";
|
|
5
5
|
|
|
6
6
|
// src/cache.ts
|
|
7
7
|
var MemoryCache = class {
|
|
@@ -144,7 +144,7 @@ function cleanName(value, where) {
|
|
|
144
144
|
function pushUnique(list, name) {
|
|
145
145
|
if (!list.includes(name)) list.push(name);
|
|
146
146
|
}
|
|
147
|
-
function normalize(
|
|
147
|
+
function normalize(core, definition, options) {
|
|
148
148
|
const list = Array.isArray(definition) ? definition : [definition];
|
|
149
149
|
const blocks = [];
|
|
150
150
|
list.forEach((raw, index) => {
|
|
@@ -153,12 +153,12 @@ function normalize(rbac, definition, options) {
|
|
|
153
153
|
const def = raw;
|
|
154
154
|
const guardInput = def["guard"] ?? options.guard;
|
|
155
155
|
if (guardInput !== void 0 && typeof guardInput !== "string") throw invalid(`${at}.guard must be a string.`);
|
|
156
|
-
const guard =
|
|
156
|
+
const guard = core.resolveGuard(guardInput === void 0 ? void 0 : cleanName(guardInput, `${at}.guard`));
|
|
157
157
|
const teamInput = def["team"] !== void 0 ? def["team"] : options.team;
|
|
158
158
|
if (teamInput !== void 0 && teamInput !== null && typeof teamInput !== "string" && typeof teamInput !== "number") {
|
|
159
159
|
throw invalid(`${at}.team must be a string, a number or null.`);
|
|
160
160
|
}
|
|
161
|
-
const team =
|
|
161
|
+
const team = core.resolveTeam(teamInput);
|
|
162
162
|
let block = blocks.find((b) => b.guard === guard && idKey(b.team) === idKey(team));
|
|
163
163
|
if (!block) {
|
|
164
164
|
block = { guard, team, permissions: [], roles: /* @__PURE__ */ new Map() };
|
|
@@ -194,8 +194,8 @@ function normalize(rbac, definition, options) {
|
|
|
194
194
|
});
|
|
195
195
|
return blocks;
|
|
196
196
|
}
|
|
197
|
-
async function syncDefinition(rbac, definition, options = {}) {
|
|
198
|
-
const blocks = normalize(
|
|
197
|
+
async function syncDefinition(rbac, core, definition, options = {}) {
|
|
198
|
+
const blocks = normalize(core, definition, options);
|
|
199
199
|
const prune = options.prune ?? false;
|
|
200
200
|
const dryRun = options.dryRun ?? false;
|
|
201
201
|
const result = {
|
|
@@ -327,6 +327,7 @@ function normalizeName(name) {
|
|
|
327
327
|
if (trimmed === "") throw new RbacError("INVALID_NAME", "Role and permission names must not be empty.");
|
|
328
328
|
return trimmed;
|
|
329
329
|
}
|
|
330
|
+
var internalsOf;
|
|
330
331
|
var Rbac = class _Rbac {
|
|
331
332
|
adapter;
|
|
332
333
|
defaultGuard;
|
|
@@ -339,6 +340,10 @@ var Rbac = class _Rbac {
|
|
|
339
340
|
#events = new RbacEmitter();
|
|
340
341
|
/** Bumped on every change, so a lookup that started before the change never caches its stale result. */
|
|
341
342
|
#generation = 0;
|
|
343
|
+
#internals;
|
|
344
|
+
static {
|
|
345
|
+
internalsOf = (rbac) => rbac.#internals;
|
|
346
|
+
}
|
|
342
347
|
constructor(options) {
|
|
343
348
|
this.adapter = options.adapter;
|
|
344
349
|
this.defaultGuard = options.defaultGuard ?? "web";
|
|
@@ -348,15 +353,27 @@ var Rbac = class _Rbac {
|
|
|
348
353
|
this.#cacheTtl = options.cacheTtl ?? DAY_MS;
|
|
349
354
|
this.#before = options.before;
|
|
350
355
|
this.#superAdminRoles = options.superAdminRoles ?? [];
|
|
356
|
+
this.#internals = {
|
|
357
|
+
events: this.#events,
|
|
358
|
+
remember: (key2, compute) => this.#remember(key2, compute),
|
|
359
|
+
permissionExists: (name, guardName) => this.#permissionExists(name, guardName),
|
|
360
|
+
gate: (subject, permission) => this.#gate(subject, permission),
|
|
361
|
+
resolveGuard: (guard) => this.#resolveGuard(guard),
|
|
362
|
+
resolveTeam: (team) => this.#resolveTeam(team),
|
|
363
|
+
visibleTeams: (teamId) => this.#visibleTeams(teamId),
|
|
364
|
+
resolvePermissions: (inputs, guardName) => this.#resolvePermissions(inputs, guardName),
|
|
365
|
+
resolveRoles: (inputs, guardName, teamId) => this.#resolveRoles(inputs, guardName, teamId),
|
|
366
|
+
permissionMatches: (granted, required) => this.#permissionMatches(granted, required)
|
|
367
|
+
};
|
|
351
368
|
}
|
|
352
369
|
/** Work with the roles & permissions of a subject (usually a user). */
|
|
353
370
|
for(subject, options = {}) {
|
|
354
|
-
return new SubjectRbac(this, subject, this
|
|
371
|
+
return new SubjectRbac(this, subject, this.#resolveGuard(options.guard), this.#resolveTeam(options.team));
|
|
355
372
|
}
|
|
356
373
|
/** Work with the permissions of a role. */
|
|
357
374
|
role(role, options = {}) {
|
|
358
375
|
if (typeof role !== "string") return new RoleRbac(this, role, role.guardName, role.teamId);
|
|
359
|
-
return new RoleRbac(this, role, this
|
|
376
|
+
return new RoleRbac(this, role, this.#resolveGuard(options.guard), this.#resolveTeam(options.team));
|
|
360
377
|
}
|
|
361
378
|
// ----------------------------------------------------------------------- events
|
|
362
379
|
/**
|
|
@@ -370,13 +387,9 @@ var Rbac = class _Rbac {
|
|
|
370
387
|
off(event, listener) {
|
|
371
388
|
this.#events.off(event, listener);
|
|
372
389
|
}
|
|
373
|
-
/** @internal */
|
|
374
|
-
get events() {
|
|
375
|
-
return this.#events;
|
|
376
|
-
}
|
|
377
390
|
// ------------------------------------------------------------------ permissions
|
|
378
391
|
async createPermission(name, options = {}) {
|
|
379
|
-
const guardName = this
|
|
392
|
+
const guardName = this.#resolveGuard(options.guard);
|
|
380
393
|
const permissionName = normalizeName(name);
|
|
381
394
|
if (this.wildcard) parseWildcardPermission(permissionName);
|
|
382
395
|
if (await this.findPermission(permissionName, { guard: guardName })) {
|
|
@@ -390,32 +403,32 @@ var Rbac = class _Rbac {
|
|
|
390
403
|
async findPermission(name, options = {}) {
|
|
391
404
|
const [permission] = await this.adapter.findPermissions({
|
|
392
405
|
names: [normalizeName(name)],
|
|
393
|
-
guardName: this
|
|
406
|
+
guardName: this.#resolveGuard(options.guard)
|
|
394
407
|
});
|
|
395
408
|
return permission ?? null;
|
|
396
409
|
}
|
|
397
410
|
async findPermissionOrFail(name, options = {}) {
|
|
398
411
|
const permission = await this.findPermission(name, options);
|
|
399
|
-
if (!permission) throw new PermissionDoesNotExist(normalizeName(name), this
|
|
412
|
+
if (!permission) throw new PermissionDoesNotExist(normalizeName(name), this.#resolveGuard(options.guard));
|
|
400
413
|
return permission;
|
|
401
414
|
}
|
|
402
415
|
async findOrCreatePermission(name, options = {}) {
|
|
403
416
|
return await this.findPermission(name, options) ?? this.createPermission(name, options);
|
|
404
417
|
}
|
|
405
418
|
async getPermissions(options = {}) {
|
|
406
|
-
return this.adapter.findPermissions({ guardName: this
|
|
419
|
+
return this.adapter.findPermissions({ guardName: this.#resolveGuard(options.guard) });
|
|
407
420
|
}
|
|
408
421
|
async deletePermission(permission, options = {}) {
|
|
409
|
-
const guardName = typeof permission === "string" ? this
|
|
410
|
-
const [resolved] = await this
|
|
422
|
+
const guardName = typeof permission === "string" ? this.#resolveGuard(options.guard) : permission.guardName;
|
|
423
|
+
const [resolved] = await this.#resolvePermissions([permission], guardName);
|
|
411
424
|
await this.adapter.deletePermission(resolved.id);
|
|
412
425
|
await this.forgetCachedPermissions();
|
|
413
426
|
await this.#events.emit({ event: "permission.deleted", permission: resolved });
|
|
414
427
|
}
|
|
415
428
|
// ------------------------------------------------------------------------ roles
|
|
416
429
|
async createRole(name, options = {}) {
|
|
417
|
-
const guardName = this
|
|
418
|
-
const teamId = this
|
|
430
|
+
const guardName = this.#resolveGuard(options.guard);
|
|
431
|
+
const teamId = this.#resolveTeam(options.team);
|
|
419
432
|
const roleName = normalizeName(name);
|
|
420
433
|
const existing = await this.adapter.findRoles({ names: [roleName], guardName, teamIds: [teamId] });
|
|
421
434
|
if (existing.length > 0) throw new RoleAlreadyExists(roleName, guardName);
|
|
@@ -426,15 +439,15 @@ var Rbac = class _Rbac {
|
|
|
426
439
|
}
|
|
427
440
|
/** Finds a role. With a team, a team-specific role wins over a global role of the same name. */
|
|
428
441
|
async findRole(name, options = {}) {
|
|
429
|
-
const guardName = this
|
|
430
|
-
const teamId = this
|
|
442
|
+
const guardName = this.#resolveGuard(options.guard);
|
|
443
|
+
const teamId = this.#resolveTeam(options.team);
|
|
431
444
|
const roleName = normalizeName(name);
|
|
432
|
-
const roles = await this.adapter.findRoles({ names: [roleName], guardName, teamIds: this
|
|
445
|
+
const roles = await this.adapter.findRoles({ names: [roleName], guardName, teamIds: this.#visibleTeams(teamId) });
|
|
433
446
|
return _Rbac.pickRole(roles, roleName, teamId) ?? null;
|
|
434
447
|
}
|
|
435
448
|
async findRoleOrFail(name, options = {}) {
|
|
436
449
|
const role = await this.findRole(name, options);
|
|
437
|
-
if (!role) throw new RoleDoesNotExist(normalizeName(name), this
|
|
450
|
+
if (!role) throw new RoleDoesNotExist(normalizeName(name), this.#resolveGuard(options.guard));
|
|
438
451
|
return role;
|
|
439
452
|
}
|
|
440
453
|
async findOrCreateRole(name, options = {}) {
|
|
@@ -442,8 +455,8 @@ var Rbac = class _Rbac {
|
|
|
442
455
|
}
|
|
443
456
|
/** Lists roles visible in the given scope (global roles + roles of the given team). */
|
|
444
457
|
async getRoles(options = {}) {
|
|
445
|
-
const teamId = this
|
|
446
|
-
return this.adapter.findRoles({ guardName: this
|
|
458
|
+
const teamId = this.#resolveTeam(options.team);
|
|
459
|
+
return this.adapter.findRoles({ guardName: this.#resolveGuard(options.guard), teamIds: this.#visibleTeams(teamId) });
|
|
447
460
|
}
|
|
448
461
|
async deleteRole(role, options = {}) {
|
|
449
462
|
const resolved = await this.role(role, options).get();
|
|
@@ -473,7 +486,7 @@ var Rbac = class _Rbac {
|
|
|
473
486
|
* Not atomic: adapters have no transactions, so a failure midway leaves the earlier changes.
|
|
474
487
|
*/
|
|
475
488
|
async sync(definition, options = {}) {
|
|
476
|
-
return syncDefinition(this, definition, options);
|
|
489
|
+
return syncDefinition(this, this.#internals, definition, options);
|
|
477
490
|
}
|
|
478
491
|
// ---------------------------------------------------------------------- queries
|
|
479
492
|
/**
|
|
@@ -481,9 +494,9 @@ var Rbac = class _Rbac {
|
|
|
481
494
|
* (spatie: `User::role('writer')->get()`). Throws `RoleDoesNotExist` for unknown roles.
|
|
482
495
|
*/
|
|
483
496
|
async subjectsWithRole(subjectType, roles, options = {}) {
|
|
484
|
-
const guardName = this
|
|
485
|
-
const teamId = this
|
|
486
|
-
const resolved = await this
|
|
497
|
+
const guardName = this.#resolveGuard(options.guard);
|
|
498
|
+
const teamId = this.#resolveTeam(options.team);
|
|
499
|
+
const resolved = await this.#resolveRoles(flatten([roles]), guardName, teamId);
|
|
487
500
|
if (resolved.length === 0) return [];
|
|
488
501
|
return this.adapter.getSubjectIdsWithRoles(
|
|
489
502
|
subjectType,
|
|
@@ -497,13 +510,13 @@ var Rbac = class _Rbac {
|
|
|
497
510
|
* Throws `PermissionDoesNotExist` for unknown permissions. Wildcards are not expanded.
|
|
498
511
|
*/
|
|
499
512
|
async subjectsWithPermission(subjectType, permissions, options = {}) {
|
|
500
|
-
const guardName = this
|
|
501
|
-
const teamId = this
|
|
502
|
-
const resolved = await this
|
|
513
|
+
const guardName = this.#resolveGuard(options.guard);
|
|
514
|
+
const teamId = this.#resolveTeam(options.team);
|
|
515
|
+
const resolved = await this.#resolvePermissions(flatten([permissions]), guardName);
|
|
503
516
|
if (resolved.length === 0) return [];
|
|
504
517
|
const permissionIds = resolved.map((p) => p.id);
|
|
505
518
|
const wanted = new Set(permissionIds.map(idKey2));
|
|
506
|
-
const roles = await this.adapter.findRoles({ guardName, teamIds: this
|
|
519
|
+
const roles = await this.adapter.findRoles({ guardName, teamIds: this.#visibleTeams(teamId) });
|
|
507
520
|
const links = roles.length > 0 ? await this.adapter.getRolePermissionLinks(roles.map((r) => r.id)) : [];
|
|
508
521
|
const roleIds = uniqueIds(links.filter((l) => wanted.has(idKey2(l.permissionId))).map((l) => l.roleId));
|
|
509
522
|
const viaRoles = roleIds.length > 0 ? await this.adapter.getSubjectIdsWithRoles(subjectType, roleIds, teamId) : [];
|
|
@@ -516,8 +529,7 @@ var Rbac = class _Rbac {
|
|
|
516
529
|
this.#generation++;
|
|
517
530
|
await this.#cache?.clear();
|
|
518
531
|
}
|
|
519
|
-
|
|
520
|
-
async remember(key2, compute) {
|
|
532
|
+
async #remember(key2, compute) {
|
|
521
533
|
if (!this.#cache) return compute();
|
|
522
534
|
const hit = await this.#cache.get(key2);
|
|
523
535
|
if (hit !== void 0) return hit;
|
|
@@ -527,24 +539,24 @@ var Rbac = class _Rbac {
|
|
|
527
539
|
return value;
|
|
528
540
|
}
|
|
529
541
|
/**
|
|
530
|
-
*
|
|
542
|
+
* Whether a permission with this name exists in the guard.
|
|
531
543
|
* With caching on, the guard's permission names are cached (cleared on every change),
|
|
532
544
|
* so repeated `can()` checks don't hit the database.
|
|
533
545
|
*/
|
|
534
|
-
async permissionExists(name, guardName) {
|
|
546
|
+
async #permissionExists(name, guardName) {
|
|
535
547
|
if (!this.#cache) {
|
|
536
548
|
const [permission] = await this.adapter.findPermissions({ names: [name], guardName });
|
|
537
549
|
return permission !== void 0;
|
|
538
550
|
}
|
|
539
|
-
const names = await this
|
|
551
|
+
const names = await this.#remember(
|
|
540
552
|
`rbac:permissions:${JSON.stringify([guardName])}`,
|
|
541
553
|
async () => (await this.adapter.findPermissions({ guardName })).map((p) => p.name)
|
|
542
554
|
);
|
|
543
555
|
return names.includes(name);
|
|
544
556
|
}
|
|
545
557
|
// ---------------------------------------------------------------------- internal
|
|
546
|
-
/**
|
|
547
|
-
async gate(subject, permission) {
|
|
558
|
+
/** Runs the `before` hook and the super-admin check. `undefined` = no decision. */
|
|
559
|
+
async #gate(subject, permission) {
|
|
548
560
|
if (this.#before) {
|
|
549
561
|
const decision = await this.#before({ subject, permission });
|
|
550
562
|
if (typeof decision === "boolean") return decision;
|
|
@@ -554,24 +566,21 @@ var Rbac = class _Rbac {
|
|
|
554
566
|
}
|
|
555
567
|
return void 0;
|
|
556
568
|
}
|
|
557
|
-
|
|
558
|
-
resolveGuard(guard) {
|
|
569
|
+
#resolveGuard(guard) {
|
|
559
570
|
return guard ?? this.defaultGuard;
|
|
560
571
|
}
|
|
561
|
-
|
|
562
|
-
resolveTeam(team) {
|
|
572
|
+
#resolveTeam(team) {
|
|
563
573
|
if (team === void 0 || team === null) return null;
|
|
564
574
|
if (!this.teams) {
|
|
565
575
|
throw new RbacError("TEAMS_NOT_ENABLED", "A team was given but teams are not enabled. Use `createRbac({ teams: true })`.");
|
|
566
576
|
}
|
|
567
577
|
return team;
|
|
568
578
|
}
|
|
569
|
-
|
|
570
|
-
visibleTeams(teamId) {
|
|
579
|
+
#visibleTeams(teamId) {
|
|
571
580
|
return teamId === null ? [null] : [teamId, null];
|
|
572
581
|
}
|
|
573
|
-
/**
|
|
574
|
-
async resolvePermissions(inputs, guardName) {
|
|
582
|
+
/** Resolves names/objects to permissions, validating existence and guard. */
|
|
583
|
+
async #resolvePermissions(inputs, guardName) {
|
|
575
584
|
const names = [];
|
|
576
585
|
const objects = [];
|
|
577
586
|
for (const input of inputs) {
|
|
@@ -588,8 +597,8 @@ var Rbac = class _Rbac {
|
|
|
588
597
|
}
|
|
589
598
|
return uniqueById([...found, ...objects]);
|
|
590
599
|
}
|
|
591
|
-
/**
|
|
592
|
-
async resolveRoles(inputs, guardName, teamId) {
|
|
600
|
+
/** Resolves names/objects to roles, validating existence, guard and team. */
|
|
601
|
+
async #resolveRoles(inputs, guardName, teamId) {
|
|
593
602
|
const names = [];
|
|
594
603
|
const objects = [];
|
|
595
604
|
for (const input of inputs) {
|
|
@@ -603,7 +612,7 @@ var Rbac = class _Rbac {
|
|
|
603
612
|
objects.push(input);
|
|
604
613
|
}
|
|
605
614
|
}
|
|
606
|
-
const found = names.length > 0 ? await this.adapter.findRoles({ names, guardName, teamIds: this
|
|
615
|
+
const found = names.length > 0 ? await this.adapter.findRoles({ names, guardName, teamIds: this.#visibleTeams(teamId) }) : [];
|
|
607
616
|
const picked = names.map((name) => {
|
|
608
617
|
const role = _Rbac.pickRole(found, name, teamId);
|
|
609
618
|
if (!role) throw new RoleDoesNotExist(name, guardName);
|
|
@@ -611,8 +620,7 @@ var Rbac = class _Rbac {
|
|
|
611
620
|
});
|
|
612
621
|
return uniqueById([...picked, ...objects]);
|
|
613
622
|
}
|
|
614
|
-
|
|
615
|
-
permissionMatches(granted, required) {
|
|
623
|
+
#permissionMatches(granted, required) {
|
|
616
624
|
if (!this.wildcard) return granted.includes(required);
|
|
617
625
|
return granted.some((pattern) => {
|
|
618
626
|
try {
|
|
@@ -633,20 +641,22 @@ var RoleRbac = class {
|
|
|
633
641
|
this.input = input;
|
|
634
642
|
this.guardName = guardName;
|
|
635
643
|
this.teamId = teamId;
|
|
644
|
+
this.#core = internalsOf(rbac);
|
|
636
645
|
}
|
|
637
646
|
rbac;
|
|
638
647
|
input;
|
|
639
648
|
guardName;
|
|
640
649
|
teamId;
|
|
650
|
+
#core;
|
|
641
651
|
/** Returns the role, throwing `RoleDoesNotExist` if it can't be found. */
|
|
642
652
|
async get() {
|
|
643
|
-
const [role] = await this.
|
|
653
|
+
const [role] = await this.#core.resolveRoles([this.input], this.guardName, this.teamId);
|
|
644
654
|
return role;
|
|
645
655
|
}
|
|
646
656
|
async givePermissionTo(...permissions) {
|
|
647
657
|
const role = await this.get();
|
|
648
|
-
const resolved = await this.
|
|
649
|
-
const track = this.
|
|
658
|
+
const resolved = await this.#core.resolvePermissions(flatten(permissions), role.guardName);
|
|
659
|
+
const track = this.#core.events.has("permission.attached");
|
|
650
660
|
const before = track ? await this.currentPermissionIds(role) : [];
|
|
651
661
|
await this.rbac.adapter.attachRolePermissions(role.id, resolved.map((p) => p.id));
|
|
652
662
|
await this.rbac.forgetCachedPermissions();
|
|
@@ -655,8 +665,8 @@ var RoleRbac = class {
|
|
|
655
665
|
}
|
|
656
666
|
async revokePermissionTo(...permissions) {
|
|
657
667
|
const role = await this.get();
|
|
658
|
-
const resolved = await this.
|
|
659
|
-
const track = this.
|
|
668
|
+
const resolved = await this.#core.resolvePermissions(flatten(permissions), role.guardName);
|
|
669
|
+
const track = this.#core.events.has("permission.detached");
|
|
660
670
|
const before = track ? await this.currentPermissionIds(role) : [];
|
|
661
671
|
await this.rbac.adapter.detachRolePermissions(role.id, resolved.map((p) => p.id));
|
|
662
672
|
await this.rbac.forgetCachedPermissions();
|
|
@@ -666,8 +676,8 @@ var RoleRbac = class {
|
|
|
666
676
|
/** Replaces all permissions of the role with the given ones. */
|
|
667
677
|
async syncPermissions(...permissions) {
|
|
668
678
|
const role = await this.get();
|
|
669
|
-
const resolved = await this.
|
|
670
|
-
const track = this.
|
|
679
|
+
const resolved = await this.#core.resolvePermissions(flatten(permissions), role.guardName);
|
|
680
|
+
const track = this.#core.events.has("permission.attached") || this.#core.events.has("permission.detached");
|
|
671
681
|
const current = track ? await this.getPermissions() : [];
|
|
672
682
|
const newIds = resolved.map((p) => p.id);
|
|
673
683
|
await this.rbac.adapter.attachRolePermissions(role.id, newIds);
|
|
@@ -709,14 +719,14 @@ var RoleRbac = class {
|
|
|
709
719
|
}
|
|
710
720
|
async hasPermissionTo(permission) {
|
|
711
721
|
const name = typeof permission === "string" ? normalizeName(permission) : permission.name;
|
|
712
|
-
return this.
|
|
722
|
+
return this.#core.permissionMatches(await this.getPermissionNames(), name);
|
|
713
723
|
}
|
|
714
724
|
async currentPermissionIds(role) {
|
|
715
725
|
return (await this.rbac.adapter.getRolePermissionLinks([role.id])).map((l) => l.permissionId);
|
|
716
726
|
}
|
|
717
727
|
async emit(event, role, permissions) {
|
|
718
728
|
if (permissions.length === 0) return;
|
|
719
|
-
await this.
|
|
729
|
+
await this.#core.events.emit({
|
|
720
730
|
event,
|
|
721
731
|
target: "role",
|
|
722
732
|
role,
|
|
@@ -732,15 +742,18 @@ var SubjectRbac = class _SubjectRbac {
|
|
|
732
742
|
this.subject = subject;
|
|
733
743
|
this.guardName = guardName;
|
|
734
744
|
this.teamId = teamId;
|
|
745
|
+
this.#core = internalsOf(rbac);
|
|
735
746
|
}
|
|
736
747
|
rbac;
|
|
737
748
|
subject;
|
|
738
749
|
guardName;
|
|
739
750
|
teamId;
|
|
751
|
+
#core;
|
|
740
752
|
// ------------------------------------------------------------------------ roles
|
|
741
753
|
async assignRole(...roles) {
|
|
742
|
-
const { adapter
|
|
743
|
-
const
|
|
754
|
+
const { adapter } = this.rbac;
|
|
755
|
+
const { events } = this.#core;
|
|
756
|
+
const resolved = await this.#core.resolveRoles(flatten(roles), this.guardName, this.teamId);
|
|
744
757
|
const track = events.has("role.attached");
|
|
745
758
|
const before = track ? await adapter.getSubjectRoleIds(this.subject, this.teamId) : [];
|
|
746
759
|
await adapter.attachSubjectRoles(this.subject, resolved.map((r) => r.id), this.teamId);
|
|
@@ -749,8 +762,9 @@ var SubjectRbac = class _SubjectRbac {
|
|
|
749
762
|
return this;
|
|
750
763
|
}
|
|
751
764
|
async removeRole(...roles) {
|
|
752
|
-
const { adapter
|
|
753
|
-
const
|
|
765
|
+
const { adapter } = this.rbac;
|
|
766
|
+
const { events } = this.#core;
|
|
767
|
+
const resolved = await this.#core.resolveRoles(flatten(roles), this.guardName, this.teamId);
|
|
754
768
|
const track = events.has("role.detached");
|
|
755
769
|
const before = track ? await adapter.getSubjectRoleIds(this.subject, this.teamId) : [];
|
|
756
770
|
await adapter.detachSubjectRoles(this.subject, resolved.map((r) => r.id), this.teamId);
|
|
@@ -760,7 +774,7 @@ var SubjectRbac = class _SubjectRbac {
|
|
|
760
774
|
}
|
|
761
775
|
/** Replaces all roles (in the current guard & team) with the given ones. */
|
|
762
776
|
async syncRoles(...roles) {
|
|
763
|
-
const resolved = await this.
|
|
777
|
+
const resolved = await this.#core.resolveRoles(flatten(roles), this.guardName, this.teamId);
|
|
764
778
|
const current = (await this.state()).roles;
|
|
765
779
|
const newIds = resolved.map((r) => r.id);
|
|
766
780
|
await this.rbac.adapter.attachSubjectRoles(this.subject, newIds, this.teamId);
|
|
@@ -802,8 +816,9 @@ var SubjectRbac = class _SubjectRbac {
|
|
|
802
816
|
}
|
|
803
817
|
// ------------------------------------------------------------ direct permissions
|
|
804
818
|
async givePermissionTo(...permissions) {
|
|
805
|
-
const { adapter
|
|
806
|
-
const
|
|
819
|
+
const { adapter } = this.rbac;
|
|
820
|
+
const { events } = this.#core;
|
|
821
|
+
const resolved = await this.#core.resolvePermissions(flatten(permissions), this.guardName);
|
|
807
822
|
const track = events.has("permission.attached");
|
|
808
823
|
const before = track ? await adapter.getSubjectPermissionIds(this.subject, this.teamId) : [];
|
|
809
824
|
await adapter.attachSubjectPermissions(this.subject, resolved.map((p) => p.id), this.teamId);
|
|
@@ -812,8 +827,9 @@ var SubjectRbac = class _SubjectRbac {
|
|
|
812
827
|
return this;
|
|
813
828
|
}
|
|
814
829
|
async revokePermissionTo(...permissions) {
|
|
815
|
-
const { adapter
|
|
816
|
-
const
|
|
830
|
+
const { adapter } = this.rbac;
|
|
831
|
+
const { events } = this.#core;
|
|
832
|
+
const resolved = await this.#core.resolvePermissions(flatten(permissions), this.guardName);
|
|
817
833
|
const track = events.has("permission.detached");
|
|
818
834
|
const before = track ? await adapter.getSubjectPermissionIds(this.subject, this.teamId) : [];
|
|
819
835
|
await adapter.detachSubjectPermissions(this.subject, resolved.map((p) => p.id), this.teamId);
|
|
@@ -823,7 +839,7 @@ var SubjectRbac = class _SubjectRbac {
|
|
|
823
839
|
}
|
|
824
840
|
/** Replaces all direct permissions (in the current guard & team) with the given ones. */
|
|
825
841
|
async syncPermissions(...permissions) {
|
|
826
|
-
const resolved = await this.
|
|
842
|
+
const resolved = await this.#core.resolvePermissions(flatten(permissions), this.guardName);
|
|
827
843
|
const current = (await this.state()).direct;
|
|
828
844
|
const newIds = resolved.map((p) => p.id);
|
|
829
845
|
await this.rbac.adapter.attachSubjectPermissions(this.subject, newIds, this.teamId);
|
|
@@ -858,13 +874,13 @@ var SubjectRbac = class _SubjectRbac {
|
|
|
858
874
|
*/
|
|
859
875
|
async hasPermissionTo(permission) {
|
|
860
876
|
const name = await this.requirePermissionName(permission);
|
|
861
|
-
return this.
|
|
877
|
+
return this.#core.permissionMatches(await this.getPermissionNames(), name);
|
|
862
878
|
}
|
|
863
879
|
/** Checks a direct permission only (ignores roles). */
|
|
864
880
|
async hasDirectPermission(permission) {
|
|
865
881
|
const name = await this.requirePermissionName(permission);
|
|
866
882
|
const direct = (await this.state()).direct.map((p) => p.name);
|
|
867
|
-
return this.
|
|
883
|
+
return this.#core.permissionMatches(direct, name);
|
|
868
884
|
}
|
|
869
885
|
/**
|
|
870
886
|
* Authorization check: runs the `before` hook and `superAdminRoles` first, then falls back
|
|
@@ -872,7 +888,7 @@ var SubjectRbac = class _SubjectRbac {
|
|
|
872
888
|
*/
|
|
873
889
|
async can(permission) {
|
|
874
890
|
const name = typeof permission === "string" ? permission.trim() : permission.name;
|
|
875
|
-
const decision = await this.
|
|
891
|
+
const decision = await this.#core.gate(this, name);
|
|
876
892
|
if (decision !== void 0) return decision;
|
|
877
893
|
return this.check(permission);
|
|
878
894
|
}
|
|
@@ -925,14 +941,14 @@ var SubjectRbac = class _SubjectRbac {
|
|
|
925
941
|
return permission.name;
|
|
926
942
|
}
|
|
927
943
|
const name = normalizeName(permission);
|
|
928
|
-
if (!this.rbac.wildcard && !await this.
|
|
944
|
+
if (!this.rbac.wildcard && !await this.#core.permissionExists(name, this.guardName)) {
|
|
929
945
|
throw new PermissionDoesNotExist(name, this.guardName);
|
|
930
946
|
}
|
|
931
947
|
return name;
|
|
932
948
|
}
|
|
933
949
|
state() {
|
|
934
950
|
const key2 = `rbac:${JSON.stringify([this.guardName, this.teamId, this.subject.type, String(this.subject.id)])}`;
|
|
935
|
-
return this.
|
|
951
|
+
return this.#core.remember(key2, async () => {
|
|
936
952
|
const { adapter } = this.rbac;
|
|
937
953
|
const roleIds = await adapter.getSubjectRoleIds(this.subject, this.teamId);
|
|
938
954
|
const roles = roleIds.length > 0 ? await adapter.findRoles({ ids: roleIds, guardName: this.guardName }) : [];
|
|
@@ -945,7 +961,7 @@ var SubjectRbac = class _SubjectRbac {
|
|
|
945
961
|
}
|
|
946
962
|
async emitRoles(event, roles) {
|
|
947
963
|
if (roles.length === 0) return;
|
|
948
|
-
await this.
|
|
964
|
+
await this.#core.events.emit({
|
|
949
965
|
event,
|
|
950
966
|
subject: this.subject,
|
|
951
967
|
roles,
|
|
@@ -955,7 +971,7 @@ var SubjectRbac = class _SubjectRbac {
|
|
|
955
971
|
}
|
|
956
972
|
async emitPermissions(event, permissions) {
|
|
957
973
|
if (permissions.length === 0) return;
|
|
958
|
-
await this.
|
|
974
|
+
await this.#core.events.emit({
|
|
959
975
|
event,
|
|
960
976
|
target: "subject",
|
|
961
977
|
subject: this.subject,
|