@terreno/admin-backend 57.16.2 → 57.18.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/adminApp.d.ts +8 -1
- package/dist/adminApp.js +104 -36
- package/dist/adminRowScope.d.ts +26 -0
- package/dist/adminRowScope.js +115 -0
- package/dist/adminScriptJob.d.ts +7 -1
- package/dist/adminScriptJob.js +19 -2
- package/package.json +4 -4
- package/src/adminApp.models.test.ts +475 -0
- package/src/adminApp.ts +122 -41
- package/src/adminRowScope.test.ts +230 -0
- package/src/adminRowScope.ts +155 -0
- package/src/adminScriptJob.test.ts +125 -0
- package/src/adminScriptJob.ts +22 -2
- package/src/adminSyncMutationAuthMismatch.test.ts +123 -0
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import {afterEach, beforeEach, describe, expect, it, spyOn} from "bun:test";
|
|
2
2
|
import {
|
|
3
3
|
ADMIN_MODEL_ACCESS,
|
|
4
|
+
type AdminConfig,
|
|
4
5
|
AuditApp,
|
|
5
6
|
addAuthRoutes,
|
|
6
7
|
apiErrorMiddleware,
|
|
@@ -13,6 +14,7 @@ import {
|
|
|
13
14
|
modelRouter,
|
|
14
15
|
Organization,
|
|
15
16
|
orgScopedPlugin,
|
|
17
|
+
type PermissionMethod,
|
|
16
18
|
Permissions,
|
|
17
19
|
resetAuditRecorderForTests,
|
|
18
20
|
setupAuth,
|
|
@@ -2031,6 +2033,24 @@ describe("AdminApp per-model queryFilter", () => {
|
|
|
2031
2033
|
await agent.get("/admin/foods?sort=-created").expect(200);
|
|
2032
2034
|
});
|
|
2033
2035
|
|
|
2036
|
+
it("sorts by a configured sortableFields entry that is not a list or filter field", async () => {
|
|
2037
|
+
const localApp = buildApp([
|
|
2038
|
+
{
|
|
2039
|
+
displayName: "Foods",
|
|
2040
|
+
listFields: ["name"],
|
|
2041
|
+
model: FoodModel,
|
|
2042
|
+
routePath: "/foods",
|
|
2043
|
+
sortableFields: ["calories"],
|
|
2044
|
+
},
|
|
2045
|
+
]);
|
|
2046
|
+
await FoodModel.create({calories: 1, name: "Low"});
|
|
2047
|
+
await FoodModel.create({calories: 9, name: "High"});
|
|
2048
|
+
const agent = await authAsUser(localApp, "admin");
|
|
2049
|
+
const res = await agent.get("/admin/foods?sort=-calories").expect(200);
|
|
2050
|
+
expect(res.body.data.map((row: {name: string}) => row.name)).toEqual(["High", "Low"]);
|
|
2051
|
+
await agent.get("/admin/foods?sort=name").expect(400);
|
|
2052
|
+
});
|
|
2053
|
+
|
|
2034
2054
|
it("strips excludeFields from admin create and update bodies", async () => {
|
|
2035
2055
|
const localApp = buildApp([
|
|
2036
2056
|
{
|
|
@@ -2057,6 +2077,234 @@ describe("AdminApp per-model queryFilter", () => {
|
|
|
2057
2077
|
const after = await FoodModel.findById(createdId).lean();
|
|
2058
2078
|
expect(after?.calories).toBe(12);
|
|
2059
2079
|
});
|
|
2080
|
+
|
|
2081
|
+
describe("row scope on by-id routes and bulk-patch", () => {
|
|
2082
|
+
const ownerScopedConfig: AdminModelConfig = {
|
|
2083
|
+
...foodModelConfig,
|
|
2084
|
+
bulkPatchAllowlist: ["name", "calories"],
|
|
2085
|
+
queryFilter: (user, query): Record<string, unknown> => ({
|
|
2086
|
+
...(query ?? {}),
|
|
2087
|
+
ownerId: user?.id,
|
|
2088
|
+
}),
|
|
2089
|
+
};
|
|
2090
|
+
|
|
2091
|
+
let agent: TestAgent;
|
|
2092
|
+
let actorId: string;
|
|
2093
|
+
let otherOwnerId: mongoose.Types.ObjectId;
|
|
2094
|
+
|
|
2095
|
+
beforeEach(async () => {
|
|
2096
|
+
const localApp = buildApp([ownerScopedConfig]);
|
|
2097
|
+
agent = await authAsUser(localApp, "admin");
|
|
2098
|
+
const actor = await findOneOrNoneFor(UserModel, {email: "admin@example.com"});
|
|
2099
|
+
actorId = String(actor?._id);
|
|
2100
|
+
otherOwnerId = new mongoose.Types.ObjectId();
|
|
2101
|
+
});
|
|
2102
|
+
|
|
2103
|
+
it("reads, updates, and deletes rows inside the scope", async () => {
|
|
2104
|
+
const mine = await FoodModel.create({calories: 1, name: "Mine", ownerId: actorId});
|
|
2105
|
+
await agent.get(`/admin/foods/${mine._id}`).expect(200);
|
|
2106
|
+
await agent.patch(`/admin/foods/${mine._id}`).send({calories: 5}).expect(200);
|
|
2107
|
+
const bulk = await agent
|
|
2108
|
+
.post("/admin/foods/bulk-patch")
|
|
2109
|
+
.send({ids: [String(mine._id)], patch: {name: "MineBulk"}})
|
|
2110
|
+
.expect(200);
|
|
2111
|
+
expect(bulk.body.updated).toBe(1);
|
|
2112
|
+
await agent.delete(`/admin/foods/${mine._id}`).expect(204);
|
|
2113
|
+
});
|
|
2114
|
+
|
|
2115
|
+
it("denies read, update, delete, and bulk-patch of rows outside the scope", async () => {
|
|
2116
|
+
const theirs = await FoodModel.create({calories: 1, name: "Theirs", ownerId: otherOwnerId});
|
|
2117
|
+
const theirsToo = await FoodModel.create({
|
|
2118
|
+
calories: 2,
|
|
2119
|
+
name: "TheirsToo",
|
|
2120
|
+
ownerId: otherOwnerId,
|
|
2121
|
+
});
|
|
2122
|
+
|
|
2123
|
+
await agent.get(`/admin/foods/${theirs._id}`).expect(403);
|
|
2124
|
+
await agent.patch(`/admin/foods/${theirs._id}`).send({calories: 999}).expect(403);
|
|
2125
|
+
const bulk = await agent
|
|
2126
|
+
.post("/admin/foods/bulk-patch")
|
|
2127
|
+
.send({ids: [String(theirs._id)], patch: {name: "BULK"}})
|
|
2128
|
+
.expect(200);
|
|
2129
|
+
expect(bulk.body.updated).toBe(0);
|
|
2130
|
+
expect(bulk.body.failures).toHaveLength(1);
|
|
2131
|
+
await agent.delete(`/admin/foods/${theirsToo._id}`).expect(403);
|
|
2132
|
+
|
|
2133
|
+
const reloaded = await FoodModel.findById(theirs._id).lean();
|
|
2134
|
+
expect(reloaded?.name).toBe("Theirs");
|
|
2135
|
+
expect(reloaded?.calories).toBe(1);
|
|
2136
|
+
expect(await FoodModel.findById(theirsToo._id).lean()).not.toBeNull();
|
|
2137
|
+
});
|
|
2138
|
+
|
|
2139
|
+
it("denies moving an in-scope row outside the scope", async () => {
|
|
2140
|
+
const mine = await FoodModel.create({calories: 1, name: "Mine", ownerId: actorId});
|
|
2141
|
+
await agent
|
|
2142
|
+
.patch(`/admin/foods/${mine._id}`)
|
|
2143
|
+
.send({ownerId: String(otherOwnerId)})
|
|
2144
|
+
.expect(403);
|
|
2145
|
+
const reloaded = await FoodModel.findById(mine._id).lean();
|
|
2146
|
+
expect(String(reloaded?.ownerId)).toBe(actorId);
|
|
2147
|
+
});
|
|
2148
|
+
|
|
2149
|
+
it("denies by-id routes when the scope returns null", async () => {
|
|
2150
|
+
const localApp = buildApp([{...foodModelConfig, queryFilter: (): null => null}]);
|
|
2151
|
+
const nullAgent = await authAsUser(localApp, "admin");
|
|
2152
|
+
const row = await FoodModel.create({calories: 1, name: "Hidden", ownerId: actorId});
|
|
2153
|
+
await nullAgent.get("/admin/foods").expect(200);
|
|
2154
|
+
await nullAgent.get(`/admin/foods/${row._id}`).expect(403);
|
|
2155
|
+
});
|
|
2156
|
+
});
|
|
2157
|
+
|
|
2158
|
+
describe("declared list filters on a scoped field", () => {
|
|
2159
|
+
let agent: TestAgent;
|
|
2160
|
+
let actorId: string;
|
|
2161
|
+
let otherOwnerId: mongoose.Types.ObjectId;
|
|
2162
|
+
|
|
2163
|
+
beforeEach(async () => {
|
|
2164
|
+
const localApp = buildApp([
|
|
2165
|
+
{
|
|
2166
|
+
...foodModelConfig,
|
|
2167
|
+
filters: [
|
|
2168
|
+
{field: "ownerId", kind: "ref", refModel: "User"},
|
|
2169
|
+
{field: "name", kind: "text"},
|
|
2170
|
+
],
|
|
2171
|
+
queryFilter: (user, query): Record<string, unknown> => ({
|
|
2172
|
+
...(query ?? {}),
|
|
2173
|
+
ownerId: user?.id,
|
|
2174
|
+
}),
|
|
2175
|
+
searchFields: ["name"],
|
|
2176
|
+
},
|
|
2177
|
+
]);
|
|
2178
|
+
agent = await authAsUser(localApp, "admin");
|
|
2179
|
+
const actor = await findOneOrNoneFor(UserModel, {email: "admin@example.com"});
|
|
2180
|
+
actorId = String(actor?._id);
|
|
2181
|
+
otherOwnerId = new mongoose.Types.ObjectId();
|
|
2182
|
+
await FoodModel.create({calories: 1, name: "Mine", ownerId: actorId});
|
|
2183
|
+
await FoodModel.create({calories: 2, name: "Theirs", ownerId: otherOwnerId});
|
|
2184
|
+
});
|
|
2185
|
+
|
|
2186
|
+
it("keeps the row scope when the client filters on the scoped field", async () => {
|
|
2187
|
+
const res = await agent.get(`/admin/foods?ownerId=${otherOwnerId}`).expect(200);
|
|
2188
|
+
expect(res.body.data.map((row: {name: string}) => row.name)).toEqual(["Mine"]);
|
|
2189
|
+
});
|
|
2190
|
+
|
|
2191
|
+
it("keeps the row scope when the scoped-field filter is combined with search", async () => {
|
|
2192
|
+
const res = await agent.get(`/admin/foods?ownerId=${otherOwnerId}&q=T`).expect(200);
|
|
2193
|
+
expect(res.body.data).toEqual([]);
|
|
2194
|
+
});
|
|
2195
|
+
|
|
2196
|
+
it("still applies declared filters on other fields inside the scope", async () => {
|
|
2197
|
+
const mine = await agent.get("/admin/foods?name=Mine").expect(200);
|
|
2198
|
+
expect(mine.body.data.map((row: {name: string}) => row.name)).toEqual(["Mine"]);
|
|
2199
|
+
const theirs = await agent.get("/admin/foods?name=Theirs").expect(200);
|
|
2200
|
+
expect(theirs.body.data).toEqual([]);
|
|
2201
|
+
const searched = await agent.get("/admin/foods?name=Mine&q=M").expect(200);
|
|
2202
|
+
expect(searched.body.data.map((row: {name: string}) => row.name)).toEqual(["Mine"]);
|
|
2203
|
+
});
|
|
2204
|
+
});
|
|
2205
|
+
});
|
|
2206
|
+
|
|
2207
|
+
describe("AdminApp adminPermissions methods", () => {
|
|
2208
|
+
const buildContributionApp = (
|
|
2209
|
+
adminPermissions: NonNullable<AdminConfig["adminPermissions"]>
|
|
2210
|
+
): express.Application => {
|
|
2211
|
+
const plugin: TerrenoPlugin = {
|
|
2212
|
+
adminContribution: () => ({
|
|
2213
|
+
models: [
|
|
2214
|
+
{
|
|
2215
|
+
admin: {
|
|
2216
|
+
adminPermissions,
|
|
2217
|
+
bulkPatchAllowlist: ["calories"],
|
|
2218
|
+
displayName: "Locked foods",
|
|
2219
|
+
listFields: ["name", "calories"],
|
|
2220
|
+
searchFields: ["name"],
|
|
2221
|
+
},
|
|
2222
|
+
model: FoodModel,
|
|
2223
|
+
routePath: "/locked-foods",
|
|
2224
|
+
},
|
|
2225
|
+
],
|
|
2226
|
+
}),
|
|
2227
|
+
register() {},
|
|
2228
|
+
};
|
|
2229
|
+
const terrenoApp = {
|
|
2230
|
+
getPlugins: () => [plugin],
|
|
2231
|
+
getRegistrations: () => [],
|
|
2232
|
+
} as unknown as TerrenoApp;
|
|
2233
|
+
const localApp = getBaseServer();
|
|
2234
|
+
setupAuth(localApp, UserModel as unknown as UserModelType);
|
|
2235
|
+
addAuthRoutes(localApp, UserModel as unknown as UserModelType);
|
|
2236
|
+
new AdminApp({basePath: "/admin"}).register(localApp, undefined, terrenoApp);
|
|
2237
|
+
localApp.use(apiUnauthorizedMiddleware);
|
|
2238
|
+
localApp.use(apiErrorMiddleware);
|
|
2239
|
+
return localApp;
|
|
2240
|
+
};
|
|
2241
|
+
|
|
2242
|
+
beforeEach(async () => {
|
|
2243
|
+
await setupDb();
|
|
2244
|
+
});
|
|
2245
|
+
|
|
2246
|
+
afterEach(async () => {
|
|
2247
|
+
await FoodModel.deleteMany({});
|
|
2248
|
+
});
|
|
2249
|
+
|
|
2250
|
+
it("denies list, read, and search when list and read are empty arrays", async () => {
|
|
2251
|
+
const row = await FoodModel.create({calories: 5, name: "Locked"});
|
|
2252
|
+
const agent = await authAsUser(buildContributionApp({list: [], read: []}), "admin");
|
|
2253
|
+
await agent.get("/admin/locked-foods").expect(405);
|
|
2254
|
+
await agent.get(`/admin/locked-foods/${row._id}`).expect(405);
|
|
2255
|
+
await agent.get("/admin/locked-foods/search?q=Lock").expect(403);
|
|
2256
|
+
const config = await agent.get("/admin/config").expect(200);
|
|
2257
|
+
expect(config.body.models.map((model: {routePath: string}) => model.routePath)).not.toContain(
|
|
2258
|
+
"/admin/locked-foods"
|
|
2259
|
+
);
|
|
2260
|
+
});
|
|
2261
|
+
|
|
2262
|
+
it("evaluates configured update methods on PATCH, bulk-patch, and capabilities", async () => {
|
|
2263
|
+
const row = await FoodModel.create({calories: 5, name: "Locked"});
|
|
2264
|
+
let denyCalls = 0;
|
|
2265
|
+
const denyAll: PermissionMethod<unknown> = async () => {
|
|
2266
|
+
denyCalls += 1;
|
|
2267
|
+
return false;
|
|
2268
|
+
};
|
|
2269
|
+
const agent = await authAsUser(buildContributionApp({update: [denyAll]}), "admin");
|
|
2270
|
+
|
|
2271
|
+
const read = await agent.get(`/admin/locked-foods/${row._id}`).expect(200);
|
|
2272
|
+
expect(read.body.data._adminCapabilities.update).toBe(false);
|
|
2273
|
+
expect(read.body.data._adminCapabilities.delete).toBe(true);
|
|
2274
|
+
await agent.patch(`/admin/locked-foods/${row._id}`).send({calories: 77}).expect(405);
|
|
2275
|
+
await agent
|
|
2276
|
+
.post("/admin/locked-foods/bulk-patch")
|
|
2277
|
+
.send({ids: [String(row._id)], patch: {calories: 78}})
|
|
2278
|
+
.expect(403);
|
|
2279
|
+
const config = await agent.get("/admin/config").expect(200);
|
|
2280
|
+
const meta = config.body.models.find(
|
|
2281
|
+
(model: {routePath: string}) => model.routePath === "/admin/locked-foods"
|
|
2282
|
+
);
|
|
2283
|
+
expect(meta?.permissions.update).toBe(false);
|
|
2284
|
+
|
|
2285
|
+
expect(denyCalls).toBeGreaterThan(0);
|
|
2286
|
+
const stored = await FoodModel.findById(row._id).lean();
|
|
2287
|
+
expect(stored?.calories).toBe(5);
|
|
2288
|
+
});
|
|
2289
|
+
|
|
2290
|
+
it("ANDs configured methods with the admin check and passes the document", async () => {
|
|
2291
|
+
const row = await FoodModel.create({calories: 5, name: "Editable"});
|
|
2292
|
+
const seen: unknown[] = [];
|
|
2293
|
+
const onlyEditable: PermissionMethod<unknown> = async (_method, _user, obj) => {
|
|
2294
|
+
seen.push(obj);
|
|
2295
|
+
return obj === undefined || (obj as {name?: string}).name === "Editable";
|
|
2296
|
+
};
|
|
2297
|
+
const app = buildContributionApp({update: [onlyEditable]});
|
|
2298
|
+
const agent = await authAsUser(app, "admin");
|
|
2299
|
+
await agent.patch(`/admin/locked-foods/${row._id}`).send({calories: 9}).expect(200);
|
|
2300
|
+
expect(seen.some((obj) => obj !== undefined)).toBe(true);
|
|
2301
|
+
|
|
2302
|
+
const other = await FoodModel.create({calories: 5, name: "Other"});
|
|
2303
|
+
await agent.patch(`/admin/locked-foods/${other._id}`).send({calories: 9}).expect(403);
|
|
2304
|
+
|
|
2305
|
+
const notAdmin = await authAsUser(app, "notAdmin");
|
|
2306
|
+
await notAdmin.patch(`/admin/locked-foods/${row._id}`).send({calories: 10}).expect(405);
|
|
2307
|
+
});
|
|
2060
2308
|
});
|
|
2061
2309
|
|
|
2062
2310
|
describe("AdminApp organization scoping", () => {
|
|
@@ -2516,6 +2764,233 @@ describe("AdminApp user elevation and scoped bulk-patch", () => {
|
|
|
2516
2764
|
expect((await UserModel.findById(target?._id))?.admin).toBe(true);
|
|
2517
2765
|
});
|
|
2518
2766
|
|
|
2767
|
+
it("does not let a user editor without assignRoles write roles through dotted paths", async () => {
|
|
2768
|
+
const accessControl = createAccess({
|
|
2769
|
+
connection: mongoose.connection,
|
|
2770
|
+
defaultRoles: [
|
|
2771
|
+
{
|
|
2772
|
+
displayName: "Support",
|
|
2773
|
+
name: "support",
|
|
2774
|
+
permissions: {admin: ["access"], user: ["list", "read", "update"]},
|
|
2775
|
+
},
|
|
2776
|
+
],
|
|
2777
|
+
statements: terrenoStatements,
|
|
2778
|
+
userModel: UserModel as unknown as UserModelType,
|
|
2779
|
+
});
|
|
2780
|
+
await accessControl.roles.seedDefaults();
|
|
2781
|
+
const localApp = buildApp(
|
|
2782
|
+
[
|
|
2783
|
+
{
|
|
2784
|
+
displayName: "Users",
|
|
2785
|
+
listFields: ["email", "admin"],
|
|
2786
|
+
model: UserModel,
|
|
2787
|
+
routePath: "/users",
|
|
2788
|
+
},
|
|
2789
|
+
],
|
|
2790
|
+
{accessControl}
|
|
2791
|
+
);
|
|
2792
|
+
const self = await UserModel.findOne({email: "notAdmin@example.com"}).orFail();
|
|
2793
|
+
await UserModel.updateOne({_id: self._id}, {$set: {roles: ["support"]}});
|
|
2794
|
+
const agent = await authAsUser(localApp, "notAdmin");
|
|
2795
|
+
|
|
2796
|
+
await agent
|
|
2797
|
+
.patch(`/admin/users/${String(self._id)}`)
|
|
2798
|
+
.send({roles: ["support", "superadmin"]})
|
|
2799
|
+
.expect(403);
|
|
2800
|
+
|
|
2801
|
+
await agent
|
|
2802
|
+
.patch(`/admin/users/${String(self._id)}`)
|
|
2803
|
+
.send({name: "Dotted", "organizationIds.0": "other-tenant", "roles.1": "superadmin"})
|
|
2804
|
+
.expect(200);
|
|
2805
|
+
await agent
|
|
2806
|
+
.patch(`/admin/users/${String(self._id)}`)
|
|
2807
|
+
.send({"roles.0": "superadmin"})
|
|
2808
|
+
.expect(200);
|
|
2809
|
+
|
|
2810
|
+
const after = await UserModel.findById(self._id).lean();
|
|
2811
|
+
expect(after?.name).toBe("Dotted");
|
|
2812
|
+
expect((after as {roles?: string[]} | null)?.roles).toEqual(["support"]);
|
|
2813
|
+
expect((after as {organizationIds?: string[]} | null)?.organizationIds ?? []).toEqual([]);
|
|
2814
|
+
const permissions = await accessControl.can({
|
|
2815
|
+
permissions: {rbac: ["assignRoles"]},
|
|
2816
|
+
user: after as never,
|
|
2817
|
+
});
|
|
2818
|
+
expect(permissions.allowed).toBe(false);
|
|
2819
|
+
});
|
|
2820
|
+
|
|
2821
|
+
it("applies the User role and admin-flag gates to a user model not compiled as User", async () => {
|
|
2822
|
+
const AccountModel =
|
|
2823
|
+
(mongoose.models.AdminRbacAccount as mongoose.Model<Record<string, unknown>> | undefined) ??
|
|
2824
|
+
mongoose.model<Record<string, unknown>>("AdminRbacAccount", UserModel.schema.clone());
|
|
2825
|
+
await AccountModel.deleteMany({});
|
|
2826
|
+
const accessControl = createAccess({
|
|
2827
|
+
connection: mongoose.connection,
|
|
2828
|
+
defaultRoles: [
|
|
2829
|
+
{
|
|
2830
|
+
displayName: "Account editor",
|
|
2831
|
+
name: "account-editor",
|
|
2832
|
+
permissions: {admin: ["access"], user: ["list", "read", "update"]},
|
|
2833
|
+
},
|
|
2834
|
+
],
|
|
2835
|
+
statements: terrenoStatements,
|
|
2836
|
+
userModel: AccountModel as unknown as UserModelType,
|
|
2837
|
+
});
|
|
2838
|
+
await accessControl.roles.seedDefaults();
|
|
2839
|
+
const localApp = buildApp(
|
|
2840
|
+
[
|
|
2841
|
+
{
|
|
2842
|
+
adminAccess: {resource: "user"},
|
|
2843
|
+
displayName: "Accounts",
|
|
2844
|
+
listFields: ["email", "admin"],
|
|
2845
|
+
model: AccountModel,
|
|
2846
|
+
routePath: "/accounts",
|
|
2847
|
+
},
|
|
2848
|
+
],
|
|
2849
|
+
{accessControl}
|
|
2850
|
+
);
|
|
2851
|
+
const actor = await UserModel.findOne({email: "notAdmin@example.com"}).orFail();
|
|
2852
|
+
await UserModel.updateOne({_id: actor._id}, {$set: {roles: ["account-editor"]}});
|
|
2853
|
+
const target = await AccountModel.create({email: "renamed-target@example.com", name: "T"});
|
|
2854
|
+
const agent = await authAsUser(localApp, "notAdmin");
|
|
2855
|
+
|
|
2856
|
+
await agent
|
|
2857
|
+
.patch(`/admin/accounts/${String(target._id)}`)
|
|
2858
|
+
.send({roles: ["superadmin"]})
|
|
2859
|
+
.expect(403);
|
|
2860
|
+
await agent
|
|
2861
|
+
.patch(`/admin/accounts/${String(target._id)}`)
|
|
2862
|
+
.send({admin: true})
|
|
2863
|
+
.expect(403);
|
|
2864
|
+
await agent
|
|
2865
|
+
.patch(`/admin/accounts/${String(target._id)}`)
|
|
2866
|
+
.send({name: "Renamed", organizationIds: ["other-tenant"], tokenEpoch: 99})
|
|
2867
|
+
.expect(200);
|
|
2868
|
+
|
|
2869
|
+
const after = await AccountModel.findById(target._id).lean();
|
|
2870
|
+
expect(after?.name).toBe("Renamed");
|
|
2871
|
+
expect(after?.admin).toBe(false);
|
|
2872
|
+
expect(after?.roles ?? []).toEqual([]);
|
|
2873
|
+
expect(after?.organizationIds ?? []).toEqual([]);
|
|
2874
|
+
expect(after?.tokenEpoch ?? 0).toBe(0);
|
|
2875
|
+
});
|
|
2876
|
+
|
|
2877
|
+
it("does not let an RBAC admin delegate rewrite a more privileged user", async () => {
|
|
2878
|
+
const accessControl = createAccess({
|
|
2879
|
+
connection: mongoose.connection,
|
|
2880
|
+
statements: terrenoStatements,
|
|
2881
|
+
userModel: UserModel as unknown as UserModelType,
|
|
2882
|
+
});
|
|
2883
|
+
await accessControl.roles.seedDefaults();
|
|
2884
|
+
const localApp = buildApp(
|
|
2885
|
+
[
|
|
2886
|
+
{
|
|
2887
|
+
bulkPatchAllowlist: ["disabled", "name"],
|
|
2888
|
+
displayName: "Users",
|
|
2889
|
+
listFields: ["email", "admin"],
|
|
2890
|
+
model: UserModel,
|
|
2891
|
+
routePath: "/users",
|
|
2892
|
+
},
|
|
2893
|
+
],
|
|
2894
|
+
{accessControl}
|
|
2895
|
+
);
|
|
2896
|
+
const delegate = await UserModel.findOne({email: "notAdmin@example.com"}).orFail();
|
|
2897
|
+
await UserModel.updateOne({_id: delegate._id}, {$set: {roles: ["admin"]}});
|
|
2898
|
+
const [superadmin, member] = await UserModel.create([
|
|
2899
|
+
{email: "admin-target-super@example.com", name: "Super", roles: ["superadmin"]},
|
|
2900
|
+
{email: "admin-target-member@example.com", name: "Member", roles: ["member"]},
|
|
2901
|
+
]);
|
|
2902
|
+
const agent = await authAsUser(localApp, "notAdmin");
|
|
2903
|
+
|
|
2904
|
+
await agent
|
|
2905
|
+
.patch(`/admin/users/${String(superadmin._id)}`)
|
|
2906
|
+
.send({email: "redirect@example.com"})
|
|
2907
|
+
.expect(403);
|
|
2908
|
+
await agent
|
|
2909
|
+
.patch(`/admin/users/${String(superadmin._id)}`)
|
|
2910
|
+
.send({disabled: true})
|
|
2911
|
+
.expect(403);
|
|
2912
|
+
const bulk = await agent
|
|
2913
|
+
.post("/admin/users/bulk-patch")
|
|
2914
|
+
.send({ids: [String(superadmin._id), String(member._id)], patch: {disabled: true}})
|
|
2915
|
+
.expect(200);
|
|
2916
|
+
expect(bulk.body.updated).toBe(1);
|
|
2917
|
+
expect(bulk.body.failures).toEqual([
|
|
2918
|
+
{id: String(superadmin._id), title: "Cannot modify a user with permissions you do not hold"},
|
|
2919
|
+
]);
|
|
2920
|
+
|
|
2921
|
+
const storedSuper = await UserModel.findById(superadmin._id).lean();
|
|
2922
|
+
expect(storedSuper?.email).toBe("admin-target-super@example.com");
|
|
2923
|
+
expect((storedSuper as {disabled?: boolean} | null)?.disabled ?? false).toBe(false);
|
|
2924
|
+
const storedMember = await UserModel.findById(member._id).lean();
|
|
2925
|
+
expect((storedMember as {disabled?: boolean} | null)?.disabled).toBe(true);
|
|
2926
|
+
|
|
2927
|
+
await agent
|
|
2928
|
+
.patch(`/admin/users/${String(member._id)}`)
|
|
2929
|
+
.send({name: "Member renamed"})
|
|
2930
|
+
.expect(200);
|
|
2931
|
+
});
|
|
2932
|
+
|
|
2933
|
+
it("requires user:updateIdentity for admin identity-field writes to another user", async () => {
|
|
2934
|
+
const accessControl = createAccess({
|
|
2935
|
+
connection: mongoose.connection,
|
|
2936
|
+
defaultRoles: [
|
|
2937
|
+
{
|
|
2938
|
+
displayName: "User editor",
|
|
2939
|
+
name: "userEditor",
|
|
2940
|
+
permissions: {admin: ["access"], user: ["create", "list", "read", "update"]},
|
|
2941
|
+
},
|
|
2942
|
+
],
|
|
2943
|
+
statements: terrenoStatements,
|
|
2944
|
+
userModel: UserModel as unknown as UserModelType,
|
|
2945
|
+
});
|
|
2946
|
+
await accessControl.roles.seedDefaults();
|
|
2947
|
+
const localApp = buildApp(
|
|
2948
|
+
[
|
|
2949
|
+
{
|
|
2950
|
+
bulkPatchAllowlist: ["disabled", "name"],
|
|
2951
|
+
displayName: "Users",
|
|
2952
|
+
listFields: ["email", "admin"],
|
|
2953
|
+
model: UserModel,
|
|
2954
|
+
routePath: "/users",
|
|
2955
|
+
},
|
|
2956
|
+
],
|
|
2957
|
+
{accessControl}
|
|
2958
|
+
);
|
|
2959
|
+
const delegate = await UserModel.findOne({email: "notAdmin@example.com"}).orFail();
|
|
2960
|
+
await UserModel.updateOne({_id: delegate._id}, {$set: {roles: ["userEditor"]}});
|
|
2961
|
+
const member = await UserModel.create({
|
|
2962
|
+
email: "identity-target-member@example.com",
|
|
2963
|
+
name: "Member",
|
|
2964
|
+
roles: ["member"],
|
|
2965
|
+
});
|
|
2966
|
+
const memberId = String(member._id);
|
|
2967
|
+
const agent = await authAsUser(localApp, "notAdmin");
|
|
2968
|
+
|
|
2969
|
+
const patch = await agent.patch(`/admin/users/${memberId}`).send({disabled: true}).expect(403);
|
|
2970
|
+
expect(patch.body.title).toBe("Missing user:updateIdentity permission");
|
|
2971
|
+
await agent
|
|
2972
|
+
.patch(`/admin/users/${memberId}`)
|
|
2973
|
+
.send({email: "identity-redirect@example.com"})
|
|
2974
|
+
.expect(403);
|
|
2975
|
+
const bulk = await agent
|
|
2976
|
+
.post("/admin/users/bulk-patch")
|
|
2977
|
+
.send({ids: [memberId], patch: {disabled: true}})
|
|
2978
|
+
.expect(200);
|
|
2979
|
+
expect(bulk.body.updated).toBe(0);
|
|
2980
|
+
expect(bulk.body.failures).toEqual([
|
|
2981
|
+
{id: memberId, title: "Missing user:updateIdentity permission"},
|
|
2982
|
+
]);
|
|
2983
|
+
|
|
2984
|
+
await agent
|
|
2985
|
+
.patch(`/admin/users/${memberId}`)
|
|
2986
|
+
.send({email: "identity-target-member@example.com", name: "Member renamed"})
|
|
2987
|
+
.expect(200);
|
|
2988
|
+
const stored = await UserModel.findById(memberId).lean();
|
|
2989
|
+
expect(stored?.email).toBe("identity-target-member@example.com");
|
|
2990
|
+
expect((stored as {disabled?: boolean} | null)?.disabled ?? false).toBe(false);
|
|
2991
|
+
expect(stored?.name).toBe("Member renamed");
|
|
2992
|
+
});
|
|
2993
|
+
|
|
2519
2994
|
it("does not persist organizationIds on RBAC admin User writes", async () => {
|
|
2520
2995
|
const accessControl = createAccess({
|
|
2521
2996
|
connection: mongoose.connection,
|