use utf8;
package Hydra::Schema::Result::Users;
use strict;
use warnings;
use base 'DBIx::Class::Core';
__PACKAGE__->load_components("+Hydra::Component::ToJSON");
__PACKAGE__->table("users");
__PACKAGE__->add_columns(
"username",
{ data_type => "text", is_nullable => 0 },
"fullname",
{ data_type => "text", is_nullable => 1 },
"emailaddress",
{ data_type => "text", is_nullable => 0 },
"password",
{ data_type => "text", is_nullable => 0 },
"emailonerror",
{ data_type => "integer", default_value => 0, is_nullable => 0 },
"type",
{ data_type => "text", default_value => "hydra", is_nullable => 0 },
"publicdashboard",
{ data_type => "boolean", default_value => \"false", is_nullable => 0 },
);
__PACKAGE__->set_primary_key("username");
__PACKAGE__->has_many(
"newsitems",
"Hydra::Schema::Result::NewsItems",
{ "foreign.author" => "self.username" },
undef,
);
__PACKAGE__->has_many(
"projectmembers",
"Hydra::Schema::Result::ProjectMembers",
{ "foreign.username" => "self.username" },
undef,
);
__PACKAGE__->has_many(
"projects_2s",
"Hydra::Schema::Result::Projects",
{ "foreign.owner" => "self.username" },
undef,
);
__PACKAGE__->has_many(
"starredjobs",
"Hydra::Schema::Result::StarredJobs",
{ "foreign.username" => "self.username" },
undef,
);
__PACKAGE__->has_many(
"userroles",
"Hydra::Schema::Result::UserRoles",
{ "foreign.username" => "self.username" },
undef,
);
__PACKAGE__->many_to_many("projects", "projectmembers", "project");
use Crypt::Passphrase;
use Digest::SHA1 qw(sha1_hex);
use String::Compare::ConstantTime;
my %hint = (
columns => [
"fullname",
"emailaddress",
"username"
],
relations => {
userroles => "role"
}
);
sub json_hint {
return \%hint;
}
sub _authenticator() {
my $authenticator = Crypt::Passphrase->new(
encoder => 'Argon2',
validators => [
(sub {
my ($password, $hash) = @_;
return String::Compare::ConstantTime::equals($hash, sha1_hex($password));
})
],
);
return $authenticator;
}
sub check_password {
my ($self, $password) = @_;
my $authenticator = _authenticator();
if ($authenticator->verify_password($password, $self->password)) {
if ($authenticator->needs_rehash($self->password)) {
$self->setPassword($password);
}
return 1;
} elsif ($authenticator->verify_password(sha1_hex($password), $self->password)) {
$self->setPassword($password);
return 1;
} else {
return 0;
}
}
sub setPassword {
my ($self, $password) = @_;;
$self->update({
"password" => _authenticator()->hash_password($password),
});
}
sub setPasswordHash {
my ($self, $passwordHash) = @_;;
if ($passwordHash =~ /^[a-f0-9]{40}$/) {
$self->setPassword($passwordHash);
} else {
$self->update({ password => $passwordHash });
}
}
1;