Skip to content

Commit 24b77cf

Browse files
committed
feat(spec): declare the optional ISecurityService.describeEffectiveAccess and its EffectiveAccess answer types
Three TS interfaces beside DelegableScope in contracts/security-service.ts (EffectivePermissionSetOrigin, EffectivePermissionSet, EffectiveAccess) and one optional member after describeDelegableScope: a user's effective permission sets, each with its origin (default binding, held position or direct grant), resolved by the enforcement resolver and gated as explain is. Optional, so a security service that predates it still satisfies the contract. Type-level pins in security-service.test.ts; spec minor changeset. Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01FEkQ5odNVgHTd2MQbQCp5t
1 parent 733efbd commit 24b77cf

3 files changed

Lines changed: 159 additions & 0 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
'@objectstack/spec': minor
3+
---
4+
5+
feat(spec): declare the optional `ISecurityService.describeEffectiveAccess` and its `EffectiveAccess` answer types
6+
7+
Clause-②: yes (widening)
8+
Metadata-protocol: none
9+
10+
`@objectstack/spec/contracts` gains three exported types and one optional member of the `security` service contract, beside `DelegableScope` / `describeDelegableScope`:
11+
12+
- `EffectivePermissionSetOrigin`: how a permission set reaches a user, one of `{ kind: 'default' }` (a binding to the `everyone` audience anchor, or the human baseline the resolution adds), `{ kind: 'position'; position }` (a held position's definition names the set) and `{ kind: 'direct' }` (a `sys_user_permission_set` grant row of the user's).
13+
- `EffectivePermissionSet`: `{ name, origins }`, at least one origin; a set reached two ways is listed once with both.
14+
- `EffectiveAccess`: `{ userId, organizationId, positions, permissionSets }`, the sets in resolution order, in the caller's organization.
15+
- `ISecurityService.describeEffectiveAccess?(userId, callerContext?)`: describe a user's effective permission sets and where each comes from, resolved by the resolver enforcement uses, gated as `explain` is (`manage_users`, organization administration of an organization the user belongs to, or a delegated `adminScope` covering the user), with an unknown user refused exactly as one the caller may not manage. Optional: a security service that predates it omits it, and callers feature-detect and answer that the read is not served.
16+
17+
A TypeScript contract only: no schema, authorable key or runtime answer changes here. The reference implementation and its REST door land separately.

‎packages/spec/src/contracts/security-service.test.ts‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ import type {
1010
ControlledByParentWriteOutcome,
1111
ControlledByParentWriteDenialLeg,
1212
ControlledByParentWriteUnresolvedReason,
13+
EffectiveAccess,
14+
EffectivePermissionSet,
15+
EffectivePermissionSetOrigin,
1316
ExplainAccessRequest,
17+
SecurityContext,
1418
} from './security-service';
1519
import type { ExplainRequestSchema } from '../security/explain.zod';
1620

@@ -794,6 +798,79 @@ describe('Security Service Contract', () => {
794798
await expect(gate(missingRow)).resolves.toBe('refuse');
795799
});
796800

