@terreno/admin-backend 57.1.0 → 57.3.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/.ai/guidelines/core.md +12 -4
- package/.claude/CLAUDE.local.md +17 -2
- package/.cursor/rules/00-root.mdc +17 -2
- package/.github/copilot-instructions.md +17 -2
- package/AGENTS.md +17 -2
- package/README.md +3 -1
- package/dist/adminApp.d.ts +15 -1
- package/dist/adminApp.js +406 -175
- package/dist/aggregateAdmin.js +5 -1
- package/dist/documentStorageApp.js +1 -4
- package/dist/index.d.ts +1 -1
- package/dist/index.js +2 -1
- package/dist/resolvedAdminModel.js +1 -0
- package/package.json +5 -5
- package/src/adminApp.models.test.ts +429 -4
- package/src/adminApp.ts +285 -121
- package/src/adminUiV2.test.ts +71 -1
- package/src/aggregateAdmin.ts +6 -4
- package/src/documentStorageApp.ts +1 -3
- package/src/index.ts +1 -1
- package/src/resolvedAdminModel.ts +1 -0
package/dist/aggregateAdmin.js
CHANGED
|
@@ -51,6 +51,7 @@ var contributionScriptToConfig = function (script) { return ({
|
|
|
51
51
|
runner: script.runner,
|
|
52
52
|
}); };
|
|
53
53
|
var contributionScreenToConfig = function (screen) { return ({
|
|
54
|
+
adminAccess: screen.adminAccess,
|
|
54
55
|
displayName: screen.displayName,
|
|
55
56
|
name: screen.name,
|
|
56
57
|
}); };
|
|
@@ -180,7 +181,10 @@ var aggregateAdminContributions = function (_a) {
|
|
|
180
181
|
var path = registered.routePath;
|
|
181
182
|
var existingRegistered = registeredByPath.get(path);
|
|
182
183
|
if (existingRegistered) {
|
|
183
|
-
throw new
|
|
184
|
+
throw new api_1.APIError({
|
|
185
|
+
status: 500,
|
|
186
|
+
title: "Duplicate admin modelRouter routePath \"".concat(path, "\" (").concat(existingRegistered.sourceLabel, " and ").concat(registered.sourceLabel, ")"),
|
|
187
|
+
});
|
|
184
188
|
}
|
|
185
189
|
registeredByPath.set(path, registered);
|
|
186
190
|
var existing = modelsByPath.get(path);
|
|
@@ -223,10 +223,7 @@ var DocumentStorageApp = /** @class */ (function () {
|
|
|
223
223
|
});
|
|
224
224
|
}); })], false));
|
|
225
225
|
// POST basePath/ — Upload a file
|
|
226
|
-
app.post.apply(app, __spreadArray(__spreadArray(["".concat(basePath, "/")], __read(adminGuard), false), [
|
|
227
|
-
// noExplicitAny: multer's RequestHandler type is incompatible with Express 5's middleware signature.
|
|
228
|
-
// biome-ignore lint/suspicious/noExplicitAny: multer's RequestHandler type is incompatible with Express 5's middleware signature.
|
|
229
|
-
upload.single("file"),
|
|
226
|
+
app.post.apply(app, __spreadArray(__spreadArray(["".concat(basePath, "/")], __read(adminGuard), false), [upload.single("file"),
|
|
230
227
|
(0, api_1.asyncHandler)(function (req, res) { return __awaiter(_this, void 0, void 0, function () {
|
|
231
228
|
var file, targetPrefix, sanitizedFilename, gcsPath, gcsFile, documentFile;
|
|
232
229
|
var _a;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export type { AdminChangeEvent, AdminChangeType, AdminConfig, AdminContribution, AdminCustomScreen, AdminFieldset, AdminFilter, AdminFilterBoolean, AdminFilterChoice, AdminFilterDateRange, AdminFilterKind, AdminFilterRef, AdminFilterText, AdminHomeConfig, AdminHomeSlots, AdminHomeWidgetContribution, AdminModelContribution, AdminModelPermissions, AdminScriptContribution, TerrenoAppAdminEvent, } from "@terreno/api";
|
|
2
2
|
export type { AdminAuditEvent, AdminCustomScreenConfig, AdminFieldOverride, AdminModelConfig, AdminOptions, AdminScriptConfig, } from "./adminApp";
|
|
3
|
-
export { AdminApp } from "./adminApp";
|
|
3
|
+
export { AdminApp, adminOwnedBy } from "./adminApp";
|
|
4
4
|
export * from "./adminUiV2";
|
|
5
5
|
export type { AggregatedAdminContributions } from "./aggregateAdmin";
|
|
6
6
|
export { aggregateAdminContributions, aggregateFromTerrenoApp, collectPluginAdminContributions, collectRegisteredAdminModels, } from "./aggregateAdmin";
|
package/dist/index.js
CHANGED
|
@@ -14,9 +14,10 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.runScriptCli = exports.normalizeAdminRoutePath = exports.resolvedModelFromAdminConfig = exports.resetLegacyDeprecationWarningsForTests = exports.convertLegacyModelConfig = exports.parseAdminListFilters = exports.DocumentStorageApp = exports.collectRegisteredAdminModels = exports.collectPluginAdminContributions = exports.aggregateFromTerrenoApp = exports.aggregateAdminContributions = exports.AdminApp = void 0;
|
|
17
|
+
exports.runScriptCli = exports.normalizeAdminRoutePath = exports.resolvedModelFromAdminConfig = exports.resetLegacyDeprecationWarningsForTests = exports.convertLegacyModelConfig = exports.parseAdminListFilters = exports.DocumentStorageApp = exports.collectRegisteredAdminModels = exports.collectPluginAdminContributions = exports.aggregateFromTerrenoApp = exports.aggregateAdminContributions = exports.adminOwnedBy = exports.AdminApp = void 0;
|
|
18
18
|
var adminApp_1 = require("./adminApp");
|
|
19
19
|
Object.defineProperty(exports, "AdminApp", { enumerable: true, get: function () { return adminApp_1.AdminApp; } });
|
|
20
|
+
Object.defineProperty(exports, "adminOwnedBy", { enumerable: true, get: function () { return adminApp_1.adminOwnedBy; } });
|
|
20
21
|
__exportStar(require("./adminUiV2"), exports);
|
|
21
22
|
var aggregateAdmin_1 = require("./aggregateAdmin");
|
|
22
23
|
Object.defineProperty(exports, "aggregateAdminContributions", { enumerable: true, get: function () { return aggregateAdmin_1.aggregateAdminContributions; } });
|
|
@@ -96,6 +96,7 @@ var resolvedModelFromAdminConfig = function (_a) {
|
|
|
96
96
|
return {
|
|
97
97
|
actions: admin.actions,
|
|
98
98
|
admin: admin,
|
|
99
|
+
adminAccess: admin.adminAccess,
|
|
99
100
|
autocompleteFields: admin.autocompleteFields,
|
|
100
101
|
bulkPatchAllowlist: admin.bulkPatchAllowlist,
|
|
101
102
|
defaultSort: defaultSort,
|
package/package.json
CHANGED
|
@@ -5,16 +5,16 @@
|
|
|
5
5
|
},
|
|
6
6
|
"dependencies": {
|
|
7
7
|
"@google-cloud/storage": "^7.15.0",
|
|
8
|
-
"@terreno/api": "57.
|
|
8
|
+
"@terreno/api": "57.3.0",
|
|
9
9
|
"express": "^5.2.1",
|
|
10
10
|
"luxon": "^3.7.2",
|
|
11
11
|
"multer": "2.2.0"
|
|
12
12
|
},
|
|
13
|
-
"description": "Admin panel backend plugin for @terreno/api",
|
|
13
|
+
"description": "Admin panel backend plugin for @terreno/api — part of Terreno, the batteries-included TypeScript framework for universal apps.",
|
|
14
14
|
"devDependencies": {
|
|
15
15
|
"@biomejs/biome": "^2.3.6",
|
|
16
|
-
"@terreno/test": "57.
|
|
17
|
-
"@types/bun": "^1.
|
|
16
|
+
"@terreno/test": "57.3.0",
|
|
17
|
+
"@types/bun": "^1.4.0",
|
|
18
18
|
"@types/express": "^5.0.6",
|
|
19
19
|
"@types/luxon": "^3.7.1",
|
|
20
20
|
"@types/multer": "^1.4.12",
|
|
@@ -55,5 +55,5 @@
|
|
|
55
55
|
"test:coverage": "bun run ../scripts/check-coverage.ts"
|
|
56
56
|
},
|
|
57
57
|
"types": "dist/index.d.ts",
|
|
58
|
-
"version": "57.
|
|
58
|
+
"version": "57.3.0"
|
|
59
59
|
}
|
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import {afterEach, beforeEach, describe, expect, it} from "bun:test";
|
|
2
2
|
import {
|
|
3
|
+
ADMIN_MODEL_ACCESS,
|
|
3
4
|
addAuthRoutes,
|
|
4
5
|
apiErrorMiddleware,
|
|
5
6
|
apiUnauthorizedMiddleware,
|
|
6
7
|
BackgroundTask,
|
|
7
8
|
createAccess,
|
|
9
|
+
findOneOrNoneFor,
|
|
8
10
|
modelRouter,
|
|
9
11
|
Permissions,
|
|
10
12
|
setupAuth,
|
|
@@ -22,6 +24,7 @@ import {
|
|
|
22
24
|
setupDb,
|
|
23
25
|
UserModel,
|
|
24
26
|
} from "@terreno/api/testing";
|
|
27
|
+
import {assert} from "chai";
|
|
25
28
|
import type express from "express";
|
|
26
29
|
import mongoose from "mongoose";
|
|
27
30
|
import supertest from "supertest";
|
|
@@ -317,11 +320,9 @@ describe("AdminApp /admin/config", () => {
|
|
|
317
320
|
expect(res.body.models[0].defaultSort).toBe("name");
|
|
318
321
|
});
|
|
319
322
|
|
|
320
|
-
it("
|
|
323
|
+
it("does not inject version config into the custom screen navigation", async () => {
|
|
321
324
|
const res = await adminAgent.get("/admin/config").expect(200);
|
|
322
|
-
expect(res.body.customScreens).toEqual([
|
|
323
|
-
{displayName: "Version Config", name: "version-config"},
|
|
324
|
-
]);
|
|
325
|
+
expect(res.body.customScreens).toEqual([]);
|
|
325
326
|
});
|
|
326
327
|
|
|
327
328
|
it("normalizes home.slots so recentActivity is last in sidebar", async () => {
|
|
@@ -336,6 +337,430 @@ describe("AdminApp /admin/config", () => {
|
|
|
336
337
|
expect(res.body.home.title).toBe("Ops");
|
|
337
338
|
expect(res.body.home.slots.sidebar).toEqual(["versionConfig", "recentActivity"]);
|
|
338
339
|
});
|
|
340
|
+
|
|
341
|
+
it("filters screens and platform tools from the caller's RBAC grants", async () => {
|
|
342
|
+
const accessControl = createAccess({
|
|
343
|
+
connection: mongoose.connection,
|
|
344
|
+
resolvePermissions: async () => ({
|
|
345
|
+
admin: ["access"],
|
|
346
|
+
adminFood: ["read"],
|
|
347
|
+
adminScreen: ["allowed"],
|
|
348
|
+
}),
|
|
349
|
+
statements: {
|
|
350
|
+
...terrenoStatements,
|
|
351
|
+
adminFood: ADMIN_MODEL_ACCESS,
|
|
352
|
+
adminScreen: ["allowed", "hidden"],
|
|
353
|
+
},
|
|
354
|
+
});
|
|
355
|
+
const localApp = buildApp([{...foodModelConfig, adminAccess: {}}], {
|
|
356
|
+
accessControl,
|
|
357
|
+
customScreens: [
|
|
358
|
+
{
|
|
359
|
+
adminAccess: {action: "allowed", resource: "adminScreen"},
|
|
360
|
+
displayName: "Allowed",
|
|
361
|
+
name: "allowed",
|
|
362
|
+
},
|
|
363
|
+
{
|
|
364
|
+
adminAccess: {action: "hidden", resource: "adminScreen"},
|
|
365
|
+
displayName: "Hidden",
|
|
366
|
+
name: "hidden",
|
|
367
|
+
},
|
|
368
|
+
],
|
|
369
|
+
scripts: [
|
|
370
|
+
{
|
|
371
|
+
description: "Seed",
|
|
372
|
+
name: "seed",
|
|
373
|
+
runner: async () => ({results: [], success: true}),
|
|
374
|
+
},
|
|
375
|
+
],
|
|
376
|
+
});
|
|
377
|
+
const agent = await authAsUser(localApp, "admin");
|
|
378
|
+
|
|
379
|
+
const response = await agent.get("/admin/config").expect(200);
|
|
380
|
+
|
|
381
|
+
assert.deepEqual(
|
|
382
|
+
response.body.models.map((model: {name: string}) => model.name),
|
|
383
|
+
["Food"]
|
|
384
|
+
);
|
|
385
|
+
assert.deepInclude(response.body.models[0].permissions, {
|
|
386
|
+
create: false,
|
|
387
|
+
delete: false,
|
|
388
|
+
update: false,
|
|
389
|
+
});
|
|
390
|
+
assert.deepEqual(response.body.customScreens, [{displayName: "Allowed", name: "allowed"}]);
|
|
391
|
+
assert.deepEqual(response.body.platformTools, {
|
|
392
|
+
configuration: false,
|
|
393
|
+
roles: false,
|
|
394
|
+
runScripts: false,
|
|
395
|
+
scripts: false,
|
|
396
|
+
version: false,
|
|
397
|
+
viewScripts: false,
|
|
398
|
+
});
|
|
399
|
+
assert.deepEqual(response.body.scripts, []);
|
|
400
|
+
});
|
|
401
|
+
|
|
402
|
+
it("enforces writeOwned with the configured ownership callback", async () => {
|
|
403
|
+
const accessControl = createAccess({
|
|
404
|
+
connection: mongoose.connection,
|
|
405
|
+
resolvePermissions: async () => ({
|
|
406
|
+
admin: ["access"],
|
|
407
|
+
adminFood: ["read", "writeOwned"],
|
|
408
|
+
}),
|
|
409
|
+
statements: {...terrenoStatements, adminFood: ADMIN_MODEL_ACCESS},
|
|
410
|
+
});
|
|
411
|
+
const localApp = buildApp(
|
|
412
|
+
[
|
|
413
|
+
{
|
|
414
|
+
...foodModelConfig,
|
|
415
|
+
adminAccess: {
|
|
416
|
+
isOwned: ({instance, user}) =>
|
|
417
|
+
String((instance as {ownerId?: unknown}).ownerId) === String(user.id),
|
|
418
|
+
},
|
|
419
|
+
},
|
|
420
|
+
],
|
|
421
|
+
{accessControl}
|
|
422
|
+
);
|
|
423
|
+
const agent = await authAsUser(localApp, "admin");
|
|
424
|
+
const actor = await findOneOrNoneFor(UserModel, {email: "admin@example.com"});
|
|
425
|
+
assert.exists(actor);
|
|
426
|
+
const owned = await FoodModel.create({calories: 1, name: "Owned", ownerId: actor?._id});
|
|
427
|
+
const other = await FoodModel.create({
|
|
428
|
+
calories: 2,
|
|
429
|
+
name: "Other",
|
|
430
|
+
ownerId: new mongoose.Types.ObjectId(),
|
|
431
|
+
});
|
|
432
|
+
|
|
433
|
+
await agent.patch(`/admin/foods/${owned._id}`).send({calories: 3}).expect(200);
|
|
434
|
+
await agent.patch(`/admin/foods/${other._id}`).send({calories: 4}).expect(403);
|
|
435
|
+
const ownedRead = await agent.get(`/admin/foods/${owned._id}`).expect(200);
|
|
436
|
+
const otherRead = await agent.get(`/admin/foods/${other._id}`).expect(200);
|
|
437
|
+
assert.deepEqual(ownedRead.body.data._adminCapabilities, {delete: true, update: true});
|
|
438
|
+
assert.deepEqual(otherRead.body.data._adminCapabilities, {delete: false, update: false});
|
|
439
|
+
await agent.delete(`/admin/foods/${other._id}`).expect(403);
|
|
440
|
+
await agent
|
|
441
|
+
.post("/admin/foods")
|
|
442
|
+
.send({calories: 5, name: "Created", ownerId: actor?._id})
|
|
443
|
+
.expect(201);
|
|
444
|
+
await agent
|
|
445
|
+
.post("/admin/foods")
|
|
446
|
+
.send({calories: 6, name: "Foreign", ownerId: new mongoose.Types.ObjectId()})
|
|
447
|
+
.expect(201);
|
|
448
|
+
await agent.post("/admin/foods").send({calories: 7, name: "Unassigned"}).expect(201);
|
|
449
|
+
});
|
|
450
|
+
|
|
451
|
+
it("supports completely custom per-model authorization", async () => {
|
|
452
|
+
const accessControl = createAccess({
|
|
453
|
+
connection: mongoose.connection,
|
|
454
|
+
resolvePermissions: async () => ({admin: ["access"]}),
|
|
455
|
+
statements: terrenoStatements,
|
|
456
|
+
});
|
|
457
|
+
const localApp = buildApp(
|
|
458
|
+
[
|
|
459
|
+
{
|
|
460
|
+
...foodModelConfig,
|
|
461
|
+
adminAccess: {
|
|
462
|
+
authorize: ({action}) => action === "list" || action === "read",
|
|
463
|
+
},
|
|
464
|
+
},
|
|
465
|
+
],
|
|
466
|
+
{accessControl}
|
|
467
|
+
);
|
|
468
|
+
const agent = await authAsUser(localApp, "admin");
|
|
469
|
+
|
|
470
|
+
const config = await agent.get("/admin/config").expect(200);
|
|
471
|
+
assert.deepInclude(config.body.models[0].permissions, {
|
|
472
|
+
create: false,
|
|
473
|
+
delete: false,
|
|
474
|
+
update: false,
|
|
475
|
+
});
|
|
476
|
+
await agent.get("/admin/foods").expect(200);
|
|
477
|
+
await agent.post("/admin/foods").send({calories: 1, name: "Denied"}).expect(405);
|
|
478
|
+
});
|
|
479
|
+
});
|
|
480
|
+
|
|
481
|
+
describe("AdminApp adminAccess role matrix", () => {
|
|
482
|
+
const ADMIN_EMAIL = "admin@example.com";
|
|
483
|
+
const MEMBER_EMAIL = "notAdmin@example.com";
|
|
484
|
+
const matrixStatements = {
|
|
485
|
+
...terrenoStatements,
|
|
486
|
+
adminFood: ADMIN_MODEL_ACCESS,
|
|
487
|
+
adminScreen: ["showcase"],
|
|
488
|
+
};
|
|
489
|
+
|
|
490
|
+
const buildMatrixApp = (
|
|
491
|
+
grantsByEmail: Record<string, Record<string, string[]>>
|
|
492
|
+
): express.Application => {
|
|
493
|
+
const accessControl = createAccess({
|
|
494
|
+
connection: mongoose.connection,
|
|
495
|
+
resolvePermissions: async ({user}) => {
|
|
496
|
+
const email = "email" in user && typeof user.email === "string" ? user.email : "";
|
|
497
|
+
return grantsByEmail[email] ?? {};
|
|
498
|
+
},
|
|
499
|
+
statements: matrixStatements,
|
|
500
|
+
});
|
|
501
|
+
return buildApp(
|
|
502
|
+
[
|
|
503
|
+
{
|
|
504
|
+
...foodModelConfig,
|
|
505
|
+
adminAccess: {
|
|
506
|
+
isOwned: ({instance, user}) => {
|
|
507
|
+
return String((instance as {ownerId?: unknown}).ownerId) === String(user.id);
|
|
508
|
+
},
|
|
509
|
+
},
|
|
510
|
+
},
|
|
511
|
+
],
|
|
512
|
+
{
|
|
513
|
+
accessControl,
|
|
514
|
+
customScreens: [
|
|
515
|
+
{
|
|
516
|
+
adminAccess: {action: "showcase", resource: "adminScreen"},
|
|
517
|
+
displayName: "Showcase",
|
|
518
|
+
name: "showcase",
|
|
519
|
+
},
|
|
520
|
+
],
|
|
521
|
+
scripts: [
|
|
522
|
+
{
|
|
523
|
+
description: "Seed",
|
|
524
|
+
name: "seed",
|
|
525
|
+
runner: async () => ({results: [], success: true}),
|
|
526
|
+
},
|
|
527
|
+
],
|
|
528
|
+
}
|
|
529
|
+
);
|
|
530
|
+
};
|
|
531
|
+
|
|
532
|
+
beforeEach(async () => {
|
|
533
|
+
await setupDb();
|
|
534
|
+
});
|
|
535
|
+
|
|
536
|
+
afterEach(async () => {
|
|
537
|
+
await FoodModel.deleteMany({});
|
|
538
|
+
});
|
|
539
|
+
|
|
540
|
+
it("denies the admin shell when the caller lacks admin:access", async () => {
|
|
541
|
+
const app = buildMatrixApp({
|
|
542
|
+
[ADMIN_EMAIL]: {admin: ["access"], adminFood: ["read"]},
|
|
543
|
+
[MEMBER_EMAIL]: {},
|
|
544
|
+
});
|
|
545
|
+
const member = await authAsUser(app, "notAdmin");
|
|
546
|
+
await member.get("/admin/config").expect(403);
|
|
547
|
+
await member.get("/admin/foods").expect(405);
|
|
548
|
+
});
|
|
549
|
+
|
|
550
|
+
it("does not open the admin page with other admin permissions alone", async () => {
|
|
551
|
+
const app = buildMatrixApp({
|
|
552
|
+
[ADMIN_EMAIL]: {admin: ["runScripts", "viewBackgroundTasks"], adminFood: ["read", "write"]},
|
|
553
|
+
});
|
|
554
|
+
const agent = await authAsUser(app, "admin");
|
|
555
|
+
await agent.get("/admin/config").expect(403);
|
|
556
|
+
await agent.get("/admin/foods").expect(405);
|
|
557
|
+
});
|
|
558
|
+
|
|
559
|
+
it("hides models and mutations when the caller only has admin:access", async () => {
|
|
560
|
+
const app = buildMatrixApp({
|
|
561
|
+
[ADMIN_EMAIL]: {admin: ["access"]},
|
|
562
|
+
});
|
|
563
|
+
const agent = await authAsUser(app, "admin");
|
|
564
|
+
const config = await agent.get("/admin/config").expect(200);
|
|
565
|
+
assert.deepEqual(config.body.models, []);
|
|
566
|
+
assert.deepEqual(config.body.customScreens, []);
|
|
567
|
+
await agent.get("/admin/foods").expect(405);
|
|
568
|
+
await agent.post("/admin/foods").send({calories: 1, name: "Nope"}).expect(405);
|
|
569
|
+
});
|
|
570
|
+
|
|
571
|
+
it("allows reads and denies writes for a read-only role", async () => {
|
|
572
|
+
const app = buildMatrixApp({
|
|
573
|
+
[ADMIN_EMAIL]: {admin: ["access"], adminFood: ["read"]},
|
|
574
|
+
});
|
|
575
|
+
const agent = await authAsUser(app, "admin");
|
|
576
|
+
const actor = await findOneOrNoneFor(UserModel, {email: ADMIN_EMAIL});
|
|
577
|
+
assert.exists(actor);
|
|
578
|
+
const food = await FoodModel.create({calories: 1, name: "Owned", ownerId: actor?._id});
|
|
579
|
+
|
|
580
|
+
const config = await agent.get("/admin/config").expect(200);
|
|
581
|
+
assert.deepInclude(config.body.models[0].permissions, {
|
|
582
|
+
create: false,
|
|
583
|
+
delete: false,
|
|
584
|
+
update: false,
|
|
585
|
+
});
|
|
586
|
+
await agent.get("/admin/foods").expect(200);
|
|
587
|
+
await agent.get(`/admin/foods/${food._id}`).expect(200);
|
|
588
|
+
await agent.post("/admin/foods").send({calories: 2, name: "Denied"}).expect(405);
|
|
589
|
+
await agent.patch(`/admin/foods/${food._id}`).send({calories: 3}).expect(405);
|
|
590
|
+
await agent.delete(`/admin/foods/${food._id}`).expect(405);
|
|
591
|
+
});
|
|
592
|
+
|
|
593
|
+
it("allows full CRUD for a write role, including records the caller does not own", async () => {
|
|
594
|
+
const app = buildMatrixApp({
|
|
595
|
+
[ADMIN_EMAIL]: {admin: ["access"], adminFood: ["read", "write"]},
|
|
596
|
+
});
|
|
597
|
+
const agent = await authAsUser(app, "admin");
|
|
598
|
+
const other = await FoodModel.create({
|
|
599
|
+
calories: 1,
|
|
600
|
+
name: "Other",
|
|
601
|
+
ownerId: new mongoose.Types.ObjectId(),
|
|
602
|
+
});
|
|
603
|
+
|
|
604
|
+
const config = await agent.get("/admin/config").expect(200);
|
|
605
|
+
assert.deepInclude(config.body.models[0].permissions, {
|
|
606
|
+
create: true,
|
|
607
|
+
delete: true,
|
|
608
|
+
update: true,
|
|
609
|
+
});
|
|
610
|
+
await agent.post("/admin/foods").send({calories: 2, name: "Created"}).expect(201);
|
|
611
|
+
const patched = await agent.patch(`/admin/foods/${other._id}`).send({calories: 9}).expect(200);
|
|
612
|
+
assert.equal(patched.body.data.calories, 9);
|
|
613
|
+
await agent.delete(`/admin/foods/${other._id}`).expect(204);
|
|
614
|
+
});
|
|
615
|
+
|
|
616
|
+
it("scopes writeOwned updates and deletes to owned records for that role", async () => {
|
|
617
|
+
const app = buildMatrixApp({
|
|
618
|
+
[ADMIN_EMAIL]: {admin: ["access"], adminFood: ["read", "writeOwned"]},
|
|
619
|
+
});
|
|
620
|
+
const agent = await authAsUser(app, "admin");
|
|
621
|
+
const actor = await findOneOrNoneFor(UserModel, {email: ADMIN_EMAIL});
|
|
622
|
+
assert.exists(actor);
|
|
623
|
+
const owned = await FoodModel.create({calories: 1, name: "Owned", ownerId: actor?._id});
|
|
624
|
+
const other = await FoodModel.create({
|
|
625
|
+
calories: 2,
|
|
626
|
+
name: "Other",
|
|
627
|
+
ownerId: new mongoose.Types.ObjectId(),
|
|
628
|
+
});
|
|
629
|
+
|
|
630
|
+
const ownedRead = await agent.get(`/admin/foods/${owned._id}`).expect(200);
|
|
631
|
+
const otherRead = await agent.get(`/admin/foods/${other._id}`).expect(200);
|
|
632
|
+
assert.deepEqual(ownedRead.body.data._adminCapabilities, {delete: true, update: true});
|
|
633
|
+
assert.deepEqual(otherRead.body.data._adminCapabilities, {delete: false, update: false});
|
|
634
|
+
await agent.patch(`/admin/foods/${owned._id}`).send({calories: 4}).expect(200);
|
|
635
|
+
await agent.patch(`/admin/foods/${other._id}`).send({calories: 5}).expect(403);
|
|
636
|
+
await agent.delete(`/admin/foods/${other._id}`).expect(403);
|
|
637
|
+
await agent.delete(`/admin/foods/${owned._id}`).expect(204);
|
|
638
|
+
await agent.post("/admin/foods").send({calories: 6, name: "Unassigned"}).expect(201);
|
|
639
|
+
});
|
|
640
|
+
|
|
641
|
+
it("applies different adminAccess grants to different users on the same app", async () => {
|
|
642
|
+
const app = buildMatrixApp({
|
|
643
|
+
[ADMIN_EMAIL]: {admin: ["access"], adminFood: ["read", "write"]},
|
|
644
|
+
[MEMBER_EMAIL]: {admin: ["access"], adminFood: ["read"]},
|
|
645
|
+
});
|
|
646
|
+
const writer = await authAsUser(app, "admin");
|
|
647
|
+
const reader = await authAsUser(app, "notAdmin");
|
|
648
|
+
const writerUser = await findOneOrNoneFor(UserModel, {email: ADMIN_EMAIL});
|
|
649
|
+
assert.exists(writerUser);
|
|
650
|
+
const food = await FoodModel.create({calories: 1, name: "Shared", ownerId: writerUser?._id});
|
|
651
|
+
|
|
652
|
+
const writerConfig = await writer.get("/admin/config").expect(200);
|
|
653
|
+
const readerConfig = await reader.get("/admin/config").expect(200);
|
|
654
|
+
assert.deepInclude(writerConfig.body.models[0].permissions, {
|
|
655
|
+
create: true,
|
|
656
|
+
delete: true,
|
|
657
|
+
update: true,
|
|
658
|
+
});
|
|
659
|
+
assert.deepInclude(readerConfig.body.models[0].permissions, {
|
|
660
|
+
create: false,
|
|
661
|
+
delete: false,
|
|
662
|
+
update: false,
|
|
663
|
+
});
|
|
664
|
+
await writer.patch(`/admin/foods/${food._id}`).send({calories: 8}).expect(200);
|
|
665
|
+
await reader.patch(`/admin/foods/${food._id}`).send({calories: 9}).expect(405);
|
|
666
|
+
await reader.get("/admin/foods").expect(200);
|
|
667
|
+
await reader.post("/admin/foods").send({calories: 3, name: "Reader"}).expect(405);
|
|
668
|
+
});
|
|
669
|
+
|
|
670
|
+
it("exposes platform tools and custom screens only for roles that grant them", async () => {
|
|
671
|
+
const app = buildMatrixApp({
|
|
672
|
+
[ADMIN_EMAIL]: {
|
|
673
|
+
admin: ["access", "runScripts", "viewBackgroundTasks"],
|
|
674
|
+
adminScreen: ["showcase"],
|
|
675
|
+
configuration: ["read"],
|
|
676
|
+
rbac: ["read"],
|
|
677
|
+
},
|
|
678
|
+
[MEMBER_EMAIL]: {admin: ["access"], adminFood: ["read"]},
|
|
679
|
+
});
|
|
680
|
+
const operator = await authAsUser(app, "admin");
|
|
681
|
+
const reader = await authAsUser(app, "notAdmin");
|
|
682
|
+
|
|
683
|
+
const operatorConfig = await operator.get("/admin/config").expect(200);
|
|
684
|
+
const readerConfig = await reader.get("/admin/config").expect(200);
|
|
685
|
+
const operatorScreens = operatorConfig.body.customScreens.map(
|
|
686
|
+
(screen: {name: string}) => screen.name
|
|
687
|
+
);
|
|
688
|
+
const readerScreens = readerConfig.body.customScreens.map(
|
|
689
|
+
(screen: {name: string}) => screen.name
|
|
690
|
+
);
|
|
691
|
+
assert.include(operatorScreens, "showcase");
|
|
692
|
+
assert.notInclude(readerScreens, "showcase");
|
|
693
|
+
assert.deepEqual(operatorConfig.body.platformTools, {
|
|
694
|
+
configuration: true,
|
|
695
|
+
roles: true,
|
|
696
|
+
runScripts: true,
|
|
697
|
+
scripts: true,
|
|
698
|
+
version: true,
|
|
699
|
+
viewScripts: true,
|
|
700
|
+
});
|
|
701
|
+
assert.deepEqual(
|
|
702
|
+
operatorConfig.body.scripts.map((script: {name: string}) => script.name),
|
|
703
|
+
["seed"]
|
|
704
|
+
);
|
|
705
|
+
assert.deepEqual(readerConfig.body.customScreens, []);
|
|
706
|
+
assert.deepEqual(readerConfig.body.platformTools, {
|
|
707
|
+
configuration: false,
|
|
708
|
+
roles: false,
|
|
709
|
+
runScripts: false,
|
|
710
|
+
scripts: false,
|
|
711
|
+
version: false,
|
|
712
|
+
viewScripts: false,
|
|
713
|
+
});
|
|
714
|
+
assert.deepEqual(readerConfig.body.scripts, []);
|
|
715
|
+
assert.deepEqual(
|
|
716
|
+
readerConfig.body.models.map((model: {name: string}) => model.name),
|
|
717
|
+
["Food"]
|
|
718
|
+
);
|
|
719
|
+
});
|
|
720
|
+
|
|
721
|
+
it("lets authorize allow writes for one user and deny them for another", async () => {
|
|
722
|
+
const accessControl = createAccess({
|
|
723
|
+
connection: mongoose.connection,
|
|
724
|
+
resolvePermissions: async () => ({admin: ["access"]}),
|
|
725
|
+
statements: terrenoStatements,
|
|
726
|
+
});
|
|
727
|
+
const app = buildApp(
|
|
728
|
+
[
|
|
729
|
+
{
|
|
730
|
+
...foodModelConfig,
|
|
731
|
+
adminAccess: {
|
|
732
|
+
authorize: ({action, user}) => {
|
|
733
|
+
const email =
|
|
734
|
+
user && "email" in user && typeof user.email === "string" ? user.email : "";
|
|
735
|
+
if (email === ADMIN_EMAIL) {
|
|
736
|
+
return true;
|
|
737
|
+
}
|
|
738
|
+
return action === "list" || action === "read";
|
|
739
|
+
},
|
|
740
|
+
},
|
|
741
|
+
},
|
|
742
|
+
],
|
|
743
|
+
{accessControl}
|
|
744
|
+
);
|
|
745
|
+
const writer = await authAsUser(app, "admin");
|
|
746
|
+
const reader = await authAsUser(app, "notAdmin");
|
|
747
|
+
|
|
748
|
+
const writerConfig = await writer.get("/admin/config").expect(200);
|
|
749
|
+
const readerConfig = await reader.get("/admin/config").expect(200);
|
|
750
|
+
assert.deepInclude(writerConfig.body.models[0].permissions, {
|
|
751
|
+
create: true,
|
|
752
|
+
delete: true,
|
|
753
|
+
update: true,
|
|
754
|
+
});
|
|
755
|
+
assert.deepInclude(readerConfig.body.models[0].permissions, {
|
|
756
|
+
create: false,
|
|
757
|
+
delete: false,
|
|
758
|
+
update: false,
|
|
759
|
+
});
|
|
760
|
+
await writer.post("/admin/foods").send({calories: 1, name: "Allowed"}).expect(201);
|
|
761
|
+
await reader.post("/admin/foods").send({calories: 1, name: "Denied"}).expect(405);
|
|
762
|
+
await reader.get("/admin/foods").expect(200);
|
|
763
|
+
});
|
|
339
764
|
});
|
|
340
765
|
|
|
341
766
|
describe("AdminApp model CRUD routes", () => {
|