@@ -560,6 +560,24 @@ func (api *API) deleteUser(rw http.ResponseWriter, r *http.Request) {
560
560
}
561
561
user .Deleted = true
562
562
aReq .New = user
563
+
564
+ userAdmins , err := findUserAdmins (ctx , api .Database )
565
+ if err != nil {
566
+ api .Logger .Warn (ctx , "unable to fetch user admins" , slog .Error (err ))
567
+ rw .WriteHeader (http .StatusNoContent )
568
+ }
569
+
570
+ for _ , u := range userAdmins {
571
+ if _ , err := api .NotificationsEnqueuer .Enqueue (ctx , u .ID , notifications .TemplateUserAccountCreated ,
572
+ map [string ]string {
573
+ "deleted_account_name" : user .Username ,
574
+ }, "api-users-delete" ,
575
+ user .ID ,
576
+ ); err != nil {
577
+ api .Logger .Warn (ctx , "unable to notify about deleted user" , slog .F ("deleted_user" , user .Username ), slog .Error (err ))
578
+ }
579
+ }
580
+
563
581
rw .WriteHeader (http .StatusNoContent )
564
582
}
565
583
@@ -1280,23 +1298,12 @@ func (api *API) CreateUser(ctx context.Context, store database.Store, req Create
1280
1298
return user , req .OrganizationID , err
1281
1299
}
1282
1300
1283
- // Notify all users with user admin permission including owners
1284
- // Notice: we can't scrape the user information in parallel as pq
1285
- // fails with: unexpected describe rows response: 'D'
1286
- owners , err := store .GetUsers (ctx , database.GetUsersParams {
1287
- RbacRole : []string {codersdk .RoleOwner },
1288
- })
1301
+ userAdmins , err := findUserAdmins (ctx , store )
1289
1302
if err != nil {
1290
- return user , req .OrganizationID , xerrors .Errorf ("get owners: %w" , err )
1291
- }
1292
- userAdmins , err := store .GetUsers (ctx , database.GetUsersParams {
1293
- RbacRole : []string {codersdk .RoleUserAdmin },
1294
- })
1295
- if err != nil {
1296
- return user , req .OrganizationID , xerrors .Errorf ("get user admins: %w" , err )
1303
+ return user , req .OrganizationID , xerrors .Errorf ("find user admins: %w" , err )
1297
1304
}
1298
1305
1299
- for _ , u := range append ( owners , userAdmins ... ) {
1306
+ for _ , u := range userAdmins {
1300
1307
if _ , err := api .NotificationsEnqueuer .Enqueue (ctx , u .ID , notifications .TemplateUserAccountCreated ,
1301
1308
map [string ]string {
1302
1309
"created_account_name" : user .Username ,
@@ -1309,6 +1316,25 @@ func (api *API) CreateUser(ctx context.Context, store database.Store, req Create
1309
1316
return user , req .OrganizationID , err
1310
1317
}
1311
1318
1319
+ // findUserAdmins fetches all users with user admin permission including owners.
1320
+ func findUserAdmins (ctx context.Context , store database.Store ) ([]database.GetUsersRow , error ) {
1321
+ // Notice: we can't scrape the user information in parallel as pq
1322
+ // fails with: unexpected describe rows response: 'D'
1323
+ owners , err := store .GetUsers (ctx , database.GetUsersParams {
1324
+ RbacRole : []string {codersdk .RoleOwner },
1325
+ })
1326
+ if err != nil {
1327
+ return nil , xerrors .Errorf ("get owners: %w" , err )
1328
+ }
1329
+ userAdmins , err := store .GetUsers (ctx , database.GetUsersParams {
1330
+ RbacRole : []string {codersdk .RoleUserAdmin },
1331
+ })
1332
+ if err != nil {
1333
+ return nil , xerrors .Errorf ("get user admins: %w" , err )
1334
+ }
1335
+ return append (owners , userAdmins ... ), nil
1336
+ }
1337
+
1312
1338
func convertUsers (users []database.User , organizationIDsByUserID map [uuid.UUID ][]uuid.UUID ) []codersdk.User {
1313
1339
converted := make ([]codersdk.User , 0 , len (users ))
1314
1340
for _ , u := range users {
0 commit comments