Update admin_users to be able to remove traditions from users
[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
cd726824 9## 'id' provided by KiokuX::User stores our username
2006bd3f 10has 'password' => (is => 'rw', required => 1);
570cf8ba 11## Change this default active value if you want/need to have an admin confirm a user after they self-create.
cd726824 12has 'active' => (is => 'rw', default => sub { 1; });
13# 'traits' => ['Array'] ?
14# https://metacpan.org/module/Moose::Meta::Attribute::Native::Trait::Array
f54b1ba7 15has '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
24after add_tradition => sub {
25 my ($self, $tradition) = @_;
26 $tradition->user($self)
27 unless $tradition->has_user && $tradition->user->id eq $self->id;
28};
2006bd3f 29
ec7ea4e6 30sub 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->name ne $_->name } @traditions;
36
37 $tradition->clear_user;
38 $self->traditions(\@traditions);
39}
40
2006bd3f 411;
cd726824 42
43=head1 NAME
44
45Text::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
71User objects representing owners of L<Text::Tradition>s and authenticated users.
72
73=head2 ATTRIBUTES
74
75=head3 id
76
77Inherited from KiokuX::User, stores the 'username' (login) of the user.
78
79=head3 password
80
81User's password, encrypted on creation (by
82L<KiokuX::User::Util/crypt_password>.
83
84=head3 active
85
86Active flag, defaults to true (1). Will be set to false (0) by
87L<Text::Tradition::UserStore/deactivate_user>.
88
89=head3 traditions
90
91Returns an ArrayRef of L<Text::Tradition> objects belonging to this user.
92
93=head2 METHODS
94
95=head3 check_password
96
97Inherited from KiokuX::User, verifies a given password string against
98the stored encrypted version.
fefeeeda 99