Merge branch 'master' of github.com:tla/stemmatology
[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 (email for local users, openid url for openid/google)
10 has 'password'   => (is => 'rw', required => 1);
11 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 has 'active'     => (is => 'rw', default => sub { 1; });
14 has 'role'       => (is => 'rw', default => sub { 'user' });
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                      default => sub { [] }, 
24                      required => 0);
25
26 after add_tradition => sub { 
27     my ($self, $tradition) = @_;
28     $tradition->user($self) 
29         unless $tradition->has_user && $tradition->user->id eq $self->id;
30 };
31
32 sub _build_email {
33     my ($self) = @_;
34
35     ## no email set, so use username/id
36     return $self->id;
37 }
38
39 sub remove_tradition {
40     my ($self, $tradition) = @_;
41
42     ## FIXME: Is "name" a good unique field to compare traditions on?
43     my @traditions = @{$self->traditions};
44     @traditions = grep { $tradition != $_ } @traditions;
45
46     $tradition->clear_user;
47     $self->traditions(\@traditions);
48 }
49
50 sub is_admin {
51     my ($self) = @_;
52
53     return $self->role && $self->role eq 'admin';
54 }
55
56 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::UserStore
65
66     my $userstore = Text::Tradition::UserStore->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 =head2 METHODS
109
110 =head3 check_password
111
112 Inherited from KiokuX::User, verifies a given password string against
113 the stored encrypted version.
114