@@ -13,6 +13,7 @@ import { ApplicationError, ErrorCodes } from "@gitpod/gitpod-protocol/lib/messag
13
13
import { v1 } from "@authzed/authzed-node" ;
14
14
import { fgaRelationsUpdateClientLatency } from "../prometheus-metrics" ;
15
15
import { RedisMutex } from "../redis/mutex" ;
16
+ import { rel } from "./definitions" ;
16
17
17
18
@injectable ( )
18
19
export class RelationshipUpdater {
@@ -48,7 +49,7 @@ export class RelationshipUpdater {
48
49
}
49
50
return user ;
50
51
}
51
- if ( this . isMigrated ( user ) ) {
52
+ if ( await this . isMigrated ( user ) ) {
52
53
return user ;
53
54
}
54
55
const stopTimer = fgaRelationsUpdateClientLatency . startTimer ( ) ;
@@ -61,7 +62,7 @@ export class RelationshipUpdater {
61
62
throw new ApplicationError ( ErrorCodes . NOT_FOUND , "User not found" ) ;
62
63
}
63
64
user = updatedUser ;
64
- if ( this . isMigrated ( user ) ) {
65
+ if ( await this . isMigrated ( user ) ) {
65
66
return user ;
66
67
}
67
68
log . info ( { userId : user . id } , `Updating FGA relationships for user.` , {
@@ -93,8 +94,23 @@ export class RelationshipUpdater {
93
94
}
94
95
}
95
96
96
- private isMigrated ( user : User ) {
97
- return user . additionalData ?. fgaRelationshipsVersion === RelationshipUpdater . version ;
97
+ private async isMigrated ( user : User ) {
98
+ const isMigrated = user . additionalData ?. fgaRelationshipsVersion === RelationshipUpdater . version ;
99
+ if ( isMigrated ) {
100
+ const hasSelfRelationship = await this . authorizer . find ( rel . user ( user . id ) . self . user ( user . id ) ) ;
101
+ if ( ! hasSelfRelationship ) {
102
+ log . warn ( { userId : user . id } , `User is marked as migrated but doesn't have self relationship.` ) ;
103
+ //TODO(se) this is an extra safety net to detect
104
+ // reset the fgaRelationshipsVersion to undefined, so the migration is triggered again when the feature is enabled
105
+ AdditionalUserData . set ( user , { fgaRelationshipsVersion : undefined } ) ;
106
+ await this . userDB . updateUserPartial ( {
107
+ id : user . id ,
108
+ additionalData : user . additionalData ,
109
+ } ) ;
110
+ return false ;
111
+ }
112
+ }
113
+ return isMigrated ;
98
114
}
99
115
100
116
private async findAffectedOrganizations ( userId : string ) : Promise < Organization [ ] > {
0 commit comments