Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
logic
  • Loading branch information
mtojek committed Aug 2, 2024
commit 15fec2bfc1c0fb301a42cc9e5de5925da200ab8d
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
INSERT INTO notification_templates (id, name, title_template, body_template, "group", actions)
VALUES ('f44d9314-ad03-4bc8-95d0-5cad491da6b6', 'User account deleted', E'User account "{{.Labels.deleted_account_name}}" deleted',
E'Hi {{.UserName}},\n\nUser account **{{.Labels.created_account_name}}** has been deleted.',
E'Hi {{.UserName}},\n\nUser account **{{.Labels.deleted_account_name}}** has been deleted.',
'User Events', '[
{
"label": "View accounts",
Expand Down
1 change: 1 addition & 0 deletions coderd/notifications/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ var (
// Account-related events.
var (
TemplateUserAccountCreated = uuid.MustParse("4e19c0ac-94e1-4532-9515-d1801aa283b2")
TemplateUserAccountDeleted = uuid.MustParse("f44d9314-ad03-4bc8-95d0-5cad491da6b6")
)
54 changes: 40 additions & 14 deletions coderd/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,24 @@ func (api *API) deleteUser(rw http.ResponseWriter, r *http.Request) {
}
user.Deleted = true
aReq.New = user

userAdmins, err := findUserAdmins(ctx, api.Database)
if err != nil {
api.Logger.Warn(ctx, "unable to fetch user admins", slog.Error(err))
rw.WriteHeader(http.StatusNoContent)
}

for _, u := range userAdmins {
if _, err := api.NotificationsEnqueuer.Enqueue(ctx, u.ID, notifications.TemplateUserAccountCreated,
map[string]string{
"deleted_account_name": user.Username,
}, "api-users-delete",
user.ID,
); err != nil {
api.Logger.Warn(ctx, "unable to notify about deleted user", slog.F("deleted_user", user.Username), slog.Error(err))
}
}

rw.WriteHeader(http.StatusNoContent)
}

Expand Down Expand Up @@ -1280,23 +1298,12 @@ func (api *API) CreateUser(ctx context.Context, store database.Store, req Create
return user, req.OrganizationID, err
}

// Notify all users with user admin permission including owners
// Notice: we can't scrape the user information in parallel as pq
// fails with: unexpected describe rows response: 'D'
owners, err := store.GetUsers(ctx, database.GetUsersParams{
RbacRole: []string{codersdk.RoleOwner},
})
userAdmins, err := findUserAdmins(ctx, store)
if err != nil {
return user, req.OrganizationID, xerrors.Errorf("get owners: %w", err)
}
userAdmins, err := store.GetUsers(ctx, database.GetUsersParams{
RbacRole: []string{codersdk.RoleUserAdmin},
})
if err != nil {
return user, req.OrganizationID, xerrors.Errorf("get user admins: %w", err)
return user, req.OrganizationID, xerrors.Errorf("find user admins: %w", err)
}

for _, u := range append(owners, userAdmins...) {
for _, u := range userAdmins {
if _, err := api.NotificationsEnqueuer.Enqueue(ctx, u.ID, notifications.TemplateUserAccountCreated,
map[string]string{
"created_account_name": user.Username,
Expand All @@ -1309,6 +1316,25 @@ func (api *API) CreateUser(ctx context.Context, store database.Store, req Create
return user, req.OrganizationID, err
}

// findUserAdmins fetches all users with user admin permission including owners.
func findUserAdmins(ctx context.Context, store database.Store) ([]database.GetUsersRow, error) {
// Notice: we can't scrape the user information in parallel as pq
// fails with: unexpected describe rows response: 'D'
owners, err := store.GetUsers(ctx, database.GetUsersParams{
RbacRole: []string{codersdk.RoleOwner},
})
if err != nil {
return nil, xerrors.Errorf("get owners: %w", err)
}
userAdmins, err := store.GetUsers(ctx, database.GetUsersParams{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we also need to include org user admins here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably, I will leave it as a follow-up once the org part is final. Most likely we need to adjust create account logic too.

RbacRole: []string{codersdk.RoleUserAdmin},
})
if err != nil {
return nil, xerrors.Errorf("get user admins: %w", err)
}
return append(owners, userAdmins...), nil
}

func convertUsers(users []database.User, organizationIDsByUserID map[uuid.UUID][]uuid.UUID) []codersdk.User {
converted := make([]codersdk.User, 0, len(users))
for _, u := range users {
Expand Down