Skip to content

Commit db6f41c

Browse files
committed
feat(rest,client): serve GET /security/users/:id/effective-access beside explain
The door relays the security service's describeEffectiveAccess through the explain family's one emitter with an empty closed query set, ledgered as sdk; the client method returns the spec's EffectiveAccess. Claude-Session: https://claude.ai/code/session_01FEkQ5kUXrK37qc2vkfEHQ4 Co-authored-by: Claude <noreply@anthropic.com>
1 parent db560eb commit db6f41c

9 files changed

Lines changed: 499 additions & 7 deletions

File tree

‎packages/client/src/index.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ import type {
176176
AudienceBindingSuggestionSync,
177177
AutomationResult,
178178
DelegableScope,
179+
EffectiveAccess,
179180
FlowRuntimeState,
180181
ImportObjectResult,
181182
ObjectDraft,
@@ -6626,6 +6627,24 @@ export class ObjectStackClient {
66266627
return this.unwrapResponse<DelegableScope>(res);
66276628
},
66286629

6630+
/**
6631+
* ADR-0090 D6/D12 — one user's effective permission sets, each with
6632+
* every origin it arrives through: a default binding, a held position,
6633+
* a direct grant, or a name derived from the user's standing. Resolved
6634+
* in the caller's organization by the resolver enforcement uses.
6635+
*
6636+
* Gated as `explain` is: reading someone else needs `manage_users`,
6637+
* organization administration covering them, or a delegated
6638+
* `adminScope` covering them. An unknown id is refused with the same
6639+
* `403` as one the caller may not manage.
6640+
*/
6641+
describeEffectiveAccess: async (userId: string): Promise<EffectiveAccess> => {
6642+
const res = await this.fetch(
6643+
`${this.baseUrl}/api/v1/security/users/${encodeURIComponent(userId)}/effective-access`,
6644+
);
6645+
return this.unwrapResponse<EffectiveAccess>(res);
6646+
},
6647+
66296648
suggestedBindings: {
66306649
/** List suggestions, optionally by `status` / `packageId` (reconciles first). */
66316650
list: async (opts?: { status?: string; packageId?: string }): Promise<{ suggestions: AudienceBindingSuggestion[]; synced: AudienceBindingSuggestionSync }> => {

‎packages/client/src/return-type-precision.test.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ import type {
8484
AnalyticsResult,
8585
AutomationResult,
8686
DelegableScope,
87+
EffectiveAccess,
8788
FlowRuntimeState,
8889
FlowUnboundCause,
8990
ImportObjectResult,
@@ -139,6 +140,8 @@ export async function returnTypePrecisionPins(): Promise<void> {
139140
// ── shape class 1: the contract type, served bare ────────────────────
140141
// `res.json(await svc.describeDelegableScope(context))` — no envelope.
141142
expectTypeOf(await client.security.describeDelegableScope()).toEqualTypeOf<DelegableScope>();
143+
// `res.json(await svc.describeEffectiveAccess(id, context))` — no envelope.
144+
expectTypeOf(await client.security.describeEffectiveAccess('u1')).toEqualTypeOf<EffectiveAccess>();
142145

143146
// ── shape class 2: contract type inside a route-built ENVELOPE ───────
144147
// The census's highest-risk band: `sendOk(res, { tables })` means the
@@ -235,6 +238,9 @@ export async function returnTypePrecisionPins(): Promise<void> {
235238
// @ts-expect-error a DelegableScope is not a string
236239
const wrongScope: string = await client.security.describeDelegableScope();
237240

241+
// @ts-expect-error an EffectiveAccess is not a bare list of permission sets
242+
const wrongAccess: EffectiveAccess['permissionSets'] = await client.security.describeEffectiveAccess('u1');
243+
238244
// @ts-expect-error the route answers `{ tables }`, not a bare array
239245
const wrongTables: RemoteTable[] = await client.datasources.external.listTables('ds');
240246

@@ -254,6 +260,7 @@ export async function returnTypePrecisionPins(): Promise<void> {
254260
const wrongClone: CloneDataResult = await client.data.create('lead', {});
255261

256262
void wrongScope;
263+
void wrongAccess;
257264
void wrongTables;
258265
void wrongFlow;
259266
void wrongSearch;

‎packages/core/src/security/resolve-authz-context.direct-permissions.test.ts‎

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,12 @@ const GRANTS = [
5656
{ permission_set: 'elsewhere', organization_id: ORG_B },
5757
];
5858

59-
async function resolve(tenantId: string | undefined, ledger: unknown[] = []) {
59+
async function resolve(tenantId: string | undefined, ledger: unknown[] = [], extra: { withPermissionOrigins?: boolean } = {}) {
60+
const { out } = await resolveRecorded(tenantId, ledger, extra);
61+
return out;
62+
}
63+
64+
async function resolveRecorded(tenantId: string | undefined, ledger: unknown[] = [], extra: { withPermissionOrigins?: boolean } = {}) {
6065
const ql = bindStaticSecurityCatalog(makeRecordingQl({
6166
sys_user: [{ id: 'u1', email: 'u1@example.com' }],
6267
sys_member: [{ user_id: 'u1', organization_id: ORG_A, role: 'member' }, { user_id: 'u1', organization_id: ORG_B, role: 'member' }],
@@ -67,7 +72,8 @@ async function resolve(tenantId: string | undefined, ledger: unknown[] = []) {
6772
sys_permission_set: [],
6873
sys_metadata_activation: ledger,
6974
}), definitions);
70-
return resolveUserAuthzGrants(ql, 'u1', tenantId ? { tenantId } : {});
75+
const out = await resolveUserAuthzGrants(ql, 'u1', { ...(tenantId ? { tenantId } : {}), ...extra });
76+
return { out, calls: (ql as any).calls as Array<{ object: string }> };
7177
}
7278

7379
describe('[#25799] directPermissions is the resolver\'s own direct channel', () => {
@@ -97,3 +103,28 @@ describe('[#25799] directPermissions is the resolver\'s own direct channel', ()
97103
expect(out.directPermissions).toEqual(['both_channels']);
98104
});
99105
});
106+
107+
describe('[ADR-0090 D6] withPermissionOrigins records each name\'s channels, and changes nothing else', () => {
108+
it('a set reached through a grant row and the `everyone` anchor carries both, in channel order; each other name its one', async () => {
109+
const out = await resolve(ORG_A, [], { withPermissionOrigins: true });
110+
expect(out.permissionOrigins).toEqual({
111+
tools: [{ kind: 'direct' }],
112+
both_channels: [{ kind: 'direct' }, { kind: 'default' }],
113+
definition_only: [{ kind: 'default' }],
114+
});
115+
});
116+
117+
it('a switched-off set keeps no origin, since the envelope does not hold it', async () => {
118+
const out = await resolve(ORG_A, [{ metadata_type: 'permission', name: 'tools', package_id: null, active: false }], { withPermissionOrigins: true });
119+
expect(out.permissionOrigins).not.toHaveProperty('tools');
120+
});
121+
122+
it('the envelope and the reads are byte-identical to a resolution that did not ask', async () => {
123+
const plain = await resolveRecorded(ORG_A);
124+
const asked = await resolveRecorded(ORG_A, [], { withPermissionOrigins: true });
125+
expect(plain.out).not.toHaveProperty('permissionOrigins');
126+
const { permissionOrigins: _origins, ...rest } = asked.out;
127+
expect(rest).toEqual(plain.out);
128+
expect(asked.calls.map((c) => c.object)).toEqual(plain.calls.map((c) => c.object));
129+
});
130+
});

‎packages/core/src/security/resolve-authz-context.ts‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -890,8 +890,9 @@ export interface UserAuthzGrants {
890890
* audience anchor, whose bindings are the default ones), and `derived` where
891891
* the configured platform administrators (§6b-config) or the `ai_access` flag
892892
* (§7) confer the name. Every name in `permissions` the resolution derived has
893-
* at least one origin, each origin listed once, in the order the resolution
894-
* reached it; a `seedPermissions` name the caller supplied has none.
893+
* at least one origin, each listed once, in the order the envelope lists its
894+
* channels — `direct`, then `position` (in held-position order), `default`,
895+
* `derived`; a `seedPermissions` name the caller supplied has none.
895896
*/
896897
permissionOrigins?: Record<string, EffectivePermissionSetOrigin[]>;
897898
}
@@ -1873,8 +1874,8 @@ export async function resolveUserAuthzGrants(
18731874
const grantsCache = openUserGrantsCache(ql, userId, cacheOpts);
18741875
if (grantsCache?.hit) return grantsCache.hit;
18751876

1876-
// [ADR-0090 D6] The origin record, when asked for: each origin once per name,
1877-
// in the order the derivation below reaches it.
1877+
// [ADR-0090 D6] The origin record, when asked for: each origin once per name.
1878+
// Ordered by channel when the envelope is returned (see `permissionOrigins`).
18781879
const origins = opts.withPermissionOrigins ? new Map<string, EffectivePermissionSetOrigin[]>() : undefined;
18791880
const recordOrigin = (name: string, origin: EffectivePermissionSetOrigin): void => {
18801881
if (!origins) return;
@@ -2344,10 +2345,12 @@ export async function resolveUserAuthzGrants(
23442345
// [ADR-0090 D6] Only names the envelope holds keep an origin: a bound name
23452346
// the catalog holds no set for, or a switched-off set, confers nothing.
23462347
if (origins) {
2348+
const channelRank: Record<EffectivePermissionSetOrigin['kind'], number> = { direct: 0, position: 1, default: 2, derived: 3 };
23472349
const permissionOrigins: Record<string, EffectivePermissionSetOrigin[]> = {};
23482350
for (const name of grants.permissions) {
23492351
const list = origins.get(name);
2350-
if (list) permissionOrigins[name] = list;
2352+
// A stable sort: positions keep the held-position order they were reached in.
2353+
if (list) permissionOrigins[name] = [...list].sort((a, b) => channelRank[a.kind] - channelRank[b.kind]);
23512354
}
23522355
grants.permissionOrigins = permissionOrigins;
23532356
}

0 commit comments

Comments
 (0)