Skip to content

Commit 909c8c9

Browse files
fix(service-settings): a locked settings row binds every writer below the platform rung, at its own scope too (#28617)
<!-- claude-projects-attribution: {"github_login":"marchtian"} --> _Requested by **田騏** · [project thread](https://claude.ai/code/project/chan_01VmuUhXU9SSUzqJYyXSmYSi?thread=cmsg_01VmuUhXU9SSUzqJYyXSmYSi9paTx1Lu8ENYwwssthfK3h)_ Clause-②: no Metadata-protocol: none ## 维护者速读 - 改了什么:设置行上的「锁定」现在对本组织管理员同样生效——组织管理员保存(或重置)本组织已被平台锁定的设置会被拒(409 SETTINGS_LOCKED,原因 locked-by-tenant);已锁定的全局设置行同样只有平台能改。平台管理员(manage_platform_settings)和系统内部调用仍可写入,写完锁定保持不变。 - 为什么改:按裁决「LOCKED = platform-locked; an org admin's save of a LOCKED settings row is refused.」加固:锁定是平台的决定,不该由被锁的组织自己改掉;设置页早已把这类字段显示为锁定,现在保存端与显示一致。 - 风险与回滚:只改 `@objectstack/service-settings` 的写入预检和内存存储的更新合并;读取接口不变;目前没有 HTTP 入口能给行加锁,只有已由系统写入锁定的行才会出现新的拒绝。回滚就是 revert 本 PR。 - 先看:`packages/services/service-settings/src/settings-service.ts` 的 `lockThatRefuses` / `writesAsPlatform` 两个新方法与 `setMany` 预检里调用它们的两处;然后是 `settings-organization-isolation.pin.test.ts` 新增的两个 describe 块。 - 两分钟验证:`pnpm --filter @objectstack/service-settings exec vitest run src/settings-organization-isolation.pin.test.ts`,预期 36 个测试全过(新增 13 个,含经由 PUT 路由的 409 与 200 两条)。 - 需要你做的:无,席位落地。 ## What changes A locked settings row is platform-locked, and it now binds every writer below the platform rung — at an upper rung as before, and at the write's own rung too. - `SettingsService.setMany`'s lock pre-flight is one judgment, `lockThatRefuses(candidates, scope, platformRung)`, over the rows the caller's cascade reads lock state from (the global row, and the tenant row its reach prefers): - a locked row at an **upper** rung refuses every write below it, the platform's included (unchanged); - a locked row at the write's **own** rung refuses every writer but the platform rung. For a tenant-scope key that is the organization's own locked tenant row (`locked-by-tenant`); for a global-scope key, the locked global row (`locked-by-global`). - The platform rung is `writesAsPlatform(ctx)`: a trusted in-process caller (`enforced` unset — the seed/boot paths `assertPermitted` already lets through, and the system write that sets a lock), or an enforced caller holding `manage_platform_settings`, the capability every platform-scope settings manifest writes behind. An organization administrator holds `setup.write` and nothing more, so a lock binds it. - The platform's write through a lock leaves the row locked: the engine's update never carried `locked`; the in-memory store now merges an update the same way (`{ ...previous, ...row }`) instead of replacing the row, so the lock survives there too. - The user rung carries no lock, on purpose: the cascade has never read a user row's `locked` (`resolveKeyFromRows`), so `get()` reports none and the pre-flight judges none — the two sides agree. - The read side is unchanged. `get()`, `getMany()` and `getNamespace()` already reported these rows as `locked: true`, and the Console's settings page already rendered the field disabled; what they report as locked is now exactly what a non-platform save is refused on. - `content/docs/protocol/kernel/config-resolution.mdx` states the rule; a `patch` changeset for `@objectstack/service-settings`. ## Tests New pins in `packages/services/service-settings/src/settings-organization-isolation.pin.test.ts`, over a real `ObjectQL` engine (Map driver) through the plugin's own engine adapter, under the `isolated` posture: - `a locked row binds every writer below the platform rung` (11): the organization administrator's save over its own locked tenant row is refused (`SETTINGS_LOCKED`, `reason: 'locked-by-tenant'`, value and lock unchanged); a reset over it, through `setMany` and through the built-in `reset` action, is refused the same way; `get` / `getMany` / `getNamespace` all report `locked: true` for that caller; the platform administrator writes through and the row stays locked (and still binds the organization administrator afterwards); a trusted in-process caller writes through; another organization's administrator is not bound; an unlocked row is saved over as before; a locked global row refuses a non-platform write of a global key and the platform administrator writes through it; a locked upper rung still refuses the platform administrator's write below it; the user rung carries no lock; the in-memory store keeps the lock through the platform's write. - `the settings route answers a locked row the way the service does` (2), through `registerSettingsRoutes` `PUT /api/settings/:namespace` with the shipped `company` manifest: the organization administrator's PUT is `409` with `error.code: 'SETTINGS_LOCKED'` and `details: { namespace, key, reason: 'locked-by-tenant' }`, the value unchanged; the platform administrator's PUT is `200`, and another organization's administrator's PUT is `200`. - The existing pin `a lock on one organization's tenant row does not lock another organization's write` passes unchanged. ## Acceptance notes - No same-meaning pin needed flipping: a sweep of `packages/**`, `ui/**` and `examples/**` tests found no assertion that a same-rung locked row accepts a write; the whole `service-settings` suite stays green unchanged. - A namespace reset (`resetNamespace`) is refused whole when any key it would clear carries a lock that binds the caller — the shape it already had for a locked global row over a tenant key. - The platform administrator still sees a same-rung locked field as locked in the Console (the read reports `locked: true` to every caller), while the API accepts its save — the shape a globally-locked global key already had. The read is not made caller-relative here. ## Verification <details><summary>Red first, green, ablation</summary> - Red, on `bca27ad3f1` with only the test file changed: `Tests 7 failed | 29 passed (36)` — the seven refusal pins failed, every control passed. - Green at `67ae5e93bc`: `Tests 36 passed (36)`; at `34942f02d8` the whole package `Test Files 67 passed (67) · Tests 1120 passed (1120)`, `tsc --noEmit` exit 0. - Ablations through `scripts/ablation-replace.mjs` (mutation landed, restored to the HEAD blob, `git diff HEAD` empty each time): 1. the own-rung clause removed from `lockThatRefuses` → 7 failed; 2. `writesAsPlatform` answering `false` for a `manage_platform_settings` holder → 4 failed (every platform write-through pin); 3. the in-memory merge reverted to a replacement → 1 failed (the in-memory pin). </details> <details><summary>Gates</summary> All at head `34942f02d8` (after merging `origin/main` at `310cb96d76`, which touched nothing in this package's closure): - `node scripts/pm/dispatch-gates.mjs --commands --repo objectstack-ai/objectstack` derived 90 families; every one was run with its exit code captured before any pipe, after a full `pnpm turbo run build` (118/118 tasks): all 90 exited 0. - `--ran` reconciliation: `90 derived, 90 run, 0 NOT-MEASURED, 0 UNRUN` (a derived zero: every line carries its exit code). - `pnpm --filter @objectstack/service-settings test`: `Test Files 67 passed (67) · Tests 1120 passed (1120)`; `pnpm --filter @objectstack/service-settings typecheck` (`tsc --noEmit`, its program includes `src/**/*.test.ts`): exit 0. - Dogfood battery: not run — no `!` in the title and no `(narrowing)` arm. </details> Session: https://claude.ai/code/session_017ihL2gLghEqeDKTbDTC7Zd 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent 40e7bfc commit 909c8c9

4 files changed

Lines changed: 372 additions & 25 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
'@objectstack/service-settings': patch
3+
---
4+
5+
fix(service-settings): a locked settings row binds the organization's own administrators; only the platform rung writes through a lock at its own scope
6+
7+
Clause-②: no
8+
Metadata-protocol: none
9+
10+
A locked settings row is platform-locked. A lock at an upper scope already refused every write below it; a lock at the write's own scope now binds too:
11+
12+
- **A locked tenant row** refuses a save of that key by its own organization's administrators — `SettingsLockedError`, `409 SETTINGS_LOCKED` on `PUT /api/settings/:namespace`, `details.reason: 'locked-by-tenant'` — and a reset over it is refused the same way. Another organization's row and writes are unaffected.
13+
- **A locked global row** refuses a write of that global key from anyone but the platform (`locked-by-global`).
14+
- **The platform rung writes through**: a caller holding `manage_platform_settings`, or a trusted in-process caller (one whose `SettingsContext` is not `enforced`). The row keeps its lock after that write, on the engine store and on the in-memory store alike (the in-memory store now keeps a row's columns the write does not carry, as the engine's update does).
15+
- A locked upper rung still refuses the platform's write below it, as before.
16+
- The user rung carries no lock: `get()` reports none for a user row, and its owner's write is not refused.
17+
18+
The read side is unchanged: `get()`, `getMany()` and `getNamespace()` already reported such a row as `locked: true`, and the settings page already showed the field as locked. What they report as locked is now what a non-platform save is refused on.

‎content/docs/protocol/kernel/config-resolution.mdx‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,14 @@ Two rules follow from the walk, and both are enforced in `SettingsService`:
7575
array concatenation. A row at a higher scope replaces the layer below it
7676
wholesale.
7777
- **A lock anywhere up the chain locks the effective value.** An `OS_*`
78-
override always locks; a row may additionally carry `locked=true`, and then a
79-
write against any *lower* scope is rejected with `SETTINGS_LOCKED`.
78+
override always locks; a global or tenant row may additionally carry
79+
`locked=true`. A locked row is platform-locked: a write against any *lower*
80+
scope is rejected with `SETTINGS_LOCKED`, and so is a write at the row's own
81+
scope from anyone but the platform — an organization's administrator cannot
82+
save over its own organization's locked tenant row. Only a platform
83+
administrator (`manage_platform_settings`) or a trusted in-process caller
84+
writes through a lock at its own scope, and the row stays locked. A user row
85+
carries no lock.
8086

8187
How far down the cascade a key is even eligible to travel is **declared, not
8288
chosen by the caller** — see [Scope is declared](#scope-is-declared).
@@ -213,7 +219,7 @@ Writes fail loudly rather than silently degrading:
213219
| Condition | Thrown | Code |
214220
| :--- | :--- | :--- |
215221
| An `OS_*` override is in force for the key | `SettingsLockedError` | `SETTINGS_LOCKED` |
216-
| A row at an upper scope has `locked=true` | `SettingsLockedError` | `SETTINGS_LOCKED` (`locked-by-<scope>`) |
222+
| A row at an upper scope has `locked=true`, or a row at the write's own scope has it and the caller is not the platform | `SettingsLockedError` | `SETTINGS_LOCKED` (`locked-by-<scope>`) |
217223
| Key not declared by the manifest | `UnknownKeyError` | `SETTINGS_UNKNOWN_KEY` |
218224
| Namespace has no manifest | `UnknownNamespaceError` | `SETTINGS_UNKNOWN_NAMESPACE` |
219225
| Patch would leave a visible required field empty, or violates a declared constraint | `SettingsValidationError` | per-key `FieldError` list (ADR-0114) |

‎packages/services/service-settings/src/settings-organization-isolation.pin.test.ts‎

Lines changed: 242 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@
2424
* D8), under a wall and under `single` alike — and the global rung is read
2525
* by every organization;
2626
* - the lock pre-flight reads the same row the cascade does;
27+
* - a locked row binds every writer below the platform rung — at an upper
28+
* rung as before, and at the write's own rung too: an organization's
29+
* administrator cannot save over its own organization's locked tenant row,
30+
* nor over a locked global row, while a platform administrator
31+
* (`manage_platform_settings`) and a trusted in-process caller still write
32+
* through, and the row keeps its lock — through the service and through the
33+
* settings route alike;
2734
* - the refusal: under a walled posture a tenant-scope write that names no
2835
* organization is refused whole, and writes nothing; under `single` it is
2936
* not;
@@ -44,14 +51,17 @@
4451
* query shape it cannot express fails here instead of reading as "no row".
4552
*/
4653

47-
import { describe, it, expect, beforeEach } from 'vitest';
54+
import { describe, it, expect, beforeEach, vi } from 'vitest';
55+
import type { IHttpServer, IHttpRequest, IHttpResponse, RouteHandler } from '@objectstack/spec/contracts';
4856
import { ObjectQL } from '@objectstack/objectql';
4957
import { SysUser } from '@objectstack/platform-objects/identity';
5058
import { SysPlatformSetting, SysSetting } from '@objectstack/platform-objects/system';
5159
import type { TenancyPosture } from '@objectstack/spec/security';
5260
import { SettingsService, type SettingsOrganizationSource } from './settings-service.js';
5361
import { wrapEngineAsSettingsEngine } from './settings-service-plugin.js';
5462
import { SettingsLockedError, type SettingsContext } from './settings-service.types.js';
63+
import { registerSettingsRoutes } from './settings-routes.js';
64+
import { companySettingsManifest } from './manifests/company.manifest.js';
5565

5666
const SYS = { context: { isSystem: true } } as const;
5767
const NS = 'org_isolation_fixture';
@@ -294,6 +304,237 @@ describe('a tenant-scope value belongs to the organization that wrote it (walled
294304
});
295305
});
296306

