@@ -194,7 +194,8 @@ func (api *API) postFirstUser(rw http.ResponseWriter, r *http.Request) {
194
194
Password : createUser .Password ,
195
195
OrganizationIDs : []uuid.UUID {defaultOrg .ID },
196
196
},
197
- LoginType : database .LoginTypePassword ,
197
+ LoginType : database .LoginTypePassword ,
198
+ accountCreatorName : "coder" ,
198
199
})
199
200
if err != nil {
200
201
httpapi .Write (ctx , rw , http .StatusInternalServerError , codersdk.Response {
@@ -479,10 +480,22 @@ func (api *API) postUser(rw http.ResponseWriter, r *http.Request) {
479
480
return
480
481
}
481
482
483
+ apiKey := httpmw .APIKey (r )
484
+
485
+ accountCreator , err := api .Database .GetUserByID (ctx , apiKey .UserID )
486
+ if err != nil {
487
+ httpapi .Write (ctx , rw , http .StatusInternalServerError , codersdk.Response {
488
+ Message : "Unable to determine the details of the actor creating the account." ,
489
+ })
490
+ return
491
+ }
492
+
482
493
user , err := api .CreateUser (ctx , api .Database , CreateUserRequest {
483
494
CreateUserRequestWithOrgs : req ,
484
495
LoginType : loginType ,
496
+ accountCreatorName : accountCreator .Name ,
485
497
})
498
+
486
499
if dbauthz .IsNotAuthorizedError (err ) {
487
500
httpapi .Write (ctx , rw , http .StatusForbidden , codersdk.Response {
488
501
Message : "You are not authorized to create users." ,
@@ -576,11 +589,24 @@ func (api *API) deleteUser(rw http.ResponseWriter, r *http.Request) {
576
589
return
577
590
}
578
591
592
+ apiKey := httpmw .APIKey (r )
593
+
594
+ accountDeleter , err := api .Database .GetUserByID (ctx , apiKey .UserID )
595
+ if err != nil {
596
+ httpapi .Write (ctx , rw , http .StatusInternalServerError , codersdk.Response {
597
+ Message : "Unable to determine the details of the actor deleting the account." ,
598
+ })
599
+ return
600
+ }
601
+
579
602
for _ , u := range userAdmins {
580
603
if _ , err := api .NotificationsEnqueuer .Enqueue (ctx , u .ID , notifications .TemplateUserAccountDeleted ,
581
604
map [string ]string {
582
- "deleted_account_name" : user .Username ,
583
- }, "api-users-delete" ,
605
+ "deleted_account_name" : user .Username ,
606
+ "deleted_account_user_name" : user .Name ,
607
+ "account_deleter_user_name" : accountDeleter .Name ,
608
+ },
609
+ "api-users-delete" ,
584
610
user .ID ,
585
611
); err != nil {
586
612
api .Logger .Warn (ctx , "unable to notify about deleted user" , slog .F ("deleted_user" , user .Username ), slog .Error (err ))
@@ -844,6 +870,14 @@ func (api *API) putUserStatus(status database.UserStatus) func(rw http.ResponseW
844
870
}
845
871
}
846
872
873
+ actingUser , err := api .Database .GetUserByID (ctx , apiKey .UserID )
874
+ if err != nil {
875
+ httpapi .Write (ctx , rw , http .StatusInternalServerError , codersdk.Response {
876
+ Message : "Unable to determine the details of the actor creating the account." ,
877
+ })
878
+ return
879
+ }
880
+
847
881
targetUser , err := api .Database .UpdateUserStatus (ctx , database.UpdateUserStatusParams {
848
882
ID : user .ID ,
849
883
Status : status ,
@@ -858,7 +892,7 @@ func (api *API) putUserStatus(status database.UserStatus) func(rw http.ResponseW
858
892
}
859
893
aReq .New = targetUser
860
894
861
- err = api .notifyUserStatusChanged (ctx , user , status )
895
+ err = api .notifyUserStatusChanged (ctx , actingUser . Name , user , status )
862
896
if err != nil {
863
897
api .Logger .Warn (ctx , "unable to notify about changed user's status" , slog .F ("affected_user" , user .Username ), slog .Error (err ))
864
898
}
@@ -871,24 +905,33 @@ func (api *API) putUserStatus(status database.UserStatus) func(rw http.ResponseW
871
905
})
872
906
return
873
907
}
908
+
874
909
httpapi .Write (ctx , rw , http .StatusOK , db2sdk .User (targetUser , organizations ))
875
910
}
876
911
}
877
912
878
- func (api * API ) notifyUserStatusChanged (ctx context.Context , user database.User , status database.UserStatus ) error {
879
- var key string
913
+ func (api * API ) notifyUserStatusChanged (ctx context.Context , actingUserName string , targetUser database.User , status database.UserStatus ) error {
914
+ var labels map [ string ] string
880
915
var adminTemplateID , personalTemplateID uuid.UUID
881
916
switch status {
882
917
case database .UserStatusSuspended :
883
- key = "suspended_account_name"
918
+ labels = map [string ]string {
919
+ "suspended_account_name" : targetUser .Username ,
920
+ "suspended_account_user_name" : targetUser .Name ,
921
+ "account_suspender_user_name" : actingUserName ,
922
+ }
884
923
adminTemplateID = notifications .TemplateUserAccountSuspended
885
924
personalTemplateID = notifications .TemplateYourAccountSuspended
886
925
case database .UserStatusActive :
887
- key = "activated_account_name"
926
+ labels = map [string ]string {
927
+ "activated_account_name" : targetUser .Username ,
928
+ "activated_account_user_name" : targetUser .Name ,
929
+ "account_activator_user_name" : actingUserName ,
930
+ }
888
931
adminTemplateID = notifications .TemplateUserAccountActivated
889
932
personalTemplateID = notifications .TemplateYourAccountActivated
890
933
default :
891
- api .Logger .Error (ctx , "user status is not supported" , slog .F ("username" , user .Username ), slog .F ("user_status" , string (status )))
934
+ api .Logger .Error (ctx , "user status is not supported" , slog .F ("username" , targetUser .Username ), slog .F ("user_status" , string (status )))
892
935
return xerrors .Errorf ("unable to notify admins as the user's status is unsupported" )
893
936
}
894
937
@@ -900,21 +943,17 @@ func (api *API) notifyUserStatusChanged(ctx context.Context, user database.User,
900
943
// Send notifications to user admins and affected user
901
944
for _ , u := range userAdmins {
902
945
if _ , err := api .NotificationsEnqueuer .Enqueue (ctx , u .ID , adminTemplateID ,
903
- map [string ]string {
904
- key : user .Username ,
905
- }, "api-put-user-status" ,
906
- user .ID ,
946
+ labels , "api-put-user-status" ,
947
+ targetUser .ID ,
907
948
); err != nil {
908
- api .Logger .Warn (ctx , "unable to notify about changed user's status" , slog .F ("affected_user" , user .Username ), slog .Error (err ))
949
+ api .Logger .Warn (ctx , "unable to notify about changed user's status" , slog .F ("affected_user" , targetUser .Username ), slog .Error (err ))
909
950
}
910
951
}
911
- if _ , err := api .NotificationsEnqueuer .Enqueue (ctx , user .ID , personalTemplateID ,
912
- map [string ]string {
913
- key : user .Username ,
914
- }, "api-put-user-status" ,
915
- user .ID ,
952
+ if _ , err := api .NotificationsEnqueuer .Enqueue (ctx , targetUser .ID , personalTemplateID ,
953
+ labels , "api-put-user-status" ,
954
+ targetUser .ID ,
916
955
); err != nil {
917
- api .Logger .Warn (ctx , "unable to notify user about status change of their account" , slog .F ("affected_user" , user .Username ), slog .Error (err ))
956
+ api .Logger .Warn (ctx , "unable to notify user about status change of their account" , slog .F ("affected_user" , targetUser .Username ), slog .Error (err ))
918
957
}
919
958
return nil
920
959
}
@@ -1280,8 +1319,9 @@ func (api *API) organizationByUserAndName(rw http.ResponseWriter, r *http.Reques
1280
1319
1281
1320
type CreateUserRequest struct {
1282
1321
codersdk.CreateUserRequestWithOrgs
1283
- LoginType database.LoginType
1284
- SkipNotifications bool
1322
+ LoginType database.LoginType
1323
+ SkipNotifications bool
1324
+ accountCreatorName string
1285
1325
}
1286
1326
1287
1327
func (api * API ) CreateUser (ctx context.Context , store database.Store , req CreateUserRequest ) (database.User , error ) {
@@ -1365,13 +1405,16 @@ func (api *API) CreateUser(ctx context.Context, store database.Store, req Create
1365
1405
for _ , u := range userAdmins {
1366
1406
if _ , err := api .NotificationsEnqueuer .Enqueue (ctx , u .ID , notifications .TemplateUserAccountCreated ,
1367
1407
map [string ]string {
1368
- "created_account_name" : user .Username ,
1408
+ "created_account_name" : user .Username ,
1409
+ "created_account_user_name" : user .Name ,
1410
+ "account_creator" : req .accountCreatorName ,
1369
1411
}, "api-users-create" ,
1370
1412
user .ID ,
1371
1413
); err != nil {
1372
1414
api .Logger .Warn (ctx , "unable to notify about created user" , slog .F ("created_user" , user .Username ), slog .Error (err ))
1373
1415
}
1374
1416
}
1417
+
1375
1418
return user , err
1376
1419
}
1377
1420
0 commit comments