@@ -180,10 +180,23 @@ describe('[#21260] the ledger audit capability exempts its holder from the paren
180180 token . admin = await stack . signIn ( ) ;
181181 const userIdOf = async ( email : string ) =>
182182 String ( ( await ql . findOne ( 'sys_user' , { where : { email } , context : { ...SYS } } ) ) ?. id ?? '' ) ;
183+ // A grant is a system write on a tenant-scoped object, so it carries its
184+ // holder's organization the way a session write does (ADR-0131 D9): the one
185+ // organization the holder is a member of, the Default Organization here.
186+ const organizationOf = async ( userId : string ) => {
187+ const memberships : Row [ ] = await ql . find ( 'sys_member' , { where : { user_id : userId } , context : { ...SYS } } ) ;
188+ const organizations = memberships . map ( ( m ) => String ( m . organization_id ?? '' ) ) ;
189+ expect ( organizations , `the organizations user ${ userId } is a member of` ) . toHaveLength ( 1 ) ;
190+ expect ( organizations [ 0 ] , `the organization of user ${ userId } ` ) . toBeTruthy ( ) ;
191+ return organizations [ 0 ] ;
192+ } ;
183193 const grant = async ( email : string , setName : string ) => {
184194 const set = await catalogPermissionSet ( ql , setName ) ;
185195 expect ( set , `fixture permission set ${ setName } served by the security catalog` ) . toBeTruthy ( ) ;
186- await ql . insert ( 'sys_user_permission_set' , { user_id : await userIdOf ( email ) , permission_set : set . name } , { context : { ...SYS } } ) ;
196+ const userId = await userIdOf ( email ) ;
197+ await ql . insert ( 'sys_user_permission_set' , { user_id : userId , permission_set : set . name } , {
198+ context : { ...SYS , tenantId : await organizationOf ( userId ) } ,
199+ } ) ;
187200 } ;
188201 for ( const who of [ 'holder' , 'reader' , 'writer' ] as const ) {
189202 const email = `alc-${ who } @verify.test` ;
@@ -429,7 +442,11 @@ describe('[#21260] the ledger audit capability is bounded by its holder’s orga
429442 }
430443 const userA = await ql . findOne ( 'sys_user' , { where : { email : 'alc-org-a@verify.test' } , context : { ...SYS } } ) ;
431444 expect ( await catalogPermissionSet ( ql , capOnly . name ) , 'fixture permission set served by the security catalog' ) . toBeTruthy ( ) ;
432- await ql . insert ( 'sys_user_permission_set' , { user_id : userA . id , permission_set : capOnly . name } , { context : { ...SYS } } ) ;
445+ // The grant carries its holder's organization: under the wall no
446+ // organization is derivable, so a system write names its own (ADR-0131 D9).
447+ await ql . insert ( 'sys_user_permission_set' , { user_id : userA . id , permission_set : capOnly . name } , {
448+ context : { ...SYS , tenantId : org . a } ,
449+ } ) ;
433450 for ( const who of [ 'a' , 'b' ] as const ) {
434451 const res = await stack . apiAs ( tok [ who ] , 'POST' , `/data/${ OBJ } ` , { name : `gone ${ who } ` } ) ;
435452 expect ( res . status ) . toBeLessThan ( 300 ) ;
0 commit comments