@@ -46,8 +46,8 @@ vi.mock("~/db.server", async () => {
4646// or read roles call `useRoleStandIn()`. The stand-in replaces only the role methods, with the
4747// contract of enterprise/packages/plugins/src/rbac/controller.ts: a role read is null for a user who
4848// isn't a member, else their assigned role, else the legacy OrgMember.role mapping (ADMIN -> Owner,
49- // MEMBER -> Admin). `setUserRole` writes the assignment whether or not the user is a member, and
50- // refuses to demote the org's last Owner. Session authentication stays the real fallback's.
49+ // MEMBER -> Admin). `setUserRole` writes the assignment whether or not the user is a member, refuses
50+ // to demote the org's last Owner, and returns the role it replaced and whether the role changed . Session authentication stays the real fallback's.
5151type FakeRole = { id : string ; name : string ; isSystem : boolean } ;
5252const rbacHolder = vi . hoisted ( ( ) => ( {
5353 /** Whether the role methods come from the stand-in instead of the fallback. */
@@ -58,8 +58,6 @@ const rbacHolder = vi.hoisted(() => ({
5858 developer : { id : "role_developer" , name : "Developer" , isSystem : true } ,
5959 } as Record < string , { id : string ; name : string ; isSystem : boolean } > ,
6060 assignments : new Map < string , { id : string ; name : string ; isSystem : boolean } > ( ) ,
61- /** Makes the next `getUserRole` throw, like a failed database read. */
62- failNextGetUserRole : false ,
6361 /** How many times each read ran, to tell which reads a change made. */
6462 reads : { getUserRole : 0 , allRoles : 0 } ,
6563 /** While set, `allRoles` waits for it, like a slow database read. */
@@ -88,10 +86,6 @@ vi.mock("~/services/rbac.server", async () => {
8886 const roleStandIn : Record < string , unknown > = {
8987 async getUserRole ( { userId, organizationId } : { userId : string ; organizationId : string } ) {
9088 rbacHolder . reads . getUserRole ++ ;
91- if ( rbacHolder . failNextGetUserRole ) {
92- rbacHolder . failNextGetUserRole = false ;
93- throw new Error ( "role read failed" ) ;
94- }
9589 return effectiveRole ( userId , organizationId ) ;
9690 } ,
9791 async getUserRoles ( userIds : string [ ] , organizationId : string ) {
@@ -111,6 +105,7 @@ vi.mock("~/services/rbac.server", async () => {
111105 async setUserRole ( params : { userId : string ; organizationId : string ; roleId : string } ) {
112106 const role = Object . values ( rbacHolder . roles ) . find ( ( r ) => r . id === params . roleId ) ;
113107 if ( ! role ) return { ok : false , error : "Role not found" } ;
108+ const previousRole = await effectiveRole ( params . userId , params . organizationId ) ;
114109 const members = await prismaHolder . client ! . orgMember . findMany ( {
115110 where : { organizationId : params . organizationId } ,
116111 select : { userId : true } ,
@@ -127,7 +122,7 @@ vi.mock("~/services/rbac.server", async () => {
127122 } ;
128123 }
129124 rbacHolder . assignments . set ( key ( params . userId , params . organizationId ) , role ) ;
130- return { ok : true } ;
125+ return { ok : true , previousRole , changed : previousRole ?. id !== role . id } ;
131126 } ,
132127 async removeUserRole ( { userId, organizationId } : { userId : string ; organizationId : string } ) {
133128 rbacHolder . assignments . delete ( key ( userId , organizationId ) ) ;
@@ -211,7 +206,6 @@ let disableRecording: (() => void) | undefined;
211206beforeEach ( ( ) => {
212207 rbacHolder . roleStandIn = false ;
213208 rbacHolder . assignments . clear ( ) ;
214- rbacHolder . failNextGetUserRole = false ;
215209 rbacHolder . reads = { getUserRole : 0 , allRoles : 0 } ;
216210 rbacHolder . allRolesGate = null ;
217211 ssoHolder . overrides = { } ;
@@ -816,7 +810,7 @@ describe("reads made only for audit events", () => {
816810 } ) ;
817811 await flushAuditEvents ( ) ;
818812
819- expect ( roleChange ) . toEqual ( { ok : true } ) ;
813+ expect ( roleChange ) . toMatchObject ( { ok : true , changed : true } ) ;
820814 expect ( added . created ) . toBe ( true ) ;
821815 expect ( rbacHolder . assignments . get ( `${ organization . id } :${ member . id } ` ) ) . toEqual (
822816 rbacHolder . roles . admin
@@ -834,7 +828,8 @@ describe("reads made only for audit events", () => {
834828 roleId : rbacHolder . roles . developer . id ,
835829 } ) ;
836830 await flushAuditEvents ( ) ;
837- expect ( rbacHolder . reads ) . toEqual ( { getUserRole : 1 , allRoles : 1 } ) ;
831+ // The role it replaced comes from the plugin's write, so only the role names are read.
832+ expect ( rbacHolder . reads ) . toEqual ( { getUserRole : 0 , allRoles : 1 } ) ;
838833 expect ( recorded ) . toHaveLength ( 1 ) ;
839834 } )
840835 ) ;
@@ -861,7 +856,7 @@ describe("reads made only for audit events", () => {
861856 const recordedBeforeRelease = recorded . length ;
862857 releaseRoleNames ( ) ;
863858
864- expect ( outcome ) . toEqual ( { ok : true } ) ;
859+ expect ( outcome ) . toMatchObject ( { ok : true , changed : true } ) ;
865860 expect ( recordedBeforeRelease ) . toBe ( 0 ) ;
866861 expect ( await recordedEvents ( recorded ) ) . toEqual ( [
867862 {
@@ -926,7 +921,11 @@ describe("role change audit events", () => {
926921 roleId : rbacHolder . roles . admin . id ,
927922 } ) ;
928923
929- expect ( result ) . toEqual ( { ok : true } ) ;
924+ expect ( result ) . toMatchObject ( {
925+ ok : true ,
926+ changed : true ,
927+ previousRole : { name : "Developer" } ,
928+ } ) ;
930929 expect ( await recordedEvents ( recorded ) ) . toEqual ( [
931930 {
932931 category : "members" ,
@@ -987,7 +986,7 @@ describe("role change audit events", () => {
987986 ) ;
988987 releaseMemberReads ( ) ;
989988
990- expect ( result ) . toEqual ( { ok : true } ) ;
989+ expect ( result ) . toMatchObject ( { ok : true , changed : true } ) ;
991990 expect ( await recordedEvents ( recorded ) ) . toEqual ( [
992991 {
993992 category : "members" ,
@@ -1045,46 +1044,12 @@ describe("role change audit events", () => {
10451044 code : "last_owner" ,
10461045 } ) ;
10471046 expect ( unknownRole ) . toEqual ( { ok : false , error : "Role not found" } ) ;
1048- expect ( sameRole ) . toEqual ( { ok : true } ) ;
1049- expect ( nonMember ) . toEqual ( { ok : true } ) ;
1047+ expect ( sameRole ) . toMatchObject ( { ok : true , changed : false } ) ;
1048+ expect ( nonMember ) . toMatchObject ( { ok : true } ) ;
10501049 expect ( await recordedEvents ( recorded ) ) . toEqual ( [ ] ) ;
10511050 }
10521051 ) ;
10531052
1054- // Catches: a failed "before" read blocking the role change, or dropping its event.
1055- postgresTest (
1056- "changes the role and records it without a before when the pre-read fails" ,
1057- async ( { prisma } ) =>
1058- inAuditedRequest ( async ( ) => {
1059- prismaHolder . client = prisma ;
1060- useRoleStandIn ( ) ;
1061- const { setOrgMemberRole } = await import ( "~/models/orgMember.server" ) ;
1062- const { organization, member, regularMember } = await seedOrg ( prisma ) ;
1063- rbacHolder . failNextGetUserRole = true ;
1064-
1065- const recorded = await startRecording ( ) ;
1066- const result = await setOrgMemberRole ( {
1067- userId : member . id ,
1068- organizationId : organization . id ,
1069- roleId : rbacHolder . roles . admin . id ,
1070- } ) ;
1071-
1072- expect ( result ) . toEqual ( { ok : true } ) ;
1073- expect ( rbacHolder . assignments . get ( `${ organization . id } :${ member . id } ` ) ) . toEqual (
1074- rbacHolder . roles . admin
1075- ) ;
1076- expect ( await recordedEvents ( recorded ) ) . toEqual ( [
1077- {
1078- category : "members" ,
1079- action : "member.role_changed" ,
1080- organizationId : organization . id ,
1081- target : { type : "member" , id : regularMember . id , name : member . email } ,
1082- changes : { role : { before : null , after : "Admin" } } ,
1083- } ,
1084- ] ) ;
1085- } )
1086- ) ;
1087-
10881053 // Catches: a failed member read before the write blocking the role change, or dropping its event.
10891054 // The client below fails that one read, so the event has to find the member after the write.
10901055 postgresTest (
@@ -1119,7 +1084,7 @@ describe("role change audit events", () => {
11191084 } ) ;
11201085
11211086 expect ( failNextMemberRead ) . toBe ( false ) ;
1122- expect ( result ) . toEqual ( { ok : true } ) ;
1087+ expect ( result ) . toMatchObject ( { ok : true , changed : true } ) ;
11231088 expect ( rbacHolder . assignments . get ( `${ organization . id } :${ member . id } ` ) ) . toEqual (
11241089 rbacHolder . roles . admin
11251090 ) ;
@@ -1180,7 +1145,7 @@ describe("role change audit events", () => {
11801145 } ) ;
11811146
11821147 expect ( removeAfterNextMemberRead ) . toBe ( false ) ;
1183- expect ( result ) . toEqual ( { ok : true } ) ;
1148+ expect ( result ) . toMatchObject ( { ok : true , changed : true } ) ;
11841149 expect (
11851150 await prisma . orgMember . count ( {
11861151 where : { userId : member . id , organizationId : organization . id } ,
0 commit comments