Skip to content

Commit 9e0adbf

Browse files
committed
Allow notification templates to be auditable
Signed-off-by: Danny Kopping <danny@coder.com>
1 parent 8556c38 commit 9e0adbf

File tree

8 files changed

+42
-27
lines changed

8 files changed

+42
-27
lines changed

coderd/database/dbauthz/dbauthz.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3026,11 +3026,11 @@ func (q *querier) UpdateMemberRoles(ctx context.Context, arg database.UpdateMemb
30263026
}
30273027

30283028
// TODO: how to restrict this to admins?
3029-
func (q *querier) UpdateNotificationTemplateMethod(ctx context.Context, arg database.UpdateNotificationTemplateMethodParams) (int64, error) {
3029+
func (q *querier) UpdateNotificationTemplateMethodById(ctx context.Context, arg database.UpdateNotificationTemplateMethodByIdParams) (database.NotificationTemplate, error) {
30303030
if err := q.authorizeContext(ctx, policy.ActionUpdate, rbac.ResourceNotificationTemplate); err != nil {
3031-
return -1, err
3031+
return database.NotificationTemplate{}, err
30323032
}
3033-
return q.db.UpdateNotificationTemplateMethod(ctx, arg)
3033+
return q.db.UpdateNotificationTemplateMethodById(ctx, arg)
30343034
}
30353035

30363036
func (q *querier) UpdateOAuth2ProviderAppByID(ctx context.Context, arg database.UpdateOAuth2ProviderAppByIDParams) (database.OAuth2ProviderApp, error) {

coderd/database/dbmem/dbmem.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7541,13 +7541,16 @@ func (q *FakeQuerier) UpdateMemberRoles(_ context.Context, arg database.UpdateMe
75417541
return database.OrganizationMember{}, sql.ErrNoRows
75427542
}
75437543

7544-
func (q *FakeQuerier) UpdateNotificationTemplateMethod(ctx context.Context, arg database.UpdateNotificationTemplateMethodParams) (int64, error) {
7544+
func (q *FakeQuerier) UpdateNotificationTemplateMethodById(_ context.Context, arg database.UpdateNotificationTemplateMethodByIdParams) (database.NotificationTemplate, error) {
75457545
err := validateDatabaseType(arg)
75467546
if err != nil {
7547-
return 0, err
7547+
return database.NotificationTemplate{}, err
75487548
}
75497549

7550-
return 1, nil
7550+
return database.NotificationTemplate{
7551+
ID: arg.ID,
7552+
Method: arg.Method,
7553+
}, nil
75517554
}
75527555

75537556
func (q *FakeQuerier) UpdateOAuth2ProviderAppByID(_ context.Context, arg database.UpdateOAuth2ProviderAppByIDParams) (database.OAuth2ProviderApp, error) {

coderd/database/dbmetrics/dbmetrics.go

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

coderd/database/migrations/000229_notification_preferences.up.sql renamed to coderd/database/migrations/000231_notification_preferences.up.sql

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,24 @@ CREATE TABLE notification_preferences
77
updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP
88
);
99

10-
-- Ensure we cannot insert multiple entries for the same user/template combination
10+
-- Ensure we cannot insert multiple entries for the same user/template combination.
1111
ALTER TABLE notification_preferences
1212
ADD CONSTRAINT unique_user_notification_template UNIQUE (user_id, notification_template_id);
1313

14-
-- Allow per-template notification method (enterprise only)
14+
-- Allow per-template notification method (enterprise only).
1515
ALTER TABLE notification_templates
1616
ADD COLUMN method notification_method;
1717
COMMENT ON COLUMN notification_templates.method IS 'NULL defers to the deployment-level method';
1818

19-
-- No equivalent in down migration because ENUM values cannot be deleted
19+
-- No equivalent in down migration because ENUM values cannot be deleted.
2020
ALTER TYPE notification_message_status ADD VALUE IF NOT EXISTS 'inhibited';
2121

22-
-- Function to prevent enqueuing notifications unnecessarily
22+
-- Function to prevent enqueuing notifications unnecessarily.
2323
CREATE OR REPLACE FUNCTION inhibit_enqueue_if_disabled()
2424
RETURNS TRIGGER AS
2525
$$
2626
BEGIN
27-
-- Fail the insertion if the user has disabled this notification
27+
-- Fail the insertion if the user has disabled this notification.
2828
IF EXISTS (SELECT 1
2929
FROM notification_preferences
3030
WHERE disabled = TRUE
@@ -37,9 +37,12 @@ BEGIN
3737
END;
3838
$$ LANGUAGE plpgsql;
3939

40-
-- Trigger to execute above function on insertion
40+
-- Trigger to execute above function on insertion.
4141
CREATE TRIGGER inhibit_enqueue_if_disabled_trigger
4242
BEFORE INSERT
4343
ON notification_messages
4444
FOR EACH ROW
45-
EXECUTE FUNCTION inhibit_enqueue_if_disabled();
45+
EXECUTE FUNCTION inhibit_enqueue_if_disabled();
46+
47+
-- Allow modifications to notification templates to be audited.
48+
ALTER TYPE resource_type ADD VALUE IF NOT EXISTS 'notification_template';

coderd/database/querier.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/queries.sql.go

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

coderd/database/queries/notifications.sql

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,11 @@ ON CONFLICT (user_id, notification_template_id) DO UPDATE
157157
SET disabled = EXCLUDED.disabled,
158158
updated_at = CURRENT_TIMESTAMP;
159159

160-
-- name: UpdateNotificationTemplateMethod :execrows
160+
-- name: UpdateNotificationTemplateMethodById :one
161161
UPDATE notification_templates
162162
SET method = sqlc.narg('method')::notification_method
163-
WHERE id = @id::uuid;
163+
WHERE id = @id::uuid
164+
RETURNING *;
164165

165166
-- name: GetNotificationTemplateById :one
166167
SELECT *

0 commit comments

Comments
 (0)