display is now 'email', allowing ::Auth to email after register.
[scpubgit/stemmatology.git] / lib / Text / Tradition / User.pm
CommitLineData
2006bd3f 1package Text::Tradition::User;
2
3use strict;
4use warnings;
5
6use Moose;
7with qw(KiokuX::User);
8
a528f0f6 9## 'id' provided by KiokuX::User stores our username (email for local users, openid url for openid/google)
2006bd3f 10has 'password' => (is => 'rw', required => 1);
a528f0f6 11has 'email' => (is => 'rw', lazy => 1, builder => '_build_email');
570cf8ba 12## Change this default active value if you want/need to have an admin confirm a user after they self-create.
cd726824 13has 'active' => (is => 'rw', default => sub { 1; });
7cb56251 14has 'role' => (is => 'rw', default => sub { 'user' });
cd726824 15# 'traits' => ['Array'] ?
16# https://metacpan.org/module/Moose::Meta::Attribute::Native::Trait::Array
f54b1ba7 17has '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
26after add_tradition => sub {
27 my ($self, $tradition) = @_;
28 $tradition->user($self)
29 unless $tradition->has_user && $tradition->user->id eq $self->id;
30};
2006bd3f 31
a528f0f6 32sub _build_email {
10ef7653 33 my ($self) = @_;
34
a528f0f6 35 ## no email set, so use username/id
10ef7653 36 return $self->id;
37}
38
ec7ea4e6 39sub remove_tradition {
40 my ($self, $tradition) = @_;
41
42 ## FIXME: Is "name" a good unique field to compare traditions on?
43 my @traditions = @{$self->traditions};
d1906a56 44 @traditions = grep { $tradition != $_ } @traditions;
ec7ea4e6 45
46 $tradition->clear_user;
47 $self->traditions(\@traditions);
48}
49
7cb56251 50sub is_admin {
51 my ($self) = @_;
52
10ef7653 53 return $self->role && $self->role eq 'admin';
7cb56251 54}
55
2006bd3f 561;
cd726824 57
58=head1 NAME
59
60Text::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
86User objects representing owners of L<Text::Tradition>s and authenticated users.
87
88=head2 ATTRIBUTES
89
90=head3 id
91
92Inherited from KiokuX::User, stores the 'username' (login) of the user.
93
94=head3 password
95
96User's password, encrypted on creation (by
97L<KiokuX::User::Util/crypt_password>.
98
99=head3 active
100
101Active flag, defaults to true (1). Will be set to false (0) by
102L<Text::Tradition::UserStore/deactivate_user>.
103
104=head3 traditions
105
106Returns an ArrayRef of L<Text::Tradition> objects belonging to this user.
107
108=head2 METHODS
109
110=head3 check_password
111
112Inherited from KiokuX::User, verifies a given password string against
113the stored encrypted version.
fefeeeda 114