Skip to content
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
5 changes: 4 additions & 1 deletion coderd/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,10 @@ func (api *API) deleteUser(rw http.ResponseWriter, r *http.Request) {
return
}

workspaces, err := api.Database.GetWorkspaces(ctx, database.GetWorkspacesParams{
// This query is ONLY done to get the workspace count, so we use a system
// context to return ALL workspaces. Not just workspaces the user can view.
// nolint:gocritic
workspaces, err := api.Database.GetWorkspaces(dbauthz.AsSystemRestricted(ctx), database.GetWorkspacesParams{
OwnerID: user.ID,
})
if err != nil {
Expand Down
37 changes: 37 additions & 0 deletions coderd/users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,43 @@ func TestDeleteUser(t *testing.T) {
require.ErrorAs(t, err, &apiErr, "should be a coderd error")
require.Equal(t, http.StatusForbidden, apiErr.StatusCode(), "should be forbidden")
})
t.Run("CountCheckIncludesAllWorkspaces", func(t *testing.T) {
t.Parallel()
client, _ := coderdtest.NewWithProvisionerCloser(t, nil)
firstUser := coderdtest.CreateFirstUser(t, client)

// Create a target user who will own a workspace
targetUserClient, targetUser := coderdtest.CreateAnotherUser(t, client, firstUser.OrganizationID)

// Create a User Admin who should not have permission to see the target user's workspace
userAdminClient, userAdmin := coderdtest.CreateAnotherUser(t, client, firstUser.OrganizationID)

// Grant User Admin role to the userAdmin
userAdmin, err := client.UpdateUserRoles(context.Background(), userAdmin.ID.String(), codersdk.UpdateRoles{
Roles: []string{rbac.RoleUserAdmin().String()},
})
require.NoError(t, err)

// Create a template and workspace owned by the target user
version := coderdtest.CreateTemplateVersion(t, client, firstUser.OrganizationID, nil)
coderdtest.AwaitTemplateVersionJobCompleted(t, client, version.ID)
template := coderdtest.CreateTemplate(t, client, firstUser.OrganizationID, version.ID)
_ = coderdtest.CreateWorkspace(t, targetUserClient, template.ID)

workspaces, err := userAdminClient.Workspaces(context.Background(), codersdk.WorkspaceFilter{
Owner: targetUser.Username,
})
require.NoError(t, err)
require.Len(t, workspaces.Workspaces, 0)

// Attempt to delete the target user - this should fail because the
// user has a workspace not visible to the deleting user.
err = userAdminClient.DeleteUser(context.Background(), targetUser.ID)
var apiErr *codersdk.Error
require.ErrorAs(t, err, &apiErr)
require.Equal(t, http.StatusExpectationFailed, apiErr.StatusCode())
require.Contains(t, apiErr.Message, "has workspaces")
})
}

func TestNotifyUserStatusChanged(t *testing.T) {
Expand Down
Loading