typo in exception line. fixed
[catagits/Catalyst-Authentication-Store-DBIx-Class.git] / lib / Catalyst / Authentication / Store / DBIx / Class / User.pm
index 1693a8c..caf64af 100644 (file)
@@ -1,8 +1,9 @@
-package Catalyst::Plugin::Authentication::Store::DBIx::Class::User;
+package Catalyst::Authentication::Store::DBIx::Class::User;
 
 use strict;
 use warnings;
-use base qw/Catalyst::Plugin::Authentication::User/;
+use Data::Dumper;
+use base qw/Catalyst::Authentication::User/;
 use base qw/Class::Accessor::Fast/;
 
 BEGIN {
@@ -21,7 +22,7 @@ sub new {
     
     bless $self, $class;
     
-
+    ## Note to self- add handling of multiple-column primary keys.
     if (!exists($self->config->{'id_field'})) {
         my @pks = $self->{'resultset'}->result_source->primary_columns;
         if ($#pks == 0) {
@@ -30,6 +31,9 @@ sub new {
             Catalyst::Exception->throw("user table does not contain a single primary key column - please specify 'id_field' in config!");
         }
     }
+    if (not $self->{'resultset'}) {
+        Catalyst::Exception->throw("\$c->model('${ \$self->config->{user_class} }') did not return a resultset. Did you set user_class correctly?");
+    }
     if (!$self->{'resultset'}->result_source->has_column($self->config->{'id_field'})) {
         Catalyst::Exception->throw("id_field set to " .  $self->config->{'id_field'} . " but user table has no column by that name!");
     }
@@ -79,7 +83,11 @@ sub load {
                 $searchargs->{$key} = $authinfo->{$key};
             }
         }
-        $self->_user($self->resultset->search($searchargs)->first);
+        if (keys %{$searchargs}) {
+            $self->_user($self->resultset->search($searchargs)->first);
+        } else {
+            Catalyst::Exception->throw("User retrieval failed: no columns from " . $self->config->{'user_class'} . " were provided");
+        }
     }
 
     if ($self->get_object) {
@@ -114,7 +122,7 @@ sub roles {
     if (exists($self->config->{'role_column'})) {
         my $role_data = $self->get($self->config->{'role_column'});
         if ($role_data) { 
-            @roles = split /[ ,\|]+/, $self->get($self->config->{'role_column'});
+            @roles = split /[\s,\|]+/, $self->get($self->config->{'role_column'});
         }
         $self->_roles(\@roles);
     } elsif (exists($self->config->{'role_relation'})) {
@@ -135,15 +143,43 @@ sub roles {
 sub for_session {
     my $self = shift;
     
-    return $self->get($self->config->{'id_field'});
+    #return $self->get($self->config->{'id_field'});
+    
+    #my $frozenuser = $self->_user->result_source->schema->freeze( $self->_user );
+    #return $frozenuser;
+    
+    my %userdata = $self->_user->get_columns();
+    return \%userdata;
 }
 
 sub from_session {
     my ($self, $frozenuser, $c) = @_;
     
-    # this could be a lot better.  But for now it just assumes $frozenuser is an id and uses find_user
-    # XXX: hits the database on every request?  Not good...
-    return $self->load( { $self->config->{'id_field'} => $frozenuser }, $c);
+    #my $obj = $self->resultset->result_source->schema->thaw( $frozenuser );
+    #$self->_user($obj);
+    
+    #if (!exists($self->config->{'use_userdata_from_session'}) || $self->config->{'use_userdata_from_session'} == 0) {
+#        $self->_user->discard_changes();
+#    }
+#    
+#    return $self;
+#    
+## if use_userdata_from_session is defined in the config, we fill in the user data from the session.
+    if (exists($self->config->{'use_userdata_from_session'}) && $self->config->{'use_userdata_from_session'} != 0)
+    {
+        my $obj = $self->resultset->new_result({ %$frozenuser });
+        $obj->in_storage(1);
+        $self->_user($obj);
+        return $self;
+    } else {
+        my $id;
+        if (ref($frozenuser) eq 'HASH') {
+            $id = $frozenuser->{$self->config->{'id_field'}};
+        } else {
+            $id = $frozenuser;
+        }
+        return $self->load( { $self->config->{'id_field'} => $id }, $c);
+    }
 }
 
 sub get {
@@ -157,15 +193,30 @@ sub get {
 }
 
 sub get_object {
-    my $self = shift;
+    my ($self, $force) = @_;
     
+    if ($force) {
+        $self->_user->discard_changes;
+    }
+
     return $self->_user;
 }
 
 sub obj {
-    my $self = shift;
+    my ($self, $force) = @_;
     
-    return $self->get_object;
+    return $self->get_object($force);
+}
+
+sub auto_create {
+    my $self = shift;
+    $self->_user( $self->resultset->auto_create( @_ ) );
+    return $self;
+}
+
+sub auto_update {
+    my $self = shift;
+    $self->_user->auto_update( @_ );
 }
 
 sub AUTOLOAD {
@@ -181,8 +232,8 @@ __END__
 
 =head1 NAME
 
-Catalyst::Plugin::Authentication::Store::DBIx::Class::User - The backing user
-class for the Catalyst::Plugin::Authentication::Store::DBIx::Class storage
+Catalyst::Authentication::Store::DBIx::Class::User - The backing user
+class for the Catalyst::Authentication::Store::DBIx::Class storage
 module.
 
 =head1 VERSION
@@ -192,7 +243,7 @@ This documentation refers to version 0.10.
 =head1 SYNOPSIS
 
 Internal - not used directly, please see
-L<Catalyst::Plugin::Authentication::Store::DBIx::Class> for details on how to
+L<Catalyst::Authentication::Store::DBIx::Class> for details on how to
 use this module. If you need more information than is present there, read the
 source.
 
@@ -200,7 +251,7 @@ source.
 
 =head1 DESCRIPTION
 
-The Catalyst::Plugin::Authentication::Store::DBIx::Class::User class implements user storage
+The Catalyst::Authentication::Store::DBIx::Class::User class implements user storage
 connected to an underlying DBIx::Class schema object.
 
 =head1 SUBROUTINES / METHODS
@@ -223,13 +274,11 @@ Returns an array of roles associated with this user, if roles are configured for
 
 =head2 for_session
 
-Returns a serialized user for storage in the session.  Currently, this is the value of the field
-specified by the 'id_field' config variable.
+Returns a serialized user for storage in the session.  
 
 =head2 from_session
 
-Revives a serialized user from storage in the session.  Currently, this uses the serialized data as the
-value of the 'id_field' config variable.
+Revives a serialized user from storage in the session. 
 
 =head2 get ( $fieldname )
 
@@ -244,6 +293,29 @@ Retrieves the DBIx::Class object that corresponds to this user
 
 Synonym for get_object
 
+=head2 auto_create
+
+This is called when the auto_create_user option is turned on in 
+Catalyst::Plugin::Authentication and a user matching the authinfo provided is not found. 
+By default, this will call the C<auto_create()> method of the resultset associated
+with this object. It is up to you to implement that method.
+
+=head2 auto_update
+
+This is called when the auto_update_user option is turned on in
+Catalyst::Plugin::Authentication. Note that by default the DBIx::Class store
+uses every field in the authinfo hash to match the user. This means any
+information you provide with the intent to update must be ignored during the
+user search process. Otherwise the information will most likely cause the user
+record to not be found. To ignore fields in the search process, you
+have to add the fields you wish to update to the 'ignore_fields_in_find'
+authinfo element.  Alternately, you can use one of the advanced row retrieval
+methods (searchargs or resultset).
+
+By default, auto_update will call the C<auto_update()> method of the
+DBIx::Class::Row object associated with the user. It is up to you to implement
+that method (probably in your schema file)
+
 =head1 BUGS AND LIMITATIONS
 
 None known currently, please email the author if you find any.