Interchange6-Schema
view release on metacpan or search on metacpan
lib/Interchange6/Schema/Result/User.pm view on Meta::CPAN
Otherwise check username using L</check_username>.
=cut
sub new {
my ( $class, $attrs ) = @_;
if ( defined $attrs->{is_anonymous} && !defined $attrs->{username} ) {
my $ug = Data::UUID->new;
$attrs->{username} = "anonymous-" . lc( $ug->create_str );
$attrs->{active} = 0;
}
else {
$attrs->{username} = check_username($attrs->{username});
}
my $new = $class->next::method($attrs);
return $new;
}
=head2 insert
Overloaded method. Always add new users to Role with name 'user' unless user
has L</is_anonymous> set in which case add user to role 'anonymous';
=cut
sub insert {
my ($self, @args) = @_;
my $guard = $self->result_source->schema->txn_scope_guard;
$self->next::method(@args);
my $role_name = $self->is_anonymous ? 'anonymous' : 'user';
my $user_role = $self->result_source->schema->resultset('Role')
->find( { name => $role_name } );
# uncoverable branch false
if ( $user_role ) {
$self->create_related( 'user_roles', { roles_id => $user_role->id } );
}
else {
# we should never get here
# uncoverable statement
$self->throw_exception(
"Role with name 'user' must exist when creating a new user");
}
$guard->commit;
return $self;
}
=head2 update
Overloaded method. Check username using L</check_username> if supplied.
=cut
sub update {
my ( $self, $upd ) = @_;
my $username;
# username may have been passed as arg or previously set
if ( exists $upd->{username} ) {
$upd->{username} = check_username($upd->{username});
}
return $self->next::method($upd);
}
=head2 check_username( $username )
Die if C<$username> is undef or empty string. Otherwise return C<lc($username)>
=cut
sub check_username {
my $value = shift;
die "username cannot be undef" unless defined $value;
$value =~ s/(^\s+|\s+$)//g;
die "username cannot be empty string" if $value eq '';
return lc($value);
}
=head2 blog_posts
Returns resultset of messages that are blog posts
=cut
sub blog_posts {
my $self = shift;
return $self->messages->search( { 'message_type.name' => 'blog_post' },
{ join => 'message_type' } );
}
=head2 name
Returns L</first_name> and L</last_name> joined by a single space.
=cut
sub name {
my $self = shift;
return join( " ", $self->first_name, $self->last_name );
}
=head2 reviews
Returns resultset of messages that are reviews.
=cut
sub reviews {
my $self = shift;
return $self->messages->search( { 'message_type.name' => 'product_review' },
{ join => 'message_type' } );
( run in 1.440 second using v1.01-cache-2.11-cpan-437f7b0c052 )