Try supporting compound primary keys.
Florian Ragwitz [Mon, 29 Mar 2010 00:35:26 +0000 (00:35 +0000)]
lib/Catalyst/Authentication/Store/DBIx/Class/User.pm

index f619f5d..8a7936c 100644 (file)
@@ -2,6 +2,7 @@ package Catalyst::Authentication::Store::DBIx::Class::User;
 
 use strict;
 use warnings;
+use List::MoreUtils qw(all);
 use base qw/Catalyst::Authentication::User/;
 use base qw/Class::Accessor::Fast/;
 
@@ -31,19 +32,14 @@ sub new {
         Catalyst::Exception->throw("\$c->model('${ \$self->config->{user_model} }') did not return a resultset. Did you set user_model correctly?");
     }
 
-    ## 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) {
-            $self->config->{'id_field'} = $pks[0];
-        } else {
-            Catalyst::Exception->throw("user table does not contain a single primary key column - please specify 'id_field' in config!");
-        }
-    }
+    $self->config->{'id_field'} = [$self->{'resultset'}->result_source->primary_columns]
+        unless exists $self->config->{'id_field'};
 
-    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!");
-    }
+    $self->config->{'id_field'} = [$self->config->{'id_field'}]
+        unless ref $self->config->{'id_field'} eq 'ARRAY';
+
+    Catalyst::Exception->throw("id_field set to " . join(q{,} => @{ $self->config->{'id_field'} }) . " but user table has no column by that name!")
+        unless all { $self->{'resultset'}->result_source->has_column($_) } @{ $self->config->{'id_field'} };
 
     ## if we have lazyloading turned on - we should not query the DB unless something gets read.
     ## that's the idea anyway - still have to work out how to manage that - so for now we always force
@@ -171,21 +167,21 @@ sub from_session {
 #    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)
-    {
+    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);
     }
+
+    if (ref $frozenuser eq 'HASH') {
+        return $self->load({
+            map { ($_ => $frozenuser->{$_}) }
+            @{ $self->config->{id_field} }
+        });
+    }
+
+    return $self->load( { $self->config->{'id_field'} => $frozenuser }, $c);
 }
 
 sub get {