@dudousxd/nestjs-authz-mikro-orm 0.8.0 → 0.10.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/CHANGELOG.md +59 -0
- package/README.md +67 -3
- package/dist/authz-rbac.module.d.ts +12 -0
- package/dist/authz-rbac.module.d.ts.map +1 -1
- package/dist/authz-rbac.module.js +74 -8
- package/dist/authz-rbac.module.js.map +1 -1
- package/dist/entities.d.ts +27 -1
- package/dist/entities.d.ts.map +1 -1
- package/dist/entities.js +36 -1
- package/dist/entities.js.map +1 -1
- package/dist/index.d.ts +4 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/mikro-orm-authz.store.d.ts +47 -15
- package/dist/mikro-orm-authz.store.d.ts.map +1 -1
- package/dist/mikro-orm-authz.store.js +145 -39
- package/dist/mikro-orm-authz.store.js.map +1 -1
- package/dist/schema.d.ts.map +1 -1
- package/dist/schema.js +129 -2
- package/dist/schema.js.map +1 -1
- package/dist/tenancy.d.ts +149 -0
- package/dist/tenancy.d.ts.map +1 -0
- package/dist/tenancy.js +227 -0
- package/dist/tenancy.js.map +1 -0
- package/package.json +11 -5
package/dist/schema.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { DEFAULT_TABLE_NAMES, PermissionEntity, RoleEntity, RolePermissionEntity, UserRoleEntity, } from './entities.js';
|
|
2
|
+
import { assertSchemaSupported } from './mikro-orm-authz.store.js';
|
|
2
3
|
/** Entity class → the table name to fall back to when the entity is not registered. */
|
|
3
4
|
const AUTHZ_ENTITY_TABLE_DEFAULTS = [
|
|
4
5
|
[RoleEntity, DEFAULT_TABLE_NAMES.roles],
|
|
@@ -30,6 +31,39 @@ function isOrm(value) {
|
|
|
30
31
|
return candidate.em != null && typeof candidate.em === 'object';
|
|
31
32
|
}
|
|
32
33
|
/** The platform's schema generator for a given EM (driver + EM bound). */
|
|
34
|
+
/** The `schema` the authz entities were built with, if any (one for all four). */
|
|
35
|
+
function authzSchema(em) {
|
|
36
|
+
const metadata = em.getMetadata();
|
|
37
|
+
for (const [entity] of AUTHZ_ENTITY_TABLE_DEFAULTS) {
|
|
38
|
+
const schema = metadata.find(entity.name)?.schema;
|
|
39
|
+
if (schema !== undefined && schema !== '' && schema !== '*')
|
|
40
|
+
return schema;
|
|
41
|
+
}
|
|
42
|
+
return undefined;
|
|
43
|
+
}
|
|
44
|
+
const isMysql = (em) => /mysql|maria/i.test(em.getPlatform().constructor.name);
|
|
45
|
+
/**
|
|
46
|
+
* Create the authz `schema` (a Postgres schema, or a MySQL database) when it is missing. The
|
|
47
|
+
* catalog is checked first: `CREATE SCHEMA IF NOT EXISTS` needs the CREATE privilege even when
|
|
48
|
+
* the schema exists. A failure names the grant needed.
|
|
49
|
+
*/
|
|
50
|
+
async function ensureSchemaExists(em, schema) {
|
|
51
|
+
const connection = em.getConnection();
|
|
52
|
+
const rows = (await connection.execute('select 1 as found from information_schema.schemata where schema_name = ?', [schema]));
|
|
53
|
+
if (rows.length > 0)
|
|
54
|
+
return;
|
|
55
|
+
const mysql = isMysql(em);
|
|
56
|
+
const quoted = em.getPlatform().quoteIdentifier(schema);
|
|
57
|
+
try {
|
|
58
|
+
await connection.execute(mysql ? `create database if not exists ${quoted}` : `create schema if not exists ${quoted}`);
|
|
59
|
+
}
|
|
60
|
+
catch (error) {
|
|
61
|
+
const grant = mysql
|
|
62
|
+
? `GRANT CREATE ON *.* TO <user>, or create the database \`${schema}\` yourself`
|
|
63
|
+
: `GRANT CREATE ON DATABASE <database> TO <role>, or create the schema "${schema}" yourself`;
|
|
64
|
+
throw new Error(`nestjs-authz: could not create the ${mysql ? 'database' : 'schema'} ${JSON.stringify(schema)} for the authz tables (${error.message}). ${grant}.`);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
33
67
|
function generatorFor(em) {
|
|
34
68
|
return em.getPlatform().getSchemaGenerator(em.getDriver(), em);
|
|
35
69
|
}
|
|
@@ -37,10 +71,90 @@ function generatorFor(em) {
|
|
|
37
71
|
async function authzStatements(em) {
|
|
38
72
|
const tables = authzTables(em);
|
|
39
73
|
const sql = await generatorFor(em).getUpdateSchemaSQL({ safe: true, wrap: false });
|
|
40
|
-
|
|
74
|
+
const statements = sql
|
|
41
75
|
.split(';')
|
|
42
76
|
.map((s) => s.trim())
|
|
43
77
|
.filter((s) => s.length > 0 && tables.some((t) => new RegExp(`\\b${t}\\b`, 'i').test(s)));
|
|
78
|
+
const schema = authzSchema(em);
|
|
79
|
+
return schema !== undefined && isMysql(em) ? narrowToSchema(em, schema, statements) : statements;
|
|
80
|
+
}
|
|
81
|
+
/** Top-level comma split of a `create table (…)` body. */
|
|
82
|
+
function splitDefinitions(body) {
|
|
83
|
+
const parts = [];
|
|
84
|
+
let depth = 0;
|
|
85
|
+
let quote;
|
|
86
|
+
let start = 0;
|
|
87
|
+
for (let i = 0; i < body.length; i++) {
|
|
88
|
+
const c = body[i];
|
|
89
|
+
if (quote) {
|
|
90
|
+
if (c === quote)
|
|
91
|
+
quote = undefined;
|
|
92
|
+
}
|
|
93
|
+
else if (c === "'" || c === '`') {
|
|
94
|
+
quote = c;
|
|
95
|
+
}
|
|
96
|
+
else if (c === '(') {
|
|
97
|
+
depth++;
|
|
98
|
+
}
|
|
99
|
+
else if (c === ')') {
|
|
100
|
+
depth--;
|
|
101
|
+
}
|
|
102
|
+
else if (c === ',' && depth === 0) {
|
|
103
|
+
parts.push(body.slice(start, i).trim());
|
|
104
|
+
start = i + 1;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
parts.push(body.slice(start).trim());
|
|
108
|
+
return parts.filter((p) => p.length > 0);
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* MikroORM introspects MySQL only in the connection's own database, so for tables in another
|
|
112
|
+
* database (`schema`) its diff says "create everything" on every boot. Narrow it against that
|
|
113
|
+
* database's `information_schema`: keep `create table` for missing tables, turn it into
|
|
114
|
+
* `add column`s for existing ones, and drop index statements whose index exists.
|
|
115
|
+
*/
|
|
116
|
+
async function narrowToSchema(em, schema, statements) {
|
|
117
|
+
const connection = em.getConnection();
|
|
118
|
+
const rows = (await connection.execute('select table_name as t, column_name as c from information_schema.columns where table_schema = ?', [schema]));
|
|
119
|
+
const columns = new Map();
|
|
120
|
+
for (const row of rows) {
|
|
121
|
+
const table = (row.t ?? row.T ?? row.TABLE_NAME);
|
|
122
|
+
const column = (row.c ?? row.C ?? row.COLUMN_NAME);
|
|
123
|
+
if (!columns.has(table))
|
|
124
|
+
columns.set(table, new Set());
|
|
125
|
+
columns.get(table)?.add(column);
|
|
126
|
+
}
|
|
127
|
+
const indexRows = (await connection.execute('select table_name as t, index_name as i from information_schema.statistics where table_schema = ?', [schema]));
|
|
128
|
+
const indexes = new Set(indexRows.map((r) => `${r.t ?? r.T ?? r.TABLE_NAME}.${r.i ?? r.I ?? r.INDEX_NAME}`));
|
|
129
|
+
const out = [];
|
|
130
|
+
for (const statement of statements) {
|
|
131
|
+
const create = /^create table (`[^`]+`\.`([^`]+)`) \((.*)\)[^)]*$/is.exec(statement);
|
|
132
|
+
if (create) {
|
|
133
|
+
const [, ref, table, body] = create;
|
|
134
|
+
const existing = columns.get(table);
|
|
135
|
+
if (!existing) {
|
|
136
|
+
out.push(statement);
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
for (const definition of splitDefinitions(body)) {
|
|
140
|
+
const column = /^`([^`]+)`\s/.exec(definition)?.[1];
|
|
141
|
+
if (column !== undefined && !existing.has(column)) {
|
|
142
|
+
out.push(`alter table ${ref} add ${definition}`);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
continue;
|
|
146
|
+
}
|
|
147
|
+
const index = /^alter table `[^`]+`\.`([^`]+)` add (?:unique |index |unique index |fulltext )?(?:index )?`([^`]+)`/i.exec(statement) ?? /^create (?:unique )?index `([^`]+)` on `[^`]+`\.`([^`]+)`/i.exec(statement);
|
|
148
|
+
if (index) {
|
|
149
|
+
const [table, name] = statement.toLowerCase().startsWith('create')
|
|
150
|
+
? [index[2], index[1]]
|
|
151
|
+
: [index[1], index[2]];
|
|
152
|
+
if (indexes.has(`${table}.${name}`))
|
|
153
|
+
continue;
|
|
154
|
+
}
|
|
155
|
+
out.push(statement);
|
|
156
|
+
}
|
|
157
|
+
return out;
|
|
44
158
|
}
|
|
45
159
|
/**
|
|
46
160
|
* Returns the non-destructive SQL needed to bring only the authz RBAC tables up to date
|
|
@@ -55,7 +169,16 @@ async function authzStatements(em) {
|
|
|
55
169
|
* ```
|
|
56
170
|
*/
|
|
57
171
|
export async function authzSchemaSql(ormOrEm) {
|
|
58
|
-
const
|
|
172
|
+
const em = toEm(ormOrEm);
|
|
173
|
+
const statements = await authzStatements(em);
|
|
174
|
+
const schema = authzSchema(em);
|
|
175
|
+
// A migration also creates the schema (a MySQL database), when it has something to create.
|
|
176
|
+
if (schema !== undefined && statements.some((st) => /^create table/i.test(st))) {
|
|
177
|
+
const quoted = em.getPlatform().quoteIdentifier(schema);
|
|
178
|
+
statements.unshift(isMysql(em)
|
|
179
|
+
? `create database if not exists ${quoted}`
|
|
180
|
+
: `create schema if not exists ${quoted}`);
|
|
181
|
+
}
|
|
59
182
|
return statements.length ? `${statements.join(';\n')};` : '';
|
|
60
183
|
}
|
|
61
184
|
/**
|
|
@@ -72,6 +195,10 @@ export async function authzSchemaSql(ormOrEm) {
|
|
|
72
195
|
*/
|
|
73
196
|
export async function ensureAuthzSchema(ormOrEm) {
|
|
74
197
|
const em = toEm(ormOrEm);
|
|
198
|
+
assertSchemaSupported(em);
|
|
199
|
+
const schema = authzSchema(em);
|
|
200
|
+
if (schema !== undefined)
|
|
201
|
+
await ensureSchemaExists(em, schema);
|
|
75
202
|
const statements = await authzStatements(em);
|
|
76
203
|
if (statements.length === 0)
|
|
77
204
|
return;
|
package/dist/schema.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"schema.js","sourceRoot":"","sources":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AAC,EAAE,CAAC,CAAC;IAC7C,IAAI,UAAU,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO;IACpC,MAAM,UAAU,GAAG,EAAE,CAAC,aAAa,EAAE,CAAC;IACtC,KAAK,MAAM,SAAS,IAAI,UAAU,EAAE,CAAC;QACnC,MAAM,UAAU,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;IACtC,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import { type UserRef } from '@dudousxd/nestjs-authz/store-kit';
|
|
2
|
+
import { ForbiddenException } from '@nestjs/common';
|
|
3
|
+
import type { ModuleRef } from '@nestjs/core';
|
|
4
|
+
/** One level of a tenant hierarchy (a clinic, a unit inside it), as nestjs-context publishes it. */
|
|
5
|
+
export interface TenantLevel {
|
|
6
|
+
id: string;
|
|
7
|
+
kind: string;
|
|
8
|
+
slug?: string;
|
|
9
|
+
role?: string;
|
|
10
|
+
}
|
|
11
|
+
/** A tenant as published by `@dudousxd/nestjs-context` (`Context.tenant()`). */
|
|
12
|
+
export interface TenantRef extends TenantLevel {
|
|
13
|
+
source: string;
|
|
14
|
+
/** The active path when tenants nest, root first and this tenant last. */
|
|
15
|
+
chain?: TenantLevel[];
|
|
16
|
+
family?: string;
|
|
17
|
+
}
|
|
18
|
+
/** How the context says a lib must scope data right now (`Context.tenantFilter()`). */
|
|
19
|
+
export type TenantFilter = {
|
|
20
|
+
mode: 'system';
|
|
21
|
+
} | {
|
|
22
|
+
mode: 'all';
|
|
23
|
+
} | {
|
|
24
|
+
mode: 'tenantless';
|
|
25
|
+
} | {
|
|
26
|
+
mode: 'tenant';
|
|
27
|
+
tenant: TenantRef;
|
|
28
|
+
chain?: TenantLevel[];
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* The RBAC module's `tenancy` option. `'inherit'` (default) follows `ContextModule`'s
|
|
32
|
+
* `tenancy.enabled`; `false` keeps this module out of tenancy.
|
|
33
|
+
*/
|
|
34
|
+
export type AuthzTenancyOption = 'inherit' | false | {
|
|
35
|
+
/** `'inherit'` (default) follows the context switch; a boolean overrides it. */
|
|
36
|
+
enabled?: 'inherit' | boolean;
|
|
37
|
+
/**
|
|
38
|
+
* Tenant kinds this module honours, instead of the context's `kinds`. With nested
|
|
39
|
+
* tenants, roles and grants held at every listed level of the active chain apply
|
|
40
|
+
* (e.g. `['clinic', 'unit']`: a clinic role counts inside its units).
|
|
41
|
+
*/
|
|
42
|
+
kinds?: string[];
|
|
43
|
+
/** Only the tenant of this family counts (a request can carry several). */
|
|
44
|
+
family?: string;
|
|
45
|
+
/**
|
|
46
|
+
* A check with no tenant throws `TenantRequiredError` (403, `E_TENANT_REQUIRED`) instead
|
|
47
|
+
* of running in the global scope. Default: the context's `tenancy.required`. Console
|
|
48
|
+
* access checks and dev tools never require one.
|
|
49
|
+
*/
|
|
50
|
+
required?: boolean;
|
|
51
|
+
/**
|
|
52
|
+
* Accepted for the shared shape. It changes nothing here: a GLOBAL role assignment
|
|
53
|
+
* always applies, in every tenant.
|
|
54
|
+
*/
|
|
55
|
+
nullRows?: 'tenantless' | 'shared';
|
|
56
|
+
/**
|
|
57
|
+
* Map the caller's role inside the context tenant (`tenant.role`) to authz roles that
|
|
58
|
+
* apply in that tenant's scope only, e.g. `{ owner: 'org_admin', member: 'org_member' }`.
|
|
59
|
+
* With nested tenants, the caller's role at each level of the chain is mapped.
|
|
60
|
+
* Unmapped roles grant nothing.
|
|
61
|
+
*/
|
|
62
|
+
roles?: Record<string, string | readonly string[]>;
|
|
63
|
+
};
|
|
64
|
+
/**
|
|
65
|
+
* An explicit tenant for a check, from the user being checked. Return a tenant id to scope the
|
|
66
|
+
* check to it, `null` for the global scope, or `undefined` to fall back to the context.
|
|
67
|
+
*/
|
|
68
|
+
export type TenantResolver = (user: unknown) => string | null | undefined;
|
|
69
|
+
/** The structural slice of nestjs-context's accessor this module reads. */
|
|
70
|
+
export interface TenantContextAccessor {
|
|
71
|
+
tenantId?(): string | undefined;
|
|
72
|
+
userRef?(): {
|
|
73
|
+
type?: string;
|
|
74
|
+
id: string | number;
|
|
75
|
+
} | undefined;
|
|
76
|
+
tenancy?(): {
|
|
77
|
+
enabled: boolean;
|
|
78
|
+
kinds?: string[];
|
|
79
|
+
required?: boolean;
|
|
80
|
+
};
|
|
81
|
+
tenantFilter?(options?: {
|
|
82
|
+
kinds?: string[];
|
|
83
|
+
family?: string;
|
|
84
|
+
required?: boolean;
|
|
85
|
+
}): TenantFilter;
|
|
86
|
+
}
|
|
87
|
+
/** The error `required` raises, shaped like nestjs-context's `TenantRequiredError`. */
|
|
88
|
+
export declare class TenantRequiredError extends ForbiddenException {
|
|
89
|
+
readonly code = "E_TENANT_REQUIRED";
|
|
90
|
+
constructor();
|
|
91
|
+
}
|
|
92
|
+
/** The tenant scope of one check, plus the roles `tenancy.roles` adds in that scope. */
|
|
93
|
+
export interface RbacTenant {
|
|
94
|
+
/** The most specific scope (the matched tenant), or `{}` for the global scope. */
|
|
95
|
+
scope: {
|
|
96
|
+
tenantId?: string;
|
|
97
|
+
};
|
|
98
|
+
/**
|
|
99
|
+
* Every scope whose roles and grants apply, root first: the levels of a nested tenant the
|
|
100
|
+
* module honours, or just `scope`.
|
|
101
|
+
*/
|
|
102
|
+
scopes: Array<{
|
|
103
|
+
tenantId?: string;
|
|
104
|
+
}>;
|
|
105
|
+
roles: string[];
|
|
106
|
+
}
|
|
107
|
+
export interface ResolveRbacTenantInput {
|
|
108
|
+
/** The user as the Gate passed it. */
|
|
109
|
+
user: unknown;
|
|
110
|
+
/** The same user mapped to a store ref. */
|
|
111
|
+
ref: UserRef;
|
|
112
|
+
/** The module's explicit `tenant` resolver, if any. */
|
|
113
|
+
resolver?: TenantResolver | undefined;
|
|
114
|
+
/** The module's `tenancy` option. */
|
|
115
|
+
tenancy?: AuthzTenancyOption | undefined;
|
|
116
|
+
/** The context accessor found through DI, if any (else the global slot is read). */
|
|
117
|
+
accessor?: TenantContextAccessor | undefined;
|
|
118
|
+
/** With tenancy off, scope by the context's plain `tenantId()` (drizzle/typeorm behaviour). */
|
|
119
|
+
legacyContextTenant: boolean;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Precedence: a scope forced by the core's `runInTenantScope` (the dev tools), then the
|
|
123
|
+
* explicit `tenant` resolver, then (tenancy on) the context tenant filter, then (tenancy off)
|
|
124
|
+
* the adapter's behaviour without tenancy. `tenant` mode scopes to that tenant and, for a nested
|
|
125
|
+
* tenant, to each level above it the module honours; `tenantless`, `all` and `system` use the
|
|
126
|
+
* global scope. With `required`, `tenantless` throws {@link TenantRequiredError}.
|
|
127
|
+
*/
|
|
128
|
+
export declare function resolveRbacTenant(input: ResolveRbacTenantInput): RbacTenant;
|
|
129
|
+
/**
|
|
130
|
+
* Run `read` once per scope of `tenant` and merge the results (one call for a flat tenant).
|
|
131
|
+
*/
|
|
132
|
+
export declare function acrossScopes<T>(tenant: RbacTenant, read: (scope: {
|
|
133
|
+
tenantId?: string;
|
|
134
|
+
}) => Promise<readonly T[]>): Promise<T[]>;
|
|
135
|
+
/** True when `test` holds in any scope of `tenant`. */
|
|
136
|
+
export declare function inAnyScope(tenant: RbacTenant, test: (scope: {
|
|
137
|
+
tenantId?: string;
|
|
138
|
+
}) => Promise<boolean>): Promise<boolean>;
|
|
139
|
+
/** A provider's per-instance cache for {@link rbacTenant}'s accessor lookup. */
|
|
140
|
+
export interface TenantContextCache {
|
|
141
|
+
resolved: boolean;
|
|
142
|
+
value: TenantContextAccessor | undefined;
|
|
143
|
+
}
|
|
144
|
+
/** The tenant scope (and `tenancy.roles` roles) of one check for `user`. */
|
|
145
|
+
export declare function rbacTenant(options: {
|
|
146
|
+
tenant?: TenantResolver | undefined;
|
|
147
|
+
tenancy?: AuthzTenancyOption | undefined;
|
|
148
|
+
}, user: unknown, ref: UserRef, injected: TenantContextAccessor | undefined, moduleRef: ModuleRef | undefined, cache: TenantContextCache, legacyContextTenant: boolean): RbacTenant;
|
|
149
|
+
//# sourceMappingURL=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SAAS,GAAG,SAAS,EAChC,KAAK,EAAE,kBAAkB,EACzB,mBAAmB,EAAE,OAAO,GAC3B,UAAU,CASZ"}
|
package/dist/tenancy.js
ADDED
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
// Tenancy for the RBAC module: which tenant scope a permission/role check runs in, and which
|
|
2
|
+
// roles the caller's role inside the context tenant maps to. The same file ships in every
|
|
3
|
+
// store adapter. It reads the @dudousxd/nestjs-context tenancy contract STRUCTURALLY (no import),
|
|
4
|
+
// so it works with any context version, and with none: a missing or older context reads as
|
|
5
|
+
// tenancy off.
|
|
6
|
+
import { CONTEXT_ACCESSOR } from '@dudousxd/nestjs-authz';
|
|
7
|
+
import { normalizeUserRef } from '@dudousxd/nestjs-authz/store-kit';
|
|
8
|
+
import { ForbiddenException } from '@nestjs/common';
|
|
9
|
+
const SLOT = Symbol.for('@dudousxd/nestjs-diagnostics:accessor');
|
|
10
|
+
/** The core's `runInTenantScope` storage, read structurally (any core version, or none). */
|
|
11
|
+
const TENANT_SCOPE_SLOT = Symbol.for('@dudousxd/nestjs-authz:tenant-scope');
|
|
12
|
+
/** What the core's `runInTenantScope` / `runWithoutRequiredTenant` force here, if anything. */
|
|
13
|
+
function forced() {
|
|
14
|
+
const slot = globalThis[TENANT_SCOPE_SLOT];
|
|
15
|
+
const store = slot?.storage?.getStore?.();
|
|
16
|
+
if (!store)
|
|
17
|
+
return { relaxed: false };
|
|
18
|
+
const relaxed = store.required === false;
|
|
19
|
+
const id = store.tenantId;
|
|
20
|
+
return id === null || typeof id === 'string' ? { scope: { tenantId: id }, relaxed } : { relaxed };
|
|
21
|
+
}
|
|
22
|
+
/** The error `required` raises, shaped like nestjs-context's `TenantRequiredError`. */
|
|
23
|
+
export class TenantRequiredError extends ForbiddenException {
|
|
24
|
+
code = 'E_TENANT_REQUIRED';
|
|
25
|
+
constructor() {
|
|
26
|
+
super({
|
|
27
|
+
statusCode: 403,
|
|
28
|
+
error: 'Forbidden',
|
|
29
|
+
code: 'E_TENANT_REQUIRED',
|
|
30
|
+
message: 'This action needs an active tenant (organization). Select one and retry.',
|
|
31
|
+
});
|
|
32
|
+
this.name = 'TenantRequiredError';
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
/** Is tenancy on for this module? Call per check: the context can change between requests. */
|
|
36
|
+
function readTenancy(option, a, relaxed) {
|
|
37
|
+
if (option === false)
|
|
38
|
+
return { enabled: false };
|
|
39
|
+
if (typeof a?.tenancy !== 'function' || typeof a.tenantFilter !== 'function') {
|
|
40
|
+
return { enabled: false };
|
|
41
|
+
}
|
|
42
|
+
const own = typeof option === 'object' ? option : {};
|
|
43
|
+
const inherit = own.enabled === undefined || own.enabled === 'inherit';
|
|
44
|
+
const context = a.tenancy();
|
|
45
|
+
if (!(inherit ? context.enabled : own.enabled))
|
|
46
|
+
return { enabled: false };
|
|
47
|
+
const options = {};
|
|
48
|
+
if (own.kinds)
|
|
49
|
+
options.kinds = own.kinds;
|
|
50
|
+
if (own.family !== undefined)
|
|
51
|
+
options.family = own.family;
|
|
52
|
+
const required = relaxed ? false : (own.required ?? context.required === true);
|
|
53
|
+
if (relaxed)
|
|
54
|
+
options.required = false;
|
|
55
|
+
else if (own.required !== undefined)
|
|
56
|
+
options.required = own.required;
|
|
57
|
+
const tenantFilter = a.tenantFilter.bind(a);
|
|
58
|
+
return {
|
|
59
|
+
enabled: true,
|
|
60
|
+
kinds: own.kinds ?? context.kinds,
|
|
61
|
+
filter: () => {
|
|
62
|
+
// A context that knows `required` throws its own TenantRequiredError here.
|
|
63
|
+
const filter = tenantFilter(Object.keys(options).length > 0 ? options : undefined);
|
|
64
|
+
// An older one answers `tenantless`: raise the same error.
|
|
65
|
+
if (required && filter.mode === 'tenantless')
|
|
66
|
+
throw new TenantRequiredError();
|
|
67
|
+
return filter;
|
|
68
|
+
},
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
/** The levels of a `tenant` filter that scope this module, root first, the matched level last. */
|
|
72
|
+
function scopeLevels(filter, kinds) {
|
|
73
|
+
const path = filter.chain && filter.chain.length > 0
|
|
74
|
+
? filter.chain
|
|
75
|
+
: filter.tenant.chain && filter.tenant.chain.length > 0
|
|
76
|
+
? cutAt(filter.tenant.chain, filter.tenant)
|
|
77
|
+
: [filter.tenant];
|
|
78
|
+
const last = path[path.length - 1];
|
|
79
|
+
return path.filter((level) => level === last || !kinds || kinds.includes(level.kind));
|
|
80
|
+
}
|
|
81
|
+
/** `chain` up to and including `tenant` (the whole chain when it is not in it). */
|
|
82
|
+
function cutAt(chain, tenant) {
|
|
83
|
+
const at = chain.findIndex((level) => level.id === tenant.id && level.kind === tenant.kind);
|
|
84
|
+
return at === -1 ? [...chain, tenant] : chain.slice(0, at + 1);
|
|
85
|
+
}
|
|
86
|
+
/** The accessor found through DI, else the global slot `ContextModule` publishes. */
|
|
87
|
+
function effectiveAccessor(injected) {
|
|
88
|
+
if (injected)
|
|
89
|
+
return injected;
|
|
90
|
+
const slot = globalThis[SLOT];
|
|
91
|
+
const current = slot?.current;
|
|
92
|
+
return current && typeof current === 'object' ? current : undefined;
|
|
93
|
+
}
|
|
94
|
+
/** Today's behaviour without tenancy: the context's plain `tenantId()`, read defensively. */
|
|
95
|
+
function legacyTenantId(accessor) {
|
|
96
|
+
if (!accessor || typeof accessor.tenantId !== 'function')
|
|
97
|
+
return undefined;
|
|
98
|
+
try {
|
|
99
|
+
return accessor.tenantId() ?? undefined;
|
|
100
|
+
}
|
|
101
|
+
catch {
|
|
102
|
+
return undefined;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
/** Is `ref` the context's current user? Tenant roles belong to the caller, nobody else. */
|
|
106
|
+
function isContextUser(ref, accessor) {
|
|
107
|
+
let current;
|
|
108
|
+
try {
|
|
109
|
+
current = accessor?.userRef?.();
|
|
110
|
+
}
|
|
111
|
+
catch {
|
|
112
|
+
current = undefined;
|
|
113
|
+
}
|
|
114
|
+
if (!current || current.id === undefined || current.id === null)
|
|
115
|
+
return false;
|
|
116
|
+
const a = normalizeUserRef(ref);
|
|
117
|
+
const b = normalizeUserRef(current);
|
|
118
|
+
return a.type === b.type && a.id === b.id;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Precedence: a scope forced by the core's `runInTenantScope` (the dev tools), then the
|
|
122
|
+
* explicit `tenant` resolver, then (tenancy on) the context tenant filter, then (tenancy off)
|
|
123
|
+
* the adapter's behaviour without tenancy. `tenant` mode scopes to that tenant and, for a nested
|
|
124
|
+
* tenant, to each level above it the module honours; `tenantless`, `all` and `system` use the
|
|
125
|
+
* global scope. With `required`, `tenantless` throws {@link TenantRequiredError}.
|
|
126
|
+
*/
|
|
127
|
+
export function resolveRbacTenant(input) {
|
|
128
|
+
const force = forced();
|
|
129
|
+
const explicit = force.scope
|
|
130
|
+
? force.scope.tenantId
|
|
131
|
+
: input.resolver
|
|
132
|
+
? input.resolver(input.user)
|
|
133
|
+
: undefined;
|
|
134
|
+
const accessor = effectiveAccessor(input.accessor);
|
|
135
|
+
// An explicit scope never needs a tenant from the context.
|
|
136
|
+
const tenancy = readTenancy(input.tenancy, accessor, force.relaxed || explicit !== undefined);
|
|
137
|
+
const filter = tenancy.enabled ? tenancy.filter() : undefined;
|
|
138
|
+
const own = typeof input.tenancy === 'object' ? input.tenancy : {};
|
|
139
|
+
const levels = tenancy.enabled && filter?.mode === 'tenant' ? scopeLevels(filter, tenancy.kinds) : [];
|
|
140
|
+
let scopes;
|
|
141
|
+
if (explicit !== undefined) {
|
|
142
|
+
scopes = [explicit === null ? {} : { tenantId: explicit }];
|
|
143
|
+
}
|
|
144
|
+
else if (filter) {
|
|
145
|
+
scopes = levels.length > 0 ? levels.map((level) => ({ tenantId: level.id })) : [{}];
|
|
146
|
+
}
|
|
147
|
+
else if (input.legacyContextTenant) {
|
|
148
|
+
const id = legacyTenantId(input.accessor);
|
|
149
|
+
scopes = [id === undefined ? {} : { tenantId: id }];
|
|
150
|
+
}
|
|
151
|
+
else {
|
|
152
|
+
scopes = [{}];
|
|
153
|
+
}
|
|
154
|
+
const scope = scopes[scopes.length - 1] ?? {};
|
|
155
|
+
// The caller's role at each level in scope, mapped by `tenancy.roles`.
|
|
156
|
+
const map = own.roles;
|
|
157
|
+
const roles = new Set();
|
|
158
|
+
if (map && levels.length > 0 && isContextUser(input.ref, accessor)) {
|
|
159
|
+
const inScope = new Set(scopes.map((s) => s.tenantId));
|
|
160
|
+
const matched = filter?.mode === 'tenant' ? filter.tenant : undefined;
|
|
161
|
+
for (const level of levels) {
|
|
162
|
+
// The matched level's role may live only on the tenant itself, not on its chain entry.
|
|
163
|
+
const role = level.role ??
|
|
164
|
+
(matched && level.id === matched.id && level.kind === matched.kind
|
|
165
|
+
? matched.role
|
|
166
|
+
: undefined);
|
|
167
|
+
if (role === undefined || !inScope.has(level.id))
|
|
168
|
+
continue;
|
|
169
|
+
if (!Object.prototype.hasOwnProperty.call(map, role))
|
|
170
|
+
continue;
|
|
171
|
+
const mapped = map[role];
|
|
172
|
+
for (const role of typeof mapped === 'string' ? [mapped] : (mapped ?? []))
|
|
173
|
+
roles.add(role);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
return { scope, scopes, roles: [...roles] };
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Run `read` once per scope of `tenant` and merge the results (one call for a flat tenant).
|
|
180
|
+
*/
|
|
181
|
+
export async function acrossScopes(tenant, read) {
|
|
182
|
+
if (tenant.scopes.length <= 1)
|
|
183
|
+
return [...(await read(tenant.scope))];
|
|
184
|
+
const all = await Promise.all(tenant.scopes.map(read));
|
|
185
|
+
return [...new Set(all.flat())];
|
|
186
|
+
}
|
|
187
|
+
/** True when `test` holds in any scope of `tenant`. */
|
|
188
|
+
export async function inAnyScope(tenant, test) {
|
|
189
|
+
for (const scope of tenant.scopes)
|
|
190
|
+
if (await test(scope))
|
|
191
|
+
return true;
|
|
192
|
+
return false;
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Locate the context accessor. Prefers a value injected into the module; falls back to a
|
|
196
|
+
* non-strict {@link ModuleRef} lookup so an accessor provided by ANY module (the app root, a
|
|
197
|
+
* global ContextModule) is still found: the RBAC module is global and its providers don't import
|
|
198
|
+
* the app's modules, so a plain `@Inject` would miss a root-provided accessor.
|
|
199
|
+
*/
|
|
200
|
+
function resolveTenantContext(injected, moduleRef, cache) {
|
|
201
|
+
if (injected)
|
|
202
|
+
return injected;
|
|
203
|
+
if (cache.resolved)
|
|
204
|
+
return cache.value;
|
|
205
|
+
cache.resolved = true;
|
|
206
|
+
if (moduleRef) {
|
|
207
|
+
try {
|
|
208
|
+
cache.value = moduleRef.get(CONTEXT_ACCESSOR, { strict: false });
|
|
209
|
+
}
|
|
210
|
+
catch {
|
|
211
|
+
cache.value = undefined;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
return cache.value;
|
|
215
|
+
}
|
|
216
|
+
/** The tenant scope (and `tenancy.roles` roles) of one check for `user`. */
|
|
217
|
+
export function rbacTenant(options, user, ref, injected, moduleRef, cache, legacyContextTenant) {
|
|
218
|
+
return resolveRbacTenant({
|
|
219
|
+
user,
|
|
220
|
+
ref,
|
|
221
|
+
resolver: options.tenant,
|
|
222
|
+
tenancy: options.tenancy,
|
|
223
|
+
accessor: resolveTenantContext(injected, moduleRef, cache),
|
|
224
|
+
legacyContextTenant,
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
//# sourceMappingURL=tenancy.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tenancy.js","sourceRoot":"","sources":["../src/tenancy.ts"],"names":[],"mappings":"AAAA,6FAA6F;AAC7F,0FAA0F;AAC1F,kGAAkG;AAClG,2FAA2F;AAC3F,eAAe;AACf,OAAO,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAC1D,OAAO,EAAgB,gBAAgB,EAAE,MAAM,kCAAkC,CAAC;AAClF,OAAO,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AA+EpD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CAAC,CAAC;AACL,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dudousxd/nestjs-authz-mikro-orm",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.0",
|
|
4
4
|
"description": "MikroORM RBAC persistence for @dudousxd/nestjs-authz — roles, permissions, and a Gate seam (zero connection ownership).",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -38,7 +38,7 @@
|
|
|
38
38
|
"CHANGELOG.md"
|
|
39
39
|
],
|
|
40
40
|
"peerDependencies": {
|
|
41
|
-
"@dudousxd/nestjs-authz": ">=0.
|
|
41
|
+
"@dudousxd/nestjs-authz": ">=0.10.0",
|
|
42
42
|
"@mikro-orm/core": "^6.0.0 || ^7.0.0",
|
|
43
43
|
"@mikro-orm/nestjs": "^5.0.0 || ^6.0.0 || ^7.0.0",
|
|
44
44
|
"@nestjs/authorization": "^0.1.3",
|
|
@@ -54,20 +54,26 @@
|
|
|
54
54
|
"devDependencies": {
|
|
55
55
|
"@mikro-orm/better-sqlite": "6.6.16",
|
|
56
56
|
"@mikro-orm/core": "6.6.16",
|
|
57
|
+
"@mikro-orm/mysql": "6.6.16",
|
|
57
58
|
"@mikro-orm/nestjs": "6.1.2",
|
|
59
|
+
"@mikro-orm/postgresql": "6.6.16",
|
|
58
60
|
"@nestjs/authorization": "0.1.3",
|
|
59
61
|
"@nestjs/common": "12.0.1",
|
|
60
62
|
"@nestjs/core": "12.0.1",
|
|
61
63
|
"@nestjs/testing": "12.0.1",
|
|
62
|
-
"@
|
|
64
|
+
"@testcontainers/mysql": "12.1.0",
|
|
65
|
+
"@testcontainers/postgresql": "12.1.0",
|
|
66
|
+
"@types/node": "24.19.2",
|
|
67
|
+
"mysql2": "3.24.4",
|
|
68
|
+
"pg": "8.23.0",
|
|
63
69
|
"reflect-metadata": "0.2.2",
|
|
64
70
|
"rxjs": "7.8.2",
|
|
65
71
|
"typescript": "5.9.3",
|
|
66
72
|
"vitest": "3.2.7",
|
|
67
|
-
"@dudousxd/nestjs-authz": "^0.
|
|
73
|
+
"@dudousxd/nestjs-authz": "^0.12.0"
|
|
68
74
|
},
|
|
69
75
|
"engines": {
|
|
70
|
-
"node": ">=
|
|
76
|
+
"node": ">=24"
|
|
71
77
|
},
|
|
72
78
|
"scripts": {
|
|
73
79
|
"build": "tsc -p tsconfig.json --incremental false",
|