Renaming Backend.pm to Class.pm
Jay Kuri [Sat, 16 Dec 2006 19:46:47 +0000 (19:46 +0000)]
Updating to use Class::Accessor::Fast
Various tweaks and changes

lib/Catalyst/Plugin/Authentication/Store/DBIx/Class.pm [moved from lib/Catalyst/Plugin/Authentication/Store/DBIx/Class/Backend.pm with 95% similarity]
lib/Catalyst/Plugin/Authentication/Store/DBIx/Class.pod [deleted file]
lib/Catalyst/Plugin/Authentication/Store/DBIx/Class/User.pm

@@ -1,4 +1,4 @@
-package Catalyst::Plugin::Authentication::Store::DBIx::Class::Backend;
+package Catalyst::Plugin::Authentication::Store::DBIx::Class;
 
 use strict;
 use warnings;
@@ -22,7 +22,7 @@ sub new {
     Catalyst::Utils::ensure_class_loaded( $config->{'store_user_class'} );
     
     ## fields can be specified to be ignored during user location.  This allows
-    ## the backend to ignore certain fields in the authinfo hash.
+    ## the store to ignore certain fields in the authinfo hash.
     
     $config->{'ignore_fields_in_find'} ||= [ ];
 
diff --git a/lib/Catalyst/Plugin/Authentication/Store/DBIx/Class.pod b/lib/Catalyst/Plugin/Authentication/Store/DBIx/Class.pod
deleted file mode 100644 (file)
index bd9afb2..0000000
+++ /dev/null
@@ -1,101 +0,0 @@
-package Catalyst::Plugin::Authentication::Store::DBIx::Class;
-
-use warnings;
-use strict;
-
-=head1 NAME
-
-Catalyst::Plugin::Authentication::Store::DBIx::Class - The great new Catalyst::Plugin::Authentication::Store::DBIx::Class!
-
-=head1 VERSION
-
-Version 0.01
-
-=cut
-
-our $VERSION = '0.01';
-
-=head1 SYNOPSIS
-
-Quick summary of what the module does.
-
-Perhaps a little code snippet.
-
-    use Catalyst::Plugin::Authentication::Store::DBIx::Class;
-
-    my $foo = Catalyst::Plugin::Authentication::Store::DBIx::Class->new();
-    ...
-
-=head1 EXPORT
-
-A list of functions that can be exported.  You can delete this section
-if you don't export anything, such as for a purely object-oriented module.
-
-=head1 FUNCTIONS
-
-=head2 function1
-
-=cut
-
-sub function1 {
-}
-
-=head2 function2
-
-=cut
-
-sub function2 {
-}
-
-=head1 AUTHOR
-
-Jay Kuri, C<< <bsdmac at gmail.com> >>
-
-=head1 BUGS
-
-Please report any bugs or feature requests to
-C<bug-catalyst-plugin-authentication-store-dbix-class at rt.cpan.org>, or through the web interface at
-L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Catalyst-Plugin-Authentication-Store-DBIx-Class>.
-I will be notified, and then you'll automatically be notified of progress on
-your bug as I make changes.
-
-=head1 SUPPORT
-
-You can find documentation for this module with the perldoc command.
-
-    perldoc Catalyst::Plugin::Authentication::Store::DBIx::Class
-
-You can also look for information at:
-
-=over 4
-
-=item * AnnoCPAN: Annotated CPAN documentation
-
-L<http://annocpan.org/dist/Catalyst-Plugin-Authentication-Store-DBIx-Class>
-
-=item * CPAN Ratings
-
-L<http://cpanratings.perl.org/d/Catalyst-Plugin-Authentication-Store-DBIx-Class>
-
-=item * RT: CPAN's request tracker
-
-L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Catalyst-Plugin-Authentication-Store-DBIx-Class>
-
-=item * Search CPAN
-
-L<http://search.cpan.org/dist/Catalyst-Plugin-Authentication-Store-DBIx-Class>
-
-=back
-
-=head1 ACKNOWLEDGEMENTS
-
-=head1 COPYRIGHT & LICENSE
-
-Copyright 2006 Jay Kuri, all rights reserved.
-
-This program is free software; you can redistribute it and/or modify it
-under the same terms as Perl itself.
-
-=cut
-
-1; # End of Catalyst::Plugin::Authentication::Store::DBIx::Class
index 01d72dc..b65b36a 100644 (file)
@@ -3,72 +3,86 @@ package Catalyst::Plugin::Authentication::Store::DBIx::Class::User;
 use strict;
 use warnings;
 use base qw/Catalyst::Plugin::Authentication::User/;
+use base qw/Class::Accessor::Fast/;
+
+BEGIN {
+    __PACKAGE__->mk_accessors(qw/config resultset _user _roles/);
+}
 
 sub new {
-    my ( $class, $authinfo, $config, $c, $lazyload) = @_;
+    my ( $class, $config, $c) = @_;
 
-    my $self = {};
-    $self->{'resultset'} = $c->model($config->{'user_class'});
-    $self->{'config'} = $config;
-    $self->{'authinfo'} = {%{$authinfo}};
+    my $self = {
+        resultset => $c->model($config->{'user_class'}),
+        config => $config,
+        _user => undef
+    };
     
     bless $self, $class;
     
+
+    
     ## 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
     ## lazyload to off.
-    $lazyload = 0;
-    
-    if (!$lazyload) {
-        $self->load_user($authinfo, $c);
-        if (!$self->{'user'}) {
-            return;
-        }
-    } else {
-        ## what do we do with a lazyload?
-        ## presumably this is coming out of session storage.  
-        ## use $authinfo to fill in the user in that case?
-    }
+    $self->config->{lazyload} = 0;
     
+#    if (!$self->config->{lazyload}) {
+#        return $self->load_user($authinfo, $c);
+#    } else {
+#        ## what do we do with a lazyload?
+#        ## presumably this is coming out of session storage.  
+#        ## use $authinfo to fill in the user in that case?
+#    }
+
     return $self;
 }
 
 
-sub load_user {
+sub load {
     my ($self, $authinfo, $c) = @_;
     
+    my $dbix_class_config = 0;
+    
+    if (exists($authinfo->{'dbix_class'})) {
+        $authinfo = $authinfo->{'dbix_class'};
+        $dbix_class_config = 1;
+    }
+    
     ## User can provide an arrayref containing the arguments to search on the user class.
-    ## allowing maximum flexibility for authentication.
-    if ($authinfo->{'searchargs'}) {
-        $self->{user} = $self->{'resultset'}->search(@{$authinfo->{'searchargs'}})->first;
+    ## or even provide a prepared resultset, allowing maximum flexibility for user retreival.
+    ## these options are only available when using the dbix_class authinfo hash. 
+    if ($dbix_class_config && exists($authinfo->{'resultset'})) {
+        $self->_user($authinfo->{'resultset'}->first);
+    } elsif ($dbix_class_config && exists($authinfo->{'searchargs'})) {
+        $self->_user($self->resultset->search(@{$authinfo->{'searchargs'}})->first);    
     } else {
         ## merge the ignore fields array into a hash - so we can do an easy check while building the query
-        my %ignorefields = map { $_ => 1} @{$self->{'config'}{'ignore_fields_in_find'}},
-                                    ( ref $authinfo->{'ignore_fields'} eq 'ARRAY' ? @{$authinfo->{'ignore_fields'}} : () );
-                                    
+        my %ignorefields = map { $_ => 1} @{$self->config->{'ignore_fields_in_find'}};                                    
         my $searchargs = {};
         
         # now we walk all the fields passed in, and build up a search hash.
         foreach my $key (grep {!$ignorefields{$_}} keys %{$authinfo}) {
-
-            if ($self->{'resultset'}->result_source->has_column($key)) {
+            if ($self->resultset->result_source->has_column($key)) {
                 $searchargs->{$key} = $authinfo->{$key};
             }
-        }  
-        $self->{user} = $self->{'resultset'}->search($searchargs)->first;
+        }
+        $self->_user($self->resultset->search($searchargs)->first);
+    }
+
+    if ($self->get_object) {
+        return $self
+    } else {
+        return undef;
     }
-   #$c->log->debug(dumper($self->{'user'}));
+    #$c->log->debug(dumper($self->{'user'}));
 
 }
 
 sub supported_features {
     my $self = shift;
-    $self->{'config'}{'password_type'} = 'clear';
 
     return {
-        password => {
-            $self->{'config'}{'password_type'} => 1,
-        },
         session         => 1,
         roles           => 1,
     };
@@ -79,39 +93,49 @@ sub roles {
     my ( $self, @wanted_roles ) = @_;
 
     ## shortcut if we have already retrieved them
-    if (ref $self->{'roles'} eq 'ARRAY') {
-        return(@{$self->{'roles'}});
+    if (ref $self->_roles eq 'ARRAY') {
+        return(@{$self->_roles});
     }
     
     my @roles = ();
-    if (exists($self->{'config'}{'role_column'})) {
-        @roles = split /[ ,\|]/, $self->get($self->{'config'}{'role_column'});
-        $self->{'roles'} = \@roles;
-    } elsif (exists($self->{'config'}{'role_relation'})) {
-        my $relation = $self->{'config'}{'role_relation'};
-        if ($self->{'user'}->$relation->result_source->has_column($self->{'config'}{'role_field'})) {
-            @roles = $self->{'user'}->$relation->search(undef, { columns => [ $self->{'config'}{'role_field'}]})->all();
+    if (exists($self->config->{'role_column'})) {
+        @roles = split /[ ,\|]/, $self->get($self->config->{'role_column'});
+        $self->_roles = \@roles;
+    } elsif (exists($self->config->{'role_relation'})) {
+        my $relation = $self->config->{'role_relation'};
+        if ($self->_user->$relation->result_source->has_column($self->config->{'role_field'})) {
+            @roles = $self->_user->$relation->search(undef, { columns => [ $self->config->{'role_field'}]})->all();
         } else {
-            Catalyst::Exception->throw("role table does not have a column called " . $self->{'config'}{'role_field'});
+            Catalyst::Exception->throw("role table does not have a column called " . $self->config->{'role_field'});
         }
-        my $rolefield = $self->{'config'}{'role_field'};
-        @{$self->{'roles'}} =  map { $_->get_column($self->{'config'}{'role_field'}) } @roles;
+        my $rolefield = $self->config->{'role_field'};
+        @{$self->_roles} =  map { $_->get_column($self->config->{'role_field'}) } @roles;
     } else {
         Catalyst::Exception->throw("user->roles accessed, but no role configuration found");
     }
 
-    return @{$self->{'roles'}};
+    return @{$self->_roles};
 }
 
 sub for_session {
-    shift->id;
+    my $self = shift;
+    
+    return $self->get('id');
+}
+
+sub from_session {
+    my ($self, $frozenuser, $c) = @_;
+    
+    # this could be a lot better.  But for now it just assumes $frozenuser is an id and uses find_user
+    # XXX: hits the database on every request?  Not good...
+    return $self->load( { id => $frozenuser }, $c);
 }
 
 sub get {
     my ($self, $field) = @_;
     
-    if ($self->{'user'}->can($field)) {
-        return $self->{'user'}->$field;
+    if ($self->_user->can($field)) {
+        return $self->_user->$field;
     } else {
         return undef;
     }
@@ -119,13 +143,14 @@ sub get {
 
 sub obj {
     my $self = shift;
+    
     return $self->get_object;
 }
 
 sub get_object {
     my $self = shift;
     
-    return $self->{'user'};
+    return $self->_user;
 }
 
 sub AUTOLOAD {
@@ -133,7 +158,7 @@ sub AUTOLOAD {
     (my $method) = (our $AUTOLOAD =~ /([^:]+)$/);
     return if $method eq "DESTROY";
 
-    $self->{'user'}->$method(@_);
+    $self->_user->$method(@_);
 }
 
 1;