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 22:12:53 +01:00
use Data::UUID ;
2017-04-21 20:52:14 +01:00
__PACKAGE__ - > load_components ( qw /
InflateColumn:: DateTime
PassphraseColumn
TimeStamp
/ ) ;
2017-02-24 19:27:43 +00:00
2017-04-21 20:52:14 +01:00
__PACKAGE__ - > table ( "users" ) ;
2017-02-24 19:27:43 +00:00
__PACKAGE__ - > add_columns (
2017-04-21 20:52:14 +01:00
"id" = > {
data_type = > "integer" ,
is_auto_increment = > 1 ,
is_nullable = > 0 ,
} ,
"customer_id" = > {
data_type = > "integer" ,
is_foreign_key = > 1 ,
is_nullable = > 1 ,
} ,
"organisation_id" = > {
data_type = > "integer" ,
is_foreign_key = > 1 ,
is_nullable = > 1 ,
} ,
"email" = > {
data_type = > "text" ,
is_nullable = > 0 ,
} ,
"join_date" = > {
data_type = > "datetime" ,
set_on_create = > 1 ,
} ,
"password" = > {
2017-04-18 22:31:08 +01:00
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-02-24 19:27:43 +00:00
) ;
2017-04-21 20:52:14 +01:00
__PACKAGE__ - > set_primary_key ( "id" ) ;
2017-02-24 19:27:43 +00:00
2017-04-21 20:52:14 +01:00
__PACKAGE__ - > add_unique_constraint ( [ "customer_id" ] ) ;
2017-02-24 19:27:43 +00:00
2017-04-21 20:52:14 +01:00
__PACKAGE__ - > add_unique_constraint ( [ "email" ] ) ;
2017-02-24 19:27:43 +00:00
2017-04-21 20:52:14 +01:00
__PACKAGE__ - > add_unique_constraint ( [ "organisation_id" ] ) ;
2017-02-24 19:27:43 +00:00
__PACKAGE__ - > might_have (
"administrator" ,
"Pear::LocalLoop::Schema::Result::Administrator" ,
2017-04-21 20:52:14 +01:00
{ "foreign.user_id" = > "self.id" } ,
2017-02-24 19:27:43 +00:00
{ cascade_copy = > 0 , cascade_delete = > 0 } ,
) ;
__PACKAGE__ - > belongs_to (
2017-04-09 14:21:36 +01:00
"customer" ,
2017-02-24 19:27:43 +00:00
"Pear::LocalLoop::Schema::Result::Customer" ,
2017-04-21 22:12:53 +01:00
{ "foreign.id" = > "self.customer_id" } ,
2017-02-24 19:27:43 +00:00
{
is_deferrable = > 0 ,
join_type = > "LEFT" ,
on_delete = > "NO ACTION" ,
on_update = > "NO ACTION" ,
} ,
) ;
__PACKAGE__ - > belongs_to (
2017-04-18 18:29:30 +01:00
"organisation" ,
2017-02-24 19:27:43 +00:00
"Pear::LocalLoop::Schema::Result::Organisation" ,
2017-04-21 22:12:53 +01:00
{ "foreign.id" = > "self.organisation_id" } ,
2017-02-24 19:27:43 +00:00
{
is_deferrable = > 0 ,
join_type = > "LEFT" ,
on_delete = > "NO ACTION" ,
on_update = > "NO ACTION" ,
} ,
) ;
__PACKAGE__ - > has_many (
"pending_organisations" ,
"Pear::LocalLoop::Schema::Result::PendingOrganisation" ,
2017-04-21 20:52:14 +01:00
{ "foreign.submitted_by_id" = > "self.id" } ,
2017-02-24 19:27:43 +00:00
{ cascade_copy = > 0 , cascade_delete = > 0 } ,
) ;
__PACKAGE__ - > has_many (
"pending_transactions" ,
"Pear::LocalLoop::Schema::Result::PendingTransaction" ,
2017-04-21 22:12:53 +01:00
{ "foreign.buyer_id" = > "self.id" } ,
2017-02-24 19:27:43 +00:00
{ cascade_copy = > 0 , cascade_delete = > 0 } ,
) ;
__PACKAGE__ - > has_many (
"session_tokens" ,
"Pear::LocalLoop::Schema::Result::SessionToken" ,
2017-04-21 22:12:53 +01:00
{ "foreign.user_id" = > "self.id" } ,
2017-02-24 19:27:43 +00:00
{ cascade_copy = > 0 , cascade_delete = > 0 } ,
) ;
__PACKAGE__ - > has_many (
"transactions" ,
"Pear::LocalLoop::Schema::Result::Transaction" ,
2017-04-21 22:12:53 +01:00
{ "foreign.buyer_id" = > "self.id" } ,
2017-02-24 19:27:43 +00:00
{ cascade_copy = > 0 , cascade_delete = > 0 } ,
) ;
2017-07-24 14:55:05 +01:00
__PACKAGE__ - > has_many (
"feedback" ,
"Pear::LocalLoop::Schema::Result::Feedback" ,
{ "foreign.user_id" = > "self.id" } ,
{ cascade_copy = > 0 , cascade_delete = > 0 } ,
) ;
2017-04-21 22:12:53 +01:00
sub generate_session {
my $ self = shift ;
my $ token = Data::UUID - > new - > create_str ( ) ;
$ self - > create_related (
'session_tokens' ,
{
token = > $ token ,
} ,
) ;
return $ token ;
}
2017-04-24 12:49:18 +01:00
sub name {
my $ self = shift ;
if ( defined $ self - > customer_id ) {
2017-05-08 20:54:50 +01:00
return $ self - > customer - > display_name ;
2017-04-24 12:49:18 +01:00
} elsif ( defined $ self - > organisation_id ) {
return $ self - > organisation - > name ;
} else {
return undef ;
}
}
2017-02-24 19:27:43 +00:00
1 ;