user_exists for C::P::Authen
Yuval Kogman [Mon, 28 Nov 2005 22:01:43 +0000 (22:01 +0000)]
lib/Catalyst/Plugin/Authentication.pm
t/live_app_session.t

index 455296f..414325d 100644 (file)
@@ -55,6 +55,11 @@ sub user {
     return $user;
 }
 
+sub user_exists {
+       my $c = shift;
+       return defined($c->_user);
+}
+
 sub save_user_in_session {
     my ( $c, $user ) = @_;
 
@@ -217,6 +222,24 @@ L<Catalyst::Plugin::Session> plugin,
 
 Returns the currently logged user or undef if there is none.
 
+=item user_exists
+
+Whether or not a user is logged in right now.
+
+The reason this method exists is that C<<$c->user>> may needlessly load the
+user from the auth store.
+
+If you're just going to say
+
+       if ( $c->user_user ) {
+               # foo
+       } else {
+               $c->forward("login");
+       }
+
+it should be more efficient than C<<$c->user>> when a user is marked in the session
+but C<<$c->user>> hasn't been called yet.
+
 =item logout
 
 Delete the currently logged in user from C<user> and the session.
index 6d064e9..dc9df45 100644 (file)
@@ -8,7 +8,7 @@ use Test::More;
 BEGIN {
        eval { require Catalyst::Plugin::Session; require Catalyst::Plugin::Session::State::Cookie };
        plan skip_all => "This test needs Catalyst::Plugin::Session and Catalyst::Plugin::Session::State::Cookie installed" if $@;
-       plan tests => 12;
+       plan tests => 14;
 }
 
 {
@@ -40,6 +40,7 @@ BEGIN {
                my ( $self, $c ) = @_;
 
                ok(!$c->sessionid, "no session id yet");
+               ok(!$c->user_exists, "no user exists");
                ok(!$c->user, "no user yet");
                ok($c->login( "foo", "s3cr3t" ), "can login with clear");
                is( $c->user, $users->{foo}, "user object is in proper place");
@@ -49,6 +50,7 @@ BEGIN {
                my ( $self, $c ) = @_;
 
                ok( $c->sessionid, "session ID was restored" );
+               ok( $c->user_exists, "user exists" );
                ok( $c->user, "a user was also restored");
                is_deeply( $c->user, $users->{foo}, "restored user is the right one (deep test - store might change identity)" );