Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
mario-nt committed Jan 30, 2025
1 parent 45aba38 commit 03f97ec
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 9 deletions.
53 changes: 44 additions & 9 deletions src/services/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,9 +410,26 @@ impl ListingService {
pub struct AdminActionsService {
authorization_service: Arc<authorization::Service>,
user_authentication_repository: Arc<DbUserAuthenticationRepository>,
user_profile_repository: Arc<DbUserProfileRepository>,
mailer: Arc<mailer::Service>,
}

impl AdminActionsService {
#[must_use]
pub fn new(
authorization_service: Arc<authorization::Service>,
user_authentication_repository: Arc<DbUserAuthenticationRepository>,
user_profile_repository: Arc<DbUserProfileRepository>,
mailer: Arc<mailer::Service>,
) -> Self {
Self {
authorization_service,
user_authentication_repository,
user_profile_repository,
mailer,
}
}

/// Resets the password of the selected user.
///
/// # Errors
Expand All @@ -426,25 +443,43 @@ impl AdminActionsService {
/// * An error if unable to successfully hash the password.
/// * An error if unable to change the password in the database.
/// * An error if it is not possible to authorize the action
pub async fn reset_user_password(&self, maybe_user_id: Option<UserId>, user_info: UserProfile) -> Result<(), ServiceError> {
pub async fn reset_user_password(
&self,
maybe_admin_user_id: Option<UserId>,
reset_password_user_id: UserId,
) -> Result<(), ServiceError> {
self.authorization_service
.authorize(ACTION::ResetUserPassword, maybe_user_id)
.await?;

info!("Resetting user password for user ID: {}", user_info.username);
if let Some(email) = Some(&user_info.email) {
if user_info.email_verified {
info!("Resetting user password for user ID: {}", user_info.username);

let new_password = generate_random_password();
let new_password = generate_random_password();

let password_hash = hash_password(&new_password)?;
let password_hash = hash_password(&new_password)?;

self.user_authentication_repository
.change_password(user_info.user_id, &password_hash)
.await?;
self.user_authentication_repository
.change_password(user_info.user_id, &password_hash)
.await?;

Ok(())
let mail_res = self
.mailer
.send_reset_password_mail(email, &user_info.username, &new_password)
.await;

if mail_res.is_err() {
return Err(ServiceError::FailedToSendResetPassword);
}

()
}
return Err(ServiceError::VerifiedEmailMissing);
}
Err(ServiceError::EmailMissing)
}
}

#[cfg_attr(test, automock)]
#[async_trait]
pub trait Repository: Sync + Send {
Expand Down
27 changes: 27 additions & 0 deletions src/web/api/server/v1/contexts/user/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,33 @@ pub async fn change_password_handler(
}
}

/// It changes the user's password.
///
/// # Errors
///
/// It returns an error if:
///
/// - The user account is not found.
#[allow(clippy::unused_async)]
#[allow(clippy::missing_panics_doc)]
pub async fn reset_password_handler(
State(app_data): State<Arc<AppData>>,
ExtractOptionalLoggedInUser(maybe_user_id): ExtractOptionalLoggedInUser,
extract::Json(change_password_form): extract::Json<ChangePasswordForm>,
) -> Response {
match app_data
.profile_service
.change_password(maybe_user_id, &change_password_form)
.await
{
Ok(()) => Json(OkResponseData {
data: format!("Password changed for user with ID: {}", maybe_user_id.unwrap()),
})
.into_response(),
Err(error) => error.into_response(),
}
}

/// It bans a user from the index.
///
/// # Errors
Expand Down

0 comments on commit 03f97ec

Please sign in to comment.