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,84 +255,66 @@ $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 ( $c, $token ) = @_;
my ( $self, $token ) = @_; return defined $c->schema->resultset('AccountToken')->find({
accounttokenname => $token,
used => 0,
});
});
my ( $out ) = $self->db->selectrow_array("SELECT COUNT(AccountTokenId) FROM AccountTokens WHERE AccountTokenName = ? AND Used = 0", undef, ($token)); $self->helper(does_organisational_id_exist => sub {
my ( $c, $org_id ) = @_;
return defined $c->schema->resultset('Organisation')->find({ organisationalid => $org_id });
});
return $out != 0; $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;
});
}); $self->helper(get_userid_foreign_key => sub {
my ( $c, $email ) = @_;
my $user = $c->schema->resultset('User')->find({ email => $email });
return defined $user ? $user->userid : undef;
});
#Return true if and only if the token exists and has not been used. $self->helper(does_username_exist => sub {
$self->helper(does_organisational_id_exist => sub { my ( $c, $username ) = @_;
my ( $self, $organisationalId ) = @_; return defined $c->schema->resultset('Customer')->find({ username => $username });
});
my ( $out ) = $self->db->selectrow_array("SELECT COUNT(OrganisationalId) FROM Organisations WHERE OrganisationalId = ?", undef, ($organisationalId)); $self->helper(does_email_exist => sub {
return $out != 0; my ( $c, $email ) = @_;
}); return defined $c->schema->resultset('User')->find({ email => $email });
});
$self->helper(get_age_foreign_key => sub { $self->helper(set_token_as_used => sub {
my ( $self, $ageString ) = @_; my ( $c, $token ) = @_;
return defined $c->schema->resultset('AccountToken')->find({
my ($out) = $self->db->selectrow_array("SELECT AgeRangeId FROM AgeRanges WHERE AgeRangeString = ?", undef, ($ageString)); accounttokenname => $token,
return $out; used => 0,
}); })->update({ used => 1 });
});
$self->helper(get_userid_foreign_key => sub {
my ( $self, $email ) = @_;
my ($out) = $self->db->selectrow_array("SELECT UserId FROM Users WHERE Email = ?", undef, ($email));
return $out;
});
$self->helper(does_username_exist => sub {
my ( $self, $username ) = @_;
my ($out) = $self->db->selectrow_array("SELECT COUNT(UserName) FROM Customers WHERE UserName = ?", {}, ($username));
return $out != 0;
});
$self->helper(does_email_exist => sub {
my ( $self, $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 {
my ( $self, $token ) = @_;
#Return true if and only if the token exists and has not been used.
my $statement = $self->db->prepare("UPDATE AccountTokens SET Used = 1 WHERE AccountTokenName = ? AND Used = 0 ");
my $rows = $statement->execute($token);
return $rows != 0;
});
$self->helper(generate_hashed_password => sub {
my ( $self, $password ) = @_;
$self->helper(generate_hashed_password => sub {
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.
$self->helper(check_password_email => sub {
# We assume the user already exists. my ( $c, $email, $password ) = @_;
$self->helper(check_password_email => sub{ my $user = $c->schema->resultset('User')->find({ email => $email });
my ( $self, $email, $password) = @_; my $ppr = Authen::Passphrase::BlowfishCrypt->from_crypt($user->hashedpassword);
my ($hashedPassword) = $self->db->selectrow_array("SELECT HashedPassword FROM Users WHERE Email = ?", undef, ($email));
my $ppr = Authen::Passphrase::BlowfishCrypt->from_crypt($hashedPassword);
return $ppr->match($password); return $ppr->match($password);
}); });
} }