Skip to content

Commit dc115b8

Browse files
authored
fix: Use proper endpoint for user workspaces (#1356)
This was a silly mistake in a prior PR, so the code wasn't actually being called!
1 parent b675aec commit dc115b8

File tree

3 files changed

+27
-11
lines changed

3 files changed

+27
-11
lines changed

coderd/coderd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ func New(options *Options) (http.Handler, func()) {
258258
})
259259
r.Get("/gitsshkey", api.gitSSHKey)
260260
r.Put("/gitsshkey", api.regenerateGitSSHKey)
261-
r.Get("/workspaces", api.workspacesByOwner)
261+
r.Get("/workspaces", api.workspacesByUser)
262262
})
263263
})
264264
})

coderd/database/databasefake/databasefake.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ func (q *fakeQuerier) GetWorkspacesByOrganizationIDs(_ context.Context, req data
485485
workspaces := make([]database.Workspace, 0)
486486
for _, workspace := range q.workspaces {
487487
for _, id := range req.Ids {
488-
if workspace.ID != id {
488+
if workspace.OrganizationID != id {
489489
continue
490490
}
491491
if workspace.Deleted != req.Deleted {
@@ -494,9 +494,6 @@ func (q *fakeQuerier) GetWorkspacesByOrganizationIDs(_ context.Context, req data
494494
workspaces = append(workspaces, workspace)
495495
}
496496
}
497-
if len(workspaces) == 0 {
498-
return nil, sql.ErrNoRows
499-
}
500497
return workspaces, nil
501498
}
502499

@@ -514,9 +511,6 @@ func (q *fakeQuerier) GetWorkspacesByOwnerID(_ context.Context, req database.Get
514511
}
515512
workspaces = append(workspaces, workspace)
516513
}
517-
if len(workspaces) == 0 {
518-
return nil, sql.ErrNoRows
519-
}
520514
return workspaces, nil
521515
}
522516

coderd/users.go

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -820,7 +820,8 @@ func (api *api) workspacesByUser(rw http.ResponseWriter, r *http.Request) {
820820
organizationIDs := make([]uuid.UUID, 0)
821821
for _, organization := range organizations {
822822
err = api.Authorizer.AuthorizeByRoleName(r.Context(), user.ID.String(), roles.Roles, rbac.ActionRead, rbac.ResourceWorkspace.All().InOrg(organization.ID))
823-
if errors.Is(err, &rbac.UnauthorizedError{}) {
823+
var apiErr *rbac.UnauthorizedError
824+
if xerrors.As(err, &apiErr) {
824825
continue
825826
}
826827
if err != nil {
@@ -832,7 +833,8 @@ func (api *api) workspacesByUser(rw http.ResponseWriter, r *http.Request) {
832833
organizationIDs = append(organizationIDs, organization.ID)
833834
}
834835

835-
workspaces, err := api.Database.GetWorkspacesByOrganizationIDs(r.Context(), database.GetWorkspacesByOrganizationIDsParams{
836+
workspaceIDs := map[uuid.UUID]struct{}{}
837+
allWorkspaces, err := api.Database.GetWorkspacesByOrganizationIDs(r.Context(), database.GetWorkspacesByOrganizationIDsParams{
836838
Ids: organizationIDs,
837839
})
838840
if err != nil {
@@ -841,7 +843,27 @@ func (api *api) workspacesByUser(rw http.ResponseWriter, r *http.Request) {
841843
})
842844
return
843845
}
844-
apiWorkspaces, err := convertWorkspaces(r.Context(), api.Database, workspaces)
846+
for _, ws := range allWorkspaces {
847+
workspaceIDs[ws.ID] = struct{}{}
848+
}
849+
userWorkspaces, err := api.Database.GetWorkspacesByOwnerID(r.Context(), database.GetWorkspacesByOwnerIDParams{
850+
OwnerID: user.ID,
851+
})
852+
if err != nil {
853+
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
854+
Message: fmt.Sprintf("get workspaces for user: %s", err),
855+
})
856+
return
857+
}
858+
for _, ws := range userWorkspaces {
859+
_, exists := workspaceIDs[ws.ID]
860+
if exists {
861+
continue
862+
}
863+
allWorkspaces = append(allWorkspaces, ws)
864+
}
865+
866+
apiWorkspaces, err := convertWorkspaces(r.Context(), api.Database, allWorkspaces)
845867
if err != nil {
846868
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
847869
Message: fmt.Sprintf("convert workspaces: %s", err),

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy