1 package Catalyst::Authentication::Store::Minimal;
6 use Scalar::Util qw( blessed );
7 use base qw/Class::Accessor::Fast/;
10 __PACKAGE__->mk_accessors(qw/userhash userclass/);
14 my ( $class, $config, $app, $realm) = @_;
17 userhash => $config->{'users'},
18 userclass => $config->{'user_class'} || "Catalyst::Authentication::User::Hash",
21 Catalyst::Utils::ensure_class_loaded( $self->userclass );
27 my ( $self, $c, $id ) = @_;
29 return $id if ref $id;
31 $self->find_user( { id => $id } );
34 ## this is not necessarily a good example of what find_user can do, since all we do is
35 ## look up with the id anyway. find_user can be used to locate a user based on other
36 ## combinations of data. See C::P::Authentication::Store::DBIx::Class for a better example
38 my ( $self, $userinfo, $c ) = @_;
40 my $id = $userinfo->{'id'};
42 $id ||= $userinfo->{'username'};
44 return unless exists $self->userhash->{$id};
46 my $user = $self->userhash->{$id};
48 if ( ref($user) eq "HASH") {
50 return bless $user, $self->userclass;
51 } elsif ( ref($user) && blessed($user) && $user->isa('Catalyst::Authentication::User::Hash')) {
54 Catalyst::Exception->throw( "The user '$id' must be a hash reference or an " .
55 "object of class Catalyst::Authentication::User::Hash");
63 # choose a random user
64 scalar keys %{ $self->userhash };
65 ( undef, my $user ) = each %{ $self->userhash };
70 ## Backwards compatibility
72 # This is a backwards compatible routine. get_user is specifically for loading a user by it's unique id
73 # find_user is capable of doing the same by simply passing { id => $id }
74 # no new code should be written using get_user as it is deprecated.
76 my ( $self, $id ) = @_;
77 $self->find_user({id => $id});
88 Catalyst::Authentication::Store::Minimal - Minimal authentication store
92 # you probably just want Store::Minimal under most cases,
93 # but if you insist you can instantiate your own store:
95 use Catalyst::Authentication::Store::Minimal;
101 __PACKAGE__->config( 'Plugin::Authentication' =>
103 default_realm => 'members',
108 password_field => 'password',
109 password_type => 'clear'
115 password => "s00p3r",
117 roles => [qw/edit delete/],
120 password => "s3cr3t",
121 roles => [qw/comment/],
133 This authentication store lets you create a very quick and dirty user
134 database in your application's config hash.
136 You will need to include the Authentication plugin, and at least one Credential
137 plugin to use this Store. Credential::Password is reccommended.
139 It's purpose is mainly for testing, and it should probably be replaced by a
140 more "serious" store for production.
142 The hash in the config, as well as the user objects/hashes are freely mutable
151 The classname used for the store. This is part of
152 L<Catalyst::Plugin::Authentication> and is the method by which
153 Catalyst::Authentication::Store::Minimal is loaded as the
154 user store. For this module to be used, this must be set to
159 The class used for the user object. If you don't specify a class name, the
160 default L<Catalyst::Authentication::User::Hash> will be used. If you define your
161 own class, it must inherit from L<Catalyst::Authentication::User::Hash>.
165 This is a simple hash of users, the keys are the usenames, and the values are
166 hashrefs containing a password key/value pair, and optionally, a roles/list
167 of role-names pair. If using roles, you will also need to add the
168 Authorization::Roles plugin.
170 See the SYNOPSIS for an example.
176 There are no publicly exported routines in the Minimal store (or indeed in
177 most authentication stores) However, below is a description of the routines
178 required by L<Catalyst::Plugin::Authentication> for all authentication stores.
180 =head2 new( $config, $app, $realm )
182 Constructs a new store object, which uses the user element of the supplied config
183 hash ref as it's backing structure.
185 =head2 find_user( $authinfo, $c )
187 Keys the hash by the 'id' or 'username' element in the authinfo hash and returns the user.
189 ... documentation fairy stopped here. ...
191 If the return value is unblessed it will be blessed as
192 L<Catalyst::Authentication::User::Hash>.
194 =head2 from_session( $id )
196 Delegates to C<get_user>.
198 =head2 user_supports( )
200 Chooses a random user from the hash and delegates to it.