801+
it('describeEffectiveAccess is OPTIONAL — absence is typed, and it means the read is not served (compile-time)', async () => {
802+
// A security service that predates the read still satisfies the contract;
803+
// a caller feature-detects and answers "not served", never an empty list:
804+
// "no permission sets" is a claim about the user, absence is not.
805+
const withoutIt: ISecurityService = makeService();
806+
expect(typeof withoutIt.describeEffectiveAccess).toBe('undefined');
807+
const mustNotCompileWithoutAGuard = () =>
808+
// @ts-expect-error possibly undefined — a caller must feature-detect first
809+
withoutIt.describeEffectiveAccess('u2', { userId: 'u1' });
810+
expect(typeof mustNotCompileWithoutAGuard).toBe('function');
811+
// @ts-expect-error `describeEffectiveAccess` is OPTIONAL — not a required member
812+
const notRequired: RequiredMember = 'describeEffectiveAccess';
813+
expect(notRequired).toBe('describeEffectiveAccess');
814+
815+
const answer: EffectiveAccess = {
816+
userId: 'u2',
817+
organizationId: 'org_1',
818+
positions: ['sales_rep'],
819+
permissionSets: [
820+
{ name: 'member_default', origins: [{ kind: 'default' }] },
821+
{ name: 'sales_user', origins: [{ kind: 'direct' }, { kind: 'position', position: 'sales_rep' }] },
822+
],
823+
};
824+
const withIt = makeService({ describeEffectiveAccess: async () => answer });
825+
await expect(withIt.describeEffectiveAccess?.('u2', { userId: 'u1' })).resolves.toEqual(answer);
826+
});
827+
828+
it('EffectiveAccess: three closed origins, a position origin names its position, no organization is null (compile-time)', () => {
829+
const everyOrigin: EffectivePermissionSetOrigin[] = [
830+
{ kind: 'default' },
831+
{ kind: 'position', position: 'sales_rep' },
832+
{ kind: 'direct' },
833+
];
834+
expect(everyOrigin).toHaveLength(3);
835+
// @ts-expect-error a `position` origin must name the position
836+
const positionWithoutName: EffectivePermissionSetOrigin = { kind: 'position' };
837+
// @ts-expect-error the vocabulary is closed: a set reaches a user by default, position or direct grant
838+
const notAnOrigin: EffectivePermissionSetOrigin = { kind: 'inherited' };
839+
expect([positionWithoutName, notAnOrigin]).toHaveLength(2);
840+
841+
expectTypeOf<EffectivePermissionSet>().toEqualTypeOf<{
842+
name: string;
843+
origins: EffectivePermissionSetOrigin[];
844+
}>();
845+
expectTypeOf<EffectiveAccess>().toEqualTypeOf<{
846+
userId: string;
847+
organizationId: string | null;
848+
positions: string[];
849+
permissionSets: EffectivePermissionSet[];
850+
}>();
851+
expectTypeOf<NonNullable<ISecurityService['describeEffectiveAccess']>>().toEqualTypeOf<
852+
(userId: string, callerContext?: SecurityContext) => Promise<EffectiveAccess>
853+
>();
854+
855+
// An exhaustive consumer: a fourth origin added to the union would stop
856+
// this switch compiling at the `never` line.
857+
const label = (o: EffectivePermissionSetOrigin): string => {
858+
switch (o.kind) {
859+
case 'default':
860+
return 'default';
861+
case 'position':
862+
return `position:${o.position}`;
863+
case 'direct':
864+
return 'direct';
865+
default: {
866+
const unhandled: never = o;
867+
return unhandled;
868+
}
869+
}
870+
};
871+
expect(everyOrigin.map(label)).toEqual(['default', 'position:sales_rep', 'direct']);
872+
});
873+
797874
it('reopenAudienceBindingSuggestion is OPTIONAL — absence leaves every dismissal standing (compile-time)', async () => {
798875
// A security service that predates the re-open door still satisfies the
799876
// contract; a caller must feature-detect and answer "not served", never

‎packages/spec/src/contracts/security-service.ts‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,52 @@ export interface PositionGrantSummary {
152152
permissionSets: Array<{ name: string; via: PositionGrantVia }>;
153153
}
154154

155+
/**
156+
* How a permission set reaches a user, in {@link EffectivePermissionSet}:
157+
*
158+
* - `default` — a default binding: the set is bound to the `everyone`
159+
* audience anchor (the deployment baseline, an app's `isDefault` set, an
160+
* accepted package default; ADR-0090 D5), or it is the human baseline the
161+
* resolution adds for every authenticated member (`external_default` for an
162+
* external member).
163+
* - `position` — the definition of a position the user holds names it in
164+
* `permissionSets` (ADR-0131 D3/D4); `position` is that position's name.
165+
* `everyone` is never reported here: its bindings are `default`.
166+
* - `direct` — one of the user's own `sys_user_permission_set` grant rows
167+
* names it.
168+
*/
169+
export type EffectivePermissionSetOrigin =
170+
| { kind: 'default' }
171+
| { kind: 'position'; position: string }
172+
| { kind: 'direct' };
173+
174+
/** One permission set in {@link EffectiveAccess}. */
175+
export interface EffectivePermissionSet {
176+
/** The permission set's machine name. */
177+
name: string;
178+
/**
179+
* Every way the set reaches the user, at least one. A set reached two ways
180+
* is listed once, with both origins (a direct grant and a position, or two
181+
* positions).
182+
*/
183+
origins: EffectivePermissionSetOrigin[];
184+
}
185+
186+
/** Return shape of {@link ISecurityService.describeEffectiveAccess}. */
187+
export interface EffectiveAccess {
188+
/** The user described. */
189+
userId: string;
190+
/**
191+
* The organization the access was resolved in: the caller's active one,
192+
* vetted for the described user as `explain` vets it. `null` with none.
193+
*/
194+
organizationId: string | null;
195+
/** The positions the user holds there, in force (the resolver's `positions`). */
196+
positions: string[];
197+
/** The permission sets in force for the user there, in resolution order. */
198+
permissionSets: EffectivePermissionSet[];
199+
}
200+
155201
/** Selector for {@link ISecurityService.explain}. */
156202
export interface ExplainAccessRequest {
157203
/** Object API name to explain access for. */
@@ -1194,6 +1240,25 @@ export interface ISecurityService {
11941240
*/
11951241
describeDelegableScope(callerContext?: SecurityContext): Promise<DelegableScope>;
11961242

1243+
/**
1244+
* [ADR-0090 D6/D12] Describe ANOTHER user's (or the caller's own) effective
1245+
* permission sets, each with where it comes from: a default binding, a held
1246+
* position, or a direct grant. Resolved in the caller's organization by the
1247+
* resolver enforcement uses, so the list matches enforcement by construction.
1248+
*
1249+
* Gated as `explain` is: describing someone else requires `manage_users`,
1250+
* organization administration of an organization that user is a member of,
1251+
* or a delegated `adminScope` covering that user. A user who does not exist
1252+
* is refused exactly as one the caller may not manage, so the answer says
1253+
* nothing about which ids exist.
1254+
*
1255+
* **OPTIONAL, and absence is a defined state.** A security service that
1256+
* predates it omits it; callers feature-detect
1257+
* (`typeof svc.describeEffectiveAccess === 'function'`) and answer that the
1258+
* read is not served (a REST door answers 501), never an empty list.
1259+
*/
1260+
describeEffectiveAccess?(userId: string, callerContext?: SecurityContext): Promise<EffectiveAccess>;
1261+
11971262
/**
11981263
* List install-time audience-binding suggestions (a package proposes its
11991264
* `isDefault` set for every signed-in member; a `manage_metadata` holder

0 commit comments

Comments
 (0)