mirror of
https://github.com/coder/coder.git
synced 2025-07-15 22:20:27 +00:00
feat: add auto group create from OIDC (#8884)
* add flag for auto create groups * fixup! add flag for auto create groups * sync missing groups Also added a regex filter to filter out groups that are not important
This commit is contained in:
@ -9,10 +9,12 @@ import (
|
||||
"cdr.dev/slog"
|
||||
"github.com/coder/coder/coderd"
|
||||
"github.com/coder/coder/coderd/database"
|
||||
"github.com/coder/coder/coderd/database/dbauthz"
|
||||
"github.com/coder/coder/codersdk"
|
||||
)
|
||||
|
||||
func (api *API) setUserGroups(ctx context.Context, db database.Store, userID uuid.UUID, groupNames []string) error {
|
||||
// nolint: revive
|
||||
func (api *API) setUserGroups(ctx context.Context, logger slog.Logger, db database.Store, userID uuid.UUID, groupNames []string, createMissingGroups bool) error {
|
||||
api.entitlementsMu.RLock()
|
||||
enabled := api.entitlements.Features[codersdk.FeatureTemplateRBAC].Enabled
|
||||
api.entitlementsMu.RUnlock()
|
||||
@ -39,6 +41,25 @@ func (api *API) setUserGroups(ctx context.Context, db database.Store, userID uui
|
||||
return xerrors.Errorf("delete user groups: %w", err)
|
||||
}
|
||||
|
||||
if createMissingGroups {
|
||||
// This is the system creating these additional groups, so we use the system restricted context.
|
||||
// nolint:gocritic
|
||||
created, err := tx.InsertMissingGroups(dbauthz.AsSystemRestricted(ctx), database.InsertMissingGroupsParams{
|
||||
OrganizationID: orgs[0].ID,
|
||||
GroupNames: groupNames,
|
||||
Source: database.GroupSourceOidc,
|
||||
})
|
||||
if err != nil {
|
||||
return xerrors.Errorf("insert missing groups: %w", err)
|
||||
}
|
||||
if len(created) > 0 {
|
||||
logger.Debug(ctx, "auto created missing groups",
|
||||
slog.F("org_id", orgs[0].ID),
|
||||
slog.F("created", created),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// Re-add the user to all groups returned by the auth provider.
|
||||
err = tx.InsertUserGroupsByName(ctx, database.InsertUserGroupsByNameParams{
|
||||
UserID: userID,
|
||||
@ -53,13 +74,13 @@ func (api *API) setUserGroups(ctx context.Context, db database.Store, userID uui
|
||||
}, nil)
|
||||
}
|
||||
|
||||
func (api *API) setUserSiteRoles(ctx context.Context, db database.Store, userID uuid.UUID, roles []string) error {
|
||||
func (api *API) setUserSiteRoles(ctx context.Context, logger slog.Logger, db database.Store, userID uuid.UUID, roles []string) error {
|
||||
api.entitlementsMu.RLock()
|
||||
enabled := api.entitlements.Features[codersdk.FeatureUserRoleManagement].Enabled
|
||||
api.entitlementsMu.RUnlock()
|
||||
|
||||
if !enabled {
|
||||
api.Logger.Warn(ctx, "attempted to assign OIDC user roles without enterprise entitlement, roles left unchanged",
|
||||
logger.Warn(ctx, "attempted to assign OIDC user roles without enterprise entitlement, roles left unchanged",
|
||||
slog.F("user_id", userID), slog.F("roles", roles),
|
||||
)
|
||||
return nil
|
||||
|
Reference in New Issue
Block a user