Refactored more helpers to use DBIC

This commit is contained in:
Tom Bloor 2017-04-06 00:19:20 +01:00
parent ce038fd46d
commit 8bda8dba73

View file

@ -4,7 +4,9 @@ use Mojo::Base 'Mojolicious';
use Data::UUID; use Data::UUID;
use Mojo::JSON; use Mojo::JSON;
use Email::Valid; use Email::Valid;
use ORM::Date;
use Authen::Passphrase::BlowfishCrypt; use Authen::Passphrase::BlowfishCrypt;
use Scalar::Util qw(looks_like_number);
use Pear::LocalLoop::Schema; use Pear::LocalLoop::Schema;
has schema => sub { has schema => sub {
@ -253,82 +255,64 @@ $self->helper(expire_current_session => sub {
return $rowsRemoved != 0; return $rowsRemoved != 0;
}); });
#Return true if and only if the token exists and has not been used.
$self->helper(is_token_unused => sub { $self->helper(is_token_unused => sub {
my ( $self, $token ) = @_; my ( $c, $token ) = @_;
return defined $c->schema->resultset('AccountToken')->find({
my ( $out ) = $self->db->selectrow_array("SELECT COUNT(AccountTokenId) FROM AccountTokens WHERE AccountTokenName = ? AND Used = 0", undef, ($token)); accounttokenname => $token,
used => 0,
return $out != 0; });
}); });
#Return true if and only if the token exists and has not been used.
$self->helper(does_organisational_id_exist => sub { $self->helper(does_organisational_id_exist => sub {
my ( $self, $organisationalId ) = @_; my ( $c, $org_id ) = @_;
return defined $c->schema->resultset('Organisation')->find({ organisationalid => $org_id });
my ( $out ) = $self->db->selectrow_array("SELECT COUNT(OrganisationalId) FROM Organisations WHERE OrganisationalId = ?", undef, ($organisationalId));
return $out != 0;
}); });
$self->helper(get_age_foreign_key => sub { $self->helper(get_age_foreign_key => sub {
my ( $self, $ageString ) = @_; my ( $c, $age_string ) = @_;
my $age_range = $c->schema->resultset('AgeRange')->find({ agerangestring => $age_string });
my ($out) = $self->db->selectrow_array("SELECT AgeRangeId FROM AgeRanges WHERE AgeRangeString = ?", undef, ($ageString)); return defined $age_range ? $age_range->agerangeid : undef;
return $out;
}); });
$self->helper(get_userid_foreign_key => sub { $self->helper(get_userid_foreign_key => sub {
my ( $self, $email ) = @_; my ( $c, $email ) = @_;
my $user = $c->schema->resultset('User')->find({ email => $email });
my ($out) = $self->db->selectrow_array("SELECT UserId FROM Users WHERE Email = ?", undef, ($email)); return defined $user ? $user->userid : undef;
return $out;
}); });
$self->helper(does_username_exist => sub { $self->helper(does_username_exist => sub {
my ( $self, $username ) = @_; my ( $c, $username ) = @_;
return defined $c->schema->resultset('Customer')->find({ username => $username });
my ($out) = $self->db->selectrow_array("SELECT COUNT(UserName) FROM Customers WHERE UserName = ?", {}, ($username));
return $out != 0;
}); });
$self->helper(does_email_exist => sub { $self->helper(does_email_exist => sub {
my ( $self, $email ) = @_; my ( $c, $email ) = @_;
return defined $c->schema->resultset('User')->find({ email => $email });
my ($out) = $self->db->selectrow_array("SELECT COUNT(Email) FROM Users WHERE Email = ?", {}, ($email));
return $out != 0;
}); });
$self->helper(set_token_as_used => sub { $self->helper(set_token_as_used => sub {
my ( $self, $token ) = @_; my ( $c, $token ) = @_;
return defined $c->schema->resultset('AccountToken')->find({
#Return true if and only if the token exists and has not been used. accounttokenname => $token,
my $statement = $self->db->prepare("UPDATE AccountTokens SET Used = 1 WHERE AccountTokenName = ? AND Used = 0 "); used => 0,
my $rows = $statement->execute($token); })->update({ used => 1 });
return $rows != 0;
}); });
$self->helper(generate_hashed_password => sub { $self->helper(generate_hashed_password => sub {
my ( $self, $password ) = @_; my ( $c, $password ) = @_;
my $ppr = Authen::Passphrase::BlowfishCrypt->new( my $ppr = Authen::Passphrase::BlowfishCrypt->new(
cost => 8, salt_random => 1, cost => 8,
passphrase => $password); salt_random => 1,
passphrase => $password,
);
return $ppr->as_crypt; return $ppr->as_crypt;
}); });
# We assume the user already exists. # We assume the user already exists.
$self->helper(check_password_email => sub { $self->helper(check_password_email => sub {
my ( $self, $email, $password) = @_; my ( $c, $email, $password ) = @_;
my $user = $c->schema->resultset('User')->find({ email => $email });
my ($hashedPassword) = $self->db->selectrow_array("SELECT HashedPassword FROM Users WHERE Email = ?", undef, ($email)); my $ppr = Authen::Passphrase::BlowfishCrypt->from_crypt($user->hashedpassword);
my $ppr = Authen::Passphrase::BlowfishCrypt->from_crypt($hashedPassword);
return $ppr->match($password); return $ppr->match($password);
}); });