Skip to content

Commit 5b39130

Browse files
committed
Merge branch 'dk/notification-prefs/db' into dk/notification-prefs/audit
2 parents ec9db0e + 65e8546 commit 5b39130

25 files changed

+963
-222
lines changed

coderd/apidoc/docs.go

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

coderd/apidoc/swagger.json

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

coderd/database/dbauthz/dbauthz.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1474,6 +1474,21 @@ func (q *querier) GetNotificationMessagesByStatus(ctx context.Context, arg datab
14741474
return q.db.GetNotificationMessagesByStatus(ctx, arg)
14751475
}
14761476

1477+
func (q *querier) GetNotificationTemplateById(ctx context.Context, id uuid.UUID) (database.NotificationTemplate, error) {
1478+
if err := q.authorizeContext(ctx, policy.ActionRead, rbac.ResourceNotificationTemplate); err != nil {
1479+
return database.NotificationTemplate{}, err
1480+
}
1481+
return q.db.GetNotificationTemplateById(ctx, id)
1482+
}
1483+
1484+
func (q *querier) GetNotificationTemplatesByKind(ctx context.Context, kind database.NotificationTemplateKind) ([]database.NotificationTemplate, error) {
1485+
// TODO: restrict 'system' kind to admins only?
1486+
if err := q.authorizeContext(ctx, policy.ActionRead, rbac.ResourceNotificationTemplate); err != nil {
1487+
return nil, err
1488+
}
1489+
return q.db.GetNotificationTemplatesByKind(ctx, kind)
1490+
}
1491+
14771492
func (q *querier) GetNotificationsSettings(ctx context.Context) (string, error) {
14781493
// No authz checks
14791494
return q.db.GetNotificationsSettings(ctx)
@@ -2085,6 +2100,13 @@ func (q *querier) GetUserLinksByUserID(ctx context.Context, userID uuid.UUID) ([
20852100
return q.db.GetUserLinksByUserID(ctx, userID)
20862101
}
20872102

2103+
func (q *querier) GetUserNotificationPreferences(ctx context.Context, userID uuid.UUID) ([]database.NotificationPreference, error) {
2104+
if err := q.authorizeContext(ctx, policy.ActionRead, rbac.ResourceNotificationPreference.WithOwner(userID.String())); err != nil {
2105+
return nil, err
2106+
}
2107+
return q.db.GetUserNotificationPreferences(ctx, userID)
2108+
}
2109+
20882110
func (q *querier) GetUserWorkspaceBuildParameters(ctx context.Context, params database.GetUserWorkspaceBuildParametersParams) ([]database.GetUserWorkspaceBuildParametersRow, error) {
20892111
u, err := q.db.GetUserByID(ctx, params.OwnerID)
20902112
if err != nil {
@@ -3011,6 +3033,14 @@ func (q *querier) UpdateMemberRoles(ctx context.Context, arg database.UpdateMemb
30113033
return q.db.UpdateMemberRoles(ctx, arg)
30123034
}
30133035

3036+
func (q *querier) UpdateNotificationTemplateMethodById(ctx context.Context, arg database.UpdateNotificationTemplateMethodByIdParams) (database.NotificationTemplate, error) {
3037+
// TODO: how to restrict this to admins?
3038+
if err := q.authorizeContext(ctx, policy.ActionUpdate, rbac.ResourceNotificationTemplate); err != nil {
3039+
return database.NotificationTemplate{}, err
3040+
}
3041+
return q.db.UpdateNotificationTemplateMethodById(ctx, arg)
3042+
}
3043+
30143044
func (q *querier) UpdateOAuth2ProviderAppByID(ctx context.Context, arg database.UpdateOAuth2ProviderAppByIDParams) (database.OAuth2ProviderApp, error) {
30153045
if err := q.authorizeContext(ctx, policy.ActionUpdate, rbac.ResourceOauth2App); err != nil {
30163046
return database.OAuth2ProviderApp{}, err
@@ -3309,6 +3339,13 @@ func (q *querier) UpdateUserLoginType(ctx context.Context, arg database.UpdateUs
33093339
return q.db.UpdateUserLoginType(ctx, arg)
33103340
}
33113341

3342+
func (q *querier) UpdateUserNotificationPreferences(ctx context.Context, arg database.UpdateUserNotificationPreferencesParams) (int64, error) {
3343+
if err := q.authorizeContext(ctx, policy.ActionUpdate, rbac.ResourceNotificationPreference.WithOwner(arg.UserID.String())); err != nil {
3344+
return -1, err
3345+
}
3346+
return q.db.UpdateUserNotificationPreferences(ctx, arg)
3347+
}
3348+
33123349
func (q *querier) UpdateUserProfile(ctx context.Context, arg database.UpdateUserProfileParams) (database.User, error) {
33133350
u, err := q.db.GetUserByID(ctx, arg.ID)
33143351
if err != nil {

coderd/database/dbmem/dbmem.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ func New() database.Store {
6565
files: make([]database.File, 0),
6666
gitSSHKey: make([]database.GitSSHKey, 0),
6767
notificationMessages: make([]database.NotificationMessage, 0),
68+
notificationPreferences: make([]database.NotificationPreference, 0),
6869
parameterSchemas: make([]database.ParameterSchema, 0),
6970
provisionerDaemons: make([]database.ProvisionerDaemon, 0),
7071
workspaceAgents: make([]database.WorkspaceAgent, 0),
@@ -160,6 +161,7 @@ type data struct {
160161
jfrogXRayScans []database.JfrogXrayScan
161162
licenses []database.License
162163
notificationMessages []database.NotificationMessage
164+
notificationPreferences []database.NotificationPreference
163165
oauth2ProviderApps []database.OAuth2ProviderApp
164166
oauth2ProviderAppSecrets []database.OAuth2ProviderAppSecret
165167
oauth2ProviderAppCodes []database.OAuth2ProviderAppCode
@@ -2708,6 +2710,14 @@ func (q *FakeQuerier) GetNotificationMessagesByStatus(_ context.Context, arg dat
27082710
return out, nil
27092711
}
27102712

2713+
func (*FakeQuerier) GetNotificationTemplateById(_ context.Context, _ uuid.UUID) (database.NotificationTemplate, error) {
2714+
return database.NotificationTemplate{}, ErrUnimplemented
2715+
}
2716+
2717+
func (q *FakeQuerier) GetNotificationTemplatesByKind(ctx context.Context, kind database.NotificationTemplateKind) ([]database.NotificationTemplate, error) {
2718+
return nil, ErrUnimplemented
2719+
}
2720+
27112721
func (q *FakeQuerier) GetNotificationsSettings(_ context.Context) (string, error) {
27122722
q.mutex.RLock()
27132723
defer q.mutex.RUnlock()
@@ -4853,6 +4863,22 @@ func (q *FakeQuerier) GetUserLinksByUserID(_ context.Context, userID uuid.UUID)
48534863
return uls, nil
48544864
}
48554865

4866+
func (q *FakeQuerier) GetUserNotificationPreferences(_ context.Context, userID uuid.UUID) ([]database.NotificationPreference, error) {
4867+
q.mutex.RLock()
4868+
defer q.mutex.RUnlock()
4869+
4870+
out := make([]database.NotificationPreference, 0, len(q.notificationPreferences))
4871+
for _, np := range q.notificationPreferences {
4872+
if np.UserID != userID {
4873+
continue
4874+
}
4875+
4876+
out = append(out, np)
4877+
}
4878+
4879+
return out, nil
4880+
}
4881+
48564882
func (q *FakeQuerier) GetUserWorkspaceBuildParameters(_ context.Context, params database.GetUserWorkspaceBuildParametersParams) ([]database.GetUserWorkspaceBuildParametersRow, error) {
48574883
q.mutex.RLock()
48584884
defer q.mutex.RUnlock()
@@ -7520,6 +7546,10 @@ func (q *FakeQuerier) UpdateMemberRoles(_ context.Context, arg database.UpdateMe
75207546
return database.OrganizationMember{}, sql.ErrNoRows
75217547
}
75227548

7549+
func (*FakeQuerier) UpdateNotificationTemplateMethodById(_ context.Context, _ database.UpdateNotificationTemplateMethodByIdParams) (database.NotificationTemplate, error) {
7550+
return database.NotificationTemplate{}, ErrUnimplemented
7551+
}
7552+
75237553
func (q *FakeQuerier) UpdateOAuth2ProviderAppByID(_ context.Context, arg database.UpdateOAuth2ProviderAppByIDParams) (database.OAuth2ProviderApp, error) {
75247554
err := validateDatabaseType(arg)
75257555
if err != nil {
@@ -8094,6 +8124,57 @@ func (q *FakeQuerier) UpdateUserLoginType(_ context.Context, arg database.Update
80948124
return database.User{}, sql.ErrNoRows
80958125
}
80968126

8127+
func (q *FakeQuerier) UpdateUserNotificationPreferences(_ context.Context, arg database.UpdateUserNotificationPreferencesParams) (int64, error) {
8128+
err := validateDatabaseType(arg)
8129+
if err != nil {
8130+
return -1, err
8131+
}
8132+
8133+
q.mutex.Lock()
8134+
defer q.mutex.Unlock()
8135+
8136+
var upserted int64
8137+
for i := range arg.NotificationTemplateIds {
8138+
var (
8139+
found bool
8140+
templateID = arg.NotificationTemplateIds[i]
8141+
disabled = arg.Disableds[i]
8142+
)
8143+
8144+
for j, np := range q.notificationPreferences {
8145+
if np.UserID != arg.UserID {
8146+
continue
8147+
}
8148+
8149+
if np.NotificationTemplateID != templateID {
8150+
continue
8151+
}
8152+
8153+
np.Disabled = disabled
8154+
np.UpdatedAt = time.Now()
8155+
q.notificationPreferences[j] = np
8156+
8157+
upserted++
8158+
found = true
8159+
break
8160+
}
8161+
8162+
if !found {
8163+
np := database.NotificationPreference{
8164+
Disabled: disabled,
8165+
UserID: arg.UserID,
8166+
NotificationTemplateID: templateID,
8167+
CreatedAt: time.Now(),
8168+
UpdatedAt: time.Now(),
8169+
}
8170+
q.notificationPreferences = append(q.notificationPreferences, np)
8171+
upserted++
8172+
}
8173+
}
8174+
8175+
return upserted, nil
8176+
}
8177+
80978178
func (q *FakeQuerier) UpdateUserProfile(_ context.Context, arg database.UpdateUserProfileParams) (database.User, error) {
80988179
if err := validateDatabaseType(arg); err != nil {
80998180
return database.User{}, err

coderd/database/dbmetrics/dbmetrics.go

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

0 commit comments

Comments
 (0)