-
Notifications
You must be signed in to change notification settings - Fork 881
feat: set groupsync to use default org #12146
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
Conversation
coderd/userauth.go
Outdated
//nolint:gocritic // No user present in the context. | ||
defaultOrganization, err := tx.GetDefaultOrganization(dbauthz.AsSystemRestricted(ctx)) | ||
if err != nil { | ||
// If there is no default org, then we can't assign groups. | ||
// By default, we assume all groups belong to the default org. | ||
return xerrors.Errorf("get default organization: %w", err) | ||
} | ||
|
||
//nolint:gocritic // No user present in the context. | ||
memberships, err := tx.GetOrganizationMembershipsByUserID(dbauthz.AsSystemRestricted(ctx), user.ID) | ||
if err != nil { | ||
return xerrors.Errorf("get organization memberships: %w", err) | ||
} | ||
|
||
inDefault := false | ||
for _, membership := range memberships { | ||
if membership.OrganizationID == defaultOrganization.ID { | ||
inDefault = true | ||
break | ||
} | ||
} | ||
|
||
if !inDefault { | ||
return xerrors.Errorf("user %s is not a member of the default organization, cannot assign to groups in the org", user.ID) | ||
} | ||
|
||
//nolint:gocritic | ||
err := api.Options.SetUserGroups(dbauthz.AsSystemRestricted(ctx), logger, tx, user.ID, filtered, params.CreateMissingGroups) | ||
err = api.Options.SetUserGroups(dbauthz.AsSystemRestricted(ctx), logger, tx, user.ID, map[uuid.UUID][]string{ | ||
defaultOrganization.ID: filtered, | ||
}, params.CreateMissingGroups) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do not love how much code this ended up being. I can reduce it by adding another query, but this is all temporary. We need to solve group mapping to orgs, this code is static to only work with the default org.
This PR is required to remove the single org assumption.
// TODO: This could likely be improved by making these single queries. | ||
// Either by batching or some other means. This for loop could be really | ||
// inefficient if there are a lot of organizations. There was deployments | ||
// on v1 with >100 orgs. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is just phase 1 to getting the codebase to accept multi orgs. I do not want to prematurely optimize by coming up with some batching that might be removed.
I think we should move to pgx
as our pg driver in sqlc to get batching support.
5322f4e
to
ddb828c
Compare
9e0314c
to
723f42d
Compare
ddb828c
to
fc2fce2
Compare
723f42d
to
6b10251
Compare
This is not a final solution, as we eventually want to be able to map to different orgs. This makes it so multi-org does not break oauth/oidc.
fc2fce2
to
872f4a2
Compare
5bbfa1a
to
ced4905
Compare
ced4905
to
da6c21c
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks for adding comments along the way!
coderd/userauth.go
Outdated
inDefault = true | ||
break | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@@ -1301,7 +1303,6 @@ func (api *API) oauthLogin(r *http.Request, params *oauthLoginParams) ([]*http.C | |||
link database.UserLink | |||
err error | |||
) | |||
|
|||
user = params.User | |||
link = params.Link | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Closes #11938