2019-03-22 15:56:49 +00:00
|
|
|
|
package Travelynx::Controller::Account;
|
2021-04-20 19:59:17 +00:00
|
|
|
|
|
2020-11-27 21:12:56 +00:00
|
|
|
|
# Copyright (C) 2020 Daniel Friesel
|
|
|
|
|
#
|
2021-01-29 17:32:13 +00:00
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
2019-03-22 15:56:49 +00:00
|
|
|
|
use Mojo::Base 'Mojolicious::Controller';
|
|
|
|
|
|
|
|
|
|
use Crypt::Eksblowfish::Bcrypt qw(bcrypt en_base64);
|
|
|
|
|
use UUID::Tiny qw(:std);
|
|
|
|
|
|
|
|
|
|
sub hash_password {
|
|
|
|
|
my ($password) = @_;
|
|
|
|
|
my @salt_bytes = map { int( rand(255) ) + 1 } ( 1 .. 16 );
|
2020-07-27 16:53:22 +00:00
|
|
|
|
my $salt = en_base64( pack( 'C[16]', @salt_bytes ) );
|
2019-03-22 15:56:49 +00:00
|
|
|
|
|
|
|
|
|
return bcrypt( $password, '$2a$12$' . $salt );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sub make_token {
|
|
|
|
|
return create_uuid_as_string(UUID_V4);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sub login_form {
|
|
|
|
|
my ($self) = @_;
|
|
|
|
|
$self->render('login');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sub do_login {
|
|
|
|
|
my ($self) = @_;
|
|
|
|
|
my $user = $self->req->param('user');
|
|
|
|
|
my $password = $self->req->param('password');
|
|
|
|
|
|
|
|
|
|
# Keep cookies for 6 months
|
|
|
|
|
$self->session( expiration => 60 * 60 * 24 * 180 );
|
|
|
|
|
|
|
|
|
|
if ( $self->validation->csrf_protect->has_error('csrf_token') ) {
|
|
|
|
|
$self->render(
|
|
|
|
|
'login',
|
|
|
|
|
invalid => 'csrf',
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
if ( $self->authenticate( $user, $password ) ) {
|
|
|
|
|
$self->redirect_to( $self->req->param('redirect_to') // '/' );
|
2020-07-27 16:53:22 +00:00
|
|
|
|
$self->users->mark_seen( uid => $self->current_user->{id} );
|
2019-03-22 15:56:49 +00:00
|
|
|
|
}
|
|
|
|
|
else {
|
2020-07-27 16:53:22 +00:00
|
|
|
|
my $data = $self->users->get_login_data( name => $user );
|
2019-03-22 15:56:49 +00:00
|
|
|
|
if ( $data and $data->{status} == 0 ) {
|
|
|
|
|
$self->render( 'login', invalid => 'confirmation' );
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
$self->render( 'login', invalid => 'credentials' );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sub registration_form {
|
|
|
|
|
my ($self) = @_;
|
|
|
|
|
$self->render('register');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sub register {
|
|
|
|
|
my ($self) = @_;
|
2021-04-20 19:59:17 +00:00
|
|
|
|
my $dt = $self->req->param('dt');
|
2019-03-22 15:56:49 +00:00
|
|
|
|
my $user = $self->req->param('user');
|
|
|
|
|
my $email = $self->req->param('email');
|
|
|
|
|
my $password = $self->req->param('password');
|
|
|
|
|
my $password2 = $self->req->param('password2');
|
|
|
|
|
my $ip = $self->req->headers->header('X-Forwarded-For');
|
|
|
|
|
my $ua = $self->req->headers->user_agent;
|
2019-12-06 20:42:26 +00:00
|
|
|
|
my $date = DateTime->now( time_zone => 'Europe/Berlin' )
|
2019-03-22 15:56:49 +00:00
|
|
|
|
->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 );
|
|
|
|
|
|
|
|
|
|
if ( $self->validation->csrf_protect->has_error('csrf_token') ) {
|
|
|
|
|
$self->render(
|
|
|
|
|
'register',
|
|
|
|
|
invalid => 'csrf',
|
|
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 06:48:08 +00:00
|
|
|
|
if ( my $error = $self->users->is_name_invalid( name => $user ) ) {
|
|
|
|
|
$self->render( 'register', invalid => $error );
|
2019-03-22 15:56:49 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( not length($email) ) {
|
|
|
|
|
$self->render( 'register', invalid => 'mail_empty' );
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 04:20:21 +00:00
|
|
|
|
if ( $self->users->mail_is_blacklisted( email => $email ) ) {
|
2019-03-22 15:56:49 +00:00
|
|
|
|
$self->render( 'register', invalid => 'mail_blacklisted' );
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $password ne $password2 ) {
|
|
|
|
|
$self->render( 'register', invalid => 'password_notequal' );
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( length($password) < 8 ) {
|
|
|
|
|
$self->render( 'register', invalid => 'password_short' );
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-20 19:59:17 +00:00
|
|
|
|
if ( not $dt
|
|
|
|
|
or DateTime->now( time_zone => 'Europe/Berlin' )->epoch - $dt < 6 )
|
|
|
|
|
{
|
|
|
|
|
# a human user should take at least five seconds to fill out the form.
|
|
|
|
|
# Throw a CSRF error at presumed spammers.
|
|
|
|
|
$self->render(
|
|
|
|
|
'register',
|
|
|
|
|
invalid => 'csrf',
|
|
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-27 16:53:22 +00:00
|
|
|
|
my $token = make_token();
|
|
|
|
|
my $pw_hash = hash_password($password);
|
|
|
|
|
my $db = $self->pg->db;
|
|
|
|
|
my $tx = $db->begin;
|
|
|
|
|
my $user_id = $self->users->add_user(
|
|
|
|
|
db => $db,
|
|
|
|
|
name => $user,
|
|
|
|
|
email => $email,
|
|
|
|
|
token => $token,
|
|
|
|
|
password_hash => $pw_hash
|
|
|
|
|
);
|
2019-03-22 15:56:49 +00:00
|
|
|
|
my $reg_url = $self->url_for('reg')->to_abs->scheme('https');
|
|
|
|
|
my $imprint_url = $self->url_for('impressum')->to_abs->scheme('https');
|
|
|
|
|
|
|
|
|
|
my $body = "Hallo, ${user}!\n\n";
|
|
|
|
|
$body .= "Mit deiner E-Mail-Adresse (${email}) wurde ein Account bei\n";
|
|
|
|
|
$body .= "travelynx angelegt.\n\n";
|
|
|
|
|
$body
|
|
|
|
|
.= "Falls die Registrierung von dir ausging, kannst du den Account unter\n";
|
|
|
|
|
$body .= "${reg_url}/${user_id}/${token}\n";
|
|
|
|
|
$body .= "freischalten.\n\n";
|
|
|
|
|
$body
|
|
|
|
|
.= "Falls nicht, ignoriere diese Mail bitte. Nach etwa 48 Stunden wird deine\n";
|
|
|
|
|
$body
|
|
|
|
|
.= "Mail-Adresse erneut zur Registrierung freigeschaltet. Falls auch diese fehlschlägt,\n";
|
|
|
|
|
$body
|
|
|
|
|
.= "werden wir sie dauerhaft sperren und keine Mails mehr dorthin schicken.\n\n";
|
|
|
|
|
$body .= "Daten zur Registrierung:\n";
|
|
|
|
|
$body .= " * Datum: ${date}\n";
|
2019-04-06 09:20:10 +00:00
|
|
|
|
$body .= " * Client: ${ip}\n";
|
|
|
|
|
$body .= " * UserAgent: ${ua}\n\n\n";
|
2019-03-22 15:56:49 +00:00
|
|
|
|
$body .= "Impressum: ${imprint_url}\n";
|
|
|
|
|
|
2019-04-06 09:20:10 +00:00
|
|
|
|
my $success
|
|
|
|
|
= $self->sendmail->custom( $email, 'Registrierung bei travelynx', $body );
|
2019-03-22 15:56:49 +00:00
|
|
|
|
if ($success) {
|
2019-04-22 10:11:22 +00:00
|
|
|
|
$tx->commit;
|
2019-03-22 15:56:49 +00:00
|
|
|
|
$self->render( 'login', from => 'register' );
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
$self->render( 'register', invalid => 'sendmail' );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sub verify {
|
|
|
|
|
my ($self) = @_;
|
|
|
|
|
|
|
|
|
|
my $id = $self->stash('id');
|
|
|
|
|
my $token = $self->stash('token');
|
|
|
|
|
|
2019-05-02 09:34:52 +00:00
|
|
|
|
if ( not $id =~ m{ ^ \d+ $ }x or $id > 2147483647 ) {
|
2019-03-22 15:56:49 +00:00
|
|
|
|
$self->render( 'register', invalid => 'token' );
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-27 16:53:22 +00:00
|
|
|
|
if (
|
|
|
|
|
not $self->users->verify_registration_token(
|
|
|
|
|
uid => $id,
|
|
|
|
|
token => $token
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
{
|
2019-03-22 15:56:49 +00:00
|
|
|
|
$self->render( 'register', invalid => 'token' );
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-04-30 16:05:07 +00:00
|
|
|
|
|
2019-03-22 15:56:49 +00:00
|
|
|
|
$self->render( 'login', from => 'verification' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sub delete {
|
|
|
|
|
my ($self) = @_;
|
|
|
|
|
if ( $self->validation->csrf_protect->has_error('csrf_token') ) {
|
|
|
|
|
$self->render( 'account', invalid => 'csrf' );
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $self->param('action') eq 'delete' ) {
|
|
|
|
|
if (
|
|
|
|
|
not $self->authenticate(
|
|
|
|
|
$self->current_user->{name},
|
|
|
|
|
$self->param('password')
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
{
|
2019-04-30 08:46:46 +00:00
|
|
|
|
$self->render( 'account', invalid => 'deletion password' );
|
2019-03-22 15:56:49 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2020-07-27 16:53:22 +00:00
|
|
|
|
$self->users->flag_deletion( uid => $self->current_user->{id} );
|
2019-03-22 15:56:49 +00:00
|
|
|
|
}
|
|
|
|
|
else {
|
2020-07-27 16:53:22 +00:00
|
|
|
|
$self->users->unflag_deletion( uid => $self->current_user->{id} );
|
2019-03-22 15:56:49 +00:00
|
|
|
|
}
|
|
|
|
|
$self->redirect_to('account');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sub do_logout {
|
|
|
|
|
my ($self) = @_;
|
|
|
|
|
if ( $self->validation->csrf_protect->has_error('csrf_token') ) {
|
|
|
|
|
$self->render( 'login', invalid => 'csrf' );
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
$self->logout;
|
|
|
|
|
$self->redirect_to('/login');
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-30 21:23:49 +00:00
|
|
|
|
sub privacy {
|
|
|
|
|
my ($self) = @_;
|
|
|
|
|
|
|
|
|
|
my $user = $self->current_user;
|
|
|
|
|
my $public_level = $user->{is_public};
|
|
|
|
|
|
|
|
|
|
if ( $self->param('action') and $self->param('action') eq 'save' ) {
|
2019-12-08 10:06:17 +00:00
|
|
|
|
if ( $self->param('status_level') eq 'intern' ) {
|
|
|
|
|
$public_level |= 0x01;
|
|
|
|
|
$public_level &= ~0x02;
|
|
|
|
|
}
|
|
|
|
|
elsif ( $self->param('status_level') eq 'extern' ) {
|
2019-04-30 21:23:49 +00:00
|
|
|
|
$public_level |= 0x02;
|
2019-12-08 10:06:17 +00:00
|
|
|
|
$public_level &= ~0x01;
|
2019-04-30 21:23:49 +00:00
|
|
|
|
}
|
|
|
|
|
else {
|
2019-12-08 10:06:17 +00:00
|
|
|
|
$public_level &= ~0x03;
|
2019-04-30 21:23:49 +00:00
|
|
|
|
}
|
2019-12-08 09:32:22 +00:00
|
|
|
|
|
|
|
|
|
# public comment with non-public status does not make sense
|
2019-12-08 10:06:17 +00:00
|
|
|
|
if ( $self->param('public_comment')
|
|
|
|
|
and $self->param('status_level') ne 'private' )
|
2019-12-08 09:32:22 +00:00
|
|
|
|
{
|
2019-12-06 20:42:26 +00:00
|
|
|
|
$public_level |= 0x04;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
$public_level &= ~0x04;
|
|
|
|
|
}
|
2020-02-14 15:29:44 +00:00
|
|
|
|
|
|
|
|
|
if ( $self->param('history_level') eq 'intern' ) {
|
|
|
|
|
$public_level |= 0x10;
|
|
|
|
|
$public_level &= ~0x20;
|
|
|
|
|
}
|
|
|
|
|
elsif ( $self->param('history_level') eq 'extern' ) {
|
|
|
|
|
$public_level |= 0x20;
|
|
|
|
|
$public_level &= ~0x10;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
$public_level &= ~0x30;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-04 16:38:35 +00:00
|
|
|
|
if ( $self->param('history_age') eq 'infinite' ) {
|
|
|
|
|
$public_level |= 0x40;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
$public_level &= ~0x40;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-27 16:53:22 +00:00
|
|
|
|
$self->users->set_privacy(
|
|
|
|
|
uid => $user->{id},
|
|
|
|
|
level => $public_level
|
|
|
|
|
);
|
2019-05-10 17:03:18 +00:00
|
|
|
|
|
|
|
|
|
$self->flash( success => 'privacy' );
|
2019-05-02 08:05:15 +00:00
|
|
|
|
$self->redirect_to('account');
|
2019-04-30 21:23:49 +00:00
|
|
|
|
}
|
|
|
|
|
else {
|
2019-12-08 10:06:17 +00:00
|
|
|
|
$self->param(
|
|
|
|
|
status_level => $public_level & 0x01 ? 'intern'
|
|
|
|
|
: $public_level & 0x02 ? 'extern'
|
|
|
|
|
: 'private'
|
|
|
|
|
);
|
2019-12-06 20:42:26 +00:00
|
|
|
|
$self->param( public_comment => $public_level & 0x04 ? 1 : 0 );
|
2020-02-14 15:29:44 +00:00
|
|
|
|
$self->param(
|
|
|
|
|
history_level => $public_level & 0x10 ? 'intern'
|
|
|
|
|
: $public_level & 0x20 ? 'extern'
|
|
|
|
|
: 'private'
|
|
|
|
|
);
|
2020-09-04 16:38:35 +00:00
|
|
|
|
$self->param(
|
|
|
|
|
history_age => $public_level & 0x40 ? 'infinite' : 'month' );
|
2019-05-02 08:05:15 +00:00
|
|
|
|
$self->render( 'privacy', name => $user->{name} );
|
2019-04-30 21:23:49 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-20 17:15:21 +00:00
|
|
|
|
sub insight {
|
|
|
|
|
my ($self) = @_;
|
|
|
|
|
|
|
|
|
|
my $user = $self->current_user;
|
2020-07-27 16:53:22 +00:00
|
|
|
|
my $use_history = $self->users->use_history( uid => $user->{id} );
|
2019-05-20 17:15:21 +00:00
|
|
|
|
|
|
|
|
|
if ( $self->param('action') and $self->param('action') eq 'save' ) {
|
|
|
|
|
if ( $self->param('on_departure') ) {
|
|
|
|
|
$use_history |= 0x01;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
$use_history &= ~0x01;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $self->param('on_arrival') ) {
|
|
|
|
|
$use_history |= 0x02;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
$use_history &= ~0x02;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-27 16:53:22 +00:00
|
|
|
|
$self->users->use_history(
|
|
|
|
|
uid => $user->{id},
|
|
|
|
|
set => $use_history
|
|
|
|
|
);
|
2019-05-20 17:15:21 +00:00
|
|
|
|
$self->flash( success => 'use_history' );
|
|
|
|
|
$self->redirect_to('account');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$self->param( on_departure => $use_history & 0x01 ? 1 : 0 );
|
|
|
|
|
$self->param( on_arrival => $use_history & 0x02 ? 1 : 0 );
|
|
|
|
|
$self->render('use_history');
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-05 16:09:11 +00:00
|
|
|
|
sub webhook {
|
|
|
|
|
my ($self) = @_;
|
|
|
|
|
|
|
|
|
|
my $hook = $self->get_webhook;
|
|
|
|
|
|
|
|
|
|
if ( $self->param('action') and $self->param('action') eq 'save' ) {
|
|
|
|
|
$hook->{url} = $self->param('url');
|
|
|
|
|
$hook->{token} = $self->param('token');
|
|
|
|
|
$hook->{enabled} = $self->param('enabled') // 0;
|
|
|
|
|
$self->set_webhook(
|
|
|
|
|
url => $hook->{url},
|
|
|
|
|
token => $hook->{token},
|
|
|
|
|
enabled => $hook->{enabled}
|
|
|
|
|
);
|
2019-05-14 18:05:45 +00:00
|
|
|
|
$self->run_hook(
|
|
|
|
|
$self->current_user->{id},
|
|
|
|
|
'ping',
|
|
|
|
|
sub {
|
|
|
|
|
$self->render(
|
|
|
|
|
'webhooks',
|
|
|
|
|
hook => $self->get_webhook,
|
|
|
|
|
new_hook => 1
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
return;
|
2019-05-05 16:09:11 +00:00
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
$self->param( url => $hook->{url} );
|
|
|
|
|
$self->param( token => $hook->{token} );
|
|
|
|
|
$self->param( enabled => $hook->{enabled} );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$self->render( 'webhooks', hook => $hook );
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-30 10:08:51 +00:00
|
|
|
|
sub change_mail {
|
|
|
|
|
my ($self) = @_;
|
|
|
|
|
|
|
|
|
|
my $action = $self->req->param('action');
|
|
|
|
|
my $password = $self->req->param('password');
|
|
|
|
|
my $email = $self->req->param('email');
|
|
|
|
|
|
|
|
|
|
if ( $action and $action eq 'update_mail' ) {
|
|
|
|
|
if ( $self->validation->csrf_protect->has_error('csrf_token') ) {
|
|
|
|
|
$self->render(
|
|
|
|
|
'change_mail',
|
|
|
|
|
invalid => 'csrf',
|
|
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( not length($email) ) {
|
|
|
|
|
$self->render( 'change_mail', invalid => 'mail_empty' );
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
not $self->authenticate(
|
|
|
|
|
$self->current_user->{name},
|
|
|
|
|
$self->param('password')
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
$self->render( 'change_mail', invalid => 'password' );
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
my $token = make_token();
|
|
|
|
|
my $name = $self->current_user->{name};
|
|
|
|
|
my $db = $self->pg->db;
|
|
|
|
|
my $tx = $db->begin;
|
|
|
|
|
|
2020-07-27 16:53:22 +00:00
|
|
|
|
$self->users->mark_for_mail_change(
|
|
|
|
|
db => $db,
|
|
|
|
|
uid => $self->current_user->{id},
|
|
|
|
|
email => $email,
|
|
|
|
|
token => $token
|
|
|
|
|
);
|
2019-04-30 10:08:51 +00:00
|
|
|
|
|
|
|
|
|
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 $confirm_url
|
|
|
|
|
= $self->url_for('confirm_mail')->to_abs->scheme('https');
|
|
|
|
|
my $imprint_url = $self->url_for('impressum')->to_abs->scheme('https');
|
|
|
|
|
|
|
|
|
|
my $body = "Hallo ${name},\n\n";
|
|
|
|
|
$body .= "Bitte bestätige unter <${confirm_url}/${token}>,\n";
|
|
|
|
|
$body .= "dass du mit dieser Adresse E-Mail empfangen kannst.\n\n";
|
|
|
|
|
$body
|
|
|
|
|
.= "Du erhältst diese Mail, da eine Änderung der deinem travelynx-Account\n";
|
|
|
|
|
$body .= "zugeordneten Mail-Adresse beantragt wurde.\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: 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');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-12 17:00:42 +00:00
|
|
|
|
sub change_name {
|
|
|
|
|
my ($self) = @_;
|
|
|
|
|
|
|
|
|
|
my $action = $self->req->param('action');
|
|
|
|
|
my $password = $self->req->param('password');
|
|
|
|
|
my $old_name = $self->current_user->{name};
|
|
|
|
|
my $new_name = $self->req->param('name');
|
|
|
|
|
|
|
|
|
|
if ( $action and $action eq 'update_name' ) {
|
|
|
|
|
if ( $self->validation->csrf_protect->has_error('csrf_token') ) {
|
|
|
|
|
$self->render(
|
|
|
|
|
'change_name',
|
2021-06-14 17:30:58 +00:00
|
|
|
|
name => $old_name,
|
2021-06-12 17:00:42 +00:00
|
|
|
|
invalid => 'csrf',
|
|
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 06:48:08 +00:00
|
|
|
|
if ( my $error = $self->users->is_name_invalid( name => $new_name ) ) {
|
2021-06-14 17:30:58 +00:00
|
|
|
|
$self->render(
|
|
|
|
|
'change_name',
|
|
|
|
|
name => $old_name,
|
|
|
|
|
invalid => $error
|
|
|
|
|
);
|
2021-06-12 17:00:42 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( not $self->authenticate( $old_name, $self->param('password') ) ) {
|
2021-06-14 17:30:58 +00:00
|
|
|
|
$self->render(
|
|
|
|
|
'change_name',
|
|
|
|
|
name => $old_name,
|
|
|
|
|
invalid => 'password'
|
|
|
|
|
);
|
2021-06-12 17:00:42 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 06:48:08 +00:00
|
|
|
|
# The users table has a unique constraint on the "name" column, so having
|
|
|
|
|
# two users with the same name is not possible. The race condition
|
|
|
|
|
# between the user_name_exists check in is_name_invalid and this
|
|
|
|
|
# change_name call is harmless.
|
2021-06-12 17:00:42 +00:00
|
|
|
|
my $success = $self->users->change_name(
|
|
|
|
|
uid => $self->current_user->{id},
|
|
|
|
|
name => $new_name
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if ( not $success ) {
|
2021-06-14 17:30:58 +00:00
|
|
|
|
$self->render(
|
|
|
|
|
'change_name',
|
|
|
|
|
name => $old_name,
|
|
|
|
|
invalid => 'user_collision'
|
|
|
|
|
);
|
2021-06-12 17:00:42 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$self->flash( success => 'name' );
|
|
|
|
|
$self->redirect_to('account');
|
|
|
|
|
|
|
|
|
|
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 $confirm_url
|
|
|
|
|
= $self->url_for('confirm_mail')->to_abs->scheme('https');
|
|
|
|
|
my $imprint_url = $self->url_for('impressum')->to_abs->scheme('https');
|
|
|
|
|
|
|
|
|
|
my $body = "Hallo ${new_name},\n\n";
|
|
|
|
|
$body
|
|
|
|
|
.= "Der Name deines Travelynx-Accounts wurde erfolgreich geändert.\n";
|
|
|
|
|
$body .= "Alter Name: ${old_name}\n";
|
|
|
|
|
$body .= "Neue Name: ${new_name}\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";
|
|
|
|
|
|
|
|
|
|
$self->sendmail->custom( $self->current_user->{email},
|
|
|
|
|
'travelynx: Name geändert', $body );
|
|
|
|
|
}
|
|
|
|
|
else {
|
2021-06-14 17:30:58 +00:00
|
|
|
|
$self->render( 'change_name', name => $old_name );
|
2021-06-12 17:00:42 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-06 09:20:10 +00:00
|
|
|
|
sub password_form {
|
|
|
|
|
my ($self) = @_;
|
|
|
|
|
|
|
|
|
|
$self->render('change_password');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sub change_password {
|
|
|
|
|
my ($self) = @_;
|
|
|
|
|
my $old_password = $self->req->param('oldpw');
|
|
|
|
|
my $password = $self->req->param('newpw');
|
|
|
|
|
my $password2 = $self->req->param('newpw2');
|
|
|
|
|
|
|
|
|
|
if ( $self->validation->csrf_protect->has_error('csrf_token') ) {
|
|
|
|
|
$self->render( 'change_password', invalid => 'csrf' );
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $password ne $password2 ) {
|
|
|
|
|
$self->render( 'change_password', invalid => 'password_notequal' );
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( length($password) < 8 ) {
|
|
|
|
|
$self->render( 'change_password', invalid => 'password_short' );
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
not $self->authenticate(
|
|
|
|
|
$self->current_user->{name},
|
|
|
|
|
$self->param('oldpw')
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
$self->render( 'change_password', invalid => 'password' );
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
my $pw_hash = hash_password($password);
|
2020-07-27 16:53:22 +00:00
|
|
|
|
$self->users->set_password_hash(
|
|
|
|
|
uid => $self->current_user->{id},
|
|
|
|
|
password_hash => $pw_hash
|
|
|
|
|
);
|
2019-04-06 09:20:10 +00:00
|
|
|
|
|
2019-04-30 10:08:51 +00:00
|
|
|
|
$self->flash( success => 'password' );
|
2019-04-06 09:20:10 +00:00
|
|
|
|
$self->redirect_to('account');
|
|
|
|
|
|
|
|
|
|
my $user = $self->current_user->{name};
|
|
|
|
|
my $email = $self->current_user->{email};
|
|
|
|
|
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 $imprint_url = $self->url_for('impressum')->to_abs->scheme('https');
|
|
|
|
|
|
|
|
|
|
my $body = "Hallo ${user},\n\n";
|
|
|
|
|
$body
|
|
|
|
|
.= "Das Passwort deines travelynx-Accounts wurde soeben geändert.\n\n";
|
|
|
|
|
$body .= "Daten zur Änderung:\n";
|
|
|
|
|
$body .= " * Datum: ${date}\n";
|
|
|
|
|
$body .= " * Client: ${ip}\n";
|
|
|
|
|
$body .= " * UserAgent: ${ua}\n\n\n";
|
|
|
|
|
$body .= "Impressum: ${imprint_url}\n";
|
|
|
|
|
|
|
|
|
|
$self->sendmail->custom( $email, 'travelynx: Passwort geändert', $body );
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-29 18:12:59 +00:00
|
|
|
|
sub request_password_reset {
|
|
|
|
|
my ($self) = @_;
|
|
|
|
|
|
|
|
|
|
if ( $self->param('action') and $self->param('action') eq 'initiate' ) {
|
|
|
|
|
if ( $self->validation->csrf_protect->has_error('csrf_token') ) {
|
|
|
|
|
$self->render( 'recover_password', invalid => 'csrf' );
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
my $name = $self->param('user');
|
|
|
|
|
my $email = $self->param('email');
|
|
|
|
|
|
2020-07-27 16:53:22 +00:00
|
|
|
|
my $uid = $self->users->get_uid_by_name_and_mail(
|
|
|
|
|
name => $name,
|
|
|
|
|
email => $email
|
|
|
|
|
);
|
2019-04-29 18:12:59 +00:00
|
|
|
|
|
|
|
|
|
if ( not $uid ) {
|
2019-04-30 08:46:46 +00:00
|
|
|
|
$self->render( 'recover_password',
|
|
|
|
|
invalid => 'recovery credentials' );
|
2019-04-29 18:12:59 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
my $token = make_token();
|
|
|
|
|
my $db = $self->pg->db;
|
|
|
|
|
my $tx = $db->begin;
|
|
|
|
|
|
2020-07-27 16:53:22 +00:00
|
|
|
|
my $error = $self->users->mark_for_password_reset(
|
|
|
|
|
db => $db,
|
|
|
|
|
uid => $uid,
|
|
|
|
|
token => $token
|
|
|
|
|
);
|
2019-04-29 18:12:59 +00:00
|
|
|
|
|
|
|
|
|
if ($error) {
|
|
|
|
|
$self->render( 'recover_password', invalid => $error );
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 );
|
|
|
|
|
|
|
|
|
|
if ($success) {
|
|
|
|
|
$tx->commit;
|
|
|
|
|
$self->render( 'recover_password', success => 1 );
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
$self->render( 'recover_password', invalid => 'sendmail' );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
elsif ( $self->param('action')
|
|
|
|
|
and $self->param('action') eq 'set_password' )
|
|
|
|
|
{
|
|
|
|
|
my $id = $self->param('id');
|
|
|
|
|
my $token = $self->param('token');
|
|
|
|
|
my $password = $self->param('newpw');
|
|
|
|
|
my $password2 = $self->param('newpw2');
|
|
|
|
|
|
|
|
|
|
if ( $self->validation->csrf_protect->has_error('csrf_token') ) {
|
|
|
|
|
$self->render( 'set_password', invalid => 'csrf' );
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-07-27 16:53:22 +00:00
|
|
|
|
if (
|
|
|
|
|
not $self->users->verify_password_token(
|
|
|
|
|
uid => $id,
|
|
|
|
|
token => $token
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
{
|
2019-04-30 10:08:51 +00:00
|
|
|
|
$self->render( 'recover_password', invalid => 'change token' );
|
2019-04-29 18:12:59 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if ( $password ne $password2 ) {
|
|
|
|
|
$self->render( 'set_password', invalid => 'password_notequal' );
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( length($password) < 8 ) {
|
|
|
|
|
$self->render( 'set_password', invalid => 'password_short' );
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
my $pw_hash = hash_password($password);
|
2020-07-27 16:53:22 +00:00
|
|
|
|
$self->users->set_password_hash(
|
|
|
|
|
uid => $id,
|
|
|
|
|
password_hash => $pw_hash
|
|
|
|
|
);
|
2019-04-29 18:12:59 +00:00
|
|
|
|
|
|
|
|
|
my $account = $self->get_user_data($id);
|
|
|
|
|
|
|
|
|
|
if ( not $self->authenticate( $account->{name}, $password ) ) {
|
|
|
|
|
$self->render( 'set_password',
|
|
|
|
|
invalid => 'Authentication failure – WTF?' );
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-30 10:08:51 +00:00
|
|
|
|
$self->flash( success => 'password' );
|
2019-04-29 18:12:59 +00:00
|
|
|
|
$self->redirect_to('account');
|
|
|
|
|
|
2020-07-27 16:53:22 +00:00
|
|
|
|
$self->users->remove_password_token(
|
|
|
|
|
uid => $id,
|
|
|
|
|
token => $token
|
|
|
|
|
);
|
2019-04-29 18:12:59 +00:00
|
|
|
|
|
|
|
|
|
my $user = $account->{name};
|
|
|
|
|
my $email = $account->{email};
|
|
|
|
|
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 $imprint_url = $self->url_for('impressum')->to_abs->scheme('https');
|
|
|
|
|
|
|
|
|
|
my $body = "Hallo ${user},\n\n";
|
|
|
|
|
$body
|
|
|
|
|
.= "Das Passwort deines travelynx-Accounts wurde soeben über die";
|
|
|
|
|
$body .= " 'Passwort vergessen'-Funktion geändert.\n\n";
|
|
|
|
|
$body .= "Daten zur Änderung:\n";
|
|
|
|
|
$body .= " * Datum: ${date}\n";
|
|
|
|
|
$body .= " * Client: ${ip}\n";
|
|
|
|
|
$body .= " * UserAgent: ${ua}\n\n\n";
|
|
|
|
|
$body .= "Impressum: ${imprint_url}\n";
|
|
|
|
|
|
|
|
|
|
$self->sendmail->custom( $email, 'travelynx: Passwort geändert',
|
|
|
|
|
$body );
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
$self->render('recover_password');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sub recover_password {
|
|
|
|
|
my ($self) = @_;
|
|
|
|
|
|
|
|
|
|
my $id = $self->stash('id');
|
|
|
|
|
my $token = $self->stash('token');
|
|
|
|
|
|
2019-05-02 09:34:52 +00:00
|
|
|
|
if ( not $id =~ m{ ^ \d+ $ }x or $id > 2147483647 ) {
|
|
|
|
|
$self->render( 'recover_password', invalid => 'recovery token' );
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-27 16:53:22 +00:00
|
|
|
|
if (
|
|
|
|
|
$self->users->verify_password_token(
|
|
|
|
|
uid => $id,
|
|
|
|
|
token => $token
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
{
|
2019-04-29 18:12:59 +00:00
|
|
|
|
$self->render('set_password');
|
|
|
|
|
}
|
|
|
|
|
else {
|
2019-04-30 08:46:46 +00:00
|
|
|
|
$self->render( 'recover_password', invalid => 'recovery token' );
|
2019-04-29 18:12:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-30 10:08:51 +00:00
|
|
|
|
sub confirm_mail {
|
|
|
|
|
my ($self) = @_;
|
|
|
|
|
my $id = $self->current_user->{id};
|
|
|
|
|
my $token = $self->stash('token');
|
|
|
|
|
|
2020-07-27 16:53:22 +00:00
|
|
|
|
if (
|
|
|
|
|
$self->users->change_mail_with_token(
|
|
|
|
|
uid => $id,
|
|
|
|
|
token => $token
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
{
|
2019-04-30 10:08:51 +00:00
|
|
|
|
$self->flash( success => 'mail' );
|
|
|
|
|
$self->redirect_to('account');
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
$self->render( 'change_mail', invalid => 'change token' );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-22 15:56:49 +00:00
|
|
|
|
sub account {
|
|
|
|
|
my ($self) = @_;
|
|
|
|
|
|
|
|
|
|
$self->render('account');
|
2020-07-27 16:53:22 +00:00
|
|
|
|
$self->users->mark_seen( uid => $self->current_user->{id} );
|
2019-03-22 15:56:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sub json_export {
|
|
|
|
|
my ($self) = @_;
|
2019-04-23 16:08:07 +00:00
|
|
|
|
my $uid = $self->current_user->{id};
|
|
|
|
|
|
|
|
|
|
my $db = $self->pg->db;
|
2019-03-22 15:56:49 +00:00
|
|
|
|
|
|
|
|
|
$self->render(
|
2019-04-23 16:08:07 +00:00
|
|
|
|
json => {
|
2020-07-27 16:53:22 +00:00
|
|
|
|
account => $db->select( 'users', '*', { id => $uid } )->hash,
|
2019-04-24 05:13:38 +00:00
|
|
|
|
in_transit => [
|
2019-05-03 19:14:49 +00:00
|
|
|
|
$db->select( 'in_transit_str', '*', { user_id => $uid } )
|
2019-04-24 05:13:38 +00:00
|
|
|
|
->hashes->each
|
|
|
|
|
],
|
2019-04-23 16:08:07 +00:00
|
|
|
|
journeys => [
|
2019-05-03 19:14:49 +00:00
|
|
|
|
$db->select( 'journeys_str', '*', { user_id => $uid } )
|
2019-04-23 16:08:07 +00:00
|
|
|
|
->hashes->each
|
|
|
|
|
],
|
|
|
|
|
}
|
2019-03-22 15:56:49 +00:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
1;
|