From: Yuval Kogman Date: Mon, 21 Nov 2005 20:46:30 +0000 (+0000) Subject: Refactor + perltidy of Authentication X-Git-Tag: v0.01~14 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=12dae309c0ef75b277ef95cd5c97d80a9718554f;p=catagits%2FCatalyst-Plugin-Authentication.git Refactor + perltidy of Authentication --- diff --git a/lib/Catalyst/Plugin/Authentication.pm b/lib/Catalyst/Plugin/Authentication.pm index 5518495..f0b34db 100644 --- a/lib/Catalyst/Plugin/Authentication.pm +++ b/lib/Catalyst/Plugin/Authentication.pm @@ -13,6 +13,7 @@ use strict; use warnings; use Tie::RefHash; +use Class::Inspector; our $VERSION = "0.01"; @@ -23,14 +24,20 @@ sub set_authenticated { if ( $c->isa("Catalyst::Plugin::Session") and $c->config->{authentication}{use_session} - and $user->supports("session") -) + and $user->supports("session") ) { - $c->session->{__user_store} = $c->get_auth_store_name( $user->store ); - $c->session->{__user} = $user->for_session; + $c->save_user_in_session($user); } } +sub save_user_in_session { + my ( $c, $user ) = @_; + + my $store = $user->store || ref $user; + $c->session->{__user_store} = $c->get_auth_store_name($store) || $store; + $c->session->{__user} = $user->for_session; +} + sub logout { my $c = shift; @@ -64,9 +71,9 @@ sub prepare { and !$c->user ) { if ( $c->sessionid and my $user_id = $c->session->{__user} ) { - my $store = $c->get_auth_store( $c->session->{__user_store} ); + 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 + $c->request->{user} = $c->user; # compatibility kludge } } @@ -76,7 +83,6 @@ sub prepare { sub setup { my $c = shift; - my $cfg = $c->config->{authentication} || {}; %$cfg = ( @@ -84,58 +90,58 @@ sub setup { %$cfg, ); - $c->register_auth_stores( - default => $cfg->{store}, - %{ $cfg->{stores} || {} }, - ); + $c->register_auth_stores( + default => $cfg->{store}, + %{ $cfg->{stores} || {} }, + ); $c->NEXT::setup(@_); } sub get_auth_store { - my ( $self, $name ) = @_; - $self->auth_stores->{$name}; + my ( $self, $name ) = @_; + $self->auth_stores->{$name} || ( Class::Inspector->loaded($name) && $name ); } sub get_auth_store_name { - my ( $self, $store ) = @_; - $self->auth_store_names->{$store}; + my ( $self, $store ) = @_; + $self->auth_store_names->{$store}; } sub register_auth_stores { - my ( $self, %new ) = @_; + 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; - } + 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({}); + my $self = shift; + $self->_auth_stores(@_) || $self->_auth_stores( {} ); } sub auth_store_names { - my $self = shift; + my $self = shift; - unless ($self->_auth_store_names) { - tie my %hash, 'Tie::RefHash'; - $self->_auth_store_names( \%hash ); - }; + unless ( $self->_auth_store_names ) { + tie my %hash, 'Tie::RefHash'; + $self->_auth_store_names( \%hash ); + } - $self->_auth_store_names; + $self->_auth_store_names; } sub default_auth_store { - my $self = shift; + my $self = shift; - if ( my $new = shift ) { - $self->register_auth_stores( default => $new ); - } + if ( my $new = shift ) { + $self->register_auth_stores( default => $new ); + } - $self->get_auth_store("default"); + $self->get_auth_store("default"); } __PACKAGE__; diff --git a/lib/Catalyst/Plugin/Authentication/Store/Minimal/Backend.pm b/lib/Catalyst/Plugin/Authentication/Store/Minimal/Backend.pm index e8ae826..b82261f 100644 --- a/lib/Catalyst/Plugin/Authentication/Store/Minimal/Backend.pm +++ b/lib/Catalyst/Plugin/Authentication/Store/Minimal/Backend.pm @@ -36,8 +36,6 @@ sub get_user { return $user; } elsif ( ref $user eq "HASH" ) { - $user->{store} = $self; - $user->{id} = $id; return bless $user, "Catalyst::Plugin::Authentication::User::Hash"; } else { diff --git a/lib/Catalyst/Plugin/Authentication/User/Hash.pm b/lib/Catalyst/Plugin/Authentication/User/Hash.pm index 2d9c202..49f92eb 100644 --- a/lib/Catalyst/Plugin/Authentication/User/Hash.pm +++ b/lib/Catalyst/Plugin/Authentication/User/Hash.pm @@ -26,7 +26,7 @@ sub id { sub store { my $self = shift; - $self->_accessor( "store", @_ ); + ref $self || $self; } sub _accessor { @@ -52,7 +52,7 @@ my %features = ( self_check => undef, }, roles => ["roles"], - session => [qw/store id/], + session => 1, ); sub supports { @@ -87,6 +87,11 @@ sub for_session { return $self; # let's hope we're serialization happy } +sub from_session { + my ( $self, $c, $user ) = @_; + $user; +} + __PACKAGE__; __END__