@voltro/plugin-rbac 0.85.0 → 0.87.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/dist/index.js CHANGED
@@ -1,109 +1,73 @@
1
- import { a as e, c as t, d as n, f as r, i, l as a, n as o, o as s, p as c, r as l, s as u, t as d, u as f } from "./permission-DhL6E-Nc.js";
2
- import { Effect as p } from "effect";
3
- import { ADMIN_SCOPE as m, ScopeError as h, ScopeError as g, definePlugin as _, pluginInstanceName as v, setResourceScopeResolver as y } from "@voltro/protocol";
4
- import { createLogger as b } from "@voltro/logger";
5
- import { and as x, boolean as S, eq as C, id as w, json as T, or as E, reference as D, requireTenants as O, table as k, text as A } from "@voltro/database";
6
- //#region src/tables.ts
7
- var j = "_voltro_rbac_roles", M = "_voltro_rbac_user_roles", N = k(j, {
8
- id: w({ prefix: "role" }),
9
- slug: A().unique(),
10
- name: A(),
11
- permissions: T(),
12
- isDefault: S().default(!1)
13
- }).index("_voltro_rbac_roles_by_slug", ["slug"]), P = (e = {}) => k(M, {
14
- id: w({ prefix: "urole" }),
15
- userId: A(),
16
- tenantId: e.multitenant ? D(O(), { index: !1 }).nullable() : A().nullable(),
17
- roleSlug: A()
18
- }).index("_voltro_rbac_user_roles_by_user_tenant", ["userId", "tenantId"]).index("_voltro_rbac_user_roles_by_role_slug", ["roleSlug"]), F = P(), I = (e = {}) => [N, P(e)], L = (e, t) => e.query({
19
- table: M,
20
- predicate: t,
21
- order: [{
22
- column: "id",
23
- direction: "asc"
24
- }],
25
- take: void 0,
26
- skip: void 0,
27
- projection: void 0
28
- }), R = (e) => {
29
- let t = (t, n, r) => L(e, x(C("userId", t), C("roleSlug", n), C("tenantId", r)));
30
- return {
31
- assign: async (n, r, i = null) => {
32
- (await t(n, r, i)).length > 0 || await e.insert(M, {
33
- userId: n,
34
- roleSlug: r,
35
- tenantId: i
36
- });
37
- },
38
- revoke: async (n, r, i = null) => {
39
- let a = await t(n, r, i);
40
- for (let t of a) await e.delete(M, String(t.id));
41
- },
42
- rolesOf: async (t, n) => {
43
- let r = (n === void 0 ? await L(e, C("userId", t)) : await L(e, x(C("userId", t), z(n)))).map((e) => String(e.roleSlug));
44
- return [...new Set(r)];
45
- }
46
- };
47
- }, z = (e) => e === null ? C("tenantId", null) : E(C("tenantId", e), C("tenantId", null)), B = (e) => (t, n) => {
48
- let r = ("tenantId" in t ? t.tenantId : void 0) ?? null, i = ("id" in t ? t.id : void 0) ?? null;
49
- if (!i) return [];
50
- let a = e ?? (n?.store === void 0 ? void 0 : R(n.store));
51
- return a === void 0 ? [] : a.rolesOf(i, r);
52
- }, V = b({ scope: "@voltro/plugin-rbac" }), H = (e) => p.suspend(() => {
1
+ import { WILDCARD_PERMISSION as e, compileRoles as t, declaredScopesOf as n, rolesFromMap as r } from "./roles.js";
2
+ import { mergeResolvedScopes as i, metadataRoleResolver as a, resolvedRoles as o, resolvedScopes as s, setResolvedScopes as c } from "./resolve.js";
3
+ import { anyPermission as l, assertPermission as u, can as d, permission as f } from "./permission.js";
4
+ import { ROLES_TABLE as p, USER_ROLES_TABLE as m, buildUserRolesTable as h, rbacTables as g, rolesTable as _, userRolesTable as v } from "./tables.js";
5
+ import { dataStoreRoleResolver as y, userRoleStore as b } from "./userRoleStore.js";
6
+ import { Effect as x } from "effect";
7
+ import { ADMIN_SCOPE as S, ScopeError as C, ScopeError as w, definePlugin as T, pluginInstanceName as E, setEffectiveScopesRefresher as D, setResourceScopeResolver as O } from "@voltro/protocol";
8
+ import { createLogger as k } from "@voltro/logger";
9
+ //#region src/index.ts
10
+ var A = k({ scope: "@voltro/plugin-rbac" }), j = (e) => x.suspend(() => {
53
11
  let t = e();
54
- return t instanceof Promise ? p.promise(() => t) : p.succeed(t);
55
- }), U = "@voltro/plugin-rbac", W = (t) => {
56
- let i = t.resolveRoles ?? s, o = t.resolvePermissions, c = t.tables !== void 0 && t.tables !== !1, l = typeof t.tables == "object" ? t.tables : {};
57
- if (t.resolveResourceRoles) {
58
- let e = t.resolveResourceRoles;
59
- y(({ subject: r, scope: i, resource: a }) => H(() => e(r, a)).pipe(p.map((e) => {
60
- let r = n(e, t.roles);
61
- return r.includes(m) || r.includes(i);
62
- }), p.catchCause((e) => (V.warn("resolveResourceRoles failed; denying (fail-closed)", {
12
+ return t instanceof Promise ? x.promise(() => t) : x.succeed(t);
13
+ }), M = "@voltro/plugin-rbac", N = (e) => {
14
+ let r = e.resolveRoles ?? a, o = e.resolvePermissions, s = e.tables !== void 0 && e.tables !== !1, l = typeof e.tables == "object" ? e.tables : {};
15
+ if (e.resolveResourceRoles) {
16
+ let n = e.resolveResourceRoles;
17
+ O(({ subject: r, scope: i, resource: a }) => j(() => n(r, a)).pipe(x.map((n) => {
18
+ let r = t(n, e.roles);
19
+ return r.includes(S) || r.includes(i);
20
+ }), x.catchCause((e) => (A.warn("resolveResourceRoles failed; denying (fail-closed)", {
63
21
  scope: i,
64
22
  resource: a,
65
23
  cause: String(e)
66
- }), p.succeed(!1)))));
24
+ }), x.succeed(!1)))));
67
25
  }
68
- let u, d = (n, r) => p.gen(function* () {
69
- let s = yield* H(() => i(r.subject, { ...u === void 0 ? {} : { store: u } })).pipe(p.catchCause((e) => (V.warn("resolveRoles failed; falling back to raw subject scopes", {
70
- tag: r.tag,
71
- traceId: r.traceId,
26
+ let u, d = (t, n, a) => x.gen(function* () {
27
+ let s = yield* j(() => r(t, { ...u === void 0 ? {} : { store: u } })).pipe(x.catchCause((e) => (A.warn("resolveRoles failed; falling back to raw subject scopes", {
28
+ tag: n,
29
+ traceId: a,
72
30
  cause: String(e)
73
- }), p.succeed([])))), c = o ? yield* H(() => o(r.subject)).pipe(p.catchCause(() => p.succeed([]))) : [], l = e(r.subject, s, t.roles, c);
74
- return a(r.subject, l, s), yield* n;
75
- }), f = [
31
+ }), x.succeed([])))), l = o ? yield* j(() => o(t)).pipe(x.catchCause(() => x.succeed([]))) : [], d = i(t, s, e.roles, l);
32
+ c(t, d, s);
33
+ }), f = (e) => {
34
+ D(e.subject, (t) => x.runPromise(d(t, e.tag, e.traceId)));
35
+ }, p = (e, t) => x.gen(function* () {
36
+ return yield* d(t.subject, t.tag, t.traceId), f(t), yield* e;
37
+ }), m = [
38
+ "rpc:authority:replay",
76
39
  "rpc:intercept:mutation",
77
40
  "rpc:intercept:query",
78
41
  "rpc:intercept:action",
79
42
  "rpc:intercept:workflow",
80
- ...c ? ["store:write"] : []
81
- ], h = o === void 0 ? r(t.roles) : void 0;
82
- return _({
83
- name: v({
84
- base: U,
85
- alias: t.alias
43
+ ...s ? ["store:write"] : []
44
+ ], h = o === void 0 ? n(e.roles) : void 0;
45
+ return T({
46
+ name: E({
47
+ base: M,
48
+ alias: e.alias
86
49
  }),
87
- baseName: U,
50
+ baseName: M,
88
51
  ...h === void 0 ? {} : { declaredScopes: h },
89
52
  description: "Role-based access control — roles compile to scopes on top of the protocol scope system. Ships the permission() handler guard + a role→scope interceptor + a resource-aware guard resolver, failing with protocol's typed ScopeError (one denial tag framework-wide).",
90
- permissions: f,
53
+ permissions: m,
91
54
  bindDataStore: (e) => {
92
55
  u = e;
93
56
  },
94
- interceptMutation: d,
95
- interceptQuery: d,
96
- interceptAction: d,
97
- interceptWorkflow: d,
57
+ bindReplayAuthority: f,
58
+ interceptMutation: p,
59
+ interceptQuery: p,
60
+ interceptAction: p,
61
+ interceptWorkflow: p,
98
62
  errorSchemas: [{
99
- schema: g,
63
+ schema: w,
100
64
  import: {
101
65
  module: "@voltro/protocol",
102
66
  name: "ScopeError"
103
67
  }
104
68
  }],
105
- ...c ? { extendSchema: { tables: [...I(l)] } } : {}
69
+ ...s ? { extendSchema: { tables: [...g(l)] } } : {}
106
70
  });
107
71
  };
108
72
  //#endregion
109
- export { j as ROLES_TABLE, h as ScopeError, M as USER_ROLES_TABLE, f as WILDCARD_PERMISSION, d as anyPermission, o as assertPermission, P as buildUserRolesTable, l as can, n as compileRoles, B as dataStoreRoleResolver, e as mergeResolvedScopes, s as metadataRoleResolver, i as permission, W as rbacPlugin, I as rbacTables, u as resolvedRoles, t as resolvedScopes, c as rolesFromMap, N as rolesTable, a as setResolvedScopes, R as userRoleStore, F as userRolesTable };
73
+ export { p as ROLES_TABLE, C as ScopeError, m as USER_ROLES_TABLE, e as WILDCARD_PERMISSION, l as anyPermission, u as assertPermission, h as buildUserRolesTable, d as can, t as compileRoles, y as dataStoreRoleResolver, i as mergeResolvedScopes, a as metadataRoleResolver, f as permission, N as rbacPlugin, g as rbacTables, o as resolvedRoles, s as resolvedScopes, r as rolesFromMap, _ as rolesTable, c as setResolvedScopes, b as userRoleStore, v as userRolesTable };
@@ -1,2 +1,33 @@
1
- import { i as e, n as t, r as n, t as r } from "./permission-DhL6E-Nc.js";
2
- export { r as anyPermission, t as assertPermission, n as can, e as permission };
1
+ import { resolvedScopes as e } from "./resolve.js";
2
+ import { Effect as t } from "effect";
3
+ import { ADMIN_SCOPE as n, ScopeError as r } from "@voltro/protocol";
4
+ //#region src/permission.ts
5
+ var i = (t, r) => {
6
+ let i = e(t);
7
+ return i.includes(n) || i.includes(r);
8
+ }, a = (e) => typeof e == "string" ? [e] : e, o = (e, t) => {
9
+ for (let n of t) if (!i(e, n)) return n;
10
+ return null;
11
+ }, s = (e, n) => {
12
+ let i = a(n), s = o(e.request.subject, i);
13
+ return s === null ? t.void : t.fail(new r({
14
+ required: s,
15
+ message: `missing required permission '${s}'`
16
+ }));
17
+ }, c = (e, n) => {
18
+ let a = e.request.subject;
19
+ if (n.length === 0) return t.void;
20
+ for (let e of n) if (i(a, e)) return t.void;
21
+ return t.fail(new r({
22
+ required: n[0],
23
+ message: `missing all of required permissions [${n.join(", ")}]`
24
+ }));
25
+ }, l = (e, t) => {
26
+ let n = o(e.request.subject, a(t));
27
+ if (n !== null) throw new r({
28
+ required: n,
29
+ message: `missing required permission '${n}'`
30
+ });
31
+ }, u = (e, t) => o(e.request.subject, a(t)) === null;
32
+ //#endregion
33
+ export { c as anyPermission, l as assertPermission, u as can, s as permission };
@@ -0,0 +1,20 @@
1
+ import { compileRoles as e } from "./roles.js";
2
+ import { currentSubjectAuthority as t, effectiveScopes as n, setEffectiveScopes as r, subjectScopes as i } from "@voltro/protocol";
3
+ //#region src/resolve.ts
4
+ var a = /* @__PURE__ */ new WeakMap(), o = (e, t, n) => {
5
+ r(e, t), n !== void 0 && a.set(e, n);
6
+ }, s = (e) => n(e), c = (e) => a.get(t(e)) ?? [], l = (e) => {
7
+ if (!("metadata" in e) || e.metadata === void 0) return [];
8
+ let t = e.metadata.roles;
9
+ return Array.isArray(t) ? t.filter((e) => typeof e == "string") : [];
10
+ }, u = (t, n, r, a = []) => {
11
+ let o = e(n, r), s = [], c = /* @__PURE__ */ new Set();
12
+ for (let e of [
13
+ ...i(t),
14
+ ...o,
15
+ ...a
16
+ ]) c.has(e) || (c.add(e), s.push(e));
17
+ return s;
18
+ };
19
+ //#endregion
20
+ export { u as mergeResolvedScopes, l as metadataRoleResolver, c as resolvedRoles, s as resolvedScopes, o as setResolvedScopes };
package/dist/roles.js ADDED
@@ -0,0 +1,22 @@
1
+ import { ADMIN_SCOPE as e } from "@voltro/protocol";
2
+ //#region src/roles.ts
3
+ var t = "*", n = (e) => {
4
+ let t = /* @__PURE__ */ new Set();
5
+ for (let n of Object.values(e)) for (let e of n) e !== "*" && t.add(e);
6
+ return [...t].sort();
7
+ }, r = (t, n) => {
8
+ let r = [], i = /* @__PURE__ */ new Set();
9
+ for (let a of t) {
10
+ let t = n[a];
11
+ if (t !== void 0) for (let n of t) {
12
+ if (n === "*") return [e];
13
+ i.has(n) || (i.add(n), r.push(n));
14
+ }
15
+ }
16
+ return r;
17
+ }, i = (e) => Object.entries(e).map(([e, t]) => ({
18
+ slug: e,
19
+ permissions: t
20
+ }));
21
+ //#endregion
22
+ export { t as WILDCARD_PERMISSION, r as compileRoles, n as declaredScopesOf, i as rolesFromMap };
package/dist/tables.js ADDED
@@ -0,0 +1,16 @@
1
+ import { boolean as e, id as t, json as n, reference as r, requireTenants as i, table as a, text as o } from "@voltro/database";
2
+ //#region src/tables.ts
3
+ var s = "_voltro_rbac_roles", c = "_voltro_rbac_user_roles", l = a(s, {
4
+ id: t({ prefix: "role" }),
5
+ slug: o().unique(),
6
+ name: o(),
7
+ permissions: n(),
8
+ isDefault: e().default(!1)
9
+ }).index("_voltro_rbac_roles_by_slug", ["slug"]), u = (e = {}) => a(c, {
10
+ id: t({ prefix: "urole" }),
11
+ userId: o(),
12
+ tenantId: e.multitenant ? r(i(), { index: !1 }).nullable() : o().nullable(),
13
+ roleSlug: o()
14
+ }).index("_voltro_rbac_user_roles_by_user_tenant", ["userId", "tenantId"]).index("_voltro_rbac_user_roles_by_role_slug", ["roleSlug"]), d = u(), f = (e = {}) => [l, u(e)];
15
+ //#endregion
16
+ export { s as ROLES_TABLE, c as USER_ROLES_TABLE, u as buildUserRolesTable, f as rbacTables, l as rolesTable, d as userRolesTable };
@@ -0,0 +1,40 @@
1
+ import { USER_ROLES_TABLE as e } from "./tables.js";
2
+ import { and as t, eq as n, or as r } from "@voltro/database";
3
+ //#region src/userRoleStore.ts
4
+ var i = (t, n) => t.query({
5
+ table: e,
6
+ predicate: n,
7
+ order: [{
8
+ column: "id",
9
+ direction: "asc"
10
+ }],
11
+ take: void 0,
12
+ skip: void 0,
13
+ projection: void 0
14
+ }), a = (r) => {
15
+ let a = (e, a, o) => i(r, t(n("userId", e), n("roleSlug", a), n("tenantId", o)));
16
+ return {
17
+ assign: async (t, n, i = null) => {
18
+ (await a(t, n, i)).length > 0 || await r.insert(e, {
19
+ userId: t,
20
+ roleSlug: n,
21
+ tenantId: i
22
+ });
23
+ },
24
+ revoke: async (t, n, i = null) => {
25
+ let o = await a(t, n, i);
26
+ for (let t of o) await r.delete(e, String(t.id));
27
+ },
28
+ rolesOf: async (e, a) => {
29
+ let s = (a === void 0 ? await i(r, n("userId", e)) : await i(r, t(n("userId", e), o(a)))).map((e) => String(e.roleSlug));
30
+ return [...new Set(s)];
31
+ }
32
+ };
33
+ }, o = (e) => e === null ? n("tenantId", null) : r(n("tenantId", e), n("tenantId", null)), s = (e) => (t, n) => {
34
+ let r = ("tenantId" in t ? t.tenantId : void 0) ?? null, i = ("id" in t ? t.id : void 0) ?? null;
35
+ if (!i) return [];
36
+ let o = e ?? (n?.store === void 0 ? void 0 : a(n.store));
37
+ return o === void 0 ? [] : o.rolesOf(i, r);
38
+ };
39
+ //#endregion
40
+ export { s as dataStoreRoleResolver, a as userRoleStore };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@voltro/plugin-rbac",
3
- "version": "0.85.0",
3
+ "version": "0.87.0",
4
4
  "description": "Role-based access control — roles compile to scopes on top of @voltro/protocol's scope system. Ships rbacPlugin() (role→scope interceptor + resource-aware guard resolver, failing with protocol's typed ScopeError), the permission()/assertPermission()/anyPermission() handler guards, the compileRoles() pure compiler, cross-dialect roles + userRoles tables. The UI gate lives in @voltro/client (useCan / useCanAny) — scopes are a framework concept, so gating a button must not require this plugin.",
5
5
  "keywords": [
6
6
  "voltro",
@@ -33,9 +33,9 @@
33
33
  "node": ">=24.0.0"
34
34
  },
35
35
  "dependencies": {
36
- "@voltro/database": "0.85.0",
37
- "@voltro/logger": "0.85.0",
38
- "@voltro/protocol": "0.85.0"
36
+ "@voltro/database": "0.87.0",
37
+ "@voltro/logger": "0.87.0",
38
+ "@voltro/protocol": "0.87.0"
39
39
  },
40
40
  "peerDependencies": {
41
41
  "effect": "4.0.1",
@@ -1,63 +0,0 @@
1
- import { Effect as e } from "effect";
2
- import { ADMIN_SCOPE as t, ScopeError as n, effectiveScopes as r, setEffectiveScopes as i, subjectScopes as a } from "@voltro/protocol";
3
- //#region src/roles.ts
4
- var o = "*", s = (e) => {
5
- let t = /* @__PURE__ */ new Set();
6
- for (let n of Object.values(e)) for (let e of n) e !== "*" && t.add(e);
7
- return [...t].sort();
8
- }, c = (e, n) => {
9
- let r = [], i = /* @__PURE__ */ new Set();
10
- for (let a of e) {
11
- let e = n[a];
12
- if (e !== void 0) for (let n of e) {
13
- if (n === "*") return [t];
14
- i.has(n) || (i.add(n), r.push(n));
15
- }
16
- }
17
- return r;
18
- }, l = (e) => Object.entries(e).map(([e, t]) => ({
19
- slug: e,
20
- permissions: t
21
- })), u = /* @__PURE__ */ new WeakMap(), d = (e, t, n) => {
22
- i(e, t), n !== void 0 && u.set(e, n);
23
- }, f = (e) => r(e), p = (e) => u.get(e) ?? [], m = (e) => {
24
- if (!("metadata" in e) || e.metadata === void 0) return [];
25
- let t = e.metadata.roles;
26
- return Array.isArray(t) ? t.filter((e) => typeof e == "string") : [];
27
- }, h = (e, t, n, r = []) => {
28
- let i = c(t, n), o = [], s = /* @__PURE__ */ new Set();
29
- for (let t of [
30
- ...a(e),
31
- ...i,
32
- ...r
33
- ]) s.has(t) || (s.add(t), o.push(t));
34
- return o;
35
- }, g = (e, n) => {
36
- let r = f(e);
37
- return r.includes(t) || r.includes(n);
38
- }, _ = (e) => typeof e == "string" ? [e] : e, v = (e, t) => {
39
- for (let n of t) if (!g(e, n)) return n;
40
- return null;
41
- }, y = (t, r) => {
42
- let i = _(r), a = v(t.request.subject, i);
43
- return a === null ? e.void : e.fail(new n({
44
- required: a,
45
- message: `missing required permission '${a}'`
46
- }));
47
- }, b = (t, r) => {
48
- let i = t.request.subject;
49
- if (r.length === 0) return e.void;
50
- for (let t of r) if (g(i, t)) return e.void;
51
- return e.fail(new n({
52
- required: r[0],
53
- message: `missing all of required permissions [${r.join(", ")}]`
54
- }));
55
- }, x = (e, t) => {
56
- let r = v(e.request.subject, _(t));
57
- if (r !== null) throw new n({
58
- required: r,
59
- message: `missing required permission '${r}'`
60
- });
61
- }, S = (e, t) => v(e.request.subject, _(t)) === null;
62
- //#endregion
63
- export { h as a, f as c, c as d, s as f, y as i, d as l, x as n, m as o, l as p, S as r, p as s, b as t, o as u };