-
Notifications
You must be signed in to change notification settings - Fork 983
feat: notify when a user account is deleted #14113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
42f4bd7
9c47637
4e787ec
15fec2b
37bbb41
96fa649
ac5a3e6
085c5bf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
} | ||
|
||
|
@@ -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, | ||
|
@@ -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{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we also need to include org user admins here? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
|
Uh oh!
There was an error while loading. Please reload this page.