@@ -410,9 +410,26 @@ impl ListingService {
410
410
pub struct AdminActionsService {
411
411
authorization_service : Arc < authorization:: Service > ,
412
412
user_authentication_repository : Arc < DbUserAuthenticationRepository > ,
413
+ user_profile_repository : Arc < DbUserProfileRepository > ,
414
+ mailer : Arc < mailer:: Service > ,
413
415
}
414
416
415
417
impl AdminActionsService {
418
+ #[ must_use]
419
+ pub fn new (
420
+ authorization_service : Arc < authorization:: Service > ,
421
+ user_authentication_repository : Arc < DbUserAuthenticationRepository > ,
422
+ user_profile_repository : Arc < DbUserProfileRepository > ,
423
+ mailer : Arc < mailer:: Service > ,
424
+ ) -> Self {
425
+ Self {
426
+ authorization_service,
427
+ user_authentication_repository,
428
+ user_profile_repository,
429
+ mailer,
430
+ }
431
+ }
432
+
416
433
/// Resets the password of the selected user.
417
434
///
418
435
/// # Errors
@@ -426,25 +443,43 @@ impl AdminActionsService {
426
443
/// * An error if unable to successfully hash the password.
427
444
/// * An error if unable to change the password in the database.
428
445
/// * An error if it is not possible to authorize the action
429
- pub async fn reset_user_password ( & self , maybe_user_id : Option < UserId > , user_info : UserProfile ) -> Result < ( ) , ServiceError > {
446
+ pub async fn reset_user_password (
447
+ & self ,
448
+ maybe_admin_user_id : Option < UserId > ,
449
+ reset_password_user_id : UserId ,
450
+ ) -> Result < ( ) , ServiceError > {
430
451
self . authorization_service
431
452
. authorize ( ACTION :: ResetUserPassword , maybe_user_id)
432
453
. await ?;
433
454
434
- info ! ( "Resetting user password for user ID: {}" , user_info. username) ;
455
+ if let Some ( email) = Some ( & user_info. email ) {
456
+ if user_info. email_verified {
457
+ info ! ( "Resetting user password for user ID: {}" , user_info. username) ;
435
458
436
- let new_password = generate_random_password ( ) ;
459
+ let new_password = generate_random_password ( ) ;
437
460
438
- let password_hash = hash_password ( & new_password) ?;
461
+ let password_hash = hash_password ( & new_password) ?;
439
462
440
- self . user_authentication_repository
441
- . change_password ( user_info. user_id , & password_hash)
442
- . await ?;
463
+ self . user_authentication_repository
464
+ . change_password ( user_info. user_id , & password_hash)
465
+ . await ?;
443
466
444
- Ok ( ( ) )
467
+ let mail_res = self
468
+ . mailer
469
+ . send_reset_password_mail ( email, & user_info. username , & new_password)
470
+ . await ;
471
+
472
+ if mail_res. is_err ( ) {
473
+ return Err ( ServiceError :: FailedToSendResetPassword ) ;
474
+ }
475
+
476
+ ( )
477
+ }
478
+ return Err ( ServiceError :: VerifiedEmailMissing ) ;
479
+ }
480
+ Err ( ServiceError :: EmailMissing )
445
481
}
446
482
}
447
-
448
483
#[ cfg_attr( test, automock) ]
449
484
#[ async_trait]
450
485
pub trait Repository : Sync + Send {
0 commit comments