@@ -16,6 +16,7 @@ import {
16
16
Relation ,
17
17
ResourceType ,
18
18
UserPermission ,
19
+ WorkspacePermission ,
19
20
rel ,
20
21
} from "./definitions" ;
21
22
import { SpiceDBAuthorizer } from "./spicedb-authorizer" ;
@@ -101,11 +102,11 @@ export class Authorizer {
101
102
) ;
102
103
}
103
104
104
- async hasPermissionOnUser ( userId : string , permission : UserPermission , userResourceId : string ) : Promise < boolean > {
105
+ async hasPermissionOnUser ( userId : string , permission : UserPermission , resourceUserId : string ) : Promise < boolean > {
105
106
const req = v1 . CheckPermissionRequest . create ( {
106
107
subject : subject ( "user" , userId ) ,
107
108
permission,
108
- resource : object ( "user" , userResourceId ) ,
109
+ resource : object ( "user" , resourceUserId ) ,
109
110
consistency,
110
111
} ) ;
111
112
@@ -126,6 +127,35 @@ export class Authorizer {
126
127
) ;
127
128
}
128
129
130
+ async hasPermissionOnWorkspace (
131
+ userId : string ,
132
+ permission : WorkspacePermission ,
133
+ workspaceId : string ,
134
+ ) : Promise < boolean > {
135
+ const req = v1 . CheckPermissionRequest . create ( {
136
+ subject : subject ( "user" , userId ) ,
137
+ permission,
138
+ resource : object ( "workspace" , workspaceId ) ,
139
+ consistency,
140
+ } ) ;
141
+
142
+ return this . authorizer . check ( req , { userId } ) ;
143
+ }
144
+
145
+ async checkPermissionOnWorkspace ( userId : string , permission : WorkspacePermission , workspaceId : string ) {
146
+ if ( await this . hasPermissionOnWorkspace ( userId , permission , workspaceId ) ) {
147
+ return ;
148
+ }
149
+ if ( "read_info" === permission || ! ( await this . hasPermissionOnWorkspace ( userId , "read_info" , workspaceId ) ) ) {
150
+ throw new ApplicationError ( ErrorCodes . NOT_FOUND , `Workspace ${ workspaceId } not found.` ) ;
151
+ }
152
+
153
+ throw new ApplicationError (
154
+ ErrorCodes . PERMISSION_DENIED ,
155
+ `You do not have ${ permission } on workspace ${ workspaceId } ` ,
156
+ ) ;
157
+ }
158
+
129
159
// write operations below
130
160
131
161
public async removeAllRelationships ( type : ResourceType , id : string ) {
@@ -158,7 +188,7 @@ export class Authorizer {
158
188
159
189
async addUser ( userId : string , owningOrgId ?: string ) {
160
190
await this . authorizer . writeRelationships (
161
- set ( rel . user ( userId ) . self . user ( userId ) ) , //
191
+ set ( rel . user ( userId ) . self . user ( userId ) ) ,
162
192
set (
163
193
owningOrgId
164
194
? rel . user ( userId ) . organization . organization ( owningOrgId )
@@ -186,15 +216,11 @@ export class Authorizer {
186
216
}
187
217
188
218
async addProjectToOrg ( orgID : string , projectID : string ) : Promise < void > {
189
- await this . authorizer . writeRelationships (
190
- set ( rel . project ( projectID ) . org . organization ( orgID ) ) , //
191
- ) ;
219
+ await this . authorizer . writeRelationships ( set ( rel . project ( projectID ) . org . organization ( orgID ) ) ) ;
192
220
}
193
221
194
222
async removeProjectFromOrg ( orgID : string , projectID : string ) : Promise < void > {
195
- await this . authorizer . writeRelationships (
196
- remove ( rel . project ( projectID ) . org . organization ( orgID ) ) , //
197
- ) ;
223
+ await this . authorizer . writeRelationships ( remove ( rel . project ( projectID ) . org . organization ( orgID ) ) ) ;
198
224
}
199
225
200
226
async addOrganization ( org : Organization , members : TeamMemberInfo [ ] , projects : Project [ ] ) : Promise < void > {
@@ -206,35 +232,40 @@ export class Authorizer {
206
232
await this . addProjectToOrg ( org . id , project . id ) ;
207
233
}
208
234
209
- await this . authorizer . writeRelationships (
210
- set ( rel . organization ( org . id ) . installation . installation ) , //
211
- ) ;
235
+ await this . authorizer . writeRelationships ( set ( rel . organization ( org . id ) . installation . installation ) ) ;
212
236
}
213
237
214
238
async addInstallationMemberRole ( userID : string ) {
215
- await this . authorizer . writeRelationships (
216
- set ( rel . installation . member . user ( userID ) ) , //
217
- ) ;
239
+ await this . authorizer . writeRelationships ( set ( rel . installation . member . user ( userID ) ) ) ;
218
240
}
219
241
220
242
async removeInstallationMemberRole ( userID : string ) {
221
- await this . authorizer . writeRelationships (
222
- remove ( rel . installation . member . user ( userID ) ) , //
223
- ) ;
243
+ await this . authorizer . writeRelationships ( remove ( rel . installation . member . user ( userID ) ) ) ;
224
244
}
225
245
226
246
async addInstallationAdminRole ( userID : string ) {
227
- await this . authorizer . writeRelationships (
228
- set ( rel . installation . admin . user ( userID ) ) , //
229
- ) ;
247
+ await this . authorizer . writeRelationships ( set ( rel . installation . admin . user ( userID ) ) ) ;
230
248
}
231
249
232
250
async removeInstallationAdminRole ( userID : string ) {
251
+ await this . authorizer . writeRelationships ( remove ( rel . installation . admin . user ( userID ) ) ) ;
252
+ }
253
+
254
+ async createWorkspaceInOrg ( orgID : string , userID : string , workspaceID : string ) : Promise < void > {
233
255
await this . authorizer . writeRelationships (
234
- remove ( rel . installation . admin . user ( userID ) ) , //
256
+ set ( rel . workspace ( workspaceID ) . org . organization ( orgID ) ) ,
257
+ set ( rel . workspace ( workspaceID ) . owner . user ( userID ) ) ,
235
258
) ;
236
259
}
237
260
261
+ async addWorkspaceToOrg ( orgID : string , workspaceID : string ) : Promise < void > {
262
+ await this . authorizer . writeRelationships ( set ( rel . workspace ( workspaceID ) . org . organization ( orgID ) ) ) ;
263
+ }
264
+
265
+ async removeWorkspaceFromOrg ( orgID : string , workspaceID : string ) : Promise < void > {
266
+ await this . authorizer . writeRelationships ( remove ( rel . workspace ( workspaceID ) . org . organization ( orgID ) ) ) ;
267
+ }
268
+
238
269
public async find ( relation : v1 . Relationship ) : Promise < v1 . Relationship | undefined > {
239
270
const relationships = await this . authorizer . readRelationships ( {
240
271
consistency : v1 . Consistency . create ( {
0 commit comments