Auth store registration
Yuval Kogman [Mon, 21 Nov 2005 13:36:18 +0000 (13:36 +0000)]
lib/Catalyst/Plugin/Authentication.pm
lib/Catalyst/Plugin/Authentication/Store/Minimal/Backend.pm
lib/Catalyst/Plugin/Authentication/User/Hash.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__
index 7bb0a1c..e8ae826 100644 (file)
@@ -14,6 +14,14 @@ sub new {
     bless { hash => $hash }, $class;
 }
 
+sub from_session {
+       my ( $self, $c, $id ) = @_;
+
+       return $id if ref $id;
+
+       $self->get_user( $id );
+}
+
 sub get_user {
     my ( $self, $id ) = @_;
 
@@ -23,9 +31,13 @@ sub get_user {
 
     if ( ref $user ) {
         if ( Scalar::Util::blessed($user) ) {
+                       $user->store( $self );
+                       $user->id( $id );
             return $user;
         }
         elsif ( ref $user eq "HASH" ) {
+                       $user->{store} = $self;
+                       $user->{id} = $id;
             return bless $user, "Catalyst::Plugin::Authentication::User::Hash";
         }
         else {
index dbfb8a3..2d9c202 100644 (file)
@@ -16,6 +16,23 @@ sub AUTOLOAD {
     my $self = shift;
     ( my $key ) = ( our $AUTOLOAD =~ m/([^:]*)$/ );
 
+       $self->_accessor( $key, @_ );
+}
+
+sub id {
+       my $self = shift;
+       $self->_accessor( "id", @_ );
+}
+
+sub store {
+       my $self = shift;
+       $self->_accessor( "store", @_ );
+}
+
+sub _accessor {
+       my $self = shift;
+       my $key = shift;
+
     if (@_) {
         my $arr = $self->{__hash_obj_key_is_array}{$key} = @_ > 1;
         $self->{$key} = $arr ? [@_] : shift;
@@ -35,7 +52,7 @@ my %features = (
         self_check => undef,
     },
     roles   => ["roles"],
-    session => 1,
+    session => [qw/store id/],
 );
 
 sub supports {
@@ -67,16 +84,9 @@ sub supports {
 
 sub for_session {
     my $self = shift;
-
     return $self;    # let's hope we're serialization happy
 }
 
-sub from_session {
-    my ( $self, $c, $user ) = @_;
-
-    return $user;    # if we're serialization happy this should work
-}
-
 __PACKAGE__;
 
 __END__