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