Skip to content

fix: assign new oauth users to default org #12145

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 5 commits into from
Feb 16, 2024
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
18 changes: 10 additions & 8 deletions coderd/userauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -1342,14 +1342,16 @@ func (api *API) oauthLogin(r *http.Request, params *oauthLoginParams) ([]*http.C
// with OIDC for the first time.
if user.ID == uuid.Nil {
var organizationID uuid.UUID
// Ignoring this error is a product of our unit tests. In prod this should never
// happen. Unit tests use this as a shortcut to making a new organization. We
// should really fix our unit tests and remove this.
//nolint:gocritic
organizations, _ := tx.GetOrganizations(dbauthz.AsSystemRestricted(ctx))
if len(organizations) > 0 {
// Add the user to the first organization. Once multi-organization
// support is added, we should enable a configuration map of user
// email to organization.
organizationID = organizations[0].ID
}
organization, _ := tx.GetDefaultOrganization(dbauthz.AsSystemRestricted(ctx))

// Add the user to the default organization.
// Once multi-organization we should check some configuration to see
// if we should add the user to a different organization.
organizationID = organization.ID
Comment on lines +1349 to +1354
Copy link
Member Author

Choose a reason for hiding this comment

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

This is annoyingly allowed to be uuid.Nil in unit tests. I am not going to change the behavior here, but we should really fix that so we don't have an edge case living for the sake of tests.

Copy link
Member

Choose a reason for hiding this comment

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

Fine not to fix here, but can we add a comment for the ignored err above (tx.GetDefaultOrganization) about this? Comment is just a band-aid though, we definitely want to nip this in the bud before too long.


//nolint:gocritic
_, err := tx.GetUserByEmailOrUsername(dbauthz.AsSystemRestricted(ctx), database.GetUserByEmailOrUsernameParams{
Expand Down Expand Up @@ -1395,7 +1397,7 @@ func (api *API) oauthLogin(r *http.Request, params *oauthLoginParams) ([]*http.C
// All of the userauth tests depend on this being able to create
// the first organization. It shouldn't be possible in normal
// operation.
CreateOrganization: len(organizations) == 0,
CreateOrganization: organizationID == uuid.Nil,
LoginType: params.LoginType,
})
if err != nil {
Expand Down