Skip to content

fix: Use proper endpoint for user workspaces #1356

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion coderd/coderd.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ func New(options *Options) (http.Handler, func()) {
})
r.Get("/gitsshkey", api.gitSSHKey)
r.Put("/gitsshkey", api.regenerateGitSSHKey)
r.Get("/workspaces", api.workspacesByOwner)
r.Get("/workspaces", api.workspacesByUser)
})
})
})
Expand Down
8 changes: 1 addition & 7 deletions coderd/database/databasefake/databasefake.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ func (q *fakeQuerier) GetWorkspacesByOrganizationIDs(_ context.Context, req data
workspaces := make([]database.Workspace, 0)
for _, workspace := range q.workspaces {
for _, id := range req.Ids {
if workspace.ID != id {
if workspace.OrganizationID != id {
continue
}
if workspace.Deleted != req.Deleted {
Expand All @@ -494,9 +494,6 @@ func (q *fakeQuerier) GetWorkspacesByOrganizationIDs(_ context.Context, req data
workspaces = append(workspaces, workspace)
}
}
if len(workspaces) == 0 {
return nil, sql.ErrNoRows
}
return workspaces, nil
}

Expand All @@ -514,9 +511,6 @@ func (q *fakeQuerier) GetWorkspacesByOwnerID(_ context.Context, req database.Get
}
workspaces = append(workspaces, workspace)
}
if len(workspaces) == 0 {
return nil, sql.ErrNoRows
}
return workspaces, nil
}

Expand Down
28 changes: 25 additions & 3 deletions coderd/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -820,7 +820,8 @@ func (api *api) workspacesByUser(rw http.ResponseWriter, r *http.Request) {
organizationIDs := make([]uuid.UUID, 0)
for _, organization := range organizations {
err = api.Authorizer.AuthorizeByRoleName(r.Context(), user.ID.String(), roles.Roles, rbac.ActionRead, rbac.ResourceWorkspace.All().InOrg(organization.ID))
if errors.Is(err, &rbac.UnauthorizedError{}) {
var apiErr *rbac.UnauthorizedError
if xerrors.As(err, &apiErr) {
continue
}
if err != nil {
Expand All @@ -832,7 +833,8 @@ func (api *api) workspacesByUser(rw http.ResponseWriter, r *http.Request) {
organizationIDs = append(organizationIDs, organization.ID)
}

workspaces, err := api.Database.GetWorkspacesByOrganizationIDs(r.Context(), database.GetWorkspacesByOrganizationIDsParams{
workspaceIDs := map[uuid.UUID]struct{}{}
allWorkspaces, err := api.Database.GetWorkspacesByOrganizationIDs(r.Context(), database.GetWorkspacesByOrganizationIDsParams{
Ids: organizationIDs,
})
if err != nil {
Expand All @@ -841,7 +843,27 @@ func (api *api) workspacesByUser(rw http.ResponseWriter, r *http.Request) {
})
return
}
apiWorkspaces, err := convertWorkspaces(r.Context(), api.Database, workspaces)
for _, ws := range allWorkspaces {
workspaceIDs[ws.ID] = struct{}{}
}
userWorkspaces, err := api.Database.GetWorkspacesByOwnerID(r.Context(), database.GetWorkspacesByOwnerIDParams{
OwnerID: user.ID,
})
if err != nil {
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
Message: fmt.Sprintf("get workspaces for user: %s", err),
})
return
}
for _, ws := range userWorkspaces {
_, exists := workspaceIDs[ws.ID]
if exists {
continue
}
allWorkspaces = append(allWorkspaces, ws)
}

apiWorkspaces, err := convertWorkspaces(r.Context(), api.Database, allWorkspaces)
if err != nil {
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
Message: fmt.Sprintf("convert workspaces: %s", err),
Expand Down