X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FText%2FTradition%2FUser.pm;h=868c25b880e79ff9c6f3a2edcad9cf19196ccc18;hb=a528f0f656c88f72ce332949963b0648193b3b84;hp=5a91101a28b72f12864fd16a2f12ba42a6bf1fa0;hpb=2006bd3fc1654b2355d3b7fa3261bdb4f2085e94;p=scpubgit%2Fstemmatology.git diff --git a/lib/Text/Tradition/User.pm b/lib/Text/Tradition/User.pm index 5a91101..868c25b 100644 --- a/lib/Text/Tradition/User.pm +++ b/lib/Text/Tradition/User.pm @@ -6,12 +6,109 @@ use warnings; use Moose; with qw(KiokuX::User); +## 'id' provided by KiokuX::User stores our username (email for local users, openid url for openid/google) has 'password' => (is => 'rw', required => 1); -has 'traditions' => (is => 'rw', isa => 'ArrayRef[Text::Tradition]', required => 0); +has 'email' => (is => 'rw', lazy => 1, builder => '_build_email'); +## Change this default active value if you want/need to have an admin confirm a user after they self-create. +has 'active' => (is => 'rw', default => sub { 1; }); +has 'role' => (is => 'rw', default => sub { 'user' }); +# 'traits' => ['Array'] ? +# https://metacpan.org/module/Moose::Meta::Attribute::Native::Trait::Array +has 'traditions' => (is => 'rw', + traits => ['Array'], + handles => { + 'add_tradition' => 'push', + }, + isa => 'ArrayRef[Text::Tradition]', + default => sub { [] }, + required => 0); -# after add_tradition => sub { -# $tradition->set_user($self) -# unless $tradition->user->username eq $self->name; -# } +after add_tradition => sub { + my ($self, $tradition) = @_; + $tradition->user($self) + unless $tradition->has_user && $tradition->user->id eq $self->id; +}; + +sub _build_email { + my ($self) = @_; + + ## no email set, so use username/id + return $self->id; +} + +sub remove_tradition { + my ($self, $tradition) = @_; + + ## FIXME: Is "name" a good unique field to compare traditions on? + my @traditions = @{$self->traditions}; + @traditions = grep { $tradition != $_ } @traditions; + + $tradition->clear_user; + $self->traditions(\@traditions); +} + +sub is_admin { + my ($self) = @_; + + return $self->role && $self->role eq 'admin'; +} 1; + +=head1 NAME + +Text::Tradition::User - Users which own traditions, and can login to the web app + +=head1 SYNOPSIS + + ## Users are managed by Text::Tradition::UserStore + + my $userstore = Text::Tradition::UserStore->new(dsn => 'dbi:SQLite:foo.db'); + my $newuser = $userstore->add_user({ username => 'fred', + password => 'somepassword' }); + + my $fetchuser = $userstore->find_user({ username => 'fred' }); + if($fetchuser->check_password('somepassword')) { + ## login user or .. whatever + } + + my $user = $userstore->deactivate_user({ username => 'fred' }); + if(!$user->active) { + ## shouldnt be able to login etc + } + + foreach my $t (@{ $user->traditions }) { + ## do something with traditions owned by this user. + } + +=head1 DESCRIPTION + +User objects representing owners of Ls and authenticated users. + +=head2 ATTRIBUTES + +=head3 id + +Inherited from KiokuX::User, stores the 'username' (login) of the user. + +=head3 password + +User's password, encrypted on creation (by +L. + +=head3 active + +Active flag, defaults to true (1). Will be set to false (0) by +L. + +=head3 traditions + +Returns an ArrayRef of L objects belonging to this user. + +=head2 METHODS + +=head3 check_password + +Inherited from KiokuX::User, verifies a given password string against +the stored encrypted version. +