7
7
"fmt"
8
8
"net/http"
9
9
10
+ "github.com/coder/coder/coderd/rbac"
11
+
10
12
"github.com/go-chi/chi/v5"
11
13
"github.com/google/uuid"
12
14
"github.com/moby/moby/pkg/namesgenerator"
@@ -18,8 +20,15 @@ import (
18
20
"github.com/coder/coder/codersdk"
19
21
)
20
22
21
- func (* api ) organization (rw http.ResponseWriter , r * http.Request ) {
23
+ func (api * api ) organization (rw http.ResponseWriter , r * http.Request ) {
22
24
organization := httpmw .OrganizationParam (r )
25
+
26
+ if ! api .Authorize (rw , r , rbac .ActionRead , rbac .ResourceOrganization .
27
+ InOrg (organization .ID ).
28
+ WithID (organization .ID .String ())) {
29
+ return
30
+ }
31
+
23
32
httpapi .Write (rw , http .StatusOK , convertOrganization (organization ))
24
33
}
25
34
@@ -327,6 +336,11 @@ func (api *api) templateByOrganizationAndName(rw http.ResponseWriter, r *http.Re
327
336
328
337
func (api * api ) workspacesByOrganization (rw http.ResponseWriter , r * http.Request ) {
329
338
organization := httpmw .OrganizationParam (r )
339
+
340
+ if ! api .Authorize (rw , r , rbac .ActionRead , rbac .ResourceWorkspace .InOrg (organization .ID )) {
341
+ return
342
+ }
343
+
330
344
workspaces , err := api .Database .GetWorkspacesByOrganizationID (r .Context (), database.GetWorkspacesByOrganizationIDParams {
331
345
OrganizationID : organization .ID ,
332
346
Deleted : false ,
@@ -352,6 +366,8 @@ func (api *api) workspacesByOrganization(rw http.ResponseWriter, r *http.Request
352
366
353
367
func (api * api ) workspacesByOwner (rw http.ResponseWriter , r * http.Request ) {
354
368
owner := httpmw .UserParam (r )
369
+ roles := httpmw .UserRoles (r )
370
+
355
371
workspaces , err := api .Database .GetWorkspacesByOwnerID (r .Context (), database.GetWorkspacesByOwnerIDParams {
356
372
OwnerID : owner .ID ,
357
373
})
@@ -364,7 +380,19 @@ func (api *api) workspacesByOwner(rw http.ResponseWriter, r *http.Request) {
364
380
})
365
381
return
366
382
}
367
- apiWorkspaces , err := convertWorkspaces (r .Context (), api .Database , workspaces )
383
+
384
+ allowed := make ([]database.Workspace , 0 )
385
+ for i := range workspaces {
386
+ w := workspaces [i ]
387
+ err := api .Authorizer .ByRoleName (r .Context (), roles .ID .String (), roles .Roles , rbac .ActionRead ,
388
+ rbac .ResourceWorkspace .InOrg (w .OrganizationID ).WithOwner (w .OwnerID .String ()).WithID (w .ID .String ()))
389
+
390
+ if err == nil {
391
+ allowed = append (allowed , w )
392
+ }
393
+ }
394
+
395
+ apiWorkspaces , err := convertWorkspaces (r .Context (), api .Database , allowed )
368
396
if err != nil {
369
397
httpapi .Write (rw , http .StatusInternalServerError , httpapi.Response {
370
398
Message : fmt .Sprintf ("convert workspaces: %s" , err ),
@@ -379,6 +407,10 @@ func (api *api) workspaceByOwnerAndName(rw http.ResponseWriter, r *http.Request)
379
407
organization := httpmw .OrganizationParam (r )
380
408
workspaceName := chi .URLParam (r , "workspace" )
381
409
410
+ if ! api .Authorize (rw , r , rbac .ActionRead , rbac .ResourceWorkspace .InOrg (organization .ID ).WithOwner (owner .ID .String ())) {
411
+ return
412
+ }
413
+
382
414
workspace , err := api .Database .GetWorkspaceByOwnerIDAndName (r .Context (), database.GetWorkspaceByOwnerIDAndNameParams {
383
415
OwnerID : owner .ID ,
384
416
Name : workspaceName ,
@@ -431,11 +463,18 @@ func (api *api) workspaceByOwnerAndName(rw http.ResponseWriter, r *http.Request)
431
463
432
464
// Create a new workspace for the currently authenticated user.
433
465
func (api * api ) postWorkspacesByOrganization (rw http.ResponseWriter , r * http.Request ) {
466
+ organization := httpmw .OrganizationParam (r )
467
+ apiKey := httpmw .APIKey (r )
468
+
434
469
var createWorkspace codersdk.CreateWorkspaceRequest
435
470
if ! httpapi .Read (rw , r , & createWorkspace ) {
436
471
return
437
472
}
438
- apiKey := httpmw .APIKey (r )
473
+
474
+ if ! api .Authorize (rw , r , rbac .ActionCreate , rbac .ResourceWorkspace .InOrg (organization .ID ).WithOwner (apiKey .UserID .String ())) {
475
+ return
476
+ }
477
+
439
478
template , err := api .Database .GetTemplateByID (r .Context (), createWorkspace .TemplateID )
440
479
if errors .Is (err , sql .ErrNoRows ) {
441
480
httpapi .Write (rw , http .StatusBadRequest , httpapi.Response {
@@ -453,7 +492,7 @@ func (api *api) postWorkspacesByOrganization(rw http.ResponseWriter, r *http.Req
453
492
})
454
493
return
455
494
}
456
- organization := httpmw . OrganizationParam ( r )
495
+
457
496
if organization .ID != template .OrganizationID {
458
497
httpapi .Write (rw , http .StatusUnauthorized , httpapi.Response {
459
498
Message : fmt .Sprintf ("template is not in organization %q" , organization .Name ),
0 commit comments