Skip to content

Commit 2c165f8

Browse files
committed
test(core): pin the one Default Organization definition; changeset
Claude-Session: https://claude.ai/code/session_01ERm1aSqQLiSN87PFmwRfUV Co-authored-by: Claude <noreply@anthropic.com>
1 parent 40622bd commit 2c165f8

2 files changed

Lines changed: 102 additions & 0 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
'@objectstack/core': patch
3+
'@objectstack/plugin-auth': patch
4+
'@objectstack/plugin-security': patch
5+
'@objectstack/cli': patch
6+
---
7+
8+
fix(core,plugin-auth)!: the last-admin guard judges a `sys_organization` insert that moves the Default Organization, and one definition of the Default Organization is exported from core
9+
10+
Clause-②: yes (narrowing)
11+
Metadata-protocol: none
12+
13+
<!-- adr-0087: not-required (no-migration-prescription) No key of any metadata schema is removed, renamed or re-shaped, so there is nothing for `objectstack migrate meta` to rewrite and no tombstone. The narrowing is one more organization write the last-administrator guard refuses at runtime. -->
14+
15+
**BREAKING** for a `single`-posture deployment whose only organization is not slugged `default` and whose last administrator holds nothing but the Default Organization's `admin_full_access` grant.
16+
17+
- **`registerLastAdminGuard` (`@objectstack/plugin-auth`) now judges a `sys_organization` insert.** Under `single` the Default Organization is the one slugged `default`, else the only one, and its `admin_full_access` grant is what makes the platform administrator one (ADR-0131 D5). Adding an organization can move it with no grant touched: a second organization beside a sole one with another slug leaves none, and a new one slugged `default` takes its place.
18+
- FROM: the insert was admitted. The administrator lost platform standing in silence, and the guard then admitted a ban or delete of that same administrator, because it no longer counted anyone.
19+
- TO: the insert is refused (`PERMISSION_DENIED`, 403, on `sys_organization`) when it would leave nobody who can sign in and administer. An organization owner or admin, an `OS_PLATFORM_OWNER_EMAIL` administrator, or a Default Organization slugged `default` (which a new organization cannot move) admits it, as on the update and delete halves. Under a wall no grant anchors, so the insert is admitted and costs no reads.
20+
- **One definition of the Default Organization (`@objectstack/core`).** New exports: `findDefaultOrganization(read)` (the rule, over a read the caller supplies), `DEFAULT_ORGANIZATION_SLUG`, and `readDefaultOrganizationId(ql)` (the resolver's own read, which fails loud on an unreachable store). plugin-auth's boot invariant, default-organization bootstrap, tenancy service and last-admin guard, plugin-security's `resolveDefaultOrganizationId`, and the ownership ceremony's plan now name the organization through it. Each keeps its own read policy: whether a failed read throws or reads as none, the guard's simulation of a pending write, and the ceremony's raw SQL. Two edge answers align with the resolver's. A `default`-slugged row with a blank id now falls through to the only-organization reading in the ceremony's plan. A numeric id is now read as its string in plugin-security's reader.
21+
22+
**What an operator does.** Before you create a second organization on a `single` deployment whose only organization has another slug, either give that organization the slug `default` (while it is the only one, that changes nothing else), or make sure another account can administer: an organization owner or admin, or an address in `OS_PLATFORM_OWNER_EMAIL`.
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
3+
/**
4+
* [ADR-0131 D3/D5] The one definition of the Default Organization, pinned on
5+
* its own: `slug = 'default'`, else the only organization, else none. Every
6+
* reader (the resolver, plugin-auth's boot invariant, bootstrap, tenancy
7+
* service and last-admin guard, plugin-security's promotion, the ownership
8+
* ceremony) hands its read to this rule, so these cases are what all of them
9+
* answer.
10+
*/
11+
12+
import { describe, expect, it } from 'vitest';
13+
14+
import { DEFAULT_ORGANIZATION_SLUG, findDefaultOrganization } from './default-organization.js';
15+
import { readDefaultOrganizationId } from './resolve-authz-context.js';
16+
17+
type Row = { id?: unknown; slug?: string };
18+
19+
/** A read over `rows` that answers the two questions the rule asks, and records them. */
20+
function readOver(rows: Row[]) {
21+
const asked: Array<{ where: Readonly<Record<string, unknown>>; limit: number }> = [];
22+
const read = async (where: Readonly<Record<string, unknown>>, limit: number): Promise<Row[]> => {
23+
asked.push({ where, limit });
24+
return rows.filter((row) => !('slug' in where) || row.slug === where.slug).slice(0, limit);
25+
};
26+
return { read, asked };
27+
}
28+
29+
describe('[ADR-0131 D3/D5] findDefaultOrganization — the one definition', () => {
30+
it('the organization slugged `default` wins, beside others, and the second read is never issued', async () => {
31+
const { read, asked } = readOver([{ id: 'org_acme', slug: 'acme' }, { id: 'org_def', slug: 'default' }]);
32+
expect(await findDefaultOrganization(read)).toEqual({ id: 'org_def', row: { id: 'org_def', slug: 'default' }, several: false });
33+
expect(asked).toEqual([{ where: { slug: DEFAULT_ORGANIZATION_SLUG }, limit: 1 }]);
34+
});
35+
36+
it('else the only organization, whatever its slug', async () => {
37+
const { read, asked } = readOver([{ id: 'org_acme', slug: 'acme' }]);
38+
expect(await findDefaultOrganization(read)).toMatchObject({ id: 'org_acme', several: false });
39+
expect(asked).toEqual([{ where: { slug: 'default' }, limit: 1 }, { where: {}, limit: 2 }]);
40+
});
41+
42+
it('several, none of them `default`, name none — and say so', async () => {
43+
const { read } = readOver([{ id: 'org_a', slug: 'a' }, { id: 'org_b', slug: 'b' }]);
44+
expect(await findDefaultOrganization(read)).toEqual({ id: undefined, row: undefined, several: true });
45+
});
46+
47+
it('no organization names none, and is not "several"', async () => {
48+
const { read } = readOver([]);
49+
expect(await findDefaultOrganization(read)).toEqual({ id: undefined, row: undefined, several: false });
50+
});
51+
52+
it('a row with no usable id names nothing: a blank-id `default` falls through to the only-organization reading', async () => {
53+
expect((await findDefaultOrganization(readOver([{ id: '', slug: 'default' }]).read)).id).toBeUndefined();
54+
expect((await findDefaultOrganization(readOver([{ id: null, slug: 'default' }, { id: 'org_b', slug: 'b' }]).read)).id)
55+
.toBeUndefined();
56+
});
57+
58+
it('a read that fails propagates: what an unreadable store means is the caller\'s read policy', async () => {
59+
const boom = new Error('store unreachable');
60+
await expect(findDefaultOrganization(async () => { throw boom; })).rejects.toBe(boom);
61+
});
62+
});
63+
64+
describe('[ADR-0131 D5] readDefaultOrganizationId — the resolver\'s read over the one definition', () => {
65+
const engineOver = (rows: Row[], registered = true) => ({
66+
registry: { getObject: (name: string) => (registered && name === 'sys_organization' ? { name } : undefined) },
67+
async find(_object: string, query: { where?: Record<string, unknown>; limit?: number }) {
68+
return readOver(rows).read(query.where ?? {}, query.limit ?? 100);
69+
},
70+
});
71+
72+
it('answers what the definition answers', async () => {
73+
expect(await readDefaultOrganizationId(engineOver([{ id: 'org_acme', slug: 'acme' }]))).toBe('org_acme');
74+
expect(await readDefaultOrganizationId(engineOver([{ id: 'org_acme', slug: 'acme' }, { id: 'org_b', slug: 'b' }]))).toBeUndefined();
75+
});
76+
77+
it('a composition that registers no organization object holds none, and is not read', async () => {
78+
expect(await readDefaultOrganizationId(engineOver([{ id: 'org_acme', slug: 'acme' }], false))).toBeUndefined();
79+
});
80+
});

0 commit comments

Comments
 (0)