Skip to content

Commit e6b48c3

Browse files
feat: notify users on template deprecation
1 parent 5076161 commit e6b48c3

File tree

12 files changed

+167
-1
lines changed

12 files changed

+167
-1
lines changed

coderd/database/dbauthz/dbauthz.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2339,6 +2339,16 @@ func (q *querier) GetUsersByIDs(ctx context.Context, ids []uuid.UUID) ([]databas
23392339
return q.db.GetUsersByIDs(ctx, ids)
23402340
}
23412341

2342+
func (q *querier) GetUsersWithAccessToTemplateByID(ctx context.Context, id uuid.UUID) ([]uuid.UUID, error) {
2343+
// Ensure we have permission to access this template.
2344+
_, err := q.GetTemplateByID(ctx, id)
2345+
if err != nil {
2346+
return nil, err
2347+
}
2348+
2349+
return q.db.GetUsersWithAccessToTemplateByID(ctx, id)
2350+
}
2351+
23422352
func (q *querier) GetWorkspaceAgentAndLatestBuildByAuthToken(ctx context.Context, authToken uuid.UUID) (database.GetWorkspaceAgentAndLatestBuildByAuthTokenRow, error) {
23432353
// This is a system function
23442354
if err := q.authorizeContext(ctx, policy.ActionRead, rbac.ResourceSystem); err != nil {

coderd/database/dbauthz/dbauthz_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,6 +1131,10 @@ func (s *MethodTestSuite) TestUser() {
11311131
Asserts(a, policy.ActionRead, b, policy.ActionRead).
11321132
Returns(slice.New(a, b))
11331133
}))
1134+
s.Run("GetUsersWithAccessToTemplateByID", s.Subtest(func(db database.Store, check *expects) {
1135+
a := dbgen.Template(s.T(), db, database.Template{})
1136+
check.Args(a.ID).Asserts(a, policy.ActionRead)
1137+
}))
11341138
s.Run("GetUsers", s.Subtest(func(db database.Store, check *expects) {
11351139
dbgen.User(s.T(), db, database.User{Username: "GetUsers-a-user"})
11361140
dbgen.User(s.T(), db, database.User{Username: "GetUsers-b-user"})

coderd/database/dbmem/dbmem.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5619,6 +5619,33 @@ func (q *FakeQuerier) GetUsersByIDs(_ context.Context, ids []uuid.UUID) ([]datab
56195619
return users, nil
56205620
}
56215621

5622+
func (q *FakeQuerier) GetUsersWithAccessToTemplateByID(ctx context.Context, id uuid.UUID) ([]uuid.UUID, error) {
5623+
q.mutex.RLock()
5624+
defer q.mutex.RUnlock()
5625+
5626+
groups := make(map[string]bool, 0)
5627+
for _, template := range q.templates {
5628+
if template.ID != id {
5629+
continue
5630+
}
5631+
5632+
for group := range template.GroupACL {
5633+
groups[group] = true
5634+
}
5635+
}
5636+
5637+
users := make([]uuid.UUID, 0)
5638+
for _, member := range q.organizationMembers {
5639+
if _, ok := groups[member.OrganizationID.String()]; !ok {
5640+
continue
5641+
}
5642+
5643+
users = append(users, member.UserID)
5644+
}
5645+
5646+
return users, nil
5647+
}
5648+
56225649
func (q *FakeQuerier) GetWorkspaceAgentAndLatestBuildByAuthToken(_ context.Context, authToken uuid.UUID) (database.GetWorkspaceAgentAndLatestBuildByAuthTokenRow, error) {
56235650
q.mutex.RLock()
56245651
defer q.mutex.RUnlock()

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/dbmock/dbmock.go

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DELETE FROM notification_templates WHERE id = 'f40fae84-55a2-42cd-99fa-b41c1ca64894';
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
INSERT INTO notification_templates
2+
(id, name, title_template, body_template, "group", actions)
3+
VALUES (
4+
'f40fae84-55a2-42cd-99fa-b41c1ca64894',
5+
'Template Deprecated',
6+
E'Template **{{.Labels.template}}** has been deprecated',
7+
E'Hello {{.UserName}},\n\n'||
8+
E'The template **{{.Labels.template}}** has been deprecated with the following message:\n\n' ||
9+
E'**{{.Labels.message}}**\n\n' ||
10+
E'New workspaces may not be created from this template. Existing workspaces will continue to function normally.',
11+
'Template Events',
12+
'[]'::jsonb
13+
);

coderd/database/querier.go

Lines changed: 1 addition & 0 deletions
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: 38 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/templates.sql

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,3 +199,18 @@ SET
199199
WHERE
200200
id = $1
201201
;
202+
203+
-- name: GetUsersWithAccessToTemplateByID :many
204+
SELECT
205+
user_id
206+
FROM
207+
organization_members
208+
WHERE
209+
organization_members.organization_id::text IN (
210+
SELECT
211+
jsonb_object_keys(group_acl)
212+
FROM
213+
templates
214+
WHERE templates.id = $1
215+
)
216+
;

0 commit comments

Comments
 (0)