Added ability to avoid DB hits when restoring from session
[catagits/Catalyst-Authentication-Store-DBIx-Class.git] / lib / Catalyst / Authentication / Store / DBIx / Class.pm
index e7379d0..8c20100 100644 (file)
@@ -4,7 +4,7 @@ use strict;
 use warnings;
 use base qw/Class::Accessor::Fast/;
 
-our $VERSION= "0.102";
+our $VERSION= "0.104";
 
 
 BEGIN {
@@ -43,10 +43,9 @@ sub new {
 sub from_session {
     my ( $self, $c, $frozenuser ) = @_;
 
-    return $frozenuser if ref $frozenuser;
-    
+#    return $frozenuser if ref $frozenuser;
+
     my $user = $self->config->{'store_user_class'}->new($self->{'config'}, $c);
-    
     return $user->from_session($frozenuser, $c);
 }
 
@@ -113,7 +112,7 @@ This documentation refers to version 0.10.
                                 },
                                 store => {
                                     class => 'DBIx::Class',
-                                   user_class => 'MyApp::Users',
+                                   user_class => 'MyApp::User',
                                    id_field => 'user_id',
                                    role_relation => 'roles',
                                    role_field => 'rolename',                   
@@ -165,11 +164,12 @@ The DBIx::Class storage module has several configuration options
                                 },
                                 store => {
                                     class => 'DBIx::Class',
-                                   user_class => 'MyApp::Users',
+                                   user_class => 'MyApp::User',
                                    id_field => 'user_id',
                                    role_relation => 'roles',
                                    role_field => 'rolename',
-                                   ignore_fields_in_find => [ 'remote_name' ]          
+                                   ignore_fields_in_find => [ 'remote_name' ],
+                                   use_userdata_from_session => 1,          
                                }
                                }
                        }
@@ -223,6 +223,23 @@ user. This makes it possible to avoid problems when a credential requires an
 authinfo element whose name overlaps with a column name in your users table.
 If this doesn't make sense to you, you probably don't need it.
 
+=item use_userdata_from_session
+
+Under normal circumstances, on each request the user's data is re-retrieved 
+from the database using the primary key for the user table.  When this flag 
+is set in the configuration, it causes the DBIx::Class store to avoid this
+database hit on session restore.  Instead, the user object's column data 
+is retrieved from the session and used as-is.  
+
+B<NOTE>: Since the user object's column
+data is only stored in the session during the initial authentication of 
+the user, turning this on can potentially lead to a situation where the data
+in $c->user is different from what is stored the database.  You can force
+a reload of the data from the database at any time by calling $c->user->get_object(1);
+Note that this will update $c->user for the remainder of this request.  
+It will NOT update the session.  If you need to update the session
+you should call $c->update_user_in_session() as well.  
+
 =item store_user_class
 
 This allows you to override the authentication user class that the 
@@ -321,7 +338,7 @@ the user.  An example will probably make more sense:
             password => $password,
             'dbix_class' => 
                 {
-                    searchargs = [ { -or => [ username => $username,
+                    searchargs => [ { -or => [ username => $username,
                                               email => $email,
                                               clientid => $clientid ] 
                                    },
@@ -349,7 +366,7 @@ within your login action and use it for retrieving the user. A simple example:
        
     if ($c->authenticate({ 
                            password => $password,
-                           'dbix_class' => {  resultset = $rs }
+                           'dbix_class' => {  resultset => $rs }
                          })) {
        # do successful authentication actions here.
     }