Skip to content

Commit bfddeb6

Browse files
committed
begin group sync main work
1 parent 99c97c2 commit bfddeb6

File tree

12 files changed

+331
-12
lines changed

12 files changed

+331
-12
lines changed

coderd/coderd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ func New(options *Options) *API {
312312
)
313313

314314
if options.IDPSync == nil {
315-
options.IDPSync = idpsync.NewAGPLSync(options.Logger, idpsync.SyncSettings{
315+
options.IDPSync = idpsync.NewAGPLSync(options.Logger, idpsync.DeploymentSyncSettings{
316316
OrganizationField: options.DeploymentValues.OIDC.OrganizationField.Value(),
317317
OrganizationMapping: options.DeploymentValues.OIDC.OrganizationMapping.Value,
318318
OrganizationAssignDefault: options.DeploymentValues.OIDC.OrganizationAssignDefault.Value(),

coderd/database/dbauthz/dbauthz.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2892,6 +2892,14 @@ func (q *querier) InsertUser(ctx context.Context, arg database.InsertUserParams)
28922892
return insert(q.log, q.auth, obj, q.db.InsertUser)(ctx, arg)
28932893
}
28942894

2895+
func (q *querier) InsertUserGroupsByID(ctx context.Context, arg database.InsertUserGroupsByIDParams) ([]uuid.UUID, error) {
2896+
// This is used by OIDC sync. So only used by a system user.
2897+
if err := q.authorizeContext(ctx, policy.ActionUpdate, rbac.ResourceSystem); err != nil {
2898+
return nil, err
2899+
}
2900+
return q.db.InsertUserGroupsByID(ctx, arg)
2901+
}
2902+
28952903
func (q *querier) InsertUserGroupsByName(ctx context.Context, arg database.InsertUserGroupsByNameParams) error {
28962904
// This will add the user to all named groups. This counts as updating a group.
28972905
// NOTE: instead of checking if the user has permission to update each group, we instead

coderd/database/dbauthz/dbauthz_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,17 @@ func (s *MethodTestSuite) TestGroup() {
388388
GroupNames: slice.New(g1.Name, g2.Name),
389389
}).Asserts(rbac.ResourceGroup.InOrg(o.ID), policy.ActionUpdate).Returns()
390390
}))
391+
s.Run("InsertUserGroupsByID", s.Subtest(func(db database.Store, check *expects) {
392+
o := dbgen.Organization(s.T(), db, database.Organization{})
393+
u1 := dbgen.User(s.T(), db, database.User{})
394+
g1 := dbgen.Group(s.T(), db, database.Group{OrganizationID: o.ID})
395+
g2 := dbgen.Group(s.T(), db, database.Group{OrganizationID: o.ID})
396+
_ = dbgen.GroupMember(s.T(), db, database.GroupMemberTable{GroupID: g1.ID, UserID: u1.ID})
397+
check.Args(database.InsertUserGroupsByIDParams{
398+
UserID: u1.ID,
399+
GroupIds: slice.New(g1.ID, g2.ID),
400+
}).Asserts(rbac.ResourceSystem, policy.ActionUpdate).Returns(slice.New(g1, g2))
401+
}))
391402
s.Run("RemoveUserFromAllGroups", s.Subtest(func(db database.Store, check *expects) {
392403
o := dbgen.Organization(s.T(), db, database.Organization{})
393404
u1 := dbgen.User(s.T(), db, database.User{})

coderd/database/dbmem/dbmem.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7015,7 +7015,37 @@ func (q *FakeQuerier) InsertUser(_ context.Context, arg database.InsertUserParam
70157015
return user, nil
70167016
}
70177017

7018+
func (q *FakeQuerier) InsertUserGroupsByID(ctx context.Context, arg database.InsertUserGroupsByIDParams) ([]uuid.UUID, error) {
7019+
err := validateDatabaseType(arg)
7020+
if err != nil {
7021+
return nil, err
7022+
}
7023+
7024+
q.mutex.Lock()
7025+
defer q.mutex.Unlock()
7026+
7027+
var groupIDs []uuid.UUID
7028+
for _, group := range q.groups {
7029+
for _, groupID := range arg.GroupIds {
7030+
if group.ID == groupID {
7031+
q.groupMembers = append(q.groupMembers, database.GroupMemberTable{
7032+
UserID: arg.UserID,
7033+
GroupID: groupID,
7034+
})
7035+
groupIDs = append(groupIDs, group.ID)
7036+
}
7037+
}
7038+
}
7039+
7040+
return groupIDs, nil
7041+
}
7042+
70187043
func (q *FakeQuerier) InsertUserGroupsByName(_ context.Context, arg database.InsertUserGroupsByNameParams) error {
7044+
err := validateDatabaseType(arg)
7045+
if err != nil {
7046+
return err
7047+
}
7048+
70197049
q.mutex.Lock()
70207050
defer q.mutex.Unlock()
70217051

coderd/database/dbmetrics/dbmetrics.go

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/models.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/querier.go

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries.sql.go

Lines changed: 49 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/groupmembers.sql

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,25 @@ SELECT
2929
FROM
3030
groups;
3131

32+
-- InsertUserGroupsByID adds a user to all provided groups, if they exist.
33+
-- name: InsertUserGroupsByID :many
34+
WITH groups AS (
35+
SELECT
36+
id
37+
FROM
38+
groups
39+
WHERE
40+
groups.id = ANY(@group_ids :: uuid [])
41+
)
42+
INSERT INTO
43+
group_members (user_id, group_id)
44+
SELECT
45+
@user_id,
46+
groups.id
47+
FROM
48+
groups
49+
RETURNING group_id;
50+
3251
-- name: RemoveUserFromAllGroups :exec
3352
DELETE FROM
3453
group_members

0 commit comments

Comments
 (0)