diff --git a/docs/changelog/7.x.x.txt b/docs/changelog/7.x.x.txt index a40b8dcb2..9af6e4e86 100644 --- a/docs/changelog/7.x.x.txt +++ b/docs/changelog/7.x.x.txt @@ -55,6 +55,8 @@ http://www.webgui.org/bugs/tracker/user-profile-images-disappear-after-updat - RFE: HttpProxy - regexp for URLs that will not be proxied. Url pattern filters can now be created in HttpProxy wobject properties. + - RFE: WebGUI::User has no way to get a user when you only know their username (Wes Morgan, U.S. PIRG) + http://www.webgui.org/bugs/tracker/webguiuser-has-no-way-to-get-a-user-when-you-only-know-their-username - Collaboration System wobjects can now be subclassed and still work with the existing Thread and Post assets. - fix: Added some additional i18n that was missing. diff --git a/lib/WebGUI/User.pm b/lib/WebGUI/User.pm index fdfa83795..fa65e6abd 100644 --- a/lib/WebGUI/User.pm +++ b/lib/WebGUI/User.pm @@ -31,7 +31,10 @@ This package provides an object-oriented way of managing WebGUI users as well as =head1 SYNOPSIS use WebGUI::User; - $u = WebGUI::User->new($session,3); or $u = WebGUI::User->new($session,"new"); or $u = WebGUI::User->newByEmail($session, $email); + $u = WebGUI::User->new($session,3); + $u = WebGUI::User->new($session,"new"); + $u = WebGUI::User->newByEmail($session, $email); + $u = WebGUI::User->newByUsername($session, $username); $authMethod = $u->authMethod("WebGUI"); $dateCreated = $u->dateCreated; @@ -423,6 +426,7 @@ sub new { =head2 newByEmail ( session, email ) Instanciates a user by email address. Returns undef if the email address could not be found. +Visitor may not be looked up with this method. =head3 session @@ -446,6 +450,35 @@ sub newByEmail { } +#------------------------------------------------------------------- + +=head2 newByUsername ( session, username ) + +Instanciates a user by username. Returns undef if the username could not be found. +Visitor may not be looked up with this method. + +=head3 session + +A reference to the current session. + +=head3 username + +The username to search for. + +=cut + +sub newByUsername { + my $class = shift; + my $session = shift; + my $username = shift; + my ($id) = $session->dbSlave->quickArray("select userId from users where username=?",[$username]); + my $user = $class->new($session, $id); + return undef if ($user->userId eq "1"); # visitor is never valid for this method + return undef unless $user->username; + return $user; +} + + #------------------------------------------------------------------- =head2 profileField ( fieldName [, value ] ) diff --git a/t/User.t b/t/User.t index deb37bd4c..9d9c841a9 100644 --- a/t/User.t +++ b/t/User.t @@ -20,7 +20,7 @@ use WebGUI::Cache; use WebGUI::User; use WebGUI::ProfileField; -use Test::More tests => 95; # increment this value for each test you create +use Test::More tests => 101; # increment this value for each test you create use Test::Deep; my $session = WebGUI::Test->session; @@ -335,13 +335,40 @@ is($clone, undef, 'newByEmail returns undef if you look up the Visitor'); $clone = WebGUI::User->newByEmail($session, 'noone@localdomain'); is($clone, undef, 'newByEmail returns undef if email address cannot be found'); +$dude->username(''); + $clone = WebGUI::User->newByEmail($session, 'dude@aftery2k.com'); is($clone, undef, 'newByEmail returns undef if the user does not have a username'); $dude->username('dude'); $clone = WebGUI::User->newByEmail($session, 'dude@aftery2k.com'); -is($clone->username, 'dude', 'newByEmail returns valid user object'); +isa_ok($clone, 'WebGUI::User', 'newByEmail returns a valid user object'); +is($clone->username, 'dude', '... and it has the right username'); + +################################################################ +# +# newByUsername +# +################################################################ + +my $useru = WebGUI::User->newByUsername($session, 'Visitor'); +is($useru, undef, 'newByUsername returns undef if you look up the Visitor'); + +$useru = WebGUI::User->newByUsername($session, 'NotHomeRightNow'); +is($useru, undef, 'newByUsername returns undef if username cannot be found'); + +$dude->username(''); + +$useru = WebGUI::User->newByUsername($session, 'dude@aftery2k.com'); +is($useru, undef, 'newByUsername returns undef if the user does not have a username'); + +$dude->username('dude'); + +$useru = WebGUI::User->newByUsername($session, 'dude'); +isa_ok($useru, 'WebGUI::User', 'newByUsername returns a valid user object'); +is($useru->userId, $dude->userId, '... and it is the right user object'); + ################################################################ #