@@ -171,16 +171,37 @@ func (api *API) postFirstUser(rw http.ResponseWriter, r *http.Request) {
171
171
}
172
172
}
173
173
174
+ //nolint:gocritic // needed to create first user
175
+ defaultOrg , err := api .Database .GetDefaultOrganization (dbauthz .AsSystemRestricted (ctx ))
176
+ if err != nil {
177
+ httpapi .Write (ctx , rw , http .StatusInternalServerError , codersdk.Response {
178
+ Message : "Internal error fetching default organization. If you are encountering this error, you will have to restart the Coder deployment." ,
179
+ Detail : err .Error (),
180
+ })
181
+ return
182
+ }
183
+
184
+ //nolint:gocritic // ensure everyone group
185
+ _ , err = api .Database .InsertAllUsersGroup (dbauthz .AsSystemRestricted (ctx ), defaultOrg .ID )
186
+ // A unique constraint violation just means the group already exists.
187
+ // This should not happen, but is ok if it does.
188
+ if err != nil && ! database .IsUniqueViolation (err ) {
189
+ httpapi .Write (ctx , rw , http .StatusInternalServerError , codersdk.Response {
190
+ Message : "Internal error creating all users group." ,
191
+ Detail : err .Error (),
192
+ })
193
+ return
194
+ }
195
+
174
196
//nolint:gocritic // needed to create first user
175
197
user , organizationID , err := api .CreateUser (dbauthz .AsSystemRestricted (ctx ), api .Database , CreateUserRequest {
176
198
CreateUserRequest : codersdk.CreateUserRequest {
177
- Email : createUser .Email ,
178
- Username : createUser .Username ,
179
- Password : createUser .Password ,
180
- // Create an org for the first user.
181
- OrganizationID : uuid .Nil ,
199
+ Email : createUser .Email ,
200
+ Username : createUser .Username ,
201
+ Password : createUser .Password ,
202
+ OrganizationID : defaultOrg .ID ,
182
203
},
183
- CreateOrganization : true ,
204
+ CreateOrganization : false ,
184
205
LoginType : database .LoginTypePassword ,
185
206
})
186
207
if err != nil {
@@ -1033,10 +1054,7 @@ func (api *API) userRoles(rw http.ResponseWriter, r *http.Request) {
1033
1054
}
1034
1055
1035
1056
for _ , mem := range memberships {
1036
- // If we can read the org member, include the roles.
1037
- if err == nil {
1038
- resp .OrganizationRoles [mem .OrganizationID ] = mem .Roles
1039
- }
1057
+ resp .OrganizationRoles [mem .OrganizationID ] = mem .Roles
1040
1058
}
1041
1059
1042
1060
httpapi .Write (ctx , rw , http .StatusOK , resp )
@@ -1247,9 +1265,8 @@ func (api *API) CreateUser(ctx context.Context, store database.Store, req Create
1247
1265
// TODO: When organizations are allowed to be created, we should
1248
1266
// come back to determining the default role of the person who
1249
1267
// creates the org. Until that happens, all users in an organization
1250
- // should be just regular members.
1251
- orgRoles = append (orgRoles , rbac .RoleOrgMember (req .OrganizationID ))
1252
-
1268
+ // should be just regular members. Membership role is implied, and
1269
+ // not required to be explicit.
1253
1270
_ , err = tx .InsertAllUsersGroup (ctx , organization .ID )
1254
1271
if err != nil {
1255
1272
return xerrors .Errorf ("create %q group: %w" , database .EveryoneGroup , err )
0 commit comments