From: Tara L Andrews Date: Tue, 18 Sep 2012 20:26:29 +0000 (+0200) Subject: support user lookup by email as well as id X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=a4c19656afb19c0e01b42f6199388bd098f604ef;p=scpubgit%2Fstemmatology.git support user lookup by email as well as id --- diff --git a/persistence/lib/Text/Tradition/Directory.pm b/persistence/lib/Text/Tradition/Directory.pm index 61e88d7..056a85b 100644 --- a/persistence/lib/Text/Tradition/Directory.pm +++ b/persistence/lib/Text/Tradition/Directory.pm @@ -456,7 +456,7 @@ sub _extract_openid_data { =head2 find_user( $userinfo ) -Takes a hashref of C, and possibly openIDish results from +Takes a hashref of C or C, and possibly openIDish results from L. Fetches the user object for the given username and returns it. @@ -466,19 +466,27 @@ Fetches the user object for the given username and returns it. sub find_user { my ($self, $userinfo) = @_; - ## No username means probably an OpenID based user - if(!exists $userinfo->{username}) { + ## A URL field means probably an OpenID based user + if( exists $userinfo->{url} ) { _extract_openid_data($userinfo); } - my $username = $userinfo->{username}; - - ## No logins if user is deactivated (use lookup to fetch to re-activate) - my $user = $self->lookup(Text::Tradition::User->id_for_user($username)); - return if(!$user || !$user->active); - + my $user; + if( exists $userinfo->{username} ) { + my $username = $userinfo->{username}; + ## No logins if user is deactivated (use lookup to fetch to re-activate) + $user = $self->lookup(Text::Tradition::User->id_for_user($username)); + ## If there is an inactive user, skip it + return if( $user && !$user->active ); + } elsif( exists $userinfo->{email} ) { + ## Scan the users looking for a matching email + my @matches; + $self->scan( sub { push( @matches, @_ ) + if $_[0]->isa('Text::Tradition::User') + && $_[0]->email eq $userinfo->{email} } ); + $user = shift @matches; + } # print STDERR "Found user, $username, email is :", $user->email, ":\n"; - return $user; } diff --git a/persistence/t/text_tradition_user.t b/persistence/t/text_tradition_user.t index bcaa171..34f4355 100644 --- a/persistence/t/text_tradition_user.t +++ b/persistence/t/text_tradition_user.t @@ -266,3 +266,11 @@ ok($fetched_t->public, 'Traditionlist returns public item'); is($get_openid_user->id, 'http://blahblah.com/foo/bar/baz/lotsofjunk', 'Set id to unique url from openid'); is($get_openid_user->email, 'fredbloggs@blahblah.com', 'Set email value to email from extension'); } + +{ + ## Find the same openid user just by email + my $search_user = $user_store->find_user({ email => 'fredbloggs@blahblah.com' }); + ok( $search_user, 'Found an OpenID user by email' ); + is( $search_user->id, 'http://blahblah.com/foo/bar/baz/lotsofjunk', 'User has correct URL ID' ); + is( $search_user->email, 'fredbloggs@blahblah.com', 'User has correct email' ); +} \ No newline at end of file