@anievo-id/rbac-evolution 0.6.6 → 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/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @anievo-id/rbac-evolution
|
|
2
2
|
|
|
3
|
-
📖 **[Documentation](https://
|
|
3
|
+
📖 **[Documentation](https://creation.anievo.id/rbac-evolution/guide/introduction)**
|
|
4
4
|
|
|
5
5
|
Framework-agnostic RBAC engine for [rbac-evolution](https://github.com/AniEvo-ID/rbac-evolution) —
|
|
6
6
|
roles, permissions, guards, wildcards and teams. Inspired by
|
|
@@ -139,7 +139,7 @@ function cleanName(value, where) {
|
|
|
139
139
|
function pushUnique(list, name) {
|
|
140
140
|
if (!list.includes(name)) list.push(name);
|
|
141
141
|
}
|
|
142
|
-
function normalize(
|
|
142
|
+
function normalize(core, definition, options) {
|
|
143
143
|
const list = Array.isArray(definition) ? definition : [definition];
|
|
144
144
|
const blocks = [];
|
|
145
145
|
list.forEach((raw, index) => {
|
|
@@ -148,12 +148,12 @@ function normalize(rbac, definition, options) {
|
|
|
148
148
|
const def = raw;
|
|
149
149
|
const guardInput = def["guard"] ?? options.guard;
|
|
150
150
|
if (guardInput !== void 0 && typeof guardInput !== "string") throw invalid(`${at}.guard must be a string.`);
|
|
151
|
-
const guard =
|
|
151
|
+
const guard = core.resolveGuard(guardInput === void 0 ? void 0 : cleanName(guardInput, `${at}.guard`));
|
|
152
152
|
const teamInput = def["team"] !== void 0 ? def["team"] : options.team;
|
|
153
153
|
if (teamInput !== void 0 && teamInput !== null && typeof teamInput !== "string" && typeof teamInput !== "number") {
|
|
154
154
|
throw invalid(`${at}.team must be a string, a number or null.`);
|
|
155
155
|
}
|
|
156
|
-
const team =
|
|
156
|
+
const team = core.resolveTeam(teamInput);
|
|
157
157
|
let block = blocks.find((b) => b.guard === guard && idKey(b.team) === idKey(team));
|
|
158
158
|
if (!block) {
|
|
159
159
|
block = { guard, team, permissions: [], roles: /* @__PURE__ */ new Map() };
|
|
@@ -189,8 +189,8 @@ function normalize(rbac, definition, options) {
|
|
|
189
189
|
});
|
|
190
190
|
return blocks;
|
|
191
191
|
}
|
|
192
|
-
async function syncDefinition(rbac, definition, options = {}) {
|
|
193
|
-
const blocks = normalize(
|
|
192
|
+
async function syncDefinition(rbac, core, definition, options = {}) {
|
|
193
|
+
const blocks = normalize(core, definition, options);
|
|
194
194
|
const prune = options.prune ?? false;
|
|
195
195
|
const dryRun = options.dryRun ?? false;
|
|
196
196
|
const result = {
|
|
@@ -322,6 +322,7 @@ function normalizeName(name) {
|
|
|
322
322
|
if (trimmed === "") throw new RbacError("INVALID_NAME", "Role and permission names must not be empty.");
|
|
323
323
|
return trimmed;
|
|
324
324
|
}
|
|
325
|
+
var internalsOf;
|
|
325
326
|
var Rbac = class _Rbac {
|
|
326
327
|
adapter;
|
|
327
328
|
defaultGuard;
|
|
@@ -334,6 +335,10 @@ var Rbac = class _Rbac {
|
|
|
334
335
|
#events = new RbacEmitter();
|
|
335
336
|
/** Bumped on every change, so a lookup that started before the change never caches its stale result. */
|
|
336
337
|
#generation = 0;
|
|
338
|
+
#internals;
|
|
339
|
+
static {
|
|
340
|
+
internalsOf = (rbac) => rbac.#internals;
|
|
341
|
+
}
|
|
337
342
|
constructor(options) {
|
|
338
343
|
this.adapter = options.adapter;
|
|
339
344
|
this.defaultGuard = options.defaultGuard ?? "web";
|
|
@@ -343,15 +348,27 @@ var Rbac = class _Rbac {
|
|
|
343
348
|
this.#cacheTtl = options.cacheTtl ?? DAY_MS;
|
|
344
349
|
this.#before = options.before;
|
|
345
350
|
this.#superAdminRoles = options.superAdminRoles ?? [];
|
|
351
|
+
this.#internals = {
|
|
352
|
+
events: this.#events,
|
|
353
|
+
remember: (key, compute) => this.#remember(key, compute),
|
|
354
|
+
permissionExists: (name, guardName) => this.#permissionExists(name, guardName),
|
|
355
|
+
gate: (subject, permission) => this.#gate(subject, permission),
|
|
356
|
+
resolveGuard: (guard) => this.#resolveGuard(guard),
|
|
357
|
+
resolveTeam: (team) => this.#resolveTeam(team),
|
|
358
|
+
visibleTeams: (teamId) => this.#visibleTeams(teamId),
|
|
359
|
+
resolvePermissions: (inputs, guardName) => this.#resolvePermissions(inputs, guardName),
|
|
360
|
+
resolveRoles: (inputs, guardName, teamId) => this.#resolveRoles(inputs, guardName, teamId),
|
|
361
|
+
permissionMatches: (granted, required) => this.#permissionMatches(granted, required)
|
|
362
|
+
};
|
|
346
363
|
}
|
|
347
364
|
/** Work with the roles & permissions of a subject (usually a user). */
|
|
348
365
|
for(subject, options = {}) {
|
|
349
|
-
return new SubjectRbac(this, subject, this
|
|
366
|
+
return new SubjectRbac(this, subject, this.#resolveGuard(options.guard), this.#resolveTeam(options.team));
|
|
350
367
|
}
|
|
351
368
|
/** Work with the permissions of a role. */
|
|
352
369
|
role(role, options = {}) {
|
|
353
370
|
if (typeof role !== "string") return new RoleRbac(this, role, role.guardName, role.teamId);
|
|
354
|
-
return new RoleRbac(this, role, this
|
|
371
|
+
return new RoleRbac(this, role, this.#resolveGuard(options.guard), this.#resolveTeam(options.team));
|
|
355
372
|
}
|
|
356
373
|
// ----------------------------------------------------------------------- events
|
|
357
374
|
/**
|
|
@@ -365,13 +382,9 @@ var Rbac = class _Rbac {
|
|
|
365
382
|
off(event, listener) {
|
|
366
383
|
this.#events.off(event, listener);
|
|
367
384
|
}
|
|
368
|
-
/** @internal */
|
|
369
|
-
get events() {
|
|
370
|
-
return this.#events;
|
|
371
|
-
}
|
|
372
385
|
// ------------------------------------------------------------------ permissions
|
|
373
386
|
async createPermission(name, options = {}) {
|
|
374
|
-
const guardName = this
|
|
387
|
+
const guardName = this.#resolveGuard(options.guard);
|
|
375
388
|
const permissionName = normalizeName(name);
|
|
376
389
|
if (this.wildcard) parseWildcardPermission(permissionName);
|
|
377
390
|
if (await this.findPermission(permissionName, { guard: guardName })) {
|
|
@@ -385,32 +398,32 @@ var Rbac = class _Rbac {
|
|
|
385
398
|
async findPermission(name, options = {}) {
|
|
386
399
|
const [permission] = await this.adapter.findPermissions({
|
|
387
400
|
names: [normalizeName(name)],
|
|
388
|
-
guardName: this
|
|
401
|
+
guardName: this.#resolveGuard(options.guard)
|
|
389
402
|
});
|
|
390
403
|
return permission ?? null;
|
|
391
404
|
}
|
|
392
405
|
async findPermissionOrFail(name, options = {}) {
|
|
393
406
|
const permission = await this.findPermission(name, options);
|
|
394
|
-
if (!permission) throw new PermissionDoesNotExist(normalizeName(name), this
|
|
407
|
+
if (!permission) throw new PermissionDoesNotExist(normalizeName(name), this.#resolveGuard(options.guard));
|
|
395
408
|
return permission;
|
|
396
409
|
}
|
|
397
410
|
async findOrCreatePermission(name, options = {}) {
|
|
398
411
|
return await this.findPermission(name, options) ?? this.createPermission(name, options);
|
|
399
412
|
}
|
|
400
413
|
async getPermissions(options = {}) {
|
|
401
|
-
return this.adapter.findPermissions({ guardName: this
|
|
414
|
+
return this.adapter.findPermissions({ guardName: this.#resolveGuard(options.guard) });
|
|
402
415
|
}
|
|
403
416
|
async deletePermission(permission, options = {}) {
|
|
404
|
-
const guardName = typeof permission === "string" ? this
|
|
405
|
-
const [resolved] = await this
|
|
417
|
+
const guardName = typeof permission === "string" ? this.#resolveGuard(options.guard) : permission.guardName;
|
|
418
|
+
const [resolved] = await this.#resolvePermissions([permission], guardName);
|
|
406
419
|
await this.adapter.deletePermission(resolved.id);
|
|
407
420
|
await this.forgetCachedPermissions();
|
|
408
421
|
await this.#events.emit({ event: "permission.deleted", permission: resolved });
|
|
409
422
|
}
|
|
410
423
|
// ------------------------------------------------------------------------ roles
|
|
411
424
|
async createRole(name, options = {}) {
|
|
412
|
-
const guardName = this
|
|
413
|
-
const teamId = this
|
|
425
|
+
const guardName = this.#resolveGuard(options.guard);
|
|
426
|
+
const teamId = this.#resolveTeam(options.team);
|
|
414
427
|
const roleName = normalizeName(name);
|
|
415
428
|
const existing = await this.adapter.findRoles({ names: [roleName], guardName, teamIds: [teamId] });
|
|
416
429
|
if (existing.length > 0) throw new RoleAlreadyExists(roleName, guardName);
|
|
@@ -421,15 +434,15 @@ var Rbac = class _Rbac {
|
|
|
421
434
|
}
|
|
422
435
|
/** Finds a role. With a team, a team-specific role wins over a global role of the same name. */
|
|
423
436
|
async findRole(name, options = {}) {
|
|
424
|
-
const guardName = this
|
|
425
|
-
const teamId = this
|
|
437
|
+
const guardName = this.#resolveGuard(options.guard);
|
|
438
|
+
const teamId = this.#resolveTeam(options.team);
|
|
426
439
|
const roleName = normalizeName(name);
|
|
427
|
-
const roles = await this.adapter.findRoles({ names: [roleName], guardName, teamIds: this
|
|
440
|
+
const roles = await this.adapter.findRoles({ names: [roleName], guardName, teamIds: this.#visibleTeams(teamId) });
|
|
428
441
|
return _Rbac.pickRole(roles, roleName, teamId) ?? null;
|
|
429
442
|
}
|
|
430
443
|
async findRoleOrFail(name, options = {}) {
|
|
431
444
|
const role = await this.findRole(name, options);
|
|
432
|
-
if (!role) throw new RoleDoesNotExist(normalizeName(name), this
|
|
445
|
+
if (!role) throw new RoleDoesNotExist(normalizeName(name), this.#resolveGuard(options.guard));
|
|
433
446
|
return role;
|
|
434
447
|
}
|
|
435
448
|
async findOrCreateRole(name, options = {}) {
|
|
@@ -437,8 +450,8 @@ var Rbac = class _Rbac {
|
|
|
437
450
|
}
|
|
438
451
|
/** Lists roles visible in the given scope (global roles + roles of the given team). */
|
|
439
452
|
async getRoles(options = {}) {
|
|
440
|
-
const teamId = this
|
|
441
|
-
return this.adapter.findRoles({ guardName: this
|
|
453
|
+
const teamId = this.#resolveTeam(options.team);
|
|
454
|
+
return this.adapter.findRoles({ guardName: this.#resolveGuard(options.guard), teamIds: this.#visibleTeams(teamId) });
|
|
442
455
|
}
|
|
443
456
|
async deleteRole(role, options = {}) {
|
|
444
457
|
const resolved = await this.role(role, options).get();
|
|
@@ -468,7 +481,7 @@ var Rbac = class _Rbac {
|
|
|
468
481
|
* Not atomic: adapters have no transactions, so a failure midway leaves the earlier changes.
|
|
469
482
|
*/
|
|
470
483
|
async sync(definition, options = {}) {
|
|
471
|
-
return syncDefinition(this, definition, options);
|
|
484
|
+
return syncDefinition(this, this.#internals, definition, options);
|
|
472
485
|
}
|
|
473
486
|
// ---------------------------------------------------------------------- queries
|
|
474
487
|
/**
|
|
@@ -476,9 +489,9 @@ var Rbac = class _Rbac {
|
|
|
476
489
|
* (spatie: `User::role('writer')->get()`). Throws `RoleDoesNotExist` for unknown roles.
|
|
477
490
|
*/
|
|
478
491
|
async subjectsWithRole(subjectType, roles, options = {}) {
|
|
479
|
-
const guardName = this
|
|
480
|
-
const teamId = this
|
|
481
|
-
const resolved = await this
|
|
492
|
+
const guardName = this.#resolveGuard(options.guard);
|
|
493
|
+
const teamId = this.#resolveTeam(options.team);
|
|
494
|
+
const resolved = await this.#resolveRoles(flatten([roles]), guardName, teamId);
|
|
482
495
|
if (resolved.length === 0) return [];
|
|
483
496
|
return this.adapter.getSubjectIdsWithRoles(
|
|
484
497
|
subjectType,
|
|
@@ -492,13 +505,13 @@ var Rbac = class _Rbac {
|
|
|
492
505
|
* Throws `PermissionDoesNotExist` for unknown permissions. Wildcards are not expanded.
|
|
493
506
|
*/
|
|
494
507
|
async subjectsWithPermission(subjectType, permissions, options = {}) {
|
|
495
|
-
const guardName = this
|
|
496
|
-
const teamId = this
|
|
497
|
-
const resolved = await this
|
|
508
|
+
const guardName = this.#resolveGuard(options.guard);
|
|
509
|
+
const teamId = this.#resolveTeam(options.team);
|
|
510
|
+
const resolved = await this.#resolvePermissions(flatten([permissions]), guardName);
|
|
498
511
|
if (resolved.length === 0) return [];
|
|
499
512
|
const permissionIds = resolved.map((p) => p.id);
|
|
500
513
|
const wanted = new Set(permissionIds.map(idKey2));
|
|
501
|
-
const roles = await this.adapter.findRoles({ guardName, teamIds: this
|
|
514
|
+
const roles = await this.adapter.findRoles({ guardName, teamIds: this.#visibleTeams(teamId) });
|
|
502
515
|
const links = roles.length > 0 ? await this.adapter.getRolePermissionLinks(roles.map((r) => r.id)) : [];
|
|
503
516
|
const roleIds = uniqueIds(links.filter((l) => wanted.has(idKey2(l.permissionId))).map((l) => l.roleId));
|
|
504
517
|
const viaRoles = roleIds.length > 0 ? await this.adapter.getSubjectIdsWithRoles(subjectType, roleIds, teamId) : [];
|
|
@@ -511,8 +524,7 @@ var Rbac = class _Rbac {
|
|
|
511
524
|
this.#generation++;
|
|
512
525
|
await this.#cache?.clear();
|
|
513
526
|
}
|
|
514
|
-
|
|
515
|
-
async remember(key, compute) {
|
|
527
|
+
async #remember(key, compute) {
|
|
516
528
|
if (!this.#cache) return compute();
|
|
517
529
|
const hit = await this.#cache.get(key);
|
|
518
530
|
if (hit !== void 0) return hit;
|
|
@@ -522,24 +534,24 @@ var Rbac = class _Rbac {
|
|
|
522
534
|
return value;
|
|
523
535
|
}
|
|
524
536
|
/**
|
|
525
|
-
*
|
|
537
|
+
* Whether a permission with this name exists in the guard.
|
|
526
538
|
* With caching on, the guard's permission names are cached (cleared on every change),
|
|
527
539
|
* so repeated `can()` checks don't hit the database.
|
|
528
540
|
*/
|
|
529
|
-
async permissionExists(name, guardName) {
|
|
541
|
+
async #permissionExists(name, guardName) {
|
|
530
542
|
if (!this.#cache) {
|
|
531
543
|
const [permission] = await this.adapter.findPermissions({ names: [name], guardName });
|
|
532
544
|
return permission !== void 0;
|
|
533
545
|
}
|
|
534
|
-
const names = await this
|
|
546
|
+
const names = await this.#remember(
|
|
535
547
|
`rbac:permissions:${JSON.stringify([guardName])}`,
|
|
536
548
|
async () => (await this.adapter.findPermissions({ guardName })).map((p) => p.name)
|
|
537
549
|
);
|
|
538
550
|
return names.includes(name);
|
|
539
551
|
}
|
|
540
552
|
// ---------------------------------------------------------------------- internal
|
|
541
|
-
/**
|
|
542
|
-
async gate(subject, permission) {
|
|
553
|
+
/** Runs the `before` hook and the super-admin check. `undefined` = no decision. */
|
|
554
|
+
async #gate(subject, permission) {
|
|
543
555
|
if (this.#before) {
|
|
544
556
|
const decision = await this.#before({ subject, permission });
|
|
545
557
|
if (typeof decision === "boolean") return decision;
|
|
@@ -549,24 +561,21 @@ var Rbac = class _Rbac {
|
|
|
549
561
|
}
|
|
550
562
|
return void 0;
|
|
551
563
|
}
|
|
552
|
-
|
|
553
|
-
resolveGuard(guard) {
|
|
564
|
+
#resolveGuard(guard) {
|
|
554
565
|
return guard ?? this.defaultGuard;
|
|
555
566
|
}
|
|
556
|
-
|
|
557
|
-
resolveTeam(team) {
|
|
567
|
+
#resolveTeam(team) {
|
|
558
568
|
if (team === void 0 || team === null) return null;
|
|
559
569
|
if (!this.teams) {
|
|
560
570
|
throw new RbacError("TEAMS_NOT_ENABLED", "A team was given but teams are not enabled. Use `createRbac({ teams: true })`.");
|
|
561
571
|
}
|
|
562
572
|
return team;
|
|
563
573
|
}
|
|
564
|
-
|
|
565
|
-
visibleTeams(teamId) {
|
|
574
|
+
#visibleTeams(teamId) {
|
|
566
575
|
return teamId === null ? [null] : [teamId, null];
|
|
567
576
|
}
|
|
568
|
-
/**
|
|
569
|
-
async resolvePermissions(inputs, guardName) {
|
|
577
|
+
/** Resolves names/objects to permissions, validating existence and guard. */
|
|
578
|
+
async #resolvePermissions(inputs, guardName) {
|
|
570
579
|
const names = [];
|
|
571
580
|
const objects = [];
|
|
572
581
|
for (const input of inputs) {
|
|
@@ -583,8 +592,8 @@ var Rbac = class _Rbac {
|
|
|
583
592
|
}
|
|
584
593
|
return uniqueById([...found, ...objects]);
|
|
585
594
|
}
|
|
586
|
-
/**
|
|
587
|
-
async resolveRoles(inputs, guardName, teamId) {
|
|
595
|
+
/** Resolves names/objects to roles, validating existence, guard and team. */
|
|
596
|
+
async #resolveRoles(inputs, guardName, teamId) {
|
|
588
597
|
const names = [];
|
|
589
598
|
const objects = [];
|
|
590
599
|
for (const input of inputs) {
|
|
@@ -598,7 +607,7 @@ var Rbac = class _Rbac {
|
|
|
598
607
|
objects.push(input);
|
|
599
608
|
}
|
|
600
609
|
}
|
|
601
|
-
const found = names.length > 0 ? await this.adapter.findRoles({ names, guardName, teamIds: this
|
|
610
|
+
const found = names.length > 0 ? await this.adapter.findRoles({ names, guardName, teamIds: this.#visibleTeams(teamId) }) : [];
|
|
602
611
|
const picked = names.map((name) => {
|
|
603
612
|
const role = _Rbac.pickRole(found, name, teamId);
|
|
604
613
|
if (!role) throw new RoleDoesNotExist(name, guardName);
|
|
@@ -606,8 +615,7 @@ var Rbac = class _Rbac {
|
|
|
606
615
|
});
|
|
607
616
|
return uniqueById([...picked, ...objects]);
|
|
608
617
|
}
|
|
609
|
-
|
|
610
|
-
permissionMatches(granted, required) {
|
|
618
|
+
#permissionMatches(granted, required) {
|
|
611
619
|
if (!this.wildcard) return granted.includes(required);
|
|
612
620
|
return granted.some((pattern) => {
|
|
613
621
|
try {
|
|
@@ -628,20 +636,22 @@ var RoleRbac = class {
|
|
|
628
636
|
this.input = input;
|
|
629
637
|
this.guardName = guardName;
|
|
630
638
|
this.teamId = teamId;
|
|
639
|
+
this.#core = internalsOf(rbac);
|
|
631
640
|
}
|
|
632
641
|
rbac;
|
|
633
642
|
input;
|
|
634
643
|
guardName;
|
|
635
644
|
teamId;
|
|
645
|
+
#core;
|
|
636
646
|
/** Returns the role, throwing `RoleDoesNotExist` if it can't be found. */
|
|
637
647
|
async get() {
|
|
638
|
-
const [role] = await this.
|
|
648
|
+
const [role] = await this.#core.resolveRoles([this.input], this.guardName, this.teamId);
|
|
639
649
|
return role;
|
|
640
650
|
}
|
|
641
651
|
async givePermissionTo(...permissions) {
|
|
642
652
|
const role = await this.get();
|
|
643
|
-
const resolved = await this.
|
|
644
|
-
const track = this.
|
|
653
|
+
const resolved = await this.#core.resolvePermissions(flatten(permissions), role.guardName);
|
|
654
|
+
const track = this.#core.events.has("permission.attached");
|
|
645
655
|
const before = track ? await this.currentPermissionIds(role) : [];
|
|
646
656
|
await this.rbac.adapter.attachRolePermissions(role.id, resolved.map((p) => p.id));
|
|
647
657
|
await this.rbac.forgetCachedPermissions();
|
|
@@ -650,8 +660,8 @@ var RoleRbac = class {
|
|
|
650
660
|
}
|
|
651
661
|
async revokePermissionTo(...permissions) {
|
|
652
662
|
const role = await this.get();
|
|
653
|
-
const resolved = await this.
|
|
654
|
-
const track = this.
|
|
663
|
+
const resolved = await this.#core.resolvePermissions(flatten(permissions), role.guardName);
|
|
664
|
+
const track = this.#core.events.has("permission.detached");
|
|
655
665
|
const before = track ? await this.currentPermissionIds(role) : [];
|
|
656
666
|
await this.rbac.adapter.detachRolePermissions(role.id, resolved.map((p) => p.id));
|
|
657
667
|
await this.rbac.forgetCachedPermissions();
|
|
@@ -661,8 +671,8 @@ var RoleRbac = class {
|
|
|
661
671
|
/** Replaces all permissions of the role with the given ones. */
|
|
662
672
|
async syncPermissions(...permissions) {
|
|
663
673
|
const role = await this.get();
|
|
664
|
-
const resolved = await this.
|
|
665
|
-
const track = this.
|
|
674
|
+
const resolved = await this.#core.resolvePermissions(flatten(permissions), role.guardName);
|
|
675
|
+
const track = this.#core.events.has("permission.attached") || this.#core.events.has("permission.detached");
|
|
666
676
|
const current = track ? await this.getPermissions() : [];
|
|
667
677
|
const newIds = resolved.map((p) => p.id);
|
|
668
678
|
await this.rbac.adapter.attachRolePermissions(role.id, newIds);
|
|
@@ -704,14 +714,14 @@ var RoleRbac = class {
|
|
|
704
714
|
}
|
|
705
715
|
async hasPermissionTo(permission) {
|
|
706
716
|
const name = typeof permission === "string" ? normalizeName(permission) : permission.name;
|
|
707
|
-
return this.
|
|
717
|
+
return this.#core.permissionMatches(await this.getPermissionNames(), name);
|
|
708
718
|
}
|
|
709
719
|
async currentPermissionIds(role) {
|
|
710
720
|
return (await this.rbac.adapter.getRolePermissionLinks([role.id])).map((l) => l.permissionId);
|
|
711
721
|
}
|
|
712
722
|
async emit(event, role, permissions) {
|
|
713
723
|
if (permissions.length === 0) return;
|
|
714
|
-
await this.
|
|
724
|
+
await this.#core.events.emit({
|
|
715
725
|
event,
|
|
716
726
|
target: "role",
|
|
717
727
|
role,
|
|
@@ -727,15 +737,18 @@ var SubjectRbac = class _SubjectRbac {
|
|
|
727
737
|
this.subject = subject;
|
|
728
738
|
this.guardName = guardName;
|
|
729
739
|
this.teamId = teamId;
|
|
740
|
+
this.#core = internalsOf(rbac);
|
|
730
741
|
}
|
|
731
742
|
rbac;
|
|
732
743
|
subject;
|
|
733
744
|
guardName;
|
|
734
745
|
teamId;
|
|
746
|
+
#core;
|
|
735
747
|
// ------------------------------------------------------------------------ roles
|
|
736
748
|
async assignRole(...roles) {
|
|
737
|
-
const { adapter
|
|
738
|
-
const
|
|
749
|
+
const { adapter } = this.rbac;
|
|
750
|
+
const { events } = this.#core;
|
|
751
|
+
const resolved = await this.#core.resolveRoles(flatten(roles), this.guardName, this.teamId);
|
|
739
752
|
const track = events.has("role.attached");
|
|
740
753
|
const before = track ? await adapter.getSubjectRoleIds(this.subject, this.teamId) : [];
|
|
741
754
|
await adapter.attachSubjectRoles(this.subject, resolved.map((r) => r.id), this.teamId);
|
|
@@ -744,8 +757,9 @@ var SubjectRbac = class _SubjectRbac {
|
|
|
744
757
|
return this;
|
|
745
758
|
}
|
|
746
759
|
async removeRole(...roles) {
|
|
747
|
-
const { adapter
|
|
748
|
-
const
|
|
760
|
+
const { adapter } = this.rbac;
|
|
761
|
+
const { events } = this.#core;
|
|
762
|
+
const resolved = await this.#core.resolveRoles(flatten(roles), this.guardName, this.teamId);
|
|
749
763
|
const track = events.has("role.detached");
|
|
750
764
|
const before = track ? await adapter.getSubjectRoleIds(this.subject, this.teamId) : [];
|
|
751
765
|
await adapter.detachSubjectRoles(this.subject, resolved.map((r) => r.id), this.teamId);
|
|
@@ -755,7 +769,7 @@ var SubjectRbac = class _SubjectRbac {
|
|
|
755
769
|
}
|
|
756
770
|
/** Replaces all roles (in the current guard & team) with the given ones. */
|
|
757
771
|
async syncRoles(...roles) {
|
|
758
|
-
const resolved = await this.
|
|
772
|
+
const resolved = await this.#core.resolveRoles(flatten(roles), this.guardName, this.teamId);
|
|
759
773
|
const current = (await this.state()).roles;
|
|
760
774
|
const newIds = resolved.map((r) => r.id);
|
|
761
775
|
await this.rbac.adapter.attachSubjectRoles(this.subject, newIds, this.teamId);
|
|
@@ -797,8 +811,9 @@ var SubjectRbac = class _SubjectRbac {
|
|
|
797
811
|
}
|
|
798
812
|
// ------------------------------------------------------------ direct permissions
|
|
799
813
|
async givePermissionTo(...permissions) {
|
|
800
|
-
const { adapter
|
|
801
|
-
const
|
|
814
|
+
const { adapter } = this.rbac;
|
|
815
|
+
const { events } = this.#core;
|
|
816
|
+
const resolved = await this.#core.resolvePermissions(flatten(permissions), this.guardName);
|
|
802
817
|
const track = events.has("permission.attached");
|
|
803
818
|
const before = track ? await adapter.getSubjectPermissionIds(this.subject, this.teamId) : [];
|
|
804
819
|
await adapter.attachSubjectPermissions(this.subject, resolved.map((p) => p.id), this.teamId);
|
|
@@ -807,8 +822,9 @@ var SubjectRbac = class _SubjectRbac {
|
|
|
807
822
|
return this;
|
|
808
823
|
}
|
|
809
824
|
async revokePermissionTo(...permissions) {
|
|
810
|
-
const { adapter
|
|
811
|
-
const
|
|
825
|
+
const { adapter } = this.rbac;
|
|
826
|
+
const { events } = this.#core;
|
|
827
|
+
const resolved = await this.#core.resolvePermissions(flatten(permissions), this.guardName);
|
|
812
828
|
const track = events.has("permission.detached");
|
|
813
829
|
const before = track ? await adapter.getSubjectPermissionIds(this.subject, this.teamId) : [];
|
|
814
830
|
await adapter.detachSubjectPermissions(this.subject, resolved.map((p) => p.id), this.teamId);
|
|
@@ -818,7 +834,7 @@ var SubjectRbac = class _SubjectRbac {
|
|
|
818
834
|
}
|
|
819
835
|
/** Replaces all direct permissions (in the current guard & team) with the given ones. */
|
|
820
836
|
async syncPermissions(...permissions) {
|
|
821
|
-
const resolved = await this.
|
|
837
|
+
const resolved = await this.#core.resolvePermissions(flatten(permissions), this.guardName);
|
|
822
838
|
const current = (await this.state()).direct;
|
|
823
839
|
const newIds = resolved.map((p) => p.id);
|
|
824
840
|
await this.rbac.adapter.attachSubjectPermissions(this.subject, newIds, this.teamId);
|
|
@@ -853,13 +869,13 @@ var SubjectRbac = class _SubjectRbac {
|
|
|
853
869
|
*/
|
|
854
870
|
async hasPermissionTo(permission) {
|
|
855
871
|
const name = await this.requirePermissionName(permission);
|
|
856
|
-
return this.
|
|
872
|
+
return this.#core.permissionMatches(await this.getPermissionNames(), name);
|
|
857
873
|
}
|
|
858
874
|
/** Checks a direct permission only (ignores roles). */
|
|
859
875
|
async hasDirectPermission(permission) {
|
|
860
876
|
const name = await this.requirePermissionName(permission);
|
|
861
877
|
const direct = (await this.state()).direct.map((p) => p.name);
|
|
862
|
-
return this.
|
|
878
|
+
return this.#core.permissionMatches(direct, name);
|
|
863
879
|
}
|
|
864
880
|
/**
|
|
865
881
|
* Authorization check: runs the `before` hook and `superAdminRoles` first, then falls back
|
|
@@ -867,7 +883,7 @@ var SubjectRbac = class _SubjectRbac {
|
|
|
867
883
|
*/
|
|
868
884
|
async can(permission) {
|
|
869
885
|
const name = typeof permission === "string" ? permission.trim() : permission.name;
|
|
870
|
-
const decision = await this.
|
|
886
|
+
const decision = await this.#core.gate(this, name);
|
|
871
887
|
if (decision !== void 0) return decision;
|
|
872
888
|
return this.check(permission);
|
|
873
889
|
}
|
|
@@ -920,14 +936,14 @@ var SubjectRbac = class _SubjectRbac {
|
|
|
920
936
|
return permission.name;
|
|
921
937
|
}
|
|
922
938
|
const name = normalizeName(permission);
|
|
923
|
-
if (!this.rbac.wildcard && !await this.
|
|
939
|
+
if (!this.rbac.wildcard && !await this.#core.permissionExists(name, this.guardName)) {
|
|
924
940
|
throw new PermissionDoesNotExist(name, this.guardName);
|
|
925
941
|
}
|
|
926
942
|
return name;
|
|
927
943
|
}
|
|
928
944
|
state() {
|
|
929
945
|
const key = `rbac:${JSON.stringify([this.guardName, this.teamId, this.subject.type, String(this.subject.id)])}`;
|
|
930
|
-
return this.
|
|
946
|
+
return this.#core.remember(key, async () => {
|
|
931
947
|
const { adapter } = this.rbac;
|
|
932
948
|
const roleIds = await adapter.getSubjectRoleIds(this.subject, this.teamId);
|
|
933
949
|
const roles = roleIds.length > 0 ? await adapter.findRoles({ ids: roleIds, guardName: this.guardName }) : [];
|
|
@@ -940,7 +956,7 @@ var SubjectRbac = class _SubjectRbac {
|
|
|
940
956
|
}
|
|
941
957
|
async emitRoles(event, roles) {
|
|
942
958
|
if (roles.length === 0) return;
|
|
943
|
-
await this.
|
|
959
|
+
await this.#core.events.emit({
|
|
944
960
|
event,
|
|
945
961
|
subject: this.subject,
|
|
946
962
|
roles,
|
|
@@ -950,7 +966,7 @@ var SubjectRbac = class _SubjectRbac {
|
|
|
950
966
|
}
|
|
951
967
|
async emitPermissions(event, permissions) {
|
|
952
968
|
if (permissions.length === 0) return;
|
|
953
|
-
await this.
|
|
969
|
+
await this.#core.events.emit({
|
|
954
970
|
event,
|
|
955
971
|
target: "subject",
|
|
956
972
|
subject: this.subject,
|
|
@@ -968,5 +984,5 @@ function createRbac(options) {
|
|
|
968
984
|
}
|
|
969
985
|
|
|
970
986
|
export { GuardDoesNotMatch, InvalidWildcardPermission, MemoryCache, PermissionAlreadyExists, PermissionDoesNotExist, Rbac, RbacError, RoleAlreadyExists, RoleDoesNotExist, RoleRbac, SubjectRbac, createRbac, parseWildcardPermission, wildcardImplies };
|
|
971
|
-
//# sourceMappingURL=chunk-
|
|
972
|
-
//# sourceMappingURL=chunk-
|
|
987
|
+
//# sourceMappingURL=chunk-N2QVWHD7.js.map
|
|
988
|
+
//# sourceMappingURL=chunk-N2QVWHD7.js.map
|