move get_api_token to users model

This commit is contained in:
Daniel Friesel 2023-01-22 11:34:53 +01:00
parent af82c46d57
commit 59e9a24aa6
No known key found for this signature in database
GPG key ID: 100D5BFB5166E005
4 changed files with 33 additions and 37 deletions

View file

@ -169,11 +169,6 @@ sub startup {
}; };
} }
); );
$self->attr(
token_types => sub {
return [qw(status history travel import)];
}
);
$self->attr( $self->attr(
account_public_mask => sub { account_public_mask => sub {
@ -839,27 +834,6 @@ sub startup {
} }
); );
$self->helper(
'get_api_token' => sub {
my ( $self, $uid ) = @_;
$uid //= $self->current_user->{id};
my $token = {};
my $res = $self->pg->db->select(
'tokens',
[ 'type', 'token' ],
{ user_id => $uid }
);
for my $entry ( $res->hashes->each ) {
$token->{ $self->app->token_types->[ $entry->{type} - 1 ] }
= $entry->{token};
}
return $token;
}
);
$self->helper( $self->helper(
'run_hook' => sub { 'run_hook' => sub {
my ( $self, $uid, $reason, $callback ) = @_; my ( $self, $uid, $reason, $callback ) = @_;

View file

@ -388,10 +388,11 @@ sub verify {
sub delete { sub delete {
my ($self) = @_; my ($self) = @_;
my $uid = $self->current_user->{id};
if ( $self->validation->csrf_protect->has_error('csrf_token') ) { if ( $self->validation->csrf_protect->has_error('csrf_token') ) {
$self->render( $self->render(
'account', 'account',
api_token => $self->get_api_token, api_token => $self->users->get_api_token( uid => $uid ),
invalid => 'csrf', invalid => 'csrf',
); );
return; return;
@ -407,15 +408,15 @@ sub delete {
{ {
$self->render( $self->render(
'account', 'account',
api_token => $self->get_api_token, api_token => $self->users->get_api_token( uid => $uid ),
invalid => 'deletion password' invalid => 'deletion password'
); );
return; return;
} }
$self->users->flag_deletion( uid => $self->current_user->{id} ); $self->users->flag_deletion( uid => $uid );
} }
else { else {
$self->users->unflag_deletion( uid => $self->current_user->{id} ); $self->users->unflag_deletion( uid => $uid );
} }
$self->redirect_to('account'); $self->redirect_to('account');
} }
@ -950,9 +951,11 @@ sub confirm_mail {
sub account { sub account {
my ($self) = @_; my ($self) = @_;
my $uid = $self->current_user->{id};
$self->render( 'account', api_token => $self->get_api_token ); $self->render( 'account',
$self->users->mark_seen( uid => $self->current_user->{id} ); api_token => $self->users->get_api_token( uid => $uid ) );
$self->users->mark_seen( uid => $uid );
} }
sub json_export { sub json_export {

View file

@ -35,10 +35,11 @@ sub documentation {
my ($self) = @_; my ($self) = @_;
if ( $self->is_user_authenticated ) { if ( $self->is_user_authenticated ) {
my $uid = $self->current_user->{id};
$self->render( $self->render(
'api_documentation', 'api_documentation',
uid => $self->current_user->{id}, uid => $uid,
api_token => $self->get_api_token, api_token => $self->users->get_api_token( uid => $uid ),
); );
} }
else { else {
@ -79,7 +80,7 @@ sub get_v1 {
return; return;
} }
my $token = $self->get_api_token($uid); my $token = $self->users->get_api_token( uid => $uid );
if ( not $api_token if ( not $api_token
or not $token->{$api_action} or not $token->{$api_action}
or $api_token ne $token->{$api_action} ) or $api_token ne $token->{$api_action} )
@ -145,7 +146,7 @@ sub travel_v1 {
return; return;
} }
my $token = $self->get_api_token($uid); my $token = $self->users->get_api_token( uid => $uid );
if ( not $token->{'travel'} or $api_token ne $token->{'travel'} ) { if ( not $token->{'travel'} or $api_token ne $token->{'travel'} ) {
$self->render( $self->render(
json => { json => {
@ -411,7 +412,7 @@ sub import_v1 {
return; return;
} }
my $token = $self->get_api_token($uid); my $token = $self->users->get_api_token($uid);
if ( not $token->{'import'} or $api_token ne $token->{'import'} ) { if ( not $token->{'import'} or $api_token ne $token->{'import'} ) {
$self->render( $self->render(
json => { json => {

View file

@ -19,6 +19,8 @@ my @sb_templates = (
[ 'bahn.expert/regional', 'https://bahn.expert/regional/{name}#{id}' ], [ 'bahn.expert/regional', 'https://bahn.expert/regional/{name}#{id}' ],
); );
my @token_types = (qw(status history travel import));
sub new { sub new {
my ( $class, %opt ) = @_; my ( $class, %opt ) = @_;
@ -86,6 +88,22 @@ sub verify_registration_token {
return; return;
} }
sub get_api_token {
my ( $self, %opt ) = @_;
my $db = $opt{db} // $self->{pg}->db;
my $uid = $opt{uid};
my $token = {};
my $res = $db->select( 'tokens', [ 'type', 'token' ], { user_id => $uid } );
for my $entry ( $res->hashes->each ) {
$token->{ $token_types[ $entry->{type} - 1 ] }
= $entry->{token};
}
return $token;
}
sub get_uid_by_name_and_mail { sub get_uid_by_name_and_mail {
my ( $self, %opt ) = @_; my ( $self, %opt ) = @_;
my $db = $opt{db} // $self->{pg}->db; my $db = $opt{db} // $self->{pg}->db;