Support e.g. ->roles in User::Hash if set appropriately
Yuval Kogman [Mon, 7 Nov 2005 13:53:47 +0000 (13:53 +0000)]
lib/Catalyst/Plugin/Authentication.pm
lib/Catalyst/Plugin/Authentication/User/Hash.pm
t/live_app.t

index 0b40b18..d5173ae 100644 (file)
@@ -57,7 +57,7 @@ sub prepare {
     my $c = shift->NEXT::prepare(@_);
 
     if (    $c->isa("Catalyst::Plugin::Session")
-        and $c->config->{authentication}{use_session}
+        and $c->default_auth_store
         and !$c->user )
     {
         if ( $c->sessionid and my $user = $c->session->{__user} ) {
index 62a0898..3da2b46 100644 (file)
@@ -16,8 +16,14 @@ sub AUTOLOAD {
     my $self = shift;
     ( my $key ) = ( our $AUTOLOAD =~ m/([^:]*)$/ );
 
-    $self->{$key} = shift if @_;
-    $self->{$key};
+       
+       if ( @_ ) {
+               my $arr = $self->{__hash_obj_key_is_array}{$key} = @_ > 1;
+               $self->{$key} = $arr ? [ @_ ] : shift;
+       }
+
+       my $data = $self->{$key};
+    $self->{__hash_obj_key_is_array}{$key} ? @$data : $data;
 }
 
 my %features = (
@@ -25,7 +31,9 @@ my %features = (
         clear   => ["password"],
         crypted => ["crypted_password"],
         hashed  => [qw/hashed_password hash_algorithm/],
+               self_check => undef,
     },
+       roles => ["roles"],
     session => 1,
 );
 
@@ -37,18 +45,22 @@ sub supports {
     # traverse the feature list,
     for (@spec) {
         die "bad feature spec: @spec"
-          if ref($cursor) ne "HASH"
-          or !ref( $cursor = $cursor->{$_} );
+          if ref($cursor) ne "HASH";
+        $cursor = $cursor->{$_};
     }
 
-    die "bad feature spec: @spec" unless ref $cursor eq "ARRAY";
+       if (ref $cursor) {
+               die "bad feature spec: @spec" unless ref $cursor eq "ARRAY";
 
-    # check that all the keys required for a feature are in here
-    foreach my $key (@$cursor) {
-        return undef unless exists $self->{$key};
-    }
+               # check that all the keys required for a feature are in here
+               foreach my $key (@$cursor) {
+                       return undef unless exists $self->{$key};
+               }
 
-    return 1;
+               return 1;
+       } else {
+               return $cursor;
+       }
 }
 
 sub for_session {
index 8802f04..b2070c2 100644 (file)
@@ -26,10 +26,16 @@ use Test::More 'no_plan';
                ok(!$c->user, "no user");
                ok($c->login( "foo", "s3cr3t" ), "can login with clear");
                is( $c->user, $users->{foo}, "user object is in proper place");
-               $c->logout;
 
+               ok( !$c->user->roles, "no roles for foo" );
+               my @new = qw/foo bar gorch/;
+               $c->user->roles( @new );
+               is_deeply( [ $c->user->roles ], \@new, "roles set as array");
+
+               $c->logout;
                ok(!$c->user, "no more user, after logout");
 
+
                ok($c->login( "bar", "s3cr3t" ), "can login with crypted");
                is( $c->user, $users->{bar}, "user object is in proper place");
                $c->logout;
@@ -42,6 +48,8 @@ use Test::More 'no_plan';
                ok(!$c->user, "no user");
 
                throws_ok { $c->login( "baz", "foo" ) } qr/support.*mechanism/, "can't login without any supported mech";
+
+               
        }
 
        __PACKAGE__->config->{authentication}{users} = $users = {