2017-02-24 19:27:43 +00:00
|
|
|
package Pear::LocalLoop::Schema::Result::User;
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
|
|
|
use base 'DBIx::Class::Core';
|
|
|
|
|
2017-04-21 21:12:53 +00:00
|
|
|
use Data::UUID;
|
|
|
|
|
2021-03-20 12:09:50 +00:00
|
|
|
__PACKAGE__->load_components(
|
|
|
|
qw/
|
|
|
|
InflateColumn::DateTime
|
|
|
|
PassphraseColumn
|
|
|
|
TimeStamp
|
|
|
|
FilterColumn
|
|
|
|
/
|
|
|
|
);
|
2017-02-24 19:27:43 +00:00
|
|
|
|
2017-04-21 19:52:14 +00:00
|
|
|
__PACKAGE__->table("users");
|
2017-02-24 19:27:43 +00:00
|
|
|
|
|
|
|
__PACKAGE__->add_columns(
|
2021-03-20 12:09:50 +00:00
|
|
|
"id" => {
|
|
|
|
data_type => "integer",
|
|
|
|
is_auto_increment => 1,
|
|
|
|
is_nullable => 0,
|
|
|
|
},
|
|
|
|
"entity_id" => {
|
|
|
|
data_type => "integer",
|
|
|
|
is_foreign_key => 1,
|
|
|
|
is_nullable => 0,
|
|
|
|
},
|
|
|
|
"email" => {
|
|
|
|
data_type => "text",
|
|
|
|
is_nullable => 0,
|
|
|
|
},
|
|
|
|
"join_date" => {
|
|
|
|
data_type => "datetime",
|
|
|
|
set_on_create => 1,
|
|
|
|
},
|
|
|
|
"password" => {
|
|
|
|
data_type => "varchar",
|
|
|
|
is_nullable => 0,
|
|
|
|
size => 100,
|
|
|
|
passphrase => 'crypt',
|
|
|
|
passphrase_class => 'BlowfishCrypt',
|
|
|
|
passphrase_args => {
|
|
|
|
salt_random => 1,
|
|
|
|
cost => 8,
|
|
|
|
},
|
|
|
|
passphrase_check_method => 'check_password',
|
|
|
|
},
|
|
|
|
"is_admin" => {
|
|
|
|
data_type => "boolean",
|
|
|
|
default_value => \"false",
|
|
|
|
is_nullable => 0,
|
2017-04-18 21:31:08 +00:00
|
|
|
},
|
2017-02-24 19:27:43 +00:00
|
|
|
);
|
|
|
|
|
2017-04-21 19:52:14 +00:00
|
|
|
__PACKAGE__->set_primary_key("id");
|
2017-02-24 19:27:43 +00:00
|
|
|
|
2021-03-20 12:09:50 +00:00
|
|
|
__PACKAGE__->add_unique_constraint( ["email"] );
|
2017-02-24 19:27:43 +00:00
|
|
|
|
2021-03-20 12:09:50 +00:00
|
|
|
__PACKAGE__->belongs_to( "entity", "Pear::LocalLoop::Schema::Result::Entity",
|
|
|
|
"entity_id", );
|
2017-02-24 19:27:43 +00:00
|
|
|
|
|
|
|
__PACKAGE__->has_many(
|
2021-03-20 12:09:50 +00:00
|
|
|
"session_tokens",
|
|
|
|
"Pear::LocalLoop::Schema::Result::SessionToken",
|
|
|
|
{ "foreign.user_id" => "self.id" },
|
|
|
|
{ cascade_copy => 0, cascade_delete => 0 },
|
2017-02-24 19:27:43 +00:00
|
|
|
);
|
|
|
|
|
2020-11-08 15:15:28 +00:00
|
|
|
__PACKAGE__->has_many(
|
2021-03-20 12:09:50 +00:00
|
|
|
"device_tokens",
|
|
|
|
"Pear::LocalLoop::Schema::Result::DeviceToken",
|
|
|
|
{ "foreign.user_id" => "self.id" },
|
|
|
|
{ cascade_copy => 0, cascade_delete => 0 },
|
2020-11-08 15:15:28 +00:00
|
|
|
);
|
|
|
|
|
2017-07-24 13:55:05 +00:00
|
|
|
__PACKAGE__->has_many(
|
2021-03-20 12:09:50 +00:00
|
|
|
"feedback",
|
|
|
|
"Pear::LocalLoop::Schema::Result::Feedback",
|
|
|
|
{ "foreign.user_id" => "self.id" },
|
|
|
|
{ cascade_copy => 0, cascade_delete => 0 },
|
2017-07-24 13:55:05 +00:00
|
|
|
);
|
|
|
|
|
2021-03-20 15:02:00 +00:00
|
|
|
__PACKAGE__->filter_column(
|
|
|
|
is_admin => {
|
|
|
|
filter_to_storage => 'to_bool',
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2017-09-06 15:11:57 +00:00
|
|
|
sub sqlt_deploy_hook {
|
2021-03-20 12:09:50 +00:00
|
|
|
my ( $source_instance, $sqlt_table ) = @_;
|
|
|
|
my $pending_field = $sqlt_table->get_field('is_admin');
|
|
|
|
if ( $sqlt_table->schema->translator->producer_type =~ /SQLite$/ ) {
|
|
|
|
$pending_field->{default_value} = 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$pending_field->{default_value} = \"false";
|
|
|
|
}
|
2021-03-20 15:02:00 +00:00
|
|
|
|
|
|
|
return 1;
|
2017-09-06 15:11:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub to_bool {
|
2021-03-20 12:09:50 +00:00
|
|
|
my ( $self, $val ) = @_;
|
|
|
|
my $driver_name =
|
|
|
|
$self->result_source->schema->storage->dbh->{Driver}->{Name};
|
|
|
|
if ( $driver_name eq 'SQLite' ) {
|
|
|
|
return $val ? 1 : 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return $val ? 'true' : 'false';
|
|
|
|
}
|
2017-09-06 15:11:57 +00:00
|
|
|
}
|
|
|
|
|
2017-04-21 21:12:53 +00:00
|
|
|
sub generate_session {
|
2021-03-20 12:09:50 +00:00
|
|
|
my $self = shift;
|
2017-04-21 21:12:53 +00:00
|
|
|
|
2021-03-20 12:09:50 +00:00
|
|
|
my $token = Data::UUID->new->create_str();
|
|
|
|
$self->create_related(
|
|
|
|
'session_tokens',
|
|
|
|
{
|
|
|
|
token => $token,
|
|
|
|
},
|
|
|
|
);
|
2017-04-21 21:12:53 +00:00
|
|
|
|
2021-03-20 12:09:50 +00:00
|
|
|
return $token;
|
2017-04-21 21:12:53 +00:00
|
|
|
}
|
|
|
|
|
2017-04-24 11:49:18 +00:00
|
|
|
sub name {
|
2021-03-20 12:09:50 +00:00
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
if ( defined $self->entity->customer ) {
|
|
|
|
return $self->entity->customer->display_name;
|
|
|
|
}
|
|
|
|
elsif ( defined $self->entity->organisation ) {
|
|
|
|
return $self->entity->organisation->name;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return;
|
|
|
|
}
|
2017-04-24 11:49:18 +00:00
|
|
|
}
|
|
|
|
|
2017-11-13 19:00:34 +00:00
|
|
|
sub full_name {
|
2021-03-20 12:09:50 +00:00
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
if ( defined $self->entity->customer ) {
|
|
|
|
return $self->entity->customer->full_name;
|
|
|
|
}
|
|
|
|
elsif ( defined $self->entity->organisation ) {
|
|
|
|
return $self->entity->organisation->name;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return;
|
|
|
|
}
|
2017-11-13 19:00:34 +00:00
|
|
|
}
|
|
|
|
|
2017-08-31 11:39:10 +00:00
|
|
|
# TODO Deprecate this sub?
|
2017-08-25 13:59:01 +00:00
|
|
|
sub type {
|
2021-03-20 12:09:50 +00:00
|
|
|
my $self = shift;
|
2017-08-25 13:59:01 +00:00
|
|
|
|
2021-03-20 12:09:50 +00:00
|
|
|
return $self->entity->type;
|
2017-08-25 13:59:01 +00:00
|
|
|
}
|
|
|
|
|
2017-02-24 19:27:43 +00:00
|
|
|
1;
|