@@ -583,43 +583,49 @@ function mayName(t: DelegatedTarget, side: 'next' | 'prev', column: string): boo
583583 return t . reads . has ( column ) ;
584584}
585585
586- /** The direct-grant holder column, and the object it references. */
587- const GRANT_HOLDER_FIELD = 'user_id' ;
588- const GRANT_HOLDER_OBJECT = 'sys_user' ;
586+ /** The object a target user is a row of: what a direct grant's `user_id` references. */
587+ const TARGET_USER_OBJECT = 'sys_user' ;
589588
590589/**
591- * Does the write NAME the grant's holder — an insert's row, or an update whose
592- * payload points the grant at a different user than the row stores? A holder
593- * the row already stores is not named: the engine's reference check forgives
594- * a value the row stores, and the grant-holder membership hook judges it as
595- * stored (`grant-holder-membership-refusal.ts`, `onUpdate`).
596- */
597- function namesTargetUser ( t : DelegatedTarget ) : boolean {
598- if ( ! t . next ) return false ;
599- if ( ! t . prev ) return true ;
600- const norm = ( v : unknown ) => ( v === undefined || v === null || v === '' ? null : String ( v ) ) ;
601- return norm ( t . next [ GRANT_HOLDER_FIELD ] ) !== norm ( t . prev [ GRANT_HOLDER_FIELD ] ) ;
602- }
603-
604- /**
605- * Does the engine's reference check ask the caller's own read door about the
606- * holder `object`'s write names, and refuse one it does not return? It asks
607- * when the live schema's `user_id` is a non-`readonly` `lookup` / `user`
608- * reference to `sys_user` and `sys_user` is registered
609- * (`assertReferencesResolve`, `@objectstack/objectql`; the grant-holder
610- * membership hook's `referenceCheckAsksCaller` steps aside on the same test).
611- * The direct-grant object declares that reference itself
612- * (`sys-user-permission-set.object.ts`), so its target is not resolved again
613- * here: the test reads only what a composition can still change, whether
614- * `sys_user` is registered and whether the live field is still a writable
615- * lookup. Anywhere else, an engine that hands over no schema included, it
616- * answers no, and the holder is judged on its memberships alone: the gate
617- * refuses no named holder the write would not be refused for anyway.
590+ * [ADR-0090 D12; ruling on #26059, Q3] Does the gate judge a target user
591+ * through the caller's own read ({@link DelegatedAdminGate.callerReadsUser})?
592+ * Wherever that read can be asked at all: `sys_user` is registered on the
593+ * engine, and the engine offers a `find` to ask it with. Then a delegated
594+ * answer about a user the caller cannot read does not depend on that user's
595+ * existence or memberships: such a user holds no business unit a subtree
596+ * covers, exactly as an id naming no user holds none
597+ * ({@link DelegatedAdminGate.targetUserUnits}). Anywhere else, an engine that
598+ * hands over no schema included, no user is readable to ask about, and a
599+ * target user is judged on its memberships alone.
600+ *
601+ * The test is the gate's own reason, never the schema shape of a grant's
602+ * `user_id`, and it holds for every target user the gate judges: the holder a
603+ * direct-grant write names (an insert, an update pointing the grant at another
604+ * user), the holder a stored grant keeps (a delete, an update that leaves
605+ * `user_id` as stored), and the user the explain doors ask about
606+ * ({@link DelegatedAdminGate.scopesCoverUser}). Were it the column's shape,
607+ * the answer about one unreadable user would change with whether `user_id` is
608+ * still a writable lookup.
609+ *
610+ * Two other doors ask the caller's read about a grant's holder, each only for
611+ * a holder the write NAMES. The engine's reference check asks it of a
612+ * `user_id` a write names when the live schema's `user_id` is a
613+ * non-`readonly` `lookup` / `user` reference to a registered `sys_user`
614+ * (`assertReferencesResolve`, `@objectstack/objectql`), and forgives a value
615+ * the row already stores. The grant-holder membership hook asks it through
616+ * `callerReadsHolder` and steps aside for the reference check on that same
617+ * test (`referenceCheckAsksCaller`), and judges a holder the row stores on its
618+ * membership, as stored (`grant-holder-membership-refusal.ts`, `onUpdate`).
619+ * That "as stored" mirror does not hold for this gate. Those doors validate
620+ * the value a write stores, and a value already stored was validated when it
621+ * was written; this gate JUDGES the target user — is it inside the caller's
622+ * subtree? — and that answer is about the user, whichever write shape carries
623+ * it, so a holder the row stores is asked of the caller's read too.
618624 */
619- function referenceCheckAsksCallerAboutUser ( ql : any , object : string ) : boolean {
620- if ( typeof ql ?. getSchema !== 'function' || ! ql . getSchema ( GRANT_HOLDER_OBJECT ) ) return false ;
621- const def = ql . getSchema ( object ) ?. fields ?. [ GRANT_HOLDER_FIELD ] ;
622- return Boolean ( def ) && def . readonly !== true && ( def . type === 'lookup' || def . type === 'user' ) ;
625+ function judgesTargetUserThroughCallerRead ( ql : any ) : boolean {
626+ return typeof ql ?. find === 'function'
627+ && typeof ql ?. getSchema === 'function'
628+ && Boolean ( ql . getSchema ( TARGET_USER_OBJECT ) ) ;
623629}
624630
625631export class DelegatedAdminGate {
@@ -796,6 +802,14 @@ export class DelegatedAdminGate {
796802 * `callerContext` supplies the organization the scopes resolve in. Omitted,
797803 * or naming no organization, it covers nobody: a delegated scope held in a
798804 * context that names no organization covers nothing ({@link resolveHeldScopes}).
805+ *
806+ * [ADR-0090 D12] It covers the user only through a business unit the write
807+ * gate would judge it by ({@link targetUserUnits}): a user the caller's own
808+ * read, under `callerContext`, does not return is covered by no scope, as an
809+ * id naming no user is not. So the answer about a user the caller cannot
810+ * read does not depend on that user's existence or memberships. That read is
811+ * asked only once a scope has resolved, so a context that names no
812+ * organization asks it nothing.
799813 */
800814 async scopesCoverUser (
801815 sets : PermissionSet [ ] ,
@@ -804,7 +818,7 @@ export class DelegatedAdminGate {
804818 ) : Promise < boolean > {
805819 const held = await this . resolveHeldScopes ( sets , callerOrganizationId ( callerContext ) ) ;
806820 if ( held . length === 0 ) return false ;
807- const userBUs = await this . businessUnitsOfUser ( targetUserId ) ;
821+ const userBUs = await this . targetUserUnits ( callerContext , targetUserId ) ;
808822 if ( userBUs . size === 0 ) return false ;
809823 for ( const s of held ) {
810824 for ( const bu of userBUs ) if ( s . subtree . has ( bu ) ) return true ;
@@ -1178,20 +1192,23 @@ export class DelegatedAdminGate {
11781192 * grant-holder membership hook asks before it
11791193 * (`grant-holder-membership-refusal.ts`, `callerReadsHolder`): one `sys_user`
11801194 * read WHERE `id` equals the value, projected to `id`, under the caller's
1181- * own context, so every read visibility and wall answers it, and only a row
1182- * whose id IS the value counts. A user it does not return is a user the
1183- * caller cannot read, answered as a user that does not exist (the
1184- * platform's rule for every user a non-system write names). A read the read
1185- * door REFUSES (a declared 4xx) returns no user either; a store fault
1186- * propagates as raised, as for the gate's other reads: an outage is neither
1187- * an answer nor a refusal.
1195+ * own `context` — a write's `opCtx.context`, the explain doors'
1196+ * `callerContext`, never the system's — so every read visibility and wall
1197+ * answers it, and only a row whose id IS the value counts. A user it does
1198+ * not return is a user the caller cannot read, answered as a user that does
1199+ * not exist (the platform's rule for every user a non-system write names,
1200+ * and the gate's for every target user it judges,
1201+ * {@link judgesTargetUserThroughCallerRead}). A read the read door REFUSES
1202+ * (a declared 4xx) returns no user either; a store fault propagates as
1203+ * raised, as for the gate's other reads: an outage is neither an answer nor
1204+ * a refusal.
11881205 */
1189- private async callerReadsUser ( opCtx : any , userId : string ) : Promise < boolean > {
1206+ private async callerReadsUser ( context : any , userId : string ) : Promise < boolean > {
11901207 const ql = this . deps . ql ;
11911208 if ( ! ql ?. find ) return false ;
11921209 let rows : unknown ;
11931210 try {
1194- rows = await ql . find ( GRANT_HOLDER_OBJECT , { where : { id : userId } , fields : [ 'id' ] , context : opCtx . context } ) ;
1211+ rows = await ql . find ( TARGET_USER_OBJECT , { where : { id : userId } , fields : [ 'id' ] , context } ) ;
11951212 } catch ( e ) {
11961213 const status = declaredHttpStatus ( e ) ;
11971214 if ( status !== undefined && status < 500 ) return false ;
@@ -1200,6 +1217,26 @@ export class DelegatedAdminGate {
12001217 return ( Array . isArray ( rows ) ? rows : [ ] ) . some ( ( r : any ) => r ?. id != null && String ( r . id ) === userId ) ;
12011218 }
12021219
1220+ /**
1221+ * [ADR-0090 D12; ruling on #26059, Q3] The business units through which a
1222+ * delegated subtree may cover target user `userId`, as answered to the
1223+ * caller whose own context is `context`: the user's memberships
1224+ * ({@link businessUnitsOfUser}, read by the system) when the caller's own
1225+ * read returns the user ({@link callerReadsUser}), and none when it does
1226+ * not. A user the caller cannot read thus holds no unit any subtree covers,
1227+ * exactly as an id naming no user holds none, so every delegated answer
1228+ * about it — the write gate's refusal, the explain doors' coverage — is the
1229+ * answer about no user. Asked wherever the gate judges a target through the
1230+ * caller's read ({@link judgesTargetUserThroughCallerRead}); elsewhere the
1231+ * memberships alone. A store fault in the caller's read propagates.
1232+ */
1233+ private async targetUserUnits ( context : any , userId : string ) : Promise < Set < string > > {
1234+ if ( judgesTargetUserThroughCallerRead ( this . deps . ql ) && ! ( await this . callerReadsUser ( context , userId ) ) ) {
1235+ return new Set < string > ( ) ;
1236+ }
1237+ return this . businessUnitsOfUser ( userId ) ;
1238+ }
1239+
12031240 /**
12041241 * Is position `positionName` inside the organization boundary? `null` when
12051242 * every set it carries is; otherwise the first failure, naming the set.
@@ -1595,15 +1632,13 @@ export class DelegatedAdminGate {
15951632 const row = t . next ?? t . prev ?? { } ;
15961633 const { setRow, setName, unresolved, setNamed } = await this . directGrantSet ( t , row ) ;
15971634 const targetUserId = row . user_id ? String ( row . user_id ) : null ;
1598- // A target user the write NAMES, where the engine's reference check asks
1599- // the caller's read door about it, is first asked of that door
1600- // ({ @link callerReadsUser}): a user it does not return is answered as a
1601- // user that does not exist, which holds no business unit the subtree can
1602- // cover. So an unreadable target is outside the subtree, whatever it is.
1635+ // A target user — one the write names, or one the grant stores — is first
1636+ // asked of the caller's own read door ({ @link targetUserUnits}): a user it
1637+ // does not return is answered as a user that does not exist, which holds
1638+ // no business unit the subtree can cover. So an unreadable target is
1639+ // outside the subtree, whatever it is and whichever write carries it .
16031640 const userBUs = targetUserId
1604- && ( ! namesTargetUser ( t ) || ! referenceCheckAsksCallerAboutUser ( this . deps . ql , opCtx . object )
1605- || ( await this . callerReadsUser ( opCtx , targetUserId ) ) )
1606- ? await this . businessUnitsOfUser ( targetUserId )
1641+ ? await this . targetUserUnits ( opCtx . context , targetUserId )
16071642 : new Set < string > ( ) ;
16081643 // [ruling of record 6649064874] Named only when the caller
16091644 // wrote or reads it ({@link mayName}); the verdict is the stored row's.
@@ -1621,11 +1656,11 @@ export class DelegatedAdminGate {
16211656 // [ADR-0090 D12; ruling on #26059, Q3] A target user is inside the
16221657 // subtree only through a business unit of the subtree. A user holding
16231658 // no business-unit membership has none, and neither has an id naming
1624- // no user, nor a named user the caller cannot read (above): each is
1625- // outside the subtree, exactly as a user whose units all lie elsewhere
1626- // is. So the refusal is one reason, one class, one sentence and one set
1627- // of details for all of them, whether or not the target exists or
1628- // holds a membership.
1659+ // no user, nor a user the caller cannot read, named or stored (above):
1660+ // each is outside the subtree, exactly as a user whose units all lie
1661+ // elsewhere is. So the refusal is one reason, one class, one sentence
1662+ // and one set of details for all of them, whether or not the target
1663+ // exists or holds a membership.
16291664 if ( ! [ ...userBUs ] . some ( ( bu ) => s . subtree . has ( bu ) ) ) {
16301665 return outsideScope ( userNamed
16311666 ? `target user '${ targetUserId } ' is outside the delegated subtree`
0 commit comments