Releng for auth 0.09
[catagits/Catalyst-Plugin-Authentication.git] / lib / Catalyst / Plugin / Authentication.pm
index c03dc0b..433c22b 100644 (file)
@@ -22,7 +22,7 @@ use Class::Inspector;
 #      constant->import(have_want => eval { require Want });
 #}
 
-our $VERSION = "0.04";
+our $VERSION = "0.09";
 
 sub set_authenticated {
     my ( $c, $user ) = @_;
@@ -47,19 +47,16 @@ sub user {
         return $c->_user(@_);
     }
 
-    my $user = $c->_user;
-
-    if ( $user and !Scalar::Util::blessed($user) ) {
-#              return 1 if have_want() && Want::want("BOOL");
-        return $c->auth_restore_user($user);
+    if ( defined(my $user = $c->_user) ) {
+        return $user;
+    } else {
+        return $c->auth_restore_user;
     }
-
-    return $user;
 }
 
 sub user_exists {
        my $c = shift;
-       return defined($c->_user);
+       return defined($c->_user) || defined($c->_user_in_session);
 }
 
 sub save_user_in_session {
@@ -75,9 +72,11 @@ sub logout {
 
     $c->user(undef);
 
-    if (    $c->isa("Catalyst::Plugin::Session")
-        and $c->config->{authentication}{use_session} )
-    {
+    if (
+        $c->isa("Catalyst::Plugin::Session")
+        and $c->config->{authentication}{use_session}
+        and $c->session_is_valid
+    ) {
         delete @{ $c->session }{qw/__user __user_store/};
     }
     
@@ -85,10 +84,10 @@ sub logout {
 }
 
 sub get_user {
-    my ( $c, $uid ) = @_;
+    my ( $c, $uid, @rest ) = @_;
 
     if ( my $store = $c->default_auth_store ) {
-        return $store->get_user($uid);
+        return $store->get_user( $uid, @rest );
     }
     else {
         Catalyst::Exception->throw(
@@ -97,30 +96,27 @@ sub get_user {
     }
 }
 
-sub prepare {
-    my $c = shift->NEXT::prepare(@_);
+sub _user_in_session {
+    my $c = shift;
 
-    if ( $c->isa("Catalyst::Plugin::Session")
-        and !$c->user )
-    {
-        if ( $c->sessionid and my $frozen_user = $c->session->{__user} ) {
-            $c->_user($frozen_user);
-        }
-    }
+    return unless
+        $c->isa("Catalyst::Plugin::Session")
+        and $c->config->{authentication}{use_session}
+        and $c->session_is_valid;
+
+    return $c->session->{__user};
 
-    return $c;
+    return;
 }
 
 sub auth_restore_user {
     my ( $c, $frozen_user, $store_name ) = @_;
 
-    return
-      unless $c->isa("Catalyst::Plugin::Session")
-      and $c->config->{authentication}{use_session}
-      and $c->sessionid;
+    $frozen_user ||= $c->_user_in_session;
+    return unless defined($frozen_user);
 
     $store_name  ||= $c->session->{__user_store};
-    $frozen_user ||= $c->session->{__user};
+    return unless $store_name; # FIXME die unless? This is an internal inconsistency
 
     my $store = $c->get_auth_store($store_name);
     $c->_user( my $user = $store->from_session( $c, $frozen_user ) );
@@ -357,7 +353,7 @@ It could be simplified though:
         }
     }
 
-Since the C<login> method knows how to find logically named parameters on it's
+Since the C<login> method knows how to find logically named parameters on its
 own.
 
 The credential verifier will ask the default store to get the user whose ID is
@@ -462,14 +458,14 @@ user from the auth store.
 
 If you're just going to say
 
-       if ( $c->user_user ) {
+       if ( $c->user_exists ) {
                # 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.
+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