Kill off Class::Accessor::Fast
[catagits/Catalyst-Plugin-Authentication.git] / lib / Catalyst / Authentication / Store / Minimal.pm
index b5cb140..921f8c4 100644 (file)
@@ -1,47 +1,50 @@
 package Catalyst::Authentication::Store::Minimal;
+use Moose;
+use namespace::autoclean;
 
-use strict;
-use warnings;
-
-use Catalyst::Authentication::User::Hash;
+with 'MooseX::Emulate::Class::Accessor::Fast';
 use Scalar::Util qw( blessed );
-use base qw/Class::Accessor::Fast/;
 
-BEGIN {
-    __PACKAGE__->mk_accessors(qw/userhash/);
-}
+__PACKAGE__->mk_accessors(qw/userhash userclass/);
 
 sub new {
     my ( $class, $config, $app, $realm) = @_;
 
-    bless { userhash => $config->{'users'} }, $class;
+    my $self = bless {
+        userhash => $config->{'users'},
+        userclass => $config->{'user_class'} || "Catalyst::Authentication::User::Hash",
+    }, $class;
+
+    Catalyst::Utils::ensure_class_loaded( $self->userclass );
+
+    return $self;
 }
 
 sub from_session {
-       my ( $self, $c, $id ) = @_;
+    my ( $self, $c, $id ) = @_;
 
-       return $id if ref $id;
+    return $id if ref $id;
 
-       $self->find_user( { id => $id } );
+    $self->find_user( { id => $id } );
 }
 
-## this is not necessarily a good example of what find_user can do, since all we do is   
-## look up with the id anyway.  find_user can be used to locate a user based on other 
+## this is not necessarily a good example of what find_user can do, since all we do is
+## look up with the id anyway.  find_user can be used to locate a user based on other
 ## combinations of data.  See C::P::Authentication::Store::DBIx::Class for a better example
 sub find_user {
     my ( $self, $userinfo, $c ) = @_;
 
     my $id = $userinfo->{'id'};
-    
+
     $id ||= $userinfo->{'username'};
-    
+
     return unless exists $self->userhash->{$id};
 
     my $user = $self->userhash->{$id};
 
     if ( ref($user) eq "HASH") {
         $user->{id} ||= $id;
-        return bless $user, "Catalyst::Authentication::User::Hash";
+        return bless $user, $self->userclass;
     } elsif ( ref($user) && blessed($user) && $user->isa('Catalyst::Authentication::User::Hash')) {
         return $user;
     } else {
@@ -64,7 +67,7 @@ sub user_supports {
 ## Backwards compatibility
 #
 # This is a backwards compatible routine.  get_user is specifically for loading a user by it's unique id
-# find_user is capable of doing the same by simply passing { id => $id }  
+# find_user is capable of doing the same by simply passing { id => $id }
 # no new code should be written using get_user as it is deprecated.
 sub get_user {
     my ( $self, $id ) = @_;
@@ -92,8 +95,8 @@ Catalyst::Authentication::Store::Minimal - Minimal authentication store
         Authentication
     /;
 
-    __PACKAGE__->config( 'Plugin::Authentication' => 
-                    {  
+    __PACKAGE__->config( 'Plugin::Authentication' =>
+                    {
                         default_realm => 'members',
                         realms => {
                             members => {
@@ -104,24 +107,24 @@ Catalyst::Authentication::Store::Minimal - Minimal authentication store
                                 },
                                 store => {
                                     class => 'Minimal',
-                                       users = {
-                                           bob => {
-                                               password => "s00p3r",                                       
-                                               editor => 'yes',
-                                               roles => [qw/edit delete/],
-                                           },
-                                           william => {
-                                               password => "s3cr3t",
-                                               roles => [qw/comment/],
-                                           }
-                                       }                       
-                                   }
-                               }
-                       }
+                                    users => {
+                                        bob => {
+                                            password => "s00p3r",
+                                            editor => 'yes',
+                                            roles => [qw/edit delete/],
+                                        },
+                                        william => {
+                                            password => "s3cr3t",
+                                            roles => [qw/comment/],
+                                        }
+                                    }
+                                }
+                            }
+                        }
                     }
     );
 
-    
+
 =head1 DESCRIPTION
 
 This authentication store lets you create a very quick and dirty user
@@ -140,7 +143,7 @@ at runtime.
 
 =over 4
 
-=item class 
+=item class
 
 The classname used for the store. This is part of
 L<Catalyst::Plugin::Authentication> and is the method by which
@@ -148,11 +151,17 @@ Catalyst::Authentication::Store::Minimal is loaded as the
 user store. For this module to be used, this must be set to
 'Minimal'.
 
+=item user_class
+
+The class used for the user object. If you don't specify a class name, the
+default L<Catalyst::Authentication::User::Hash> will be used. If you define your
+own class, it must inherit from L<Catalyst::Authentication::User::Hash>.
+
 =item users
 
 This is a simple hash of users, the keys are the usenames, and the values are
-hashrefs containing a password key/value pair, and optionally, a roles/list 
-of role-names pair. If using roles, you will also need to add the 
+hashrefs containing a password key/value pair, and optionally, a roles/list
+of role-names pair. If using roles, you will also need to add the
 Authorization::Roles plugin.
 
 See the SYNOPSIS for an example.
@@ -162,15 +171,15 @@ See the SYNOPSIS for an example.
 =head1 METHODS
 
 There are no publicly exported routines in the Minimal store (or indeed in
-most authentication stores)  However, below is a description of the routines 
+most authentication stores)  However, below is a description of the routines
 required by L<Catalyst::Plugin::Authentication> for all authentication stores.
 
 =head2 new( $config, $app, $realm )
 
-Constructs a new store object, which uses the user element of the supplied config 
+Constructs a new store object, which uses the user element of the supplied config
 hash ref as it's backing structure.
 
-=head2 find_user( $authinfo, $c ) 
+=head2 find_user( $authinfo, $c )
 
 Keys the hash by the 'id' or 'username' element in the authinfo hash and returns the user.