Skip to content

Commit 2dbb462

Browse files
Emyrkmtojek
authored andcommitted
chore: remove autocreate orgs on CreateUser (#12434)
New users must be explictly given an organization to join. Organizations should not be auto created as a side effect of creating a new user.
1 parent fb7f1ac commit 2dbb462

File tree

2 files changed

+13
-46
lines changed

2 files changed

+13
-46
lines changed

coderd/userauth.go

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,20 +1351,16 @@ func (api *API) oauthLogin(r *http.Request, params *oauthLoginParams) ([]*http.C
13511351
// This can happen if a user is a built-in user but is signing in
13521352
// with OIDC for the first time.
13531353
if user.ID == uuid.Nil {
1354-
var organizationID uuid.UUID
1355-
// Ignoring this error is a product of our unit tests. In prod this should never
1356-
// happen. Unit tests use this as a shortcut to making a new organization. We
1357-
// should really fix our unit tests and remove this.
1354+
// Until proper multi-org support, all users will be added to the default organization.
1355+
// The default organization should always be present.
13581356
//nolint:gocritic
1359-
organization, _ := tx.GetDefaultOrganization(dbauthz.AsSystemRestricted(ctx))
1360-
1361-
// Add the user to the default organization.
1362-
// Once multi-organization we should check some configuration to see
1363-
// if we should add the user to a different organization.
1364-
organizationID = organization.ID
1357+
defaultOrganization, err := tx.GetDefaultOrganization(dbauthz.AsSystemRestricted(ctx))
1358+
if err != nil {
1359+
return xerrors.Errorf("unable to fetch default organization: %w", err)
1360+
}
13651361

13661362
//nolint:gocritic
1367-
_, err := tx.GetUserByEmailOrUsername(dbauthz.AsSystemRestricted(ctx), database.GetUserByEmailOrUsernameParams{
1363+
_, err = tx.GetUserByEmailOrUsername(dbauthz.AsSystemRestricted(ctx), database.GetUserByEmailOrUsernameParams{
13681364
Username: params.Username,
13691365
})
13701366
if err == nil {
@@ -1402,13 +1398,9 @@ func (api *API) oauthLogin(r *http.Request, params *oauthLoginParams) ([]*http.C
14021398
CreateUserRequest: codersdk.CreateUserRequest{
14031399
Email: params.Email,
14041400
Username: params.Username,
1405-
OrganizationID: organizationID,
1401+
OrganizationID: defaultOrganization.ID,
14061402
},
1407-
// All of the userauth tests depend on this being able to create
1408-
// the first organization. It shouldn't be possible in normal
1409-
// operation.
1410-
CreateOrganization: organizationID == uuid.Nil,
1411-
LoginType: params.LoginType,
1403+
LoginType: params.LoginType,
14121404
})
14131405
if err != nil {
14141406
return xerrors.Errorf("create user: %w", err)

coderd/users.go

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,7 @@ func (api *API) postFirstUser(rw http.ResponseWriter, r *http.Request) {
201201
Password: createUser.Password,
202202
OrganizationID: defaultOrg.ID,
203203
},
204-
CreateOrganization: false,
205-
LoginType: database.LoginTypePassword,
204+
LoginType: database.LoginTypePassword,
206205
})
207206
if err != nil {
208207
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
@@ -1231,8 +1230,7 @@ func (api *API) organizationByUserAndName(rw http.ResponseWriter, r *http.Reques
12311230

12321231
type CreateUserRequest struct {
12331232
codersdk.CreateUserRequest
1234-
CreateOrganization bool
1235-
LoginType database.LoginType
1233+
LoginType database.LoginType
12361234
}
12371235

12381236
func (api *API) CreateUser(ctx context.Context, store database.Store, req CreateUserRequest) (database.User, uuid.UUID, error) {
@@ -1245,32 +1243,9 @@ func (api *API) CreateUser(ctx context.Context, store database.Store, req Create
12451243
var user database.User
12461244
return user, req.OrganizationID, store.InTx(func(tx database.Store) error {
12471245
orgRoles := make([]string, 0)
1248-
// If no organization is provided, create a new one for the user.
1246+
// Organization is required to know where to allocate the user.
12491247
if req.OrganizationID == uuid.Nil {
1250-
if !req.CreateOrganization {
1251-
return xerrors.Errorf("organization ID must be provided")
1252-
}
1253-
1254-
organization, err := tx.InsertOrganization(ctx, database.InsertOrganizationParams{
1255-
ID: uuid.New(),
1256-
Name: req.Username,
1257-
CreatedAt: dbtime.Now(),
1258-
UpdatedAt: dbtime.Now(),
1259-
Description: "",
1260-
})
1261-
if err != nil {
1262-
return xerrors.Errorf("create organization: %w", err)
1263-
}
1264-
req.OrganizationID = organization.ID
1265-
// TODO: When organizations are allowed to be created, we should
1266-
// come back to determining the default role of the person who
1267-
// creates the org. Until that happens, all users in an organization
1268-
// should be just regular members. Membership role is implied, and
1269-
// not required to be explicit.
1270-
_, err = tx.InsertAllUsersGroup(ctx, organization.ID)
1271-
if err != nil {
1272-
return xerrors.Errorf("create %q group: %w", database.EveryoneGroup, err)
1273-
}
1248+
return xerrors.Errorf("organization ID must be provided")
12741249
}
12751250

12761251
params := database.InsertUserParams{

0 commit comments

Comments
 (0)