7e53bb4355492fa1f87f2cb9b59882c6fef3eb5b
[scpubgit/stemmatology.git] / lib / Text / Tradition / User.pm
1 package Text::Tradition::User;
2
3 use strict;
4 use warnings;
5
6 use Moose;
7 with qw(KiokuX::User);
8
9 ## 'id' provided by KiokuX::User stores our username
10 has 'password'   => (is => 'rw', required => 1);
11 ## Change this default active value if you want/need to have an admin confirm a user after they self-create.
12 has 'active'     => (is => 'rw', default => sub { 1; });
13 has 'role'       => (is => 'rw', default => sub { 'user' });
14 # 'traits' => ['Array'] ?
15 # https://metacpan.org/module/Moose::Meta::Attribute::Native::Trait::Array
16 has 'traditions' => (is => 'rw', 
17                      traits => ['Array'],
18                      handles => {
19                          'add_tradition' => 'push',
20                      },
21                      isa => 'ArrayRef[Text::Tradition]', 
22                      default => sub { [] }, 
23                      required => 0);
24
25 after add_tradition => sub { 
26     my ($self, $tradition) = @_;
27     $tradition->user($self) 
28         unless $tradition->has_user && $tradition->user->id eq $self->id;
29 };
30
31 sub remove_tradition {
32     my ($self, $tradition) = @_;
33
34     ## FIXME: Is "name" a good unique field to compare traditions on?
35     my @traditions = @{$self->traditions};
36     @traditions = grep { $tradition != $_ } @traditions;
37
38     $tradition->clear_user;
39     $self->traditions(\@traditions);
40 }
41
42 sub is_admin {
43     my ($self) = @_;
44
45     return $self->role eq 'admin';
46 }
47
48 1;
49
50 =head1 NAME
51
52 Text::Tradition::User - Users which own traditions, and can login to the web app
53
54 =head1 SYNOPSIS
55
56     ## Users are managed by Text::Tradition::UserStore
57
58     my $userstore = Text::Tradition::UserStore->new(dsn => 'dbi:SQLite:foo.db');
59     my $newuser = $userstore->add_user({ username => 'fred',
60                                          password => 'somepassword' });
61
62     my $fetchuser = $userstore->find_user({ username => 'fred' });
63     if($fetchuser->check_password('somepassword')) { 
64        ## login user or .. whatever
65     }
66
67     my $user = $userstore->deactivate_user({ username => 'fred' });
68     if(!$user->active) { 
69       ## shouldnt be able to login etc
70     }
71
72     foreach my $t (@{ $user->traditions }) {
73       ## do something with traditions owned by this user.
74     }
75
76 =head1 DESCRIPTION
77
78 User objects representing owners of L<Text::Tradition>s and authenticated users.
79
80 =head2 ATTRIBUTES
81
82 =head3 id
83
84 Inherited from KiokuX::User, stores the 'username' (login) of the user.
85
86 =head3 password
87
88 User's password, encrypted on creation (by
89 L<KiokuX::User::Util/crypt_password>.
90
91 =head3 active
92
93 Active flag, defaults to true (1). Will be set to false (0) by
94 L<Text::Tradition::UserStore/deactivate_user>.
95
96 =head3 traditions
97
98 Returns an ArrayRef of L<Text::Tradition> objects belonging to this user.
99
100 =head2 METHODS
101
102 =head3 check_password
103
104 Inherited from KiokuX::User, verifies a given password string against
105 the stored encrypted version.
106