Allow plugins loaded after C::P::Authentication to hook into 'set_authenticated'
[catagits/Catalyst-Plugin-Authentication.git] / lib / Catalyst / Plugin / Authentication.pm
index 9a34f44..7887cad 100644 (file)
@@ -21,6 +21,7 @@ sub set_authenticated {
     my ( $c, $user ) = @_;
 
     $c->user($user);
+    $c->request->{user} = $user;    # compatibility kludge
 
     if (    $c->isa("Catalyst::Plugin::Session")
         and $c->config->{authentication}{use_session}
@@ -28,26 +29,28 @@ sub set_authenticated {
     {
         $c->save_user_in_session($user);
     }
+
+       $c->NEXT::set_authenticated( $user );
 }
 
 sub user {
-       my $c = shift;
+    my $c = shift;
 
-       if ( @_ ) {
-               return $c->_user( @_ );
-       }
+    if (@_) {
+        return $c->_user(@_);
+    }
 
-       my $user = $c->_user;
+    my $user = $c->_user;
 
-       if ( $user and !Scalar::Util::blessed( $user ) ) {
-               return $c->auth_restore_user( $user );
-       }
+    if ( $user and !Scalar::Util::blessed($user) ) {
+        return $c->auth_restore_user($user);
+    }
 
-       return $user;
+    return $user;
 }
 
 sub save_user_in_session {
-       my ( $c, $user ) = @_;
+    my ( $c, $user ) = @_;
 
     my $store = $user->store || ref $user;
     $c->session->{__user_store} = $c->get_auth_store_name($store) || $store;
@@ -83,11 +86,10 @@ sub prepare {
     my $c = shift->NEXT::prepare(@_);
 
     if (    $c->isa("Catalyst::Plugin::Session")
-        and $c->default_auth_store
         and !$c->user )
     {
         if ( $c->sessionid and my $frozen_user = $c->session->{__user} ) {
-                       $c->_user( $frozen_user );
+            $c->_user($frozen_user);
         }
     }
 
@@ -95,16 +97,15 @@ sub prepare {
 }
 
 sub auth_restore_user {
-       my ( $c, $frozen_user, $store_name ) = @_;
+    my ( $c, $frozen_user, $store_name ) = @_;
 
-       $store_name  ||= $c->session->{__user_store};
-       $frozen_user ||= $c->session->{__user};
+    $store_name  ||= $c->session->{__user_store};
+    $frozen_user ||= $c->session->{__user};
 
-       my $store = $c->get_auth_store( $store_name );
-       $c->_user( my $user = $store->from_session( $c, $frozen_user ) );
-       $c->request->{user} = $user;    # compatibility kludge
+    my $store = $c->get_auth_store($store_name);
+    $c->_user( my $user = $store->from_session( $c, $frozen_user ) );
 
-       return $user;
+    return $user;
 
 }
 
@@ -180,7 +181,8 @@ __END__
 
 =head1 NAME
 
-Catalyst::Plugin::Authentication - 
+Catalyst::Plugin::Authentication - Infrastructure plugin for the Catalyst
+authentication framework.
 
 =head1 SYNOPSIS
 
@@ -195,7 +197,8 @@ Catalyst::Plugin::Authentication -
 The authentication plugin is used by the various authentication and
 authorization plugins in catalyst.
 
-It defines the notion of a logged in user, and provides integration with the 
+It defines the notion of a logged in user, and provides integration with the
+L<Catalyst::Plugin::Session> plugin, 
 
 =head1 METHODS
 
@@ -232,6 +235,15 @@ authentication.
 This involves setting C<user> and the internal data in C<session> if
 L<Catalyst::Plugin::Session> is loaded.
 
+=item auth_restore_user $user
+
+Used to restore a user from the session, by C<user> only when it's actually
+needed.
+
+=item save_user_in_session $user
+
+Used to save the user in a session.
+
 =item prepare
 
 Revives a user from the session object if there is one.