Auth store registration
[catagits/Catalyst-Plugin-Authentication.git] / lib / Catalyst / Plugin / Authentication.pm
index 91f57f2..5518495 100644 (file)
@@ -6,12 +6,14 @@ use base qw/Class::Accessor::Fast Class::Data::Inheritable/;
 
 BEGIN {
     __PACKAGE__->mk_accessors(qw/user/);
-    __PACKAGE__->mk_classdata(qw/default_auth_store/);
+    __PACKAGE__->mk_classdata($_) for qw/_auth_stores _auth_store_names/;
 }
 
 use strict;
 use warnings;
 
+use Tie::RefHash;
+
 our $VERSION = "0.01";
 
 sub set_authenticated {
@@ -20,11 +22,12 @@ sub set_authenticated {
     $c->user($user);
 
     if (    $c->isa("Catalyst::Plugin::Session")
-        and $c->config->{authentication}{use_session} )
+        and $c->config->{authentication}{use_session}
+        and $user->supports("session")
+)
     {
-        $c->session->{__user} = $user->for_session
-          if $user->supports("session");
-        $c->session->{__user_class} = ref $user;
+        $c->session->{__user_store} = $c->get_auth_store_name( $user->store );
+        $c->session->{__user} = $user->for_session;
     }
 }
 
@@ -36,7 +39,7 @@ sub logout {
     if (    $c->isa("Catalyst::Plugin::Session")
         and $c->config->{authentication}{use_session} )
     {
-        delete @{ $c->session }{qw/__user __user_class/};
+        delete @{ $c->session }{qw/__user __user_store/};
     }
 }
 
@@ -60,8 +63,9 @@ sub prepare {
         and $c->default_auth_store
         and !$c->user )
     {
-        if ( $c->sessionid and my $user = $c->session->{__user} ) {
-            $c->user( $c->session->{__user_class}->from_session( $c, $user ) );
+        if ( $c->sessionid and my $user_id = $c->session->{__user} ) {
+                       my $store = $c->get_auth_store( $c->session->{__user_store} );
+            $c->user( $store->from_session( $c, $user_id ) );
             $c->request->{user} = $c->user; # compatibility kludge
         }
     }
@@ -72,6 +76,7 @@ sub prepare {
 sub setup {
     my $c = shift;
 
+
     my $cfg = $c->config->{authentication} || {};
 
     %$cfg = (
@@ -79,9 +84,60 @@ sub setup {
         %$cfg,
     );
 
+       $c->register_auth_stores(
+               default => $cfg->{store},
+               %{ $cfg->{stores} || {} },
+       );
+
     $c->NEXT::setup(@_);
 }
 
+sub get_auth_store {
+       my ( $self, $name ) = @_;
+       $self->auth_stores->{$name};
+}
+
+sub get_auth_store_name {
+       my ( $self, $store ) = @_;
+       $self->auth_store_names->{$store};
+}
+
+sub register_auth_stores {
+       my ( $self, %new ) = @_;
+
+       foreach my $name ( keys %new ) {
+               my $store = $new{$name} or next;
+               $self->auth_stores->{$name} = $store;
+               $self->auth_store_names->{$store} = $name;
+       }       
+}
+
+sub auth_stores {
+       my $self = shift;
+       $self->_auth_stores(@_) || $self->_auth_stores({});
+}
+
+sub auth_store_names {
+       my $self = shift;
+
+       unless ($self->_auth_store_names) {
+               tie my %hash, 'Tie::RefHash';
+               $self->_auth_store_names( \%hash );
+       };
+
+       $self->_auth_store_names;
+}
+
+sub default_auth_store {
+       my $self = shift;
+
+       if ( my $new = shift ) {
+               $self->register_auth_stores( default => $new );
+       }
+
+       $self->get_auth_store("default");
+}
+
 __PACKAGE__;
 
 __END__