This repository has been archived on 2023-08-16. You can view files and clone it, but cannot push or open issues or pull requests.
Foodloop-Server/lib/Pear/LocalLoop/Schema/Result/User.pm

114 lines
2 KiB
Perl
Raw Normal View History

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;
__PACKAGE__->load_components( qw/
InflateColumn::DateTime
PassphraseColumn
TimeStamp
/);
2017-02-24 19:27:43 +00:00
__PACKAGE__->table("users");
2017-02-24 19:27:43 +00:00
__PACKAGE__->add_columns(
"id" => {
data_type => "integer",
is_auto_increment => 1,
is_nullable => 0,
},
2017-08-31 11:39:10 +00:00
"entity_id" => {
data_type => "integer",
is_foreign_key => 1,
2017-08-31 11:39:10 +00:00
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',
},
2017-08-31 11:39:10 +00:00
"is_admin" => {
data_type => "boolean",
default_value => \"0",
is_nullable => 0,
},
2017-02-24 19:27:43 +00:00
);
__PACKAGE__->set_primary_key("id");
2017-02-24 19:27:43 +00:00
__PACKAGE__->add_unique_constraint(["email"]);
2017-02-24 19:27:43 +00:00
__PACKAGE__->belongs_to(
2017-08-31 11:39:10 +00:00
"entity",
"Pear::LocalLoop::Schema::Result::Entity",
"entity_id",
2017-02-24 19:27:43 +00:00
);
__PACKAGE__->has_many(
"session_tokens",
"Pear::LocalLoop::Schema::Result::SessionToken",
2017-04-21 21:12:53 +00:00
{ "foreign.user_id" => "self.id" },
2017-02-24 19:27:43 +00:00
{ cascade_copy => 0, cascade_delete => 0 },
);
2017-07-24 13:55:05 +00:00
__PACKAGE__->has_many(
"feedback",
"Pear::LocalLoop::Schema::Result::Feedback",
{ "foreign.user_id" => "self.id" },
{ cascade_copy => 0, cascade_delete => 0 },
);
2017-04-21 21:12:53 +00:00
sub generate_session {
my $self = shift;
my $token = Data::UUID->new->create_str();
$self->create_related(
'session_tokens',
{
token => $token,
},
);
return $token;
}
sub name {
my $self = shift;
2017-08-31 11:39:10 +00:00
if ( defined $self->entity->customer ) {
return $self->entity->customer->display_name;
} elsif ( defined $self->entity->organisation ) {
return $self->entity->organisation->name;
} else {
2017-08-25 13:59:01 +00:00
return;
}
}
2017-08-31 11:39:10 +00:00
# TODO Deprecate this sub?
2017-08-25 13:59:01 +00:00
sub type {
my $self = shift;
2017-08-31 11:39:10 +00:00
return $self->entity->type;
2017-08-25 13:59:01 +00:00
}
2017-02-24 19:27:43 +00:00
1;