X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FText%2FTradition%2FUser.pm;fp=lib%2FText%2FTradition%2FUser.pm;h=868c25b880e79ff9c6f3a2edcad9cf19196ccc18;hb=fc5c4949b38572067ab389afa8c1cafec2b5dbd0;hp=0000000000000000000000000000000000000000;hpb=96ba0418c65f3450b419aea78db41bf697612b63;p=scpubgit%2Fstemmatology.git diff --git a/lib/Text/Tradition/User.pm b/lib/Text/Tradition/User.pm new file mode 100644 index 0000000..868c25b --- /dev/null +++ b/lib/Text/Tradition/User.pm @@ -0,0 +1,114 @@ +package Text::Tradition::User; + +use strict; +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 '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 { + 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. +