Controller/Account: refactor mail notifications into separate functions

This commit is contained in:
Daniel Friesel 2022-01-19 19:25:03 +01:00
parent 1233796866
commit 170c7e420e

View file

@ -69,9 +69,6 @@ sub register {
my $password = $self->req->param('password'); my $password = $self->req->param('password');
my $password2 = $self->req->param('password2'); my $password2 = $self->req->param('password2');
my $ip = $self->req->headers->header('X-Forwarded-For'); my $ip = $self->req->headers->header('X-Forwarded-For');
my $ua = $self->req->headers->user_agent;
my $date = DateTime->now( time_zone => 'Europe/Berlin' )
->strftime('%d.%m.%Y %H:%M:%S %z');
# In case Mojolicious is not running behind a reverse proxy # In case Mojolicious is not running behind a reverse proxy
$ip $ip
@ -153,6 +150,35 @@ sub register {
token => $token, token => $token,
password_hash => $pw_hash password_hash => $pw_hash
); );
my $success = $self->send_registration_mail(
email => $email,
token => $token,
ip => $ip,
user => $user,
user_id => $user_id
);
if ($success) {
$tx->commit;
$self->render( 'login', from => 'register' );
}
else {
$self->render( 'register', invalid => 'sendmail' );
}
}
sub send_registration_mail {
my ( $self, %opt ) = @_;
my $email = $opt{email};
my $token = $opt{token};
my $user = $opt{user};
my $user_id = $opt{user_id};
my $ip = $opt{ip};
my $date = DateTime->now( time_zone => 'Europe/Berlin' )
->strftime('%d.%m.%Y %H:%M:%S %z');
my $ua = $self->req->headers->user_agent;
my $reg_url = $self->url_for('reg')->to_abs->scheme('https'); my $reg_url = $self->url_for('reg')->to_abs->scheme('https');
my $imprint_url = $self->url_for('impressum')->to_abs->scheme('https'); my $imprint_url = $self->url_for('impressum')->to_abs->scheme('https');
@ -175,15 +201,8 @@ sub register {
$body .= " * UserAgent: ${ua}\n\n\n"; $body .= " * UserAgent: ${ua}\n\n\n";
$body .= "Impressum: ${imprint_url}\n"; $body .= "Impressum: ${imprint_url}\n";
my $success return $self->sendmail->custom( $email, 'Registrierung bei travelynx',
= $self->sendmail->custom( $email, 'Registrierung bei travelynx', $body ); $body );
if ($success) {
$tx->commit;
$self->render( 'login', from => 'register' );
}
else {
$self->render( 'register', invalid => 'sendmail' );
}
} }
sub verify { sub verify {
@ -425,7 +444,6 @@ sub change_mail {
} }
my $token = make_token(); my $token = make_token();
my $name = $self->current_user->{name};
my $db = $self->pg->db; my $db = $self->pg->db;
my $tx = $db->begin; my $tx = $db->begin;
@ -436,6 +454,25 @@ sub change_mail {
token => $token token => $token
); );
my $success = $self->send_address_confirmation_mail( $email, $token );
if ($success) {
$tx->commit;
$self->render( 'change_mail', success => 1 );
}
else {
$self->render( 'change_mail', invalid => 'sendmail' );
}
}
else {
$self->render('change_mail');
}
}
sub send_address_confirmation_mail {
my ( $self, $email, $token ) = @_;
my $name = $self->current_user->{name};
my $ip = $self->req->headers->header('X-Forwarded-For'); my $ip = $self->req->headers->header('X-Forwarded-For');
my $ua = $self->req->headers->user_agent; my $ua = $self->req->headers->user_agent;
my $date = DateTime->now( time_zone => 'Europe/Berlin' ) my $date = DateTime->now( time_zone => 'Europe/Berlin' )
@ -443,10 +480,8 @@ sub change_mail {
# In case Mojolicious is not running behind a reverse proxy # In case Mojolicious is not running behind a reverse proxy
$ip $ip
//= sprintf( '%s:%s', $self->tx->remote_address, //= sprintf( '%s:%s', $self->tx->remote_address, $self->tx->remote_port );
$self->tx->remote_port ); my $confirm_url = $self->url_for('confirm_mail')->to_abs->scheme('https');
my $confirm_url
= $self->url_for('confirm_mail')->to_abs->scheme('https');
my $imprint_url = $self->url_for('impressum')->to_abs->scheme('https'); my $imprint_url = $self->url_for('impressum')->to_abs->scheme('https');
my $body = "Hallo ${name},\n\n"; my $body = "Hallo ${name},\n\n";
@ -461,21 +496,8 @@ sub change_mail {
$body .= " * UserAgent: ${ua}\n\n\n"; $body .= " * UserAgent: ${ua}\n\n\n";
$body .= "Impressum: ${imprint_url}\n"; $body .= "Impressum: ${imprint_url}\n";
my $success return $self->sendmail->custom( $email,
= $self->sendmail->custom( $email,
'travelynx: Mail-Adresse bestätigen', $body ); 'travelynx: Mail-Adresse bestätigen', $body );
if ($success) {
$tx->commit;
$self->render( 'change_mail', success => 1 );
}
else {
$self->render( 'change_mail', invalid => 'sendmail' );
}
}
else {
$self->render('change_mail');
}
} }
sub change_name { sub change_name {
@ -535,6 +557,16 @@ sub change_name {
$self->flash( success => 'name' ); $self->flash( success => 'name' );
$self->redirect_to('account'); $self->redirect_to('account');
$self->send_name_notification_mail( $old_name, $new_name );
}
else {
$self->render( 'change_name', name => $old_name );
}
}
sub send_name_notification_mail {
my ( $self, $old_name, $new_name ) = @_;
my $ip = $self->req->headers->header('X-Forwarded-For'); my $ip = $self->req->headers->header('X-Forwarded-For');
my $ua = $self->req->headers->user_agent; my $ua = $self->req->headers->user_agent;
my $date = DateTime->now( time_zone => 'Europe/Berlin' ) my $date = DateTime->now( time_zone => 'Europe/Berlin' )
@ -542,16 +574,15 @@ sub change_name {
# In case Mojolicious is not running behind a reverse proxy # In case Mojolicious is not running behind a reverse proxy
$ip $ip
//= sprintf( '%s:%s', $self->tx->remote_address, //= sprintf( '%s:%s', $self->tx->remote_address, $self->tx->remote_port );
$self->tx->remote_port ); my $confirm_url = $self->url_for('confirm_mail')->to_abs->scheme('https');
my $confirm_url
= $self->url_for('confirm_mail')->to_abs->scheme('https');
my $imprint_url = $self->url_for('impressum')->to_abs->scheme('https'); my $imprint_url = $self->url_for('impressum')->to_abs->scheme('https');
my $body = "Hallo ${new_name},\n\n"; my $body = "Hallo ${new_name},\n\n";
$body .= "Der Name deines Travelynx-Accounts wurde erfolgreich geändert.\n";
$body $body
.= "Der Name deines Travelynx-Accounts wurde erfolgreich geändert.\n"; .= "Bitte beachte, dass du dich ab sofort nur mit dem neuen Namen anmelden kannst.\n\n";
$body .= "Alter Name: ${old_name}\n"; $body .= "Alter Name: ${old_name}\n\n";
$body .= "Neue Name: ${new_name}\n\n"; $body .= "Neue Name: ${new_name}\n\n";
$body .= "Daten zur Anfrage:\n"; $body .= "Daten zur Anfrage:\n";
$body .= " * Datum: ${date}\n"; $body .= " * Datum: ${date}\n";
@ -559,13 +590,9 @@ sub change_name {
$body .= " * UserAgent: ${ua}\n\n\n"; $body .= " * UserAgent: ${ua}\n\n\n";
$body .= "Impressum: ${imprint_url}\n"; $body .= "Impressum: ${imprint_url}\n";
$self->sendmail->custom( $self->current_user->{email}, return $self->sendmail->custom( $self->current_user->{email},
'travelynx: Name geändert', $body ); 'travelynx: Name geändert', $body );
} }
else {
$self->render( 'change_name', name => $old_name );
}
}
sub password_form { sub password_form {
my ($self) = @_; my ($self) = @_;
@ -613,7 +640,11 @@ sub change_password {
$self->flash( success => 'password' ); $self->flash( success => 'password' );
$self->redirect_to('account'); $self->redirect_to('account');
$self->send_password_notification_mail();
}
sub send_password_notification_mail {
my ($self) = @_;
my $user = $self->current_user->{name}; my $user = $self->current_user->{name};
my $email = $self->current_user->{email}; my $email = $self->current_user->{email};
my $ip = $self->req->headers->header('X-Forwarded-For'); my $ip = $self->req->headers->header('X-Forwarded-For');
@ -676,36 +707,12 @@ sub request_password_reset {
return; return;
} }
my $ip = $self->req->headers->header('X-Forwarded-For'); my $success = $self->send_lostpassword_confirmation_mail(
my $ua = $self->req->headers->user_agent; email => $email,
my $date = DateTime->now( time_zone => 'Europe/Berlin' ) name => $name,
->strftime('%d.%m.%Y %H:%M:%S %z'); uid => $uid,
token => $token
# In case Mojolicious is not running behind a reverse proxy );
$ip
//= sprintf( '%s:%s', $self->tx->remote_address,
$self->tx->remote_port );
my $recover_url = $self->url_for('recover')->to_abs->scheme('https');
my $imprint_url = $self->url_for('impressum')->to_abs->scheme('https');
my $body = "Hallo ${name},\n\n";
$body .= "Unter ${recover_url}/${uid}/${token}\n";
$body
.= "kannst du ein neues Passwort für deinen travelynx-Account vergeben.\n\n";
$body
.= "Du erhältst diese Mail, da mit deinem Accountnamen und deiner Mail-Adresse\n";
$body
.= "ein Passwort-Reset angefordert wurde. Falls diese Anfrage nicht von dir\n";
$body .= "ausging, kannst du sie ignorieren.\n\n";
$body .= "Daten zur Anfrage:\n";
$body .= " * Datum: ${date}\n";
$body .= " * Client: ${ip}\n";
$body .= " * UserAgent: ${ua}\n\n\n";
$body .= "Impressum: ${imprint_url}\n";
my $success
= $self->sendmail->custom( $email, 'travelynx: Neues Passwort',
$body );
if ($success) { if ($success) {
$tx->commit; $tx->commit;
@ -768,6 +775,52 @@ sub request_password_reset {
token => $token token => $token
); );
$self->send_lostpassword_notification_mail($account);
}
else {
$self->render('recover_password');
}
}
sub send_lostpassword_confirmation_mail {
my ( $self, %opt ) = @_;
my $email = $opt{email};
my $name = $opt{name};
my $uid = $opt{uid};
my $token = $opt{token};
my $ip = $self->req->headers->header('X-Forwarded-For');
my $ua = $self->req->headers->user_agent;
my $date = DateTime->now( time_zone => 'Europe/Berlin' )
->strftime('%d.%m.%Y %H:%M:%S %z');
# In case Mojolicious is not running behind a reverse proxy
$ip
//= sprintf( '%s:%s', $self->tx->remote_address, $self->tx->remote_port );
my $recover_url = $self->url_for('recover')->to_abs->scheme('https');
my $imprint_url = $self->url_for('impressum')->to_abs->scheme('https');
my $body = "Hallo ${name},\n\n";
$body .= "Unter ${recover_url}/${uid}/${token}\n";
$body
.= "kannst du ein neues Passwort für deinen travelynx-Account vergeben.\n\n";
$body
.= "Du erhältst diese Mail, da mit deinem Accountnamen und deiner Mail-Adresse\n";
$body
.= "ein Passwort-Reset angefordert wurde. Falls diese Anfrage nicht von dir\n";
$body .= "ausging, kannst du sie ignorieren.\n\n";
$body .= "Daten zur Anfrage:\n";
$body .= " * Datum: ${date}\n";
$body .= " * Client: ${ip}\n";
$body .= " * UserAgent: ${ua}\n\n\n";
$body .= "Impressum: ${imprint_url}\n";
my $success
= $self->sendmail->custom( $email, 'travelynx: Neues Passwort', $body );
}
sub send_lostpassword_notification_mail {
my ( $self, $account ) = @_;
my $user = $account->{name}; my $user = $account->{name};
my $email = $account->{email}; my $email = $account->{email};
my $ip = $self->req->headers->header('X-Forwarded-For'); my $ip = $self->req->headers->header('X-Forwarded-For');
@ -777,13 +830,11 @@ sub request_password_reset {
# In case Mojolicious is not running behind a reverse proxy # In case Mojolicious is not running behind a reverse proxy
$ip $ip
//= sprintf( '%s:%s', $self->tx->remote_address, //= sprintf( '%s:%s', $self->tx->remote_address, $self->tx->remote_port );
$self->tx->remote_port );
my $imprint_url = $self->url_for('impressum')->to_abs->scheme('https'); my $imprint_url = $self->url_for('impressum')->to_abs->scheme('https');
my $body = "Hallo ${user},\n\n"; my $body = "Hallo ${user},\n\n";
$body $body .= "Das Passwort deines travelynx-Accounts wurde soeben über die";
.= "Das Passwort deines travelynx-Accounts wurde soeben über die";
$body .= " 'Passwort vergessen'-Funktion geändert.\n\n"; $body .= " 'Passwort vergessen'-Funktion geändert.\n\n";
$body .= "Daten zur Änderung:\n"; $body .= "Daten zur Änderung:\n";
$body .= " * Datum: ${date}\n"; $body .= " * Datum: ${date}\n";
@ -791,13 +842,9 @@ sub request_password_reset {
$body .= " * UserAgent: ${ua}\n\n\n"; $body .= " * UserAgent: ${ua}\n\n\n";
$body .= "Impressum: ${imprint_url}\n"; $body .= "Impressum: ${imprint_url}\n";
$self->sendmail->custom( $email, 'travelynx: Passwort geändert', return $self->sendmail->custom( $email, 'travelynx: Passwort geändert',
$body ); $body );
} }
else {
$self->render('recover_password');
}
}
sub recover_password { sub recover_password {
my ($self) = @_; my ($self) = @_;