@aooth/arbac-moost 0.2.5 → 0.2.6
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.d.mts +7 -7
- package/dist/index.mjs +33 -26
- package/package.json +17 -17
package/dist/index.d.mts
CHANGED
|
@@ -339,23 +339,23 @@ export declare const ArbacAction: (name: string) => MethodDecorator & ClassDecor
|
|
|
339
339
|
*/
|
|
340
340
|
export declare const ARBAC_MESSAGES: {
|
|
341
341
|
/** 403 — the authorize interceptor denied the handler. Params: `resource`, `action`. */
|
|
342
|
-
readonly
|
|
342
|
+
readonly ARBAC_INSUFFICIENT_PRIVILEGES: "Insufficient privileges for action \"{action}\" on resource \"{resource}\"";
|
|
343
343
|
/** 403 — `useArbac().evaluateOrThrow()` denied. Params: `resource`, `action`. */
|
|
344
344
|
readonly ARBAC_FORBIDDEN: "Forbidden: {resource}/{action}";
|
|
345
345
|
/** 401 — the authorize interceptor could not evaluate the caller (e.g. unknown user). */
|
|
346
346
|
readonly ARBAC_NOT_AUTHORIZED: "Not authorized";
|
|
347
347
|
/** 403 — a query control the role's `controls` gate denies. Params: `control`. */
|
|
348
|
-
readonly
|
|
348
|
+
readonly ARBAC_CONTROL_NOT_ALLOWED: "Control \"{control}\" is not allowed for your role";
|
|
349
349
|
/** 403 — a value outside the role's `controls` whitelist. Params: `control`, `value`. */
|
|
350
|
-
readonly
|
|
350
|
+
readonly ARBAC_CONTROL_VALUE_NOT_ALLOWED: "Control \"{control}={value}\" is not allowed for your role";
|
|
351
351
|
/** 403 — a write through a relation no scope lists in `nestedWrites`. Params: `relation`. */
|
|
352
|
-
readonly
|
|
352
|
+
readonly ARBAC_NESTED_WRITE_NOT_ALLOWED: "Nested writes through \"{relation}\" are not allowed";
|
|
353
353
|
/** 403 — a written row fails the scope `check` (WITH CHECK). */
|
|
354
|
-
readonly
|
|
354
|
+
readonly ARBAC_ROW_OUT_OF_SCOPE: "Row outside your write scope";
|
|
355
355
|
/** 403 — a foreign key the write sets points at a row outside the caller's read scope. Params: `fields`. */
|
|
356
|
-
readonly
|
|
356
|
+
readonly ARBAC_REFERENCE_OUT_OF_SCOPE: "Referenced row \"{fields}\" is outside your scope";
|
|
357
357
|
/** 400 — the query string does not parse. Params: `error` (the parser's text). */
|
|
358
|
-
readonly
|
|
358
|
+
readonly ARBAC_INVALID_QUERY_STRING: "Invalid query string: {error}";
|
|
359
359
|
};
|
|
360
360
|
/** A code of {@link ARBAC_MESSAGES}. */
|
|
361
361
|
type ArbacMessageCode = keyof typeof ARBAC_MESSAGES;
|
package/dist/index.mjs
CHANGED
|
@@ -5,8 +5,8 @@ import { cached, cachedBy, current, eventTypeKey, key, tryGetCurrent } from "@wo
|
|
|
5
5
|
import { Inherit, Injectable, TInterceptorPriority, defineBeforeInterceptor, formatMessage, getConstructor, getMoostMate, registerMessages, useControllerContext, useLogger } from "moost";
|
|
6
6
|
import { Authenticate, HttpError } from "@moostjs/event-http";
|
|
7
7
|
import { computeInsights, containsRelationFilter, findAncestorInSet, getPath, hasRelationOp, isPlainObject, isRelationOp, translateQueryTree } from "@atscript/db";
|
|
8
|
-
import { AsDbController, AsDbReadableController, AsJsonValueHelpController, AsValueHelpController, DB_CRUD_HANDLERS, VALUE_HELP_CRUD_HANDLERS, badRequest, discoverRowLevelActions, getControllerFormType, getDbEndpoint, hasActionDelegations, resolveTerminalRef, stableMeta } from "@atscript/moost-db";
|
|
9
|
-
import { groupByFields, walkFilter } from "@uniqu/core";
|
|
8
|
+
import { AsDbController, AsDbReadableController, AsJsonValueHelpController, AsValueHelpController, DB_CRUD_HANDLERS, VALUE_HELP_CRUD_HANDLERS, badRequest, discoverRowLevelActions, getControllerFormType, getDbEndpoint, hasActionDelegations, notFoundError, resolveTerminalRef, stableMeta } from "@atscript/moost-db";
|
|
9
|
+
import { groupByFields, resolveAlias, walkFilter } from "@uniqu/core";
|
|
10
10
|
// src/db/helpers.ts
|
|
11
11
|
/** `map.get(key)`, created with `create()` and stored on a miss. */
|
|
12
12
|
function getOrCreate(map, key, create) {
|
|
@@ -523,23 +523,23 @@ function requestRelations(readable) {
|
|
|
523
523
|
*/
|
|
524
524
|
const ARBAC_MESSAGES = {
|
|
525
525
|
/** 403 — the authorize interceptor denied the handler. Params: `resource`, `action`. */
|
|
526
|
-
|
|
526
|
+
ARBAC_INSUFFICIENT_PRIVILEGES: "Insufficient privileges for action \"{action}\" on resource \"{resource}\"",
|
|
527
527
|
/** 403 — `useArbac().evaluateOrThrow()` denied. Params: `resource`, `action`. */
|
|
528
528
|
ARBAC_FORBIDDEN: "Forbidden: {resource}/{action}",
|
|
529
529
|
/** 401 — the authorize interceptor could not evaluate the caller (e.g. unknown user). */
|
|
530
530
|
ARBAC_NOT_AUTHORIZED: "Not authorized",
|
|
531
531
|
/** 403 — a query control the role's `controls` gate denies. Params: `control`. */
|
|
532
|
-
|
|
532
|
+
ARBAC_CONTROL_NOT_ALLOWED: "Control \"{control}\" is not allowed for your role",
|
|
533
533
|
/** 403 — a value outside the role's `controls` whitelist. Params: `control`, `value`. */
|
|
534
|
-
|
|
534
|
+
ARBAC_CONTROL_VALUE_NOT_ALLOWED: "Control \"{control}={value}\" is not allowed for your role",
|
|
535
535
|
/** 403 — a write through a relation no scope lists in `nestedWrites`. Params: `relation`. */
|
|
536
|
-
|
|
536
|
+
ARBAC_NESTED_WRITE_NOT_ALLOWED: "Nested writes through \"{relation}\" are not allowed",
|
|
537
537
|
/** 403 — a written row fails the scope `check` (WITH CHECK). */
|
|
538
|
-
|
|
538
|
+
ARBAC_ROW_OUT_OF_SCOPE: "Row outside your write scope",
|
|
539
539
|
/** 403 — a foreign key the write sets points at a row outside the caller's read scope. Params: `fields`. */
|
|
540
|
-
|
|
540
|
+
ARBAC_REFERENCE_OUT_OF_SCOPE: "Referenced row \"{fields}\" is outside your scope",
|
|
541
541
|
/** 400 — the query string does not parse. Params: `error` (the parser's text). */
|
|
542
|
-
|
|
542
|
+
ARBAC_INVALID_QUERY_STRING: "Invalid query string: {error}"
|
|
543
543
|
};
|
|
544
544
|
registerMessages(ARBAC_MESSAGES);
|
|
545
545
|
/** An `HttpError` with `code`, `params` and the code's English text. */
|
|
@@ -608,7 +608,7 @@ function readPath(row, path) {
|
|
|
608
608
|
return path in row ? row[path] : getPath(row, path);
|
|
609
609
|
}
|
|
610
610
|
function outOfScope(fk) {
|
|
611
|
-
return arbacHttpError(403, "
|
|
611
|
+
return arbacHttpError(403, "ARBAC_REFERENCE_OUT_OF_SCOPE", { fields: fk.fields.join(", ") });
|
|
612
612
|
}
|
|
613
613
|
/** A patch row touches `fk` in part only — its tuple is completed from the pre-image. */
|
|
614
614
|
function needsPreImage(row, fk) {
|
|
@@ -901,7 +901,7 @@ function scopesOwnedBy(scopes, resource, action) {
|
|
|
901
901
|
}
|
|
902
902
|
/** The 403 every ARBAC deny answers. */
|
|
903
903
|
function insufficientPrivileges(resource, action) {
|
|
904
|
-
return arbacHttpError(403, "
|
|
904
|
+
return arbacHttpError(403, "ARBAC_INSUFFICIENT_PRIVILEGES", {
|
|
905
905
|
action,
|
|
906
906
|
resource
|
|
907
907
|
});
|
|
@@ -3218,7 +3218,7 @@ function assertNestedWritesAllowed(data, scopes, navFields) {
|
|
|
3218
3218
|
for (const key of Object.keys(row)) {
|
|
3219
3219
|
const dot = key.indexOf(".");
|
|
3220
3220
|
const rel = dot === -1 ? key : key.slice(0, dot);
|
|
3221
|
-
if (navFields.has(rel) && !effectiveScope(scopes).nestedWrites.has(rel)) throw arbacHttpError(403, "
|
|
3221
|
+
if (navFields.has(rel) && !effectiveScope(scopes).nestedWrites.has(rel)) throw arbacHttpError(403, "ARBAC_NESTED_WRITE_NOT_ALLOWED", { relation: rel });
|
|
3222
3222
|
}
|
|
3223
3223
|
}
|
|
3224
3224
|
}
|
|
@@ -3236,7 +3236,7 @@ async function assertIdFiltersInScope(table, idFilters, filter) {
|
|
|
3236
3236
|
if (await table.count({
|
|
3237
3237
|
filter: filter ? { $and: [idFilter, filter] } : idFilter,
|
|
3238
3238
|
controls: {}
|
|
3239
|
-
}) < ids.length) throw
|
|
3239
|
+
}) < ids.length) throw notFoundError();
|
|
3240
3240
|
}
|
|
3241
3241
|
/**
|
|
3242
3242
|
* WITH CHECK before the write (adapters without real transactions): insert /
|
|
@@ -3251,18 +3251,18 @@ async function assertCheckBeforeWrite(ctx, check) {
|
|
|
3251
3251
|
try {
|
|
3252
3252
|
predicate = compileScopeCheck(check);
|
|
3253
3253
|
} catch (error) {
|
|
3254
|
-
if (error instanceof UnsupportedCheckError) throw arbacHttpError(403, "
|
|
3254
|
+
if (error instanceof UnsupportedCheckError) throw arbacHttpError(403, "ARBAC_ROW_OUT_OF_SCOPE");
|
|
3255
3255
|
throw error;
|
|
3256
3256
|
}
|
|
3257
3257
|
const fields = isPatchAction(ctx.action) ? checkFields(check) : void 0;
|
|
3258
3258
|
const images = fields ? (await ctx.currentAll()).map((current, i) => current ? patchPostImage(current, ctx.rows[i], fields) : void 0) : ctx.rows;
|
|
3259
|
-
for (const image of images) if (!image || !predicate(image)) throw arbacHttpError(403, "
|
|
3259
|
+
for (const image of images) if (!image || !predicate(image)) throw arbacHttpError(403, "ARBAC_ROW_OUT_OF_SCOPE");
|
|
3260
3260
|
}
|
|
3261
3261
|
/** WITH CHECK after the write (inside the transaction — a throw rolls back). */
|
|
3262
3262
|
async function assertWrittenRowsInCheck(ctx, check) {
|
|
3263
3263
|
if (ctx.filters.length === 0) return;
|
|
3264
3264
|
const written = ctx.filters.length === 1 ? ctx.filters[0] : { $or: [...ctx.filters] };
|
|
3265
|
-
if (await ctx.count({ $and: [written, check] }) !== ctx.filters.length) throw arbacHttpError(403, "
|
|
3265
|
+
if (await ctx.count({ $and: [written, check] }) !== ctx.filters.length) throw arbacHttpError(403, "ARBAC_ROW_OUT_OF_SCOPE");
|
|
3266
3266
|
}
|
|
3267
3267
|
/**
|
|
3268
3268
|
* The in-transaction write guard of the ARBAC DB layer — the body of
|
|
@@ -3286,7 +3286,7 @@ async function guardArbacWrite(ctx, scopes, table, refs, ignoreConflicts = false
|
|
|
3286
3286
|
const eff = effectiveScope(scopes);
|
|
3287
3287
|
if (eff.filter && hasPreImage(ctx.action)) await assertIdFiltersInScope(table, ctx.rows.map((_row, i) => {
|
|
3288
3288
|
const filter = ctx.filterFor(i);
|
|
3289
|
-
if (!filter) throw
|
|
3289
|
+
if (!filter) throw notFoundError();
|
|
3290
3290
|
return filter;
|
|
3291
3291
|
}), eff.filter);
|
|
3292
3292
|
await assertRefsInScope(ctx, scopes, refs);
|
|
@@ -3294,7 +3294,7 @@ async function guardArbacWrite(ctx, scopes, table, refs, ignoreConflicts = false
|
|
|
3294
3294
|
if (!table.getAdapter().isInTransaction()) await assertCheckBeforeWrite(ctx, eff.check);
|
|
3295
3295
|
else if (!hasPreImage(ctx.action)) {
|
|
3296
3296
|
const verdict = insertRowsVerdict(ctx.rows, eff.check);
|
|
3297
|
-
if (verdict === false || verdict === void 0 && ignoreConflicts) throw arbacHttpError(403, "
|
|
3297
|
+
if (verdict === false || verdict === void 0 && ignoreConflicts) throw arbacHttpError(403, "ARBAC_ROW_OUT_OF_SCOPE");
|
|
3298
3298
|
}
|
|
3299
3299
|
}
|
|
3300
3300
|
/**
|
|
@@ -3314,7 +3314,7 @@ async function guardArbacRemove(ctx, scopes, table) {
|
|
|
3314
3314
|
if (await table.count({
|
|
3315
3315
|
filter: { $and: [ctx.filter, filter] },
|
|
3316
3316
|
controls: {}
|
|
3317
|
-
}) === 0) throw
|
|
3317
|
+
}) === 0) throw notFoundError();
|
|
3318
3318
|
}
|
|
3319
3319
|
// src/db/as-arbac-db-controller.ts
|
|
3320
3320
|
let AsArbacDbController = class AsArbacDbController extends AsDbController {
|
|
@@ -3561,10 +3561,10 @@ function enforceControlsPolicy(policy, controls) {
|
|
|
3561
3561
|
const used = controls[key];
|
|
3562
3562
|
if (used === void 0 || used === null) continue;
|
|
3563
3563
|
if (Array.isArray(used) && used.length === 0 && key !== "$groupBy") continue;
|
|
3564
|
-
if (gate === false) throw arbacHttpError(403, "
|
|
3564
|
+
if (gate === false) throw arbacHttpError(403, "ARBAC_CONTROL_NOT_ALLOWED", { control: key });
|
|
3565
3565
|
if (Array.isArray(gate)) {
|
|
3566
3566
|
const usedValues = extractUsedControlValues(key, used, controls);
|
|
3567
|
-
for (const v of usedValues) if (!gate.includes(v)) throw arbacHttpError(403, "
|
|
3567
|
+
for (const v of usedValues) if (!gate.includes(v)) throw arbacHttpError(403, "ARBAC_CONTROL_VALUE_NOT_ALLOWED", {
|
|
3568
3568
|
control: key,
|
|
3569
3569
|
value: v
|
|
3570
3570
|
});
|
|
@@ -3611,7 +3611,13 @@ function extractUsedControlValues(key, value, controls) {
|
|
|
3611
3611
|
}
|
|
3612
3612
|
return [];
|
|
3613
3613
|
}
|
|
3614
|
-
/**
|
|
3614
|
+
/**
|
|
3615
|
+
* The columns a two-level aggregate's inner level (`$from`) yields: its
|
|
3616
|
+
* `$groupBy` keys and its computed `$select` columns by output name — the
|
|
3617
|
+
* `$as`, else uniqu's default alias (`count(*)` → `count_star`, as moost-db
|
|
3618
|
+
* and the db core name them). The outer level reads only these, so a name
|
|
3619
|
+
* here is the inner column even when a table field has the same name.
|
|
3620
|
+
*/
|
|
3615
3621
|
function innerColumns(from) {
|
|
3616
3622
|
if (!from || typeof from !== "object" || Array.isArray(from)) return void 0;
|
|
3617
3623
|
const { $groupBy, $select } = from;
|
|
@@ -3620,8 +3626,9 @@ function innerColumns(from) {
|
|
|
3620
3626
|
for (const k of $groupBy) if (typeof k === "string") out.add(k);
|
|
3621
3627
|
}
|
|
3622
3628
|
if (Array.isArray($select)) for (const entry of $select) {
|
|
3623
|
-
|
|
3624
|
-
|
|
3629
|
+
if (entry === null || typeof entry !== "object") continue;
|
|
3630
|
+
const alias = resolveAlias(entry);
|
|
3631
|
+
if (typeof alias === "string" && alias) out.add(alias);
|
|
3625
3632
|
}
|
|
3626
3633
|
return out;
|
|
3627
3634
|
}
|
|
@@ -3655,7 +3662,7 @@ function remapUniquUrlSyntaxError(err) {
|
|
|
3655
3662
|
if (!(err instanceof SyntaxError)) return err;
|
|
3656
3663
|
const msg = err.message;
|
|
3657
3664
|
if (!/at (?:pos )?\d+/.test(msg)) return err;
|
|
3658
|
-
return arbacHttpError(400, "
|
|
3665
|
+
return arbacHttpError(400, "ARBAC_INVALID_QUERY_STRING", { error: msg });
|
|
3659
3666
|
}
|
|
3660
3667
|
// src/db/as-arbac-db-readable-controller.ts
|
|
3661
3668
|
let AsArbacDbReadableController = class AsArbacDbReadableController extends AsDbReadableController {
|
|
@@ -3950,7 +3957,7 @@ async function useArbacDbScope() {
|
|
|
3950
3957
|
isFieldVisible,
|
|
3951
3958
|
scope
|
|
3952
3959
|
});
|
|
3953
|
-
if (!f) throw
|
|
3960
|
+
if (!f) throw notFoundError();
|
|
3954
3961
|
return f;
|
|
3955
3962
|
})), eff.filter);
|
|
3956
3963
|
},
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aooth/arbac-moost",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.6",
|
|
4
4
|
"description": "Moost RBAC integration for aoothjs (migrated from @moostjs/arbac)",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"abac",
|
|
@@ -61,29 +61,29 @@
|
|
|
61
61
|
},
|
|
62
62
|
"dependencies": {
|
|
63
63
|
"@uniqu/core": "^0.2.1",
|
|
64
|
-
"@aooth/
|
|
65
|
-
"@aooth/arbac
|
|
66
|
-
"@aooth/
|
|
64
|
+
"@aooth/user": "0.2.6",
|
|
65
|
+
"@aooth/arbac": "0.2.6",
|
|
66
|
+
"@aooth/arbac-core": "0.2.6"
|
|
67
67
|
},
|
|
68
68
|
"devDependencies": {
|
|
69
|
-
"@atscript/core": "^0.2.
|
|
70
|
-
"@atscript/db": "^0.1.
|
|
71
|
-
"@atscript/db-sql-tools": "^0.1.
|
|
72
|
-
"@atscript/db-sqlite": "^0.1.
|
|
73
|
-
"@atscript/moost-db": "^0.1.
|
|
74
|
-
"@atscript/typescript": "^0.2.
|
|
75
|
-
"@moostjs/event-http": "^0.7.
|
|
69
|
+
"@atscript/core": "^0.2.9",
|
|
70
|
+
"@atscript/db": "^0.1.174",
|
|
71
|
+
"@atscript/db-sql-tools": "^0.1.174",
|
|
72
|
+
"@atscript/db-sqlite": "^0.1.174",
|
|
73
|
+
"@atscript/moost-db": "^0.1.174",
|
|
74
|
+
"@atscript/typescript": "^0.2.9",
|
|
75
|
+
"@moostjs/event-http": "^0.7.4",
|
|
76
76
|
"@types/better-sqlite3": "^7.6.13",
|
|
77
77
|
"better-sqlite3": "^12.6.2",
|
|
78
|
-
"unplugin-atscript": "^0.2.
|
|
78
|
+
"unplugin-atscript": "^0.2.9"
|
|
79
79
|
},
|
|
80
80
|
"peerDependencies": {
|
|
81
|
-
"@atscript/db": "^0.1.
|
|
82
|
-
"@atscript/moost-db": "^0.1.
|
|
83
|
-
"@atscript/typescript": "^0.2.
|
|
84
|
-
"@moostjs/event-http": "^0.7.
|
|
81
|
+
"@atscript/db": "^0.1.174",
|
|
82
|
+
"@atscript/moost-db": "^0.1.174",
|
|
83
|
+
"@atscript/typescript": "^0.2.9",
|
|
84
|
+
"@moostjs/event-http": "^0.7.4",
|
|
85
85
|
"@wooksjs/event-core": "^0.8.1",
|
|
86
|
-
"moost": "^0.7.
|
|
86
|
+
"moost": "^0.7.4"
|
|
87
87
|
},
|
|
88
88
|
"peerDependenciesMeta": {
|
|
89
89
|
"@atscript/db": {
|