2017-02-24 19:27:43 +00:00
|
|
|
package Pear::LocalLoop;
|
|
|
|
|
|
|
|
use Mojo::Base 'Mojolicious';
|
|
|
|
use Data::UUID;
|
|
|
|
use Mojo::JSON;
|
2017-04-05 23:19:20 +00:00
|
|
|
use Scalar::Util qw(looks_like_number);
|
2017-02-24 19:27:43 +00:00
|
|
|
use Pear::LocalLoop::Schema;
|
2017-04-18 21:31:08 +00:00
|
|
|
use DateTime;
|
2017-02-24 19:27:43 +00:00
|
|
|
|
2017-04-05 21:43:08 +00:00
|
|
|
has schema => sub {
|
|
|
|
my $c = shift;
|
|
|
|
return Pear::LocalLoop::Schema->connect(
|
|
|
|
$c->app->config->{dsn},
|
|
|
|
$c->app->config->{user},
|
|
|
|
$c->app->config->{pass},
|
|
|
|
);
|
|
|
|
};
|
2017-02-24 19:27:43 +00:00
|
|
|
|
|
|
|
sub startup {
|
|
|
|
my $self = shift;
|
|
|
|
|
2017-04-05 22:20:42 +00:00
|
|
|
$self->plugin('Config', {
|
|
|
|
default => {
|
|
|
|
sessionTimeSeconds => 60 * 60 * 24 * 7,
|
2017-04-18 21:31:08 +00:00
|
|
|
sessionTokenJsonName => 'session_key',
|
2017-04-05 22:20:42 +00:00
|
|
|
sessionExpiresJsonName => 'sessionExpires',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
my $config = $self->config;
|
|
|
|
|
2017-04-08 17:25:34 +00:00
|
|
|
$self->plugin('Pear::LocalLoop::Plugin::Validators');
|
|
|
|
|
2017-04-06 22:12:28 +00:00
|
|
|
$self->plugin('Authentication' => {
|
|
|
|
'load_user' => sub {
|
|
|
|
my ( $c, $user_id ) = @_;
|
|
|
|
return $c->schema->resultset('User')->find($user_id);
|
|
|
|
},
|
|
|
|
'validate_user' => sub {
|
|
|
|
my ( $c, $email, $password, $args) = @_;
|
|
|
|
my $user = $c->schema->resultset('User')->find({email => $email});
|
2017-04-18 21:44:59 +00:00
|
|
|
if ( defined $user ) {
|
|
|
|
if ( $user->check_password( $password ) ) {
|
|
|
|
return $user->userid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return undef;
|
2017-04-06 22:12:28 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2017-04-05 22:20:42 +00:00
|
|
|
# shortcut for use in template
|
|
|
|
$self->helper( db => sub { $self->app->schema->storage->dbh });
|
|
|
|
$self->helper( schema => sub { $self->app->schema });
|
|
|
|
|
|
|
|
|
|
|
|
my $r = $self->routes;
|
2017-04-08 13:25:06 +00:00
|
|
|
$r->get('/')->to('root#index');
|
|
|
|
$r->post('/')->to('root#auth_login');
|
2017-04-08 17:25:34 +00:00
|
|
|
$r->get('/register')->to('register#index');
|
|
|
|
$r->post('/register')->to('register#register');
|
2017-04-08 12:38:54 +00:00
|
|
|
$r->any('/logout')->to('root#auth_logout');
|
2017-04-06 22:12:28 +00:00
|
|
|
|
2017-04-18 21:31:08 +00:00
|
|
|
# Always available api routes
|
|
|
|
$r->post('api/login')->to('api-auth#post_login');
|
|
|
|
$r->post('api/register')->to('api-register#post_register');
|
|
|
|
$r->post('api/logout')->to('api-auth#post_logout');
|
2017-04-06 22:12:28 +00:00
|
|
|
|
2017-04-18 21:31:08 +00:00
|
|
|
my $api = $r->under('/api')->to('api-auth#auth');
|
2017-04-06 22:12:28 +00:00
|
|
|
|
2017-04-18 21:31:08 +00:00
|
|
|
$api->post('/' => sub {
|
|
|
|
return shift->render( json => {
|
|
|
|
success => Mojo::JSON->true,
|
|
|
|
message => 'Successful Auth',
|
|
|
|
});
|
2017-04-05 22:20:42 +00:00
|
|
|
});
|
2017-04-18 21:31:08 +00:00
|
|
|
$api->post('/upload')->to('api-upload#post_upload');
|
|
|
|
$api->post('/search')->to('api-upload#post_search');
|
|
|
|
$api->post('/admin-approve')->to('api-admin#post_admin_approve');
|
|
|
|
$api->post('/admin-merge')->to('api-admin#post_admin_merge');
|
|
|
|
$api->post('/edit')->to('api-api#post_edit');
|
|
|
|
$api->post('/fetchuser')->to('api-api#post_fetchuser');
|
|
|
|
$api->post('/user-history')->to('api-user#post_user_history');
|
2017-02-24 19:27:43 +00:00
|
|
|
|
2017-04-06 22:12:28 +00:00
|
|
|
my $admin_routes = $r->under('/admin')->to('admin#under');
|
|
|
|
|
2017-04-08 13:25:06 +00:00
|
|
|
$admin_routes->get('/')->to('admin#home');
|
2017-04-08 01:32:13 +00:00
|
|
|
$admin_routes->get('/tokens')->to('admin-tokens#index');
|
|
|
|
$admin_routes->post('/tokens')->to('admin-tokens#create');
|
|
|
|
$admin_routes->get('/tokens/:id')->to('admin-tokens#read');
|
|
|
|
$admin_routes->post('/tokens/:id')->to('admin-tokens#update');
|
|
|
|
$admin_routes->post('/tokens/:id/delete')->to('admin-tokens#delete');
|
2017-04-18 09:44:07 +00:00
|
|
|
$admin_routes->get('/users')->to('admin-users#index');
|
|
|
|
$admin_routes->get('/users/:id')->to('admin-users#read');
|
|
|
|
$admin_routes->post('/users/:id')->to('admin-users#update');
|
|
|
|
$admin_routes->post('/users/:id/delete')->to('admin-users#delete');
|
2017-04-06 23:33:50 +00:00
|
|
|
|
2017-04-08 13:25:06 +00:00
|
|
|
my $user_routes = $r->under('/')->to('root#under');
|
|
|
|
|
|
|
|
$user_routes->get('/home')->to('root#home');
|
|
|
|
|
2017-04-18 21:44:59 +00:00
|
|
|
$self->hook( before_dispatch => sub {
|
|
|
|
my $self = shift;
|
2017-02-24 19:27:43 +00:00
|
|
|
|
2017-04-18 21:44:59 +00:00
|
|
|
$self->res->headers->header('Access-Control-Allow-Origin' => '*') if $self->app->mode eq 'development';
|
|
|
|
});
|
2017-02-24 19:27:43 +00:00
|
|
|
|
2017-04-05 22:20:42 +00:00
|
|
|
$self->helper( is_admin => sub {
|
|
|
|
my ($c, $user_id) = @_;
|
|
|
|
my $admin = $c->schema->resultset('Administrator')->find($user_id);
|
|
|
|
return defined $admin;
|
|
|
|
});
|
2017-02-24 19:27:43 +00:00
|
|
|
|
2017-04-05 22:20:42 +00:00
|
|
|
$self->helper( create_hash => sub{
|
|
|
|
my ($self, $id, $name, $fullAddress, $postcode) = @_;
|
2017-02-24 19:27:43 +00:00
|
|
|
|
2017-04-05 22:20:42 +00:00
|
|
|
return {
|
|
|
|
id => $id,
|
|
|
|
name => $name,
|
|
|
|
fullAddress => $fullAddress . ", " . $postcode,
|
|
|
|
}
|
|
|
|
});
|
2017-02-24 19:27:43 +00:00
|
|
|
|
2017-04-18 21:44:59 +00:00
|
|
|
$self->helper(get_active_user_id => sub {
|
|
|
|
my $self = shift;
|
2017-02-24 19:27:43 +00:00
|
|
|
|
2017-04-18 21:44:59 +00:00
|
|
|
my $token = $self->get_session_token();
|
|
|
|
if (! defined $token){
|
|
|
|
return undef;
|
|
|
|
}
|
2017-02-24 19:27:43 +00:00
|
|
|
|
2017-04-18 21:44:59 +00:00
|
|
|
my @out = $self->db->selectrow_array("SELECT UserIdAssignedTo_FK FROM SessionTokens WHERE SessionTokenName = ?",undef,($token));
|
|
|
|
if (! @out){
|
|
|
|
return undef;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
return $out[0];
|
|
|
|
}
|
|
|
|
});
|
2017-02-24 19:27:43 +00:00
|
|
|
|
2017-04-18 21:44:59 +00:00
|
|
|
$self->helper(get_session_token => sub {
|
|
|
|
my $self = shift;
|
2017-02-24 19:27:43 +00:00
|
|
|
|
2017-04-18 21:44:59 +00:00
|
|
|
#See if logged in.
|
|
|
|
my $sessionToken = undef;
|
2017-02-24 19:27:43 +00:00
|
|
|
|
2017-04-18 21:44:59 +00:00
|
|
|
my $json = $self->req->json;
|
|
|
|
if (defined $json) {
|
|
|
|
$sessionToken = $json->{$self->app->config->{sessionTokenJsonName}};
|
|
|
|
}
|
2017-02-24 19:27:43 +00:00
|
|
|
|
2017-04-18 21:44:59 +00:00
|
|
|
if ( ! defined $sessionToken || $sessionToken eq "" ) {
|
|
|
|
$sessionToken = $self->session->{$self->app->config->{sessionTokenJsonName}};
|
|
|
|
}
|
2017-02-24 19:27:43 +00:00
|
|
|
|
2017-04-18 21:44:59 +00:00
|
|
|
if (defined $sessionToken && $sessionToken eq "" ) {
|
|
|
|
$sessionToken = undef;
|
|
|
|
}
|
2017-02-24 19:27:43 +00:00
|
|
|
|
2017-04-18 21:44:59 +00:00
|
|
|
return $sessionToken;
|
|
|
|
});
|
2017-02-24 19:27:43 +00:00
|
|
|
|
|
|
|
|
2017-04-18 21:44:59 +00:00
|
|
|
#This assumes the user has no current session on that device.
|
|
|
|
$self->helper(generate_session => sub {
|
|
|
|
my ($self, $userId) = @_;
|
2017-02-24 19:27:43 +00:00
|
|
|
|
2017-04-18 21:44:59 +00:00
|
|
|
my $sessionToken = $self->generate_session_token();
|
2017-02-24 19:27:43 +00:00
|
|
|
|
2017-04-18 21:44:59 +00:00
|
|
|
my $insertStatement = $self->db->prepare('INSERT INTO SessionTokens (SessionTokenName, UserIdAssignedTo_FK, ExpireDateTime) VALUES (?, ?, ?)');
|
|
|
|
my $rowsAdded = $insertStatement->execute($sessionToken, $userId, DateTime->now()->add( years => 1 ));
|
|
|
|
|
|
|
|
return $sessionToken;
|
2017-04-18 17:31:10 +00:00
|
|
|
});
|
2017-02-24 19:27:43 +00:00
|
|
|
|
2017-04-18 21:44:59 +00:00
|
|
|
$self->helper(generate_session_token => sub {
|
|
|
|
my $self = shift;
|
|
|
|
return Data::UUID->new->create_str();
|
2017-04-05 23:19:20 +00:00
|
|
|
});
|
2017-02-24 19:27:43 +00:00
|
|
|
|
2017-04-05 23:19:20 +00:00
|
|
|
$self->helper(does_organisational_id_exist => sub {
|
|
|
|
my ( $c, $org_id ) = @_;
|
|
|
|
return defined $c->schema->resultset('Organisation')->find({ organisationalid => $org_id });
|
|
|
|
});
|
2017-02-24 19:27:43 +00:00
|
|
|
|
2017-04-05 23:19:20 +00:00
|
|
|
$self->helper(get_age_foreign_key => sub {
|
|
|
|
my ( $c, $age_string ) = @_;
|
|
|
|
my $age_range = $c->schema->resultset('AgeRange')->find({ agerangestring => $age_string });
|
|
|
|
return defined $age_range ? $age_range->agerangeid : undef;
|
|
|
|
});
|
2017-02-24 19:27:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|