| 1 | package Text::Tradition::User; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 4 | with qw(KiokuX::User); | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | ## 'id' provided by KiokuX::User stores our username (email for local users, openid url for openid/google) | ||||||||||||||||||||||||||||
| 10 | 4 | has 'password' => (is => 'rw', required => 1); | |||||||||||||||||||||||||||
| 11 | 4 | has 'email' => (is => 'rw', lazy => 1, builder => '_build_email'); | |||||||||||||||||||||||||||
| 12 | ## Change this default active value if you want/need to have an admin confirm a user after they self-create. | ||||||||||||||||||||||||||||
| 13 | 11 | ▶ | has 'active' => (is => 'rw', default => sub { 1; }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 0 | ▶ | has 'role' => (is => 'rw', default => sub { 'user' }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 15 | # 'traits' => ['Array'] ? | ||||||||||||||||||||||||||||
| 16 | # https://metacpan.org/module/Moose::Meta::Attribute::Native::Trait::Array | ||||||||||||||||||||||||||||
| 17 | has 'traditions' => (is => 'rw', | ||||||||||||||||||||||||||||
| 18 | traits => ['Array'], | ||||||||||||||||||||||||||||
| 19 | handles => { | ||||||||||||||||||||||||||||
| 20 | 'add_tradition' => 'push', | ||||||||||||||||||||||||||||
| 21 | }, | ||||||||||||||||||||||||||||
| 22 | isa => 'ArrayRef[Text::Tradition]', | ||||||||||||||||||||||||||||
| 23 | 11 | ▶ | default => sub { [] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 4 | required => 0); | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 5 | ▶ | after add_tradition => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=13.1
| |||||||||||||||||||||||||||||
| 27 | 5 | my ($self, $tradition) = @_; | |||||||||||||||||||||||||||
| 28 | 5 | ▶ | $tradition->user($self) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$tradition->has_user and $tradition->user->id eq $self->id
MC/DC0%0/2
$tradition->has_user and $tradition->user->id eq $self->id
$tradition->has_user
$tradition->user->id eq $self->id
Truth table33%1/3
A and B
A= $tradition->has_userB= $tradition->user->id eq $self->id
| |||||||||||||||||||||||||||||
| 29 | unless $tradition->has_user && $tradition->user->id eq $self->id; | ||||||||||||||||||||||||||||
| 30 | 4 | }; | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 0 | ▶ | sub _build_email { | ||||||||||||||||||||||||||
|
Subroutine
_build_email: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 33 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | ## no email set, so use username/id | ||||||||||||||||||||||||||||
| 36 | 0 | return $self->id; | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 1 | ▶ | sub remove_tradition { | ||||||||||||||||||||||||||
|
Subroutine
remove_tradition: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 40 | 1 | my ($self, $tradition) = @_; | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | ## FIXME: Is "name" a good unique field to compare traditions on? | ||||||||||||||||||||||||||||
| 43 | 1 | my @traditions = @{$self->traditions}; | |||||||||||||||||||||||||||
| 44 | 1 | @traditions = grep { $tradition != $_ } @traditions; | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 1 | $tradition->clear_user; | |||||||||||||||||||||||||||
| 47 | 1 | $self->traditions(\@traditions); | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 6 | ▶ | sub is_admin { | ||||||||||||||||||||||||||
|
Subroutine
is_admin: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 51 | 6 | my ($self) = @_; | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 6 | ▶ | return $self->role && $self->role eq 'admin'; | ||||||||||||||||||||||||||
|
Condition66%2/3
$self->role && $self->role eq 'admin'
MC/DC50%1/2
$self->role && $self->role eq 'admin'
$self->role
$self->role eq 'admin'
Truth table66%2/3
A && B
A= $self->roleB= $self->role eq 'admin'
| |||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 4 | 1; | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | =head1 NAME | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | Text::Tradition::User - Users which own traditions, and can login to the web app | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | ## Users are managed by Text::Tradition::Directory | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | my $userstore = Text::Tradition::Directory->new(dsn => 'dbi:SQLite:foo.db'); | ||||||||||||||||||||||||||||
| 67 | my $newuser = $userstore->add_user({ username => 'fred', | ||||||||||||||||||||||||||||
| 68 | password => 'somepassword' }); | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | my $fetchuser = $userstore->find_user({ username => 'fred' }); | ||||||||||||||||||||||||||||
| 71 | if($fetchuser->check_password('somepassword')) { | ||||||||||||||||||||||||||||
| 72 | ## login user or .. whatever | ||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | my $user = $userstore->deactivate_user({ username => 'fred' }); | ||||||||||||||||||||||||||||
| 76 | if(!$user->active) { | ||||||||||||||||||||||||||||
| 77 | ## shouldnt be able to login etc | ||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | foreach my $t (@{ $user->traditions }) { | ||||||||||||||||||||||||||||
| 81 | ## do something with traditions owned by this user. | ||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | User objects representing owners of L<Text::Tradition>s and authenticated users. | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | =head2 ATTRIBUTES | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | =head3 id | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | Inherited from KiokuX::User, stores the 'username' (login) of the user. | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | =head3 password | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | User's password, encrypted on creation (by | ||||||||||||||||||||||||||||
| 97 | L<KiokuX::User::Util/crypt_password>. | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | =head3 active | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | Active flag, defaults to true (1). Will be set to false (0) by | ||||||||||||||||||||||||||||
| 102 | L<Text::Tradition::UserStore/deactivate_user>. | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | =head3 traditions | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | Returns an ArrayRef of L<Text::Tradition> objects belonging to this user. | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | =head1 METHODS | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | =head2 check_password | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | Inherited from KiokuX::User, verifies a given password string against | ||||||||||||||||||||||||||||
| 113 | the stored encrypted version. | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | =head2 add_tradition( $tradition ) | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | Assigns the given tradition to this user. | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | =head2 remove_tradition( $tradition ) | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | Removes the specified tradition from the control of this user. | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | =head2 is_admin | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | Returns true if this user has administrative privileges. | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||