X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FText%2FTradition%2FUserStore.pm;h=fdb6d4750f7b6f59c9d3218820d2563cffa3e3e5;hb=b77f6c1bee7890af1189201ff40eb8005e3e671f;hp=f2b4aea4dd0dc7ff61360db751a2ce2188c674a8;hpb=1384ec2dc339ae187e51828f7670d25e354b35a8;p=scpubgit%2Fstemmatology.git diff --git a/lib/Text/Tradition/UserStore.pm b/lib/Text/Tradition/UserStore.pm index f2b4aea..fdb6d47 100644 --- a/lib/Text/Tradition/UserStore.pm +++ b/lib/Text/Tradition/UserStore.pm @@ -5,11 +5,12 @@ use warnings; use Moose; use KiokuX::User::Util qw(crypt_password); +use Text::Tradition::Error; -extends 'KiokuX::Model'; +extends 'Text::Tradition::Directory'; +# extends 'KiokuX::Model'; use Text::Tradition::User; -# use Text::Tradition::Directory; =head1 NAME @@ -65,8 +66,6 @@ has MIN_PASS_LEN => ( is => 'ro', isa => 'Num', default => sub { 8 } ); ## XX_user, but leaving that way for now incase we merge this code ## into ::Directory for one-store. -## To die or not to die, on error, this is the question. - =head2 METHODS =head3 add_user @@ -79,14 +78,17 @@ Create a new user object, store in the KiokuDB backend, and return it. sub add_user { my ($self, $userinfo) = @_; - my $username = $userinfo->{username}; + my $username = $userinfo->{url} || $userinfo->{username}; my $password = $userinfo->{password}; - return unless $username && $self->validate_password($password); - + throw( "No username given" ) unless $username; + throw( "Invalid password - must be at least " . $self->MIN_PASS_LEN + . " characters long" ) + unless ( $self->validate_password($password) || $username =~ /^https?:/ ); + my $user = Text::Tradition::User->new( id => $username, - password => crypt_password($password), + password => ($password ? crypt_password($password) : ''), ); my $scope = $self->new_scope; @@ -95,9 +97,14 @@ sub add_user { return $user; } +sub create_user { + my $self = shift; + return $self->add_user(@_); +} + =head3 find_user -Takes a hashref of C. +Takes a hashref of C, optionally C. Fetches the user object for the given username and returns it. @@ -105,7 +112,10 @@ Fetches the user object for the given username and returns it. sub find_user { my ($self, $userinfo) = @_; - my $username = $userinfo->{username}; + ## url or display? + # 'display' => 'castaway.myopenid.com', + # 'url' => 'http://castaway.myopenid.com/', + my $username = $userinfo->{url} || $userinfo->{username}; my $scope = $self->new_scope; return $self->lookup(Text::Tradition::User->id_for_user($username)); @@ -117,9 +127,8 @@ sub find_user { Takes a hashref of C and C (same as add_user). Retrieves the user, and updates it with the new information. Username -changing is not currently supported. - -Returns the updated user object, or undef if not found. +changing is not currently supported. Returns the updated user object. +Throws an error if user not found. =cut @@ -128,10 +137,11 @@ sub modify_user { my $username = $userinfo->{username}; my $password = $userinfo->{password}; - return unless $username && $self->validate_password($password); + throw( "Missing username or bad password" ) + unless $username && $self->validate_password($password); my $user = $self->find_user({ username => $username }); - return unless $user; + throw( "Could not find user $username" ) unless $user; my $scope = $self->new_scope; $user->password(crypt_password($password)); @@ -149,7 +159,7 @@ Sets the users C flag to false (0), and sets all traditions assigned to them to non-public, updates the storage and returns the deactivated user. -Returns undef if user not found. +Throws an error if user not found. =cut @@ -157,10 +167,10 @@ sub deactivate_user { my ($self, $userinfo) = @_; my $username = $userinfo->{username}; - return if !$username; + throw( "Need to specify a username for deactivation" ) unless $username; my $user = $self->find_user({ username => $username }); - return if !$user; + throw( "User $username not found" ) unless $user; $user->active(0); foreach my $tradition (@{ $user->traditions }) { @@ -184,7 +194,7 @@ Takes a hashref of C. Returns the user object if already activated. Activates (sets the active flag to true (1)), updates the storage and returns the user. -Returns undef if the user is not found. +Throws an error if the user is not found. =cut @@ -192,10 +202,10 @@ sub reactivate_user { my ($self, $userinfo) = @_; my $username = $userinfo->{username}; - return if !$username; + throw( "Need to specify a username for reactivation" ) unless $username; my $user = $self->find_user({ username => $username }); - return if !$user; + throw( "User $username not found" ) unless $user; return $user if $user->active; @@ -212,7 +222,7 @@ CAUTION: Delets actual data! Takes a hashref of C. -Returns undef if the user doesn't exist. +Throws an error if the user doesn't exist. Removes the user from the store and returns 1. @@ -222,10 +232,10 @@ sub delete_user { my ($self, $userinfo) = @_; my $username = $userinfo->{username}; - return if !$username; + throw( "Need to specify a username for deletion" ) unless $username; my $user = $self->find_user({ username => $username }); - return if !$user; + throw( "User $username not found" ) unless $user; my $scope = $self->new_scope; @@ -256,4 +266,11 @@ sub validate_password { return 1; } +sub throw { + Text::Tradition::Error->throw( + 'ident' => 'UserStore error', + 'message' => $_[0], + ); +} + 1;