Skip to content

fix: ensure user always belongs to an organization #9781

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
Sep 19, 2023
Merged
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
fix: user should always belong to an organization
  • Loading branch information
mtojek committed Sep 19, 2023
commit 625537732b02b9c4f8a9e16521d7e849f17d62ea
4 changes: 2 additions & 2 deletions coderd/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -1165,8 +1165,8 @@ func convertUsers(users []database.User, organizationIDsByUserID map[uuid.UUID][

func userOrganizationIDs(ctx context.Context, api *API, user database.User) ([]uuid.UUID, error) {
organizationIDsByMemberIDsRows, err := api.Database.GetOrganizationIDsByMemberIDs(ctx, []uuid.UUID{user.ID})
if errors.Is(err, sql.ErrNoRows) || len(organizationIDsByMemberIDsRows) == 0 {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand that len(organizationIDsByMemberIDsRows) == 0 will never happen against the real database, and dbfake.go implementation handles this case:

	if len(getOrganizationIDsByMemberIDRows) == 0 {
		return nil, sql.ErrNoRows
	}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found a panic from this when missing org ids. The problem is that real db queries only return sql.ErrNoRows on :one queries, while :many just return zero rows without error.

return []uuid.UUID{}, nil
if errors.Is(err, sql.ErrNoRows) {
return []uuid.UUID{}, xerrors.Errorf("user %q must be a member of at least one organization", user.Email)
}
if err != nil {
return []uuid.UUID{}, err
Expand Down