Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 58428aa

Browse files
authored
fix: allow all users to read system notification templates (#14181)
1 parent 70a694e commit 58428aa

File tree

3 files changed

+48
-7
lines changed

3 files changed

+48
-7
lines changed

coderd/database/dbauthz/dbauthz.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -1489,13 +1489,13 @@ func (q *querier) GetNotificationTemplateByID(ctx context.Context, id uuid.UUID)
14891489
}
14901490

14911491
func (q *querier) GetNotificationTemplatesByKind(ctx context.Context, kind database.NotificationTemplateKind) ([]database.NotificationTemplate, error) {
1492-
// TODO: restrict 'system' kind to admins only?
1493-
// All notification templates share the same rbac.Object, so there is no need
1494-
// to authorize them individually. If this passes, all notification templates can be read.
1495-
if err := q.authorizeContext(ctx, policy.ActionRead, rbac.ResourceNotificationTemplate); err != nil {
1496-
return nil, err
1492+
// Anyone can read the system notification templates.
1493+
if kind == database.NotificationTemplateKindSystem {
1494+
return q.db.GetNotificationTemplatesByKind(ctx, kind)
14971495
}
1498-
return q.db.GetNotificationTemplatesByKind(ctx, kind)
1496+
1497+
// TODO(dannyk): handle template ownership when we support user-default notification templates.
1498+
return nil, sql.ErrNoRows
14991499
}
15001500

15011501
func (q *querier) GetNotificationsSettings(ctx context.Context) (string, error) {

coderd/database/dbauthz/dbauthz_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -2610,8 +2610,10 @@ func (s *MethodTestSuite) TestNotifications() {
26102610
}))
26112611
s.Run("GetNotificationTemplatesByKind", s.Subtest(func(db database.Store, check *expects) {
26122612
check.Args(database.NotificationTemplateKindSystem).
2613-
Asserts(rbac.ResourceNotificationTemplate, policy.ActionRead).
2613+
Asserts().
26142614
Errors(dbmem.ErrUnimplemented)
2615+
2616+
// TODO(dannyk): add support for other database.NotificationTemplateKind types once implemented.
26152617
}))
26162618
s.Run("UpdateNotificationTemplateMethodByID", s.Subtest(func(db database.Store, check *expects) {
26172619
check.Args(database.UpdateNotificationTemplateMethodByIDParams{

coderd/notifications/notifications_test.go

+39
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@ import (
2424

2525
"github.com/coder/serpent"
2626

27+
"github.com/coder/coder/v2/coderd/coderdtest"
2728
"github.com/coder/coder/v2/coderd/database"
2829
"github.com/coder/coder/v2/coderd/database/dbgen"
2930
"github.com/coder/coder/v2/coderd/database/dbtestutil"
3031
"github.com/coder/coder/v2/coderd/notifications"
3132
"github.com/coder/coder/v2/coderd/notifications/dispatch"
3233
"github.com/coder/coder/v2/coderd/notifications/render"
3334
"github.com/coder/coder/v2/coderd/notifications/types"
35+
"github.com/coder/coder/v2/coderd/rbac"
3436
"github.com/coder/coder/v2/coderd/util/syncmap"
3537
"github.com/coder/coder/v2/codersdk"
3638
"github.com/coder/coder/v2/testutil"
@@ -893,6 +895,43 @@ func TestCustomNotificationMethod(t *testing.T) {
893895
}, testutil.WaitLong, testutil.IntervalFast)
894896
}
895897

898+
func TestNotificationsTemplates(t *testing.T) {
899+
t.Parallel()
900+
901+
// SETUP
902+
if !dbtestutil.WillUsePostgres() {
903+
// Notification system templates are only served from the database and not dbmem at this time.
904+
t.Skip("This test requires postgres; it relies on business-logic only implemented in the database")
905+
}
906+
907+
ctx := testutil.Context(t, testutil.WaitLong)
908+
api := coderdtest.New(t, createOpts(t))
909+
910+
// GIVEN: the first user (owner) and a regular member
911+
firstUser := coderdtest.CreateFirstUser(t, api)
912+
memberClient, _ := coderdtest.CreateAnotherUser(t, api, firstUser.OrganizationID, rbac.RoleMember())
913+
914+
// WHEN: requesting system notification templates as owner should work
915+
templates, err := api.GetSystemNotificationTemplates(ctx)
916+
require.NoError(t, err)
917+
require.True(t, len(templates) > 1)
918+
919+
// WHEN: requesting system notification templates as member should work
920+
templates, err = memberClient.GetSystemNotificationTemplates(ctx)
921+
require.NoError(t, err)
922+
require.True(t, len(templates) > 1)
923+
}
924+
925+
func createOpts(t *testing.T) *coderdtest.Options {
926+
t.Helper()
927+
928+
dt := coderdtest.DeploymentValues(t)
929+
dt.Experiments = []string{string(codersdk.ExperimentNotifications)}
930+
return &coderdtest.Options{
931+
DeploymentValues: dt,
932+
}
933+
}
934+
896935
type fakeHandler struct {
897936
mu sync.RWMutex
898937
succeeded, failed []string

0 commit comments

Comments
 (0)