@@ -43,6 +43,9 @@ const eastScope = (allow: string[]) => ({
4343 assignablePermissionSets : allow ,
4444} ) ;
4545const CRUD = { sys_user_position : { allowRead : true , allowCreate : true , allowEdit : true , allowDelete : true } } ;
46+ /** [#29029] A plain object whose `unit` lookup names a business unit. */
47+ const UNIT_NOTE = 'qa_unit_note' ;
48+ const NOTE_CRUD = { allowRead : true , allowCreate : true , allowEdit : true } ;
4649
4750/**
4851 * The catalog. `field_rep` and `field_lead` each list only `field_user`, and
@@ -60,7 +63,9 @@ const PERMISSIONS = [
6063 // Carries an adminScope equal to the wide delegate's own: not strictly contained by it.
6164 { name : 'field_lead' , label : 'Field lead (own name)' , objects : CRUD , adminScope : eastScope ( [ 'field_user' , 'field_rep' , 'field_lead' ] ) } ,
6265 // Narrow: allowlists the sets the positions LIST, not their own-name sets.
63- { name : 'east_admin' , label : 'East delegate' , objects : CRUD , adminScope : eastScope ( [ 'field_user' ] ) } ,
66+ { name : 'east_admin' , label : 'East delegate' , objects : { ...CRUD , [ UNIT_NOTE ] : NOTE_CRUD } , adminScope : eastScope ( [ 'field_user' ] ) } ,
67+ // [#29029] A writer of the unit note with no admin scope at all.
68+ { name : 'note_writer' , label : 'Note writer' , objects : { [ UNIT_NOTE ] : NOTE_CRUD } } ,
6469 // Wide: also allowlists the own-name sets.
6570 { name : 'east_admin_wide' , label : 'East delegate (wide)' , objects : CRUD , adminScope : eastScope ( [ 'field_user' , 'field_rep' , 'field_lead' ] ) } ,
6671] ;
@@ -74,8 +79,10 @@ const POSITIONS = [
7479type Posture = 'single' | 'group' | 'isolated' ;
7580const POSTURES : Posture [ ] = [ 'single' , 'group' , 'isolated' ] ;
7681
82+ /** The users of `ORG_A`, as the runtime resolves them onto every session's context from `sys_member`. */
83+ const ORG_USER_IDS = [ 'u_delegate' , 'u_delegate_wide' , 'u_target' , 'u_writer' , 'u_org_admin' ] ;
7784const delegateCtx = ( userId : string , set : string ) =>
78- ( { userId, positions : [ ] , permissions : [ set ] , tenantId : ORG_A , accessible_org_ids : [ ORG_A ] } ) ;
85+ ( { userId, positions : [ ] , permissions : [ set ] , tenantId : ORG_A , accessible_org_ids : [ ORG_A ] , org_user_ids : ORG_USER_IDS } ) ;
7986const NARROW = delegateCtx ( 'u_delegate' , 'east_admin' ) ;
8087const WIDE = delegateCtx ( 'u_delegate_wide' , 'east_admin_wide' ) ;
8188
@@ -100,11 +107,22 @@ async function boot(posture: Posture) {
100107 version : '1.0.0' ,
101108 type : 'plugin' ,
102109 scope : 'system' ,
103- objects : [ SysUserPosition , SysUserPermissionSet , SysMember , SysBusinessUnit , SysBusinessUnitMember ] ,
110+ // [#29029] `sys_user` provisioned: an assignment's `user_id` names a user
111+ // the caller must be able to read, so the table the read door asks exists.
112+ objects : [
113+ SysUser , SysUserPosition , SysUserPermissionSet , SysMember , SysBusinessUnit , SysBusinessUnitMember ,
114+ {
115+ name : UNIT_NOTE ,
116+ label : 'Unit note' ,
117+ sharingModel : 'public_read_write' ,
118+ fields : {
119+ name : { name : 'name' , type : 'text' } ,
120+ unit : { name : 'unit' , type : 'lookup' , reference : 'sys_business_unit' } ,
121+ } ,
122+ } ,
123+ ] ,
104124 } as any ) ;
105125 await engine . syncSchemas ( ) ;
106- // `sys_user` registered and unprovisioned, as in the sibling posture suite.
107- if ( ! engine . registry . getObject ( SysUser . name ) ) engine . registry . registerObject ( SysUser as never , 'qa.delegated-own-name' ) ;
108126 engines . push ( engine ) ;
109127
110128 const services : Record < string , unknown > = {
@@ -140,8 +158,10 @@ async function boot(posture: Posture) {
140158 { id : 'bu_east' , name : 'east' , parent_business_unit_id : 'bu_hq' , organization_id : ORG_A } ,
141159 { id : 'bu_east_field' , name : 'east_field' , parent_business_unit_id : 'bu_east' , organization_id : ORG_A } ,
142160 ] , sys ) ;
143- for ( const userId of [ 'u_delegate' , 'u_delegate_wide' , 'u_target' ] ) {
144- await engine . insert ( 'sys_member' , { id : `m_${ userId } ` , user_id : userId , organization_id : ORG_A , role : 'member' } , sys ) ;
161+ for ( const userId of ORG_USER_IDS ) {
162+ await engine . insert ( 'sys_user' , { id : userId , email : `${ userId } @example.test` , name : userId } , sys ) ;
163+ const role = userId === 'u_org_admin' ? 'admin' : 'member' ;
164+ await engine . insert ( 'sys_member' , { id : `m_${ userId } ` , user_id : userId , organization_id : ORG_A , role } , sys ) ;
145165 }
146166 await engine . insert ( 'sys_business_unit_member' ,
147167 { id : 'bum_target' , user_id : 'u_target' , business_unit_id : 'bu_east_field' , organization_id : ORG_A } , sys ) ;
@@ -236,3 +256,74 @@ describe('[ADR-0090 D12, amended 2026-10-17; #24757] a delegate is judged on exa
236256 } ) ;
237257 }
238258} ) ;
259+
260+ /** [#29029] Every unit id `context` reads, sorted, or the refusal's status and code. */
261+ async function unitsRead ( h : Awaited < ReturnType < typeof boot > > , context : any ) : Promise < string [ ] | { status : number ; code ?: string } > {
262+ try {
263+ const rows = await h . engine . find ( 'sys_business_unit' , { context } as any ) ;
264+ return ( Array . isArray ( rows ) ? rows : [ ] ) . map ( ( r : any ) => String ( r . id ) ) . sort ( ) ;
265+ } catch ( e ) {
266+ const r = resolveThrownHttpError ( e ) ;
267+ return { status : r . status , code : r . code } ;
268+ }
269+ }
270+
271+ /** [#29029] A unit note naming `unit`, written as `context`: the outcome, and the refused field's code. */
272+ async function noteNaming ( h : Awaited < ReturnType < typeof boot > > , context : any , unit : string ) {
273+ try {
274+ await h . engine . insert ( UNIT_NOTE , { name : `note on ${ unit } ` , unit } , { context } as any ) ;
275+ return { status : 200 } ;
276+ } catch ( e ) {
277+ const r = resolveThrownHttpError ( e ) ;
278+ const fields = ( ( e as any ) ?. fields ?? [ ] ) as Array < { field ?: string ; code ?: string } > ;
279+ return { status : r . status , code : r . code , fields : fields . map ( ( f ) => `${ f . field } :${ f . code } ` ) } ;
280+ }
281+ }
282+
283+ async function storedNotes ( h : Awaited < ReturnType < typeof boot > > ) : Promise < string [ ] > {
284+ const rows = await h . engine . find ( UNIT_NOTE , { context : SYS } ) ;
285+ return ( Array . isArray ( rows ) ? rows : [ ] ) . map ( ( r : any ) => String ( r . unit ) ) . sort ( ) ;
286+ }
287+
288+ describe ( '[ADR-0090 D12, ruling of record 6663849489; #29029] a delegate reads the business units its scope covers, and no other — three postures' , ( ) => {
289+ const WRITER = delegateCtx ( 'u_writer' , 'note_writer' ) ;
290+ const ORG_ADMIN = delegateCtx ( 'u_org_admin' , 'organization_admin' ) ;
291+
292+ for ( const posture of POSTURES ) {
293+ describe ( `posture '${ posture } '` , ( ) => {
294+ it ( 'the delegate reads exactly the units its scope covers: the scope root and its subtree, not the unit above it' , async ( ) => {
295+ const h = await boot ( posture ) ;
296+ expect ( await unitsRead ( h , NARROW ) ) . toEqual ( [ 'bu_east' , 'bu_east_field' ] ) ;
297+ const above = await h . engine . findOne ( 'sys_business_unit' , { where : { id : 'bu_hq' } , context : NARROW } as any ) ;
298+ expect ( above ?? null ) . toBeNull ( ) ;
299+ expect ( await h . engine . count ( 'sys_business_unit' , { context : NARROW } as any ) ) . toBe ( 2 ) ;
300+ } ) ;
301+
302+ it ( 'the delegate assigns a position in a unit its scope covers, and the assignment is stored' , async ( ) => {
303+ const h = await boot ( posture ) ;
304+ expect ( await assign ( h , NARROW , 'field_rep' ) ) . toEqual ( { status : 200 } ) ;
305+ expect ( await storedPositions ( h ) ) . toEqual ( [ 'field_rep' ] ) ;
306+ } ) ;
307+
308+ it ( 'a unit outside the scope is a unit the delegate cannot read: a reference to it answers reference_not_found, and one inside is admitted' , async ( ) => {
309+ const h = await boot ( posture ) ;
310+ expect ( await noteNaming ( h , NARROW , 'bu_hq' ) ) . toEqual ( { status : 400 , code : 'VALIDATION_FAILED' , fields : [ 'unit:reference_not_found' ] } ) ;
311+ expect ( await noteNaming ( h , NARROW , 'bu_east_field' ) ) . toEqual ( { status : 200 } ) ;
312+ expect ( await storedNotes ( h ) ) . toEqual ( [ 'bu_east_field' ] ) ;
313+ } ) ;
314+
315+ it ( 'CONTROL: a caller holding no scope still reads no unit, and cannot name one' , async ( ) => {
316+ const h = await boot ( posture ) ;
317+ expect ( await unitsRead ( h , WRITER ) ) . toEqual ( { status : 403 , code : 'PERMISSION_DENIED' } ) ;
318+ expect ( await noteNaming ( h , WRITER , 'bu_east' ) ) . toEqual ( { status : 400 , code : 'VALIDATION_FAILED' , fields : [ 'unit:reference_not_found' ] } ) ;
319+ expect ( await storedNotes ( h ) ) . toEqual ( [ ] ) ;
320+ } ) ;
321+
322+ it ( 'CONTROL: an organization administrator reads and names every unit of its organization, as before' , async ( ) => {
323+ const h = await boot ( posture ) ;
324+ expect ( await unitsRead ( h , ORG_ADMIN ) ) . toEqual ( [ 'bu_east' , 'bu_east_field' , 'bu_hq' ] ) ;
325+ expect ( await noteNaming ( h , ORG_ADMIN , 'bu_hq' ) ) . toEqual ( { status : 200 } ) ;
326+ } ) ;
327+ } ) ;
328+ }
329+ } ) ;
0 commit comments