Skip to content

Commit

Permalink
Merge pull request #90 from sebadob/send-user-emails-after-admin-emai…
Browse files Browse the repository at this point in the history
…l-change

Send user emails after admin email change
  • Loading branch information
sebadob committed Oct 24, 2023
2 parents ace4daf + b2d9221 commit 8e97e31
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
9 changes: 9 additions & 0 deletions rauthy-models/src/email.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ pub struct EMailConfirmChangeHtml<'a> {
pub header: &'a str,
pub msg: &'a str,
pub email_changed_to: &'a str,
pub changed_by_admin: &'a str,
}

#[derive(Default, Template)]
Expand All @@ -97,6 +98,7 @@ pub struct EMailConfirmChangeTxt<'a> {
pub header: &'a str,
pub msg: &'a str,
pub email_changed_to: &'a str,
pub changed_by_admin: &'a str,
}

#[derive(Default, Template)]
Expand Down Expand Up @@ -254,20 +256,27 @@ pub async fn send_email_confirm_change(
user: &User,
email_addr: &str,
email_changed_to: &str,
was_admin_action: bool,
) {
let i18n = I18nEmailConfirmChange::build(&user.language);
let text = EMailConfirmChangeTxt {
email_sub_prefix: &EMAIL_SUB_PREFIX,
header: i18n.subject,
msg: i18n.msg,
email_changed_to,
changed_by_admin: if was_admin_action {
i18n.msg_from_admin
} else {
""
},
};

let html = EMailConfirmChangeHtml {
email_sub_prefix: &EMAIL_SUB_PREFIX,
header: i18n.subject,
msg: i18n.msg,
email_changed_to,
changed_by_admin: text.changed_by_admin,
};

let req = EMail {
Expand Down
18 changes: 13 additions & 5 deletions rauthy-models/src/entity/users.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,8 +363,6 @@ impl User {
let users = User::find_all(data)
.await?
.into_iter()
// // delete possibly existing cached user with the old email
// .filter(|u| Some(&u.email) != old_email.as_ref())
.map(|mut u| {
if u.id == self.id {
u = self.clone();
Expand Down Expand Up @@ -454,7 +452,17 @@ impl User {
user.email_verified = upd_user.email_verified;
user.user_expires = upd_user.user_expires;

user.save(data, old_email, None).await?;
user.save(data, old_email.clone(), None).await?;

// if the user was saved successfully and the email was changed, invalidate all existing
// sessions with the old address and send out notifications to the users addresses
Session::invalidate_for_user(data, &user.id).await?;

// send out confirmation E-Mails to both addresses
send_email_confirm_change(data, &user, &user.email, &user.email, true).await;
send_email_confirm_change(data, &user, old_email.as_ref().unwrap(), &user.email, true)
.await;

Ok(user)
}

Expand Down Expand Up @@ -817,8 +825,8 @@ impl User {
Session::invalidate_for_user(data, &user.id).await?;

// send out confirmation E-Mails to both addresses
send_email_confirm_change(data, &user, &user.email, &user.email).await;
send_email_confirm_change(data, &user, &old_email, &user.email).await;
send_email_confirm_change(data, &user, &user.email, &user.email, false).await;
send_email_confirm_change(data, &user, &old_email, &user.email, false).await;

Ok((html, nonce))
}
Expand Down
3 changes: 3 additions & 0 deletions rauthy-models/src/i18n/email_confirm_change.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use serde::Serialize;
pub struct I18nEmailConfirmChange<'a> {
pub subject: &'a str,
pub msg: &'a str,
pub msg_from_admin: &'a str,
}

impl SsrJson for I18nEmailConfirmChange<'_> {
Expand All @@ -26,13 +27,15 @@ impl I18nEmailConfirmChange<'_> {
Self {
subject: "E-Mail Change confirmed for",
msg: "Your E-Mail address has been changed successfully to:",
msg_from_admin: "This action was done by an Administrator.",
}
}

fn build_de() -> Self {
Self {
subject: "E-Mail Wechsel bestätigt für",
msg: "Ihre E-Mail Adresse wurde erfolgreich geändert zu:",
msg_from_admin: "Diese Änderung wurde durch einen Administrator durchgeführt.",
}
}
}

0 comments on commit 8e97e31

Please sign in to comment.