@@ -19,6 +19,7 @@ import {
19
19
rel ,
20
20
} from "./definitions" ;
21
21
import { SpiceDBAuthorizer } from "./spicedb-authorizer" ;
22
+ import { getExperimentsClientForBackend } from "@gitpod/gitpod-protocol/lib/experiments/configcat-server" ;
22
23
23
24
export function createInitializingAuthorizer ( spiceDbAuthorizer : SpiceDBAuthorizer ) : Authorizer {
24
25
const target = new Authorizer ( spiceDbAuthorizer ) ;
@@ -127,8 +128,10 @@ export class Authorizer {
127
128
}
128
129
129
130
// write operations below
130
-
131
- public async removeAllRelationships ( type : ResourceType , id : string ) {
131
+ public async removeAllRelationships ( userId : string , type : ResourceType , id : string ) {
132
+ if ( await this . isDisabled ( userId ) ) {
133
+ return ;
134
+ }
132
135
await this . authorizer . deleteRelationships (
133
136
v1 . DeleteRelationshipsRequest . create ( {
134
137
relationshipFilter : {
@@ -156,7 +159,18 @@ export class Authorizer {
156
159
}
157
160
}
158
161
162
+ private async isDisabled ( userId : string ) : Promise < boolean > {
163
+ return ! ( await getExperimentsClientForBackend ( ) . getValueAsync ( "centralizedPermissions" , false , {
164
+ user : {
165
+ id : userId ,
166
+ } ,
167
+ } ) ) ;
168
+ }
169
+
159
170
async addUser ( userId : string , owningOrgId ?: string ) {
171
+ if ( await this . isDisabled ( userId ) ) {
172
+ return ;
173
+ }
160
174
const oldOrgs = await this . findAll ( rel . user ( userId ) . organization . organization ( "" ) ) ;
161
175
const updates = [ set ( rel . user ( userId ) . self . user ( userId ) ) ] ;
162
176
updates . push (
@@ -184,10 +198,16 @@ export class Authorizer {
184
198
}
185
199
186
200
async removeUser ( userId : string ) {
187
- await this . removeAllRelationships ( "user" , userId ) ;
201
+ if ( await this . isDisabled ( userId ) ) {
202
+ return ;
203
+ }
204
+ await this . removeAllRelationships ( userId , "user" , userId ) ;
188
205
}
189
206
190
207
async addOrganizationRole ( orgID : string , userID : string , role : TeamMemberRole ) : Promise < void > {
208
+ if ( await this . isDisabled ( userID ) ) {
209
+ return ;
210
+ }
191
211
const updates = [ set ( rel . organization ( orgID ) . member . user ( userID ) ) ] ;
192
212
if ( role === "owner" ) {
193
213
updates . push ( set ( rel . organization ( orgID ) . owner . user ( userID ) ) ) ;
@@ -198,48 +218,61 @@ export class Authorizer {
198
218
}
199
219
200
220
async removeOrganizationRole ( orgID : string , userID : string , role : TeamMemberRole ) : Promise < void > {
221
+ if ( await this . isDisabled ( userID ) ) {
222
+ return ;
223
+ }
201
224
const updates = [ remove ( rel . organization ( orgID ) . owner . user ( userID ) ) ] ;
202
225
if ( role === "member" ) {
203
226
updates . push ( remove ( rel . organization ( orgID ) . member . user ( userID ) ) ) ;
204
227
}
205
228
await this . authorizer . writeRelationships ( ...updates ) ;
206
229
}
207
230
208
- async addProjectToOrg ( orgID : string , projectID : string ) : Promise < void > {
231
+ async addProjectToOrg ( userId : string , orgID : string , projectID : string ) : Promise < void > {
232
+ if ( await this . isDisabled ( userId ) ) {
233
+ return ;
234
+ }
209
235
await this . authorizer . writeRelationships (
210
236
set ( rel . project ( projectID ) . org . organization ( orgID ) ) , //
211
237
) ;
212
238
}
213
239
214
- async removeProjectFromOrg ( orgID : string , projectID : string ) : Promise < void > {
240
+ async removeProjectFromOrg ( userId : string , orgID : string , projectID : string ) : Promise < void > {
241
+ if ( await this . isDisabled ( userId ) ) {
242
+ return ;
243
+ }
215
244
await this . authorizer . writeRelationships (
216
245
remove ( rel . project ( projectID ) . org . organization ( orgID ) ) , //
217
246
) ;
218
247
}
219
248
220
249
async addOrganization (
250
+ userId : string ,
221
251
orgId : string ,
222
252
members : { userId : string ; role : TeamMemberRole } [ ] ,
223
253
projectIds : string [ ] ,
224
254
) : Promise < void > {
255
+ if ( await this . isDisabled ( userId ) ) {
256
+ return ;
257
+ }
225
258
await this . addOrganizationMembers ( orgId , members ) ;
226
259
227
- await this . addOrganizationProjects ( orgId , projectIds ) ;
260
+ await this . addOrganizationProjects ( userId , orgId , projectIds ) ;
228
261
229
262
await this . authorizer . writeRelationships (
230
263
set ( rel . organization ( orgId ) . installation . installation ) , //
231
264
) ;
232
265
}
233
266
234
- private async addOrganizationProjects ( orgID : string , projectIds : string [ ] ) : Promise < void > {
267
+ private async addOrganizationProjects ( userId : string , orgID : string , projectIds : string [ ] ) : Promise < void > {
235
268
const existing = await this . findAll ( rel . project ( "" ) . org . organization ( orgID ) ) ;
236
269
const toBeRemoved = asSet ( existing . map ( ( r ) => r . resource ?. objectId ) ) ;
237
270
for ( const projectId of projectIds ) {
238
- await this . addProjectToOrg ( orgID , projectId ) ;
271
+ await this . addProjectToOrg ( userId , orgID , projectId ) ;
239
272
toBeRemoved . delete ( projectId ) ;
240
273
}
241
274
for ( const projectId of toBeRemoved ) {
242
- await this . removeProjectFromOrg ( orgID , projectId ) ;
275
+ await this . removeProjectFromOrg ( userId , orgID , projectId ) ;
243
276
}
244
277
}
245
278
@@ -258,15 +291,21 @@ export class Authorizer {
258
291
}
259
292
}
260
293
261
- async addInstallationAdminRole ( userID : string ) {
294
+ async addInstallationAdminRole ( userId : string ) {
295
+ if ( await this . isDisabled ( userId ) ) {
296
+ return ;
297
+ }
262
298
await this . authorizer . writeRelationships (
263
- set ( rel . installation . admin . user ( userID ) ) , //
299
+ set ( rel . installation . admin . user ( userId ) ) , //
264
300
) ;
265
301
}
266
302
267
- async removeInstallationAdminRole ( userID : string ) {
303
+ async removeInstallationAdminRole ( userId : string ) {
304
+ if ( await this . isDisabled ( userId ) ) {
305
+ return ;
306
+ }
268
307
await this . authorizer . writeRelationships (
269
- remove ( rel . installation . admin . user ( userID ) ) , //
308
+ remove ( rel . installation . admin . user ( userId ) ) , //
270
309
) ;
271
310
}
272
311
0 commit comments