307+
/**
308+
* An organization's administrator: verified, holding the namespace's read and
309+
* write capabilities, inside its own organization — and not the platform.
310+
*/
311+
const orgAdmin = (organization: string): SettingsContext => ({
312+
enforced: true,
313+
permissions: ['setup.access', 'setup.write'],
314+
userId: `usr_admin_${organization}`,
315+
tenantId: organization,
316+
});
317+
/** A platform administrator, working inside an organization. */
318+
const platformAdmin = (organization: string): SettingsContext => ({
319+
enforced: true,
320+
permissions: ['setup.access', 'setup.write', 'manage_platform_settings'],
321+
userId: 'usr_platform_admin',
322+
tenantId: organization,
323+
});
324+
325+
/** A row put straight into the global rung's store, as the platform left it. */
326+
async function seedGlobal(row: Record<string, unknown>): Promise<void> {
327+
await engine.insert('sys_platform_setting', {
328+
namespace: NS, value_enc: null, encrypted: false, locked: false, locked_reason: null, ...row,
329+
}, SYS);
330+
}
331+
332+
/** The refusal a write ends in, or `null` when it lands. */
333+
const refusalOf = (write: Promise<unknown>): Promise<any> => write.then(() => null, (e) => e);
334+
335+
describe('a locked row binds every writer below the platform rung', () => {
336+
const LOCK = { key: 'motto', scope: 'tenant', value: 'Platform motto', locked: true, locked_reason: 'Set by the platform.' };
337+
338+
it('an organization administrator\'s save over its own organization\'s locked tenant row is refused, and the value is unchanged', async () => {
339+
await seed({ ...LOCK, organization_id: ORG_A });
340+
const svc = settings('isolated');
341+
342+
const refusal = await refusalOf(svc.setMany(NS, { motto: 'Org A override' }, orgAdmin(ORG_A)));
343+
expect(refusal).toBeInstanceOf(SettingsLockedError);
344+
expect(refusal.code).toBe('SETTINGS_LOCKED');
345+
expect(refusal.reason).toBe('locked-by-tenant');
346+
expect(refusal.key).toBe('motto');
347+
348+
const after = await svc.get(NS, 'motto', inOrg(ORG_A));
349+
expect(after.value).toBe('Platform motto');
350+
expect(settingRows().map((r) => [r.organization_id, r.value, r.locked])).toEqual([[ORG_A, 'Platform motto', true]]);
351+
});
352+
353+
it('a reset over the locked row is a write too, and is refused the same way', async () => {
354+
await seed({ ...LOCK, organization_id: ORG_A });
355+
const svc = settings('isolated');
356+
357+
const refusal = await refusalOf(svc.setMany(NS, { motto: null }, orgAdmin(ORG_A)));
358+
expect(refusal?.code).toBe('SETTINGS_LOCKED');
359+
expect(refusal?.reason).toBe('locked-by-tenant');
360+
const reset = await refusalOf(svc.runAction(NS, 'reset', null, orgAdmin(ORG_A)));
361+
expect(reset?.code).toBe('SETTINGS_LOCKED');
362+
expect((await svc.get(NS, 'motto', inOrg(ORG_A))).value).toBe('Platform motto');
363+
});
364+
365+
it('what the read reports as locked is what the write refuses: get, getMany and getNamespace all say locked', async () => {
366+
await seed({ ...LOCK, organization_id: ORG_A });
367+
const svc = settings('isolated');
368+
const ctx = orgAdmin(ORG_A);
369+
370+
const one = await svc.get(NS, 'motto', ctx);
371+
expect(one.locked).toBe(true);
372+
expect(one.lockedReason).toBe('Set by the platform.');
373+
expect((await svc.getMany(NS, ['motto'], ctx)).motto.locked).toBe(true);
374+
expect((await svc.getNamespace(NS, ctx)).values.motto.locked).toBe(true);
375+
expect((await refusalOf(svc.set(NS, 'motto', 'x', ctx)))?.code).toBe('SETTINGS_LOCKED');
376+
});
377+
378+
it('a platform administrator writes through the lock, and the row keeps its lock', async () => {
379+
await seed({ ...LOCK, organization_id: ORG_A });
380+
const svc = settings('isolated');
381+
382+
await svc.setMany(NS, { motto: 'Platform motto, revised' }, platformAdmin(ORG_A));
383+
expect((await svc.get(NS, 'motto', inOrg(ORG_A))).value).toBe('Platform motto, revised');
384+
expect(settingRows().map((r) => [r.organization_id, r.value, r.locked])).toEqual([
385+
[ORG_A, 'Platform motto, revised', true],
386+
]);
387+
// The lock still binds the organization's administrator after that write.
388+
expect((await refusalOf(svc.set(NS, 'motto', 'Org A override', orgAdmin(ORG_A))))?.code).toBe('SETTINGS_LOCKED');
389+
});
390+
391+
it('a trusted in-process caller writes through the lock', async () => {
392+
await seed({ ...LOCK, organization_id: ORG_A });
393+
const svc = settings('isolated');
394+
395+
await svc.set(NS, 'motto', 'Seeded by the platform', inOrg(ORG_A));
396+
expect((await svc.get(NS, 'motto', inOrg(ORG_A))).value).toBe('Seeded by the platform');
397+
});
398+
399+
it('another organization\'s administrator is not bound by it', async () => {
400+
await seed({ ...LOCK, organization_id: ORG_A });
401+
const svc = settings('isolated');
402+
403+
await svc.setMany(NS, { motto: 'Org B value' }, orgAdmin(ORG_B));
404+
expect((await svc.get(NS, 'motto', inOrg(ORG_B))).value).toBe('Org B value');
405+
expect((await svc.get(NS, 'motto', inOrg(ORG_A))).value).toBe('Platform motto');
406+
});
407+
408+
it('an unlocked tenant row is saved over as before', async () => {
409+
await seed({ ...LOCK, organization_id: ORG_A, locked: false });
410+
const svc = settings('isolated');
411+
412+
await svc.setMany(NS, { motto: 'Org A value' }, orgAdmin(ORG_A));
413+
expect((await svc.get(NS, 'motto', inOrg(ORG_A))).value).toBe('Org A value');
414+
});
415+
416+
it('a locked global row refuses a non-platform write of the global key, and the platform administrator writes through it', async () => {
417+
await seedGlobal({ key: 'banner', value: 'Platform banner', locked: true, locked_reason: 'Set by the platform.' });
418+
const svc = settings('isolated');
419+
420+
const refusal = await refusalOf(svc.setMany(NS, { banner: 'Org A banner' }, orgAdmin(ORG_A)));
421+
expect(refusal?.code).toBe('SETTINGS_LOCKED');
422+
expect(refusal?.reason).toBe('locked-by-global');
423+
expect((await svc.get(NS, 'banner', inOrg(ORG_A))).value).toBe('Platform banner');
424+
425+
await svc.setMany(NS, { banner: 'Platform banner, revised' }, platformAdmin(ORG_A));
426+
const after = await svc.get(NS, 'banner', inOrg(ORG_B));
427+
expect(after.value).toBe('Platform banner, revised');
428+
expect(after.locked).toBe(true);
429+
});
430+
431+
it('a locked upper rung still refuses the platform administrator\'s write below it', async () => {
432+
await seedGlobal({ key: 'motto', value: 'Platform motto', locked: true, locked_reason: 'Set by the platform.' });
433+
const svc = settings('isolated');
434+
435+
const refusal = await refusalOf(svc.setMany(NS, { motto: 'Org A motto' }, platformAdmin(ORG_A)));
436+
expect(refusal?.code).toBe('SETTINGS_LOCKED');
437+
expect(refusal?.reason).toBe('locked-by-global');
438+
});
439+
440+
it('the user rung carries no lock: the read reports none and the owner\'s write is not refused', async () => {
441+
await seed({ key: 'theme', scope: 'user', user_id: userId, value: 'dark', organization_id: ORG_A, locked: true });
442+
const svc = settings('isolated');
443+
const ctx: SettingsContext = { ...orgAdmin(ORG_A), userId };
444+
445+
expect((await svc.get(NS, 'theme', ctx)).locked).toBe(false);
446+
await svc.setMany(NS, { theme: 'blue' }, ctx);
447+
expect((await svc.get(NS, 'theme', ctx)).value).toBe('blue');
448+
});
449+
450+
it('the in-memory store keeps the lock through the platform\'s write as the engine does', async () => {
451+
const svc = new SettingsService({ env: {} });
452+
svc.registerManifest(MANIFEST);
453+
await (svc as any).upsertRow({
454+
namespace: NS, key: 'motto', scope: 'tenant', user_id: null, organization_id: ORG_A,
455+
value: 'Platform motto', value_enc: null, encrypted: false, locked: true, locked_reason: 'Set by the platform.',
456+
});
457+
458+
expect((await refusalOf(svc.set(NS, 'motto', 'Org A override', orgAdmin(ORG_A))))?.reason).toBe('locked-by-tenant');
459+
await svc.set(NS, 'motto', 'Platform motto, revised', platformAdmin(ORG_A));
460+
const after = await svc.get(NS, 'motto', inOrg(ORG_A));
461+
expect(after.value).toBe('Platform motto, revised');
462+
expect(after.locked).toBe(true);
463+
expect((await refusalOf(svc.set(NS, 'motto', 'Org A override', orgAdmin(ORG_A))))?.reason).toBe('locked-by-tenant');
464+
});
465+
});
466+
467+
/** The settings routes over the same engine, with the caller each request resolves to. */
468+
class RouteHttp implements IHttpServer {
469+
routes = new Map<string, RouteHandler>();
470+
private add(method: string, path: string, handler: RouteHandler) { this.routes.set(`${method} ${path}`, handler); }
471+
get(path: string, h: RouteHandler) { this.add('GET', path, h); return this as any; }
472+
post(path: string, h: RouteHandler) { this.add('POST', path, h); return this as any; }
473+
put(path: string, h: RouteHandler) { this.add('PUT', path, h); return this as any; }
474+
delete(path: string, h: RouteHandler) { this.add('DELETE', path, h); return this as any; }
475+
patch(path: string, h: RouteHandler) { this.add('PATCH', path, h); return this as any; }
476+
use() { return this as any; }
477+
listen() { return Promise.resolve(); }
478+
close() { return Promise.resolve(); }
479+
getInstance() { return null; }
480+
}
481+
482+
describe('the settings route answers a locked row the way the service does', () => {
483+
const COMPANY = companySettingsManifest.namespace;
484+
485+
async function companyRoutes() {
486+
const svc = new SettingsService({ env: {} });
487+
svc.registerManifest(companySettingsManifest);
488+
svc.bindEngine(wrapEngineAsSettingsEngine(engine as any), undefined, {
489+
tenancyPosture: () => 'isolated',
490+
organization: NO_ORGANIZATION_OBJECT,
491+
});
492+
let caller: SettingsContext = {};
493+
const http = new RouteHttp();
494+
registerSettingsRoutes(http, svc, { contextFromRequest: () => caller });
495+
const put = async (ctx: SettingsContext, body: Record<string, unknown>) => {
496+
caller = ctx;
497+
const state: { status: number; body?: any } = { status: 200 };
498+
const req: IHttpRequest = { params: { namespace: COMPANY }, query: {}, body, headers: {}, method: 'PUT', path: '/' };
499+
const res: IHttpResponse = {
500+
json: vi.fn((data) => { state.body = data; }) as any,
501+
send: vi.fn() as any,
502+
status: vi.fn((code: number) => { state.status = code; return res; }) as any,
503+
header: vi.fn(() => res) as any,
504+
};
505+
await http.routes.get('PUT /api/settings/:namespace')!(req, res);
506+
return state;
507+
};
508+
return { svc, put };
509+
}
510+
511+
beforeEach(async () => {
512+
await engine.insert('sys_setting', {
513+
namespace: COMPANY, key: 'legal_name', scope: 'tenant', user_id: null, organization_id: ORG_A,
514+
value: 'Platform Legal Name', value_enc: null, encrypted: false, locked: true, locked_reason: 'Set by the platform.',
515+
}, SYS);
516+
});
517+
518+
it('an organization administrator\'s PUT over its own locked row is 409 SETTINGS_LOCKED naming the key and the tenant lock', async () => {
519+
const { svc, put } = await companyRoutes();
520+
521+
const r = await put(orgAdmin(ORG_A), { legal_name: 'Org A Override' });
522+
expect(r.status).toBe(409);
523+
expect(r.body.error.code).toBe('SETTINGS_LOCKED');
524+
expect(r.body.error.details).toEqual({ namespace: COMPANY, key: 'legal_name', reason: 'locked-by-tenant' });
525+
expect((await svc.get(COMPANY, 'legal_name', inOrg(ORG_A))).value).toBe('Platform Legal Name');
526+
});
527+
528+
it('a platform administrator\'s PUT over it lands, and another organization\'s administrator is not bound by it', async () => {
529+
const { svc, put } = await companyRoutes();
530+
531+
expect((await put(platformAdmin(ORG_A), { legal_name: 'Revised Legal Name' })).status).toBe(200);
532+
expect((await svc.get(COMPANY, 'legal_name', inOrg(ORG_A))).value).toBe('Revised Legal Name');
533+
expect((await put(orgAdmin(ORG_B), { legal_name: 'Org B Legal Name' })).status).toBe(200);
534+
expect((await svc.get(COMPANY, 'legal_name', inOrg(ORG_B))).value).toBe('Org B Legal Name');
535+
});
536+
});
537+
297538
describe('a tenant-scope write that names no organization', () => {
298539
for (const posture of ['isolated', 'group'] as const) {
299540
it(`is refused whole under the '${posture}' posture, and nothing is written — while the same write from an organization lands`, async () => {

0 commit comments

Comments
 (0)