Skip to content

Commit e11d3ca

Browse files
authored
chore: move default everyone group to a migration (coder#12435)
1 parent f308322 commit e11d3ca

File tree

4 files changed

+19
-13
lines changed

4 files changed

+19
-13
lines changed

coderd/database/dbmem/dbmem.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func New() database.Store {
7878
},
7979
}
8080
// Always start with a default org. Matching migration 198.
81-
_, err := q.InsertOrganization(context.Background(), database.InsertOrganizationParams{
81+
defaultOrg, err := q.InsertOrganization(context.Background(), database.InsertOrganizationParams{
8282
ID: uuid.New(),
8383
Name: "first-organization",
8484
Description: "Builtin default organization.",
@@ -88,6 +88,12 @@ func New() database.Store {
8888
if err != nil {
8989
panic(xerrors.Errorf("failed to create default organization: %w", err))
9090
}
91+
92+
_, err = q.InsertAllUsersGroup(context.Background(), defaultOrg.ID)
93+
if err != nil {
94+
panic(fmt.Errorf("failed to create default group: %w", err))
95+
}
96+
9197
q.defaultProxyDisplayName = "Default"
9298
q.defaultProxyIconURL = "/emojis/1f3e1.png"
9399
return q
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-- Nothing to do. If the group exists, this is ok.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
-- This ensures a default everyone group exists for default org.
2+
INSERT INTO
3+
groups(name, id, organization_id)
4+
SELECT
5+
-- This is a special keyword that must be exactly this.
6+
'Everyone',
7+
-- Org ID and group ID must match.
8+
(SELECT id FROM organizations WHERE is_default = true LIMIT 1),
9+
(SELECT id FROM organizations WHERE is_default = true LIMIT 1)
10+
-- It might already exist
11+
ON CONFLICT DO NOTHING;

coderd/users.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -181,18 +181,6 @@ func (api *API) postFirstUser(rw http.ResponseWriter, r *http.Request) {
181181
return
182182
}
183183

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-
196184
//nolint:gocritic // needed to create first user
197185
user, organizationID, err := api.CreateUser(dbauthz.AsSystemRestricted(ctx), api.Database, CreateUserRequest{
198186
CreateUserRequest: codersdk.CreateUserRequest{

0 commit comments

Comments
 (0)