Skip to content

Commit 2c7b98d

Browse files
committed
feat: allow setting quotas on Everyone group
1 parent 5b2ea2e commit 2c7b98d

21 files changed

+306
-82
lines changed

coderd/database/dbauthz/dbauthz.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -916,11 +916,11 @@ func (q *querier) GetGroupByOrgAndName(ctx context.Context, arg database.GetGrou
916916
return fetch(q.log, q.auth, q.db.GetGroupByOrgAndName)(ctx, arg)
917917
}
918918

919-
func (q *querier) GetGroupMembers(ctx context.Context, groupID uuid.UUID) ([]database.User, error) {
920-
if _, err := q.GetGroupByID(ctx, groupID); err != nil { // AuthZ check
919+
func (q *querier) GetGroupMembers(ctx context.Context, arg database.GetGroupMembersParams) ([]database.User, error) {
920+
if _, err := q.GetGroupByID(ctx, arg.ID); err != nil { // AuthZ check
921921
return nil, err
922922
}
923-
return q.db.GetGroupMembers(ctx, groupID)
923+
return q.db.GetGroupMembers(ctx, arg)
924924
}
925925

926926
func (q *querier) GetGroupsByOrganizationID(ctx context.Context, organizationID uuid.UUID) ([]database.Group, error) {

coderd/database/dbauthz/dbauthz_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ func (s *MethodTestSuite) TestGroup() {
288288
s.Run("GetGroupMembers", s.Subtest(func(db database.Store, check *expects) {
289289
g := dbgen.Group(s.T(), db, database.Group{})
290290
_ = dbgen.GroupMember(s.T(), db, database.GroupMember{})
291-
check.Args(g.ID).Asserts(g, rbac.ActionRead)
291+
check.Args(database.GetGroupMembersParams{ID: g.ID, OrganizationID: g.OrganizationID}).Asserts(g, rbac.ActionRead)
292292
}))
293293
s.Run("InsertAllUsersGroup", s.Subtest(func(db database.Store, check *expects) {
294294
o := dbgen.Organization(s.T(), db, database.Organization{})

coderd/database/dbfake/dbfake.go

+17-9
Original file line numberDiff line numberDiff line change
@@ -1376,13 +1376,18 @@ func (q *FakeQuerier) GetGroupByOrgAndName(_ context.Context, arg database.GetGr
13761376
return database.Group{}, sql.ErrNoRows
13771377
}
13781378

1379-
func (q *FakeQuerier) GetGroupMembers(_ context.Context, groupID uuid.UUID) ([]database.User, error) {
1379+
func (q *FakeQuerier) GetGroupMembers(_ context.Context, arg database.GetGroupMembersParams) ([]database.User, error) {
13801380
q.mutex.RLock()
13811381
defer q.mutex.RUnlock()
13821382

1383+
if arg.ID == arg.OrganizationID {
1384+
var cp []database.User
1385+
return append(cp, q.users...), nil
1386+
}
1387+
13831388
var members []database.GroupMember
13841389
for _, member := range q.groupMembers {
1385-
if member.GroupID == groupID {
1390+
if member.GroupID == arg.ID {
13861391
members = append(members, member)
13871392
}
13881393
}
@@ -1401,17 +1406,12 @@ func (q *FakeQuerier) GetGroupMembers(_ context.Context, groupID uuid.UUID) ([]d
14011406
return users, nil
14021407
}
14031408

1404-
func (q *FakeQuerier) GetGroupsByOrganizationID(_ context.Context, organizationID uuid.UUID) ([]database.Group, error) {
1409+
func (q *FakeQuerier) GetGroupsByOrganizationID(_ context.Context, _ uuid.UUID) ([]database.Group, error) {
14051410
q.mutex.RLock()
14061411
defer q.mutex.RUnlock()
14071412

14081413
var groups []database.Group
1409-
for _, group := range q.groups {
1410-
// Omit the allUsers group.
1411-
if group.OrganizationID == organizationID && group.ID != organizationID {
1412-
groups = append(groups, group)
1413-
}
1414-
}
1414+
groups = append(groups, q.groups...)
14151415

14161416
return groups, nil
14171417
}
@@ -1838,9 +1838,17 @@ func (q *FakeQuerier) GetQuotaAllowanceForUser(_ context.Context, userID uuid.UU
18381838
for _, group := range q.groups {
18391839
if group.ID == member.GroupID {
18401840
sum += int64(group.QuotaAllowance)
1841+
continue
18411842
}
18421843
}
18431844
}
1845+
// Grab the quota for the Everyone group.
1846+
for _, group := range q.groups {
1847+
if group.ID == group.OrganizationID {
1848+
sum += int64(group.QuotaAllowance)
1849+
break
1850+
}
1851+
}
18441852
return sum, nil
18451853
}
18461854

coderd/database/dbgen/dbgen_test.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,10 @@ func TestGenerator(t *testing.T) {
105105
exp := []database.User{u}
106106
dbgen.GroupMember(t, db, database.GroupMember{GroupID: g.ID, UserID: u.ID})
107107

108-
require.Equal(t, exp, must(db.GetGroupMembers(context.Background(), g.ID)))
108+
require.Equal(t, exp, must(db.GetGroupMembers(context.Background(), database.GetGroupMembersParams{
109+
ID: g.ID,
110+
OrganizationID: g.OrganizationID,
111+
})))
109112
})
110113

111114
t.Run("Organization", func(t *testing.T) {

coderd/database/dbmetrics/dbmetrics.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/dbmock/dbmock.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/modelmethods.go

+4
Original file line numberDiff line numberDiff line change
@@ -362,3 +362,7 @@ func ConvertWorkspaceRows(rows []GetWorkspacesRow) []Workspace {
362362

363363
return workspaces
364364
}
365+
366+
func (g Group) IsEveryoneGroup() bool {
367+
return g.ID == g.OrganizationID
368+
}

coderd/database/querier.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries.sql.go

+28-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/groupmembers.sql

+17-3
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,26 @@ SELECT
33
users.*
44
FROM
55
users
6-
JOIN
6+
LEFT JOIN
77
group_members
88
ON
9-
users.id = group_members.user_id
9+
CASE WHEN @id:: uuid != @organization_id :: uuid THEN
10+
group_members.user_id = users.id
11+
END
12+
LEFT JOIN
13+
organization_members
14+
ON
15+
CASE WHEN @id :: uuid = @organization_id :: uuid THEN
16+
organization_members.user_id = users.id
17+
END
1018
WHERE
11-
group_members.group_id = $1
19+
CASE WHEN @id :: uuid != @organization_id :: uuid THEN
20+
group_members.group_id = @id
21+
ELSE true END
22+
AND
23+
CASE WHEN @id :: uuid = @organization_id :: uuid THEN
24+
organization_members.organization_id = @organization_id
25+
ELSE true END
1226
AND
1327
users.status = 'active'
1428
AND

coderd/database/queries/groups.sql

+1-3
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ SELECT
2626
FROM
2727
groups
2828
WHERE
29-
organization_id = $1
30-
AND
31-
id != $1;
29+
organization_id = $1;
3230

3331
-- name: InsertGroup :one
3432
INSERT INTO groups (

coderd/database/queries/quotas.sql

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
SELECT
33
coalesce(SUM(quota_allowance), 0)::BIGINT
44
FROM
5-
group_members gm
6-
JOIN groups g ON
5+
groups g
6+
LEFT JOIN group_members gm ON
77
g.id = gm.group_id
88
WHERE
9-
user_id = $1;
9+
user_id = $1
10+
OR
11+
g.id = g.organization_id;
1012

1113
-- name: GetQuotaConsumedForUser :one
1214
WITH latest_builds AS (

enterprise/cli/grouplist_test.go

+11-5
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,10 @@ func TestGroupList(t *testing.T) {
7474
}
7575
})
7676

77-
t.Run("NoGroups", func(t *testing.T) {
77+
t.Run("EveryoneGroup", func(t *testing.T) {
7878
t.Parallel()
7979

80-
client, _ := coderdenttest.New(t, &coderdenttest.Options{LicenseOptions: &coderdenttest.LicenseOptions{
80+
client, user := coderdenttest.New(t, &coderdenttest.Options{LicenseOptions: &coderdenttest.LicenseOptions{
8181
Features: license.Features{
8282
codersdk.FeatureTemplateRBAC: 1,
8383
},
@@ -87,13 +87,19 @@ func TestGroupList(t *testing.T) {
8787

8888
pty := ptytest.New(t)
8989

90-
inv.Stderr = pty.Output()
90+
inv.Stdout = pty.Output()
9191
clitest.SetupConfig(t, client, conf)
9292

9393
err := inv.Run()
9494
require.NoError(t, err)
9595

96-
pty.ExpectMatch("No groups found")
97-
pty.ExpectMatch("coder groups create <name>")
96+
matches := []string{
97+
"NAME", "ORGANIZATION ID", "MEMBERS", " AVATAR URL",
98+
"Everyone", user.OrganizationID.String(), coderdtest.FirstUserParams.Email, "",
99+
}
100+
101+
for _, match := range matches {
102+
pty.ExpectMatch(match)
103+
}
98104
})
99105
}

0 commit comments

Comments
 (0)