summaryrefslogtreecommitdiff
path: root/lib/Travelynx.pm
diff options
context:
space:
mode:
authorDaniel Friesel <derf@finalrewind.org>2020-07-27 18:53:22 +0200
committerDaniel Friesel <derf@finalrewind.org>2020-07-27 18:53:22 +0200
commitf08bdaca5cafc6840cbf8489d7790656bf38f9e4 (patch)
treee0fa4c3c68f19f600f4febe7a54ede46c25a07d2 /lib/Travelynx.pm
parentcdb7469f00258aac6fb96b93b2cea2780a30d06e (diff)
Move user model to a separate module
Diffstat (limited to 'lib/Travelynx.pm')
-rwxr-xr-xlib/Travelynx.pm413
1 files changed, 11 insertions, 402 deletions
diff --git a/lib/Travelynx.pm b/lib/Travelynx.pm
index 285cfef..378a2ca 100755
--- a/lib/Travelynx.pm
+++ b/lib/Travelynx.pm
@@ -19,6 +19,7 @@ use Travel::Status::DE::DBWagenreihung;
use Travel::Status::DE::IRIS;
use Travel::Status::DE::IRIS::Stations;
use Travelynx::Helper::Sendmail;
+use Travelynx::Model::Users;
use XML::LibXML;
sub check_password {
@@ -93,7 +94,8 @@ sub startup {
},
validate_user => sub {
my ( $self, $username, $password, $extradata ) = @_;
- my $user_info = $self->get_user_password($username);
+ my $user_info
+ = $self->users->get_login_data( name => $username );
if ( not $user_info ) {
return undef;
}
@@ -271,6 +273,13 @@ sub startup {
);
$self->helper(
+ users => sub {
+ my ($self) = @_;
+ state $users = Travelynx::Model::Users->new( pg => $self->pg );
+ }
+ );
+
+ $self->helper(
pg => sub {
my ($self) = @_;
my $config = $self->app->config;
@@ -919,18 +928,6 @@ sub startup {
);
$self->helper(
- 'mark_seen' => sub {
- my ( $self, $uid ) = @_;
-
- $self->pg->db->update(
- 'users',
- { last_seen => DateTime->now( time_zone => 'Europe/Berlin' ) },
- { id => $uid }
- );
- }
- );
-
- $self->helper(
'update_in_transit_comment' => sub {
my ( $self, $comment, $uid ) = @_;
$uid //= $self->current_user->{id};
@@ -1151,197 +1148,6 @@ sub startup {
}
);
- $self->helper(
- 'verify_registration_token' => sub {
- my ( $self, $uid, $token ) = @_;
-
- my $db = $self->pg->db;
- my $tx = $db->begin;
-
- my $res = $db->select(
- 'pending_registrations',
- 'count(*) as count',
- {
- user_id => $uid,
- token => $token
- }
- );
-
- if ( $res->hash->{count} ) {
- $db->update( 'users', { status => 1 }, { id => $uid } );
- $db->delete( 'pending_registrations', { user_id => $uid } );
- $tx->commit;
- return 1;
- }
- return;
- }
- );
-
- $self->helper(
- 'get_uid_by_name_and_mail' => sub {
- my ( $self, $name, $email ) = @_;
-
- my $res = $self->pg->db->select(
- 'users',
- ['id'],
- {
- name => $name,
- email => $email,
- status => 1
- }
- );
-
- if ( my $user = $res->hash ) {
- return $user->{id};
- }
- return;
- }
- );
-
- $self->helper(
- 'get_privacy_by_name' => sub {
- my ( $self, $name ) = @_;
-
- my $res = $self->pg->db->select(
- 'users',
- [ 'id', 'public_level' ],
- {
- name => $name,
- status => 1
- }
- );
-
- if ( my $user = $res->hash ) {
- return $user;
- }
- return;
- }
- );
-
- $self->helper(
- 'set_privacy' => sub {
- my ( $self, $uid, $public_level ) = @_;
-
- $self->pg->db->update(
- 'users',
- { public_level => $public_level },
- { id => $uid }
- );
- }
- );
-
- $self->helper(
- 'mark_for_password_reset' => sub {
- my ( $self, $db, $uid, $token ) = @_;
-
- my $res = $db->select(
- 'pending_passwords',
- 'count(*) as count',
- { user_id => $uid }
- );
- if ( $res->hash->{count} ) {
- return 'in progress';
- }
-
- $db->insert(
- 'pending_passwords',
- {
- user_id => $uid,
- token => $token,
- requested_at =>
- DateTime->now( time_zone => 'Europe/Berlin' )
- }
- );
-
- return undef;
- }
- );
-
- $self->helper(
- 'verify_password_token' => sub {
- my ( $self, $uid, $token ) = @_;
-
- my $res = $self->pg->db->select(
- 'pending_passwords',
- 'count(*) as count',
- {
- user_id => $uid,
- token => $token
- }
- );
-
- if ( $res->hash->{count} ) {
- return 1;
- }
- return;
- }
- );
-
- $self->helper(
- 'mark_for_mail_change' => sub {
- my ( $self, $db, $uid, $email, $token ) = @_;
-
- $db->insert(
- 'pending_mails',
- {
- user_id => $uid,
- email => $email,
- token => $token,
- requested_at =>
- DateTime->now( time_zone => 'Europe/Berlin' )
- },
- {
- on_conflict => \
-'(user_id) do update set email = EXCLUDED.email, token = EXCLUDED.token, requested_at = EXCLUDED.requested_at'
- },
- );
- }
- );
-
- $self->helper(
- 'change_mail_with_token' => sub {
- my ( $self, $uid, $token ) = @_;
-
- my $db = $self->pg->db;
- my $tx = $db->begin;
-
- my $res_h = $db->select(
- 'pending_mails',
- ['email'],
- {
- user_id => $uid,
- token => $token
- }
- )->hash;
-
- if ($res_h) {
- $db->update(
- 'users',
- { email => $res_h->{email} },
- { id => $uid }
- );
- $db->delete( 'pending_mails', { user_id => $uid } );
- $tx->commit;
- return 1;
- }
- return;
- }
- );
-
- $self->helper(
- 'remove_password_token' => sub {
- my ( $self, $uid, $token ) = @_;
-
- $self->pg->db->delete(
- 'pending_passwords',
- {
- user_id => $uid,
- token => $token
- }
- );
- }
- );
-
# This helper should only be called directly when also providing a user ID.
# If you don't have one, use current_user() instead (get_user_data will
# delegate to it anyways).
@@ -1351,39 +1157,7 @@ sub startup {
$uid //= $self->current_user->{id};
- my $user_data = $self->pg->db->select(
- 'users',
- 'id, name, status, public_level, email, '
- . 'extract(epoch from registered_at) as registered_at_ts, '
- . 'extract(epoch from last_seen) as last_seen_ts, '
- . 'extract(epoch from deletion_requested) as deletion_requested_ts',
- { id => $uid }
- )->hash;
-
- if ($user_data) {
- return {
- id => $user_data->{id},
- name => $user_data->{name},
- status => $user_data->{status},
- is_public => $user_data->{public_level},
- email => $user_data->{email},
- registered_at => DateTime->from_epoch(
- epoch => $user_data->{registered_at_ts},
- time_zone => 'Europe/Berlin'
- ),
- last_seen => DateTime->from_epoch(
- epoch => $user_data->{last_seen_ts},
- time_zone => 'Europe/Berlin'
- ),
- deletion_requested => $user_data->{deletion_requested_ts}
- ? DateTime->from_epoch(
- epoch => $user_data->{deletion_requested_ts},
- time_zone => 'Europe/Berlin'
- )
- : undef,
- };
- }
- return undef;
+ return $self->users->get_data( uid => $uid );
}
);
@@ -1535,153 +1309,6 @@ sub startup {
);
$self->helper(
- 'get_user_password' => sub {
- my ( $self, $name ) = @_;
-
- my $res_h = $self->pg->db->select(
- 'users',
- 'id, name, status, password as password_hash',
- { name => $name }
- )->hash;
-
- return $res_h;
- }
- );
-
- $self->helper(
- 'add_user' => sub {
- my ( $self, $db, $user_name, $email, $token, $password ) = @_;
-
- # This helper must be called during a transaction, as user creation
- # may fail even after the database entry has been generated, e.g. if
- # the registration mail cannot be sent. We therefore use $db (the
- # database handle performing the transaction) instead of $self->pg->db
- # (which may be a new handle not belonging to the transaction).
-
- my $now = DateTime->now( time_zone => 'Europe/Berlin' );
-
- my $res = $db->insert(
- 'users',
- {
- name => $user_name,
- status => 0,
- public_level => 0,
- email => $email,
- password => $password,
- registered_at => $now,
- last_seen => $now,
- },
- { returning => 'id' }
- );
- my $uid = $res->hash->{id};
-
- $db->insert(
- 'pending_registrations',
- {
- user_id => $uid,
- token => $token
- }
- );
-
- return $uid;
- }
- );
-
- $self->helper(
- 'flag_user_deletion' => sub {
- my ( $self, $uid ) = @_;
-
- my $now = DateTime->now( time_zone => 'Europe/Berlin' );
-
- $self->pg->db->update(
- 'users',
- { deletion_requested => $now },
- {
- id => $uid,
- }
- );
- }
- );
-
- $self->helper(
- 'unflag_user_deletion' => sub {
- my ( $self, $uid ) = @_;
-
- $self->pg->db->update(
- 'users',
- {
- deletion_requested => undef,
- },
- {
- id => $uid,
- }
- );
- }
- );
-
- $self->helper(
- 'set_user_password' => sub {
- my ( $self, $uid, $password ) = @_;
-
- $self->pg->db->update(
- 'users',
- { password => $password },
- { id => $uid }
- );
- }
- );
-
- $self->helper(
- 'check_if_user_name_exists' => sub {
- my ( $self, $user_name ) = @_;
-
- my $count = $self->pg->db->select(
- 'users',
- 'count(*) as count',
- { name => $user_name }
- )->hash->{count};
-
- if ($count) {
- return 1;
- }
- return 0;
- }
- );
-
- $self->helper(
- 'check_if_mail_is_blacklisted' => sub {
- my ( $self, $mail ) = @_;
-
- my $count = $self->pg->db->select(
- 'users',
- 'count(*) as count',
- {
- email => $mail,
- status => 0,
- }
- )->hash->{count};
-
- if ($count) {
- return 1;
- }
-
- $count = $self->pg->db->select(
- 'mail_blacklist',
- 'count(*) as count',
- {
- email => $mail,
- num_tries => { '>', 1 },
- }
- )->hash->{count};
-
- if ($count) {
- return 1;
- }
- return 0;
- }
- );
-
- $self->helper(
'delete_journey' => sub {
my ( $self, $journey_id, $checkin_epoch, $checkout_epoch ) = @_;
my $uid = $self->current_user->{id};
@@ -2911,24 +2538,6 @@ sub startup {
);
$self->helper(
- 'account_use_history' => sub {
- my ( $self, $uid, $value ) = @_;
-
- if ($value) {
- $self->pg->db->update(
- 'users',
- { use_history => $value },
- { id => $uid }
- );
- }
- else {
- return $self->pg->db->select( 'users', ['use_history'],
- { id => $uid } )->hash->{use_history};
- }
- }
- );
-
- $self->helper(
'get_user_travels' => sub {
my ( $self, %opt ) = @_;