Refactor various parts in root app module

This commit is contained in:
Tom Bloor 2017-04-05 23:20:42 +01:00
parent a6e282ebb5
commit eba73ca903

View file

@ -3,11 +3,8 @@ package Pear::LocalLoop;
use Mojo::Base 'Mojolicious';
use Data::UUID;
use Mojo::JSON;
use Data::Dumper;
use Email::Valid;
use ORM::Date;
use Authen::Passphrase::BlowfishCrypt;
use Scalar::Util qw(looks_like_number);
use Pear::LocalLoop::Schema;
has schema => sub {
@ -22,7 +19,6 @@ has schema => sub {
sub startup {
my $self = shift;
$self->plugin('Config', {
default => {
sessionTimeSeconds => 60 * 60 * 24 * 7,
@ -32,12 +28,6 @@ $self->plugin( 'Config', {
});
my $config = $self->config;
my $sessionTimeSeconds = 60 * 60 * 24 * 7; #1 week.
my $sessionTokenJsonName = 'sessionToken';
my $sessionExpiresJsonName = 'sessionExpires';
# shortcut for use in template
$self->helper( db => sub { $self->app->schema->storage->dbh });
$self->helper( schema => sub { $self->app->schema });
@ -47,31 +37,22 @@ my $r = $self->routes;
$r = $r->any('/api') if $self->app->mode ne 'testing';
$r->post("/register")->to('register#post_register');
$r->post("/upload")->to('upload#post_upload');
$r->post("/search")->to('upload#post_search');
$r->post("/admin-approve")->to('admin#post_admin_approve');
$r->post("/admin-merge")->to('admin#post_admin_merge');
$r->get("/login")->to('auth#get_login');
$r->post("/login")->to('auth#post_login');
$r->post("/logout")->to('auth#post_logout');
$r->post("/edit")->to('api#post_edit');
$r->post("/fetchuser")->to('api#post_fetchuser');
$r->post("/user-history")->to('user#post_user_history');
$r->any( '/' => sub {
my $self = shift;
return $self->render(text => 'If you are seeing this, then the server is running.', success => Mojo::JSON->true);
return $self->render(json => { success => Mojo::JSON->true });
});
$self->hook( before_dispatch => sub {
my $self = shift;
@ -114,22 +95,19 @@ $self->hook( before_dispatch => sub {
$self->helper( is_admin => sub {
my ($self, $userId) = @_;
my ($rowCount) = $self->db->selectrow_array("SELECT COUNT(UserId) FROM Administrators WHERE UserId = ?", undef, ($userId));
return $rowCount != 0;
my ($c, $user_id) = @_;
my $admin = $c->schema->resultset('Administrator')->find($user_id);
return defined $admin;
});
$self->helper( create_hash => sub{
my ($self, $id, $name, $fullAddress, $postcode) = @_;
my $hash = {};
$hash->{'id'} = $id;
$hash->{'name'} = $name;
$hash->{'fullAddress'} = $fullAddress . ", " . $postcode;
return $hash;
return {
id => $id,
name => $name,
fullAddress => $fullAddress . ", " . $postcode,
}
});
@ -169,11 +147,11 @@ $self->helper(get_session_token => sub {
my $json = $self->req->json;
if (defined $json) {
$sessionToken = $json->{$sessionTokenJsonName};
$sessionToken = $json->{$self->app->config->{sessionTokenJsonName}};
}
if ( ! defined $sessionToken || $sessionToken eq "" ) {
$sessionToken = $self->session->{$sessionTokenJsonName};
$sessionToken = $self->session->{$self->app->config->{sessionTokenJsonName}};
}
if (defined $sessionToken && $sessionToken eq "" ) {
@ -195,9 +173,9 @@ $self->helper(generate_session => sub {
my $rowsAdded = $insertStatement->execute($sessionToken, $userId, $expireDateTime);
$self->session(expires => $expireDateTime);
$self->session->{$sessionTokenJsonName} = $sessionToken;
$self->session->{$self->app->config->{sessionTokenJsonName}} = $sessionToken;
return {$sessionTokenJsonName => $sessionToken, $sessionExpiresJsonName => $expireDateTime};
return {$self->app->config->{sessionTokenJsonName} => $sessionToken, $self->app->config->{sessionExpiresJsonName} => $expireDateTime};
});
$self->helper(generate_session_token => sub {
@ -214,8 +192,8 @@ $self->helper(expire_all_sessions => sub {
});
$self->helper(session_token_expiry_date_time => sub {
my $self = shift;
return time() + $sessionTimeSeconds;
my $c = shift;
return time() + $c->app->config->{sessionTimeSeconds};
});
$self->helper(remove_all_expired_sessions => sub {
@ -270,7 +248,7 @@ $self->helper(expire_current_session => sub {
my $rowsRemoved = $removeStatement->execute($sessionToken);
$self->session(expires => 1);
$self->session->{$sessionTokenJsonName} = $sessionToken;
$self->session->{$self->app->config->{sessionTokenJsonName}} = $sessionToken;
return $rowsRemoved != 0;
});
@ -330,7 +308,6 @@ $self->helper(set_token_as_used => sub {
my $statement = $self->db->prepare("UPDATE AccountTokens SET Used = 1 WHERE AccountTokenName = ? AND Used = 0 ");
my $rows = $statement->execute($token);
#print '-set_token_as_used-'.(Dumper($rows))."-\n";
return $rows != 0;
});