1 package Catalyst::Plugin::Authentication::Store::Minimal;
6 use Catalyst::Plugin::Authentication::User::Hash;
8 use base qw/Class::Accessor::Fast/;
11 __PACKAGE__->mk_accessors(qw/userhash/);
15 my ( $class, $config, $app, $realm) = @_;
17 bless { userhash => $config->{'users'} }, $class;
21 my ( $self, $c, $id ) = @_;
23 return $id if ref $id;
25 $self->find_user( { id => $id } );
28 ## this is not necessarily a good example of what find_user can do, since all we do is
29 ## look up with the id anyway. find_user can be used to locate a user based on other
30 ## combinations of data. See C::P::Authentication::Store::DBIx::Class for a better example
32 my ( $self, $userinfo, $c ) = @_;
34 my $id = $userinfo->{'id'};
36 $id ||= $userinfo->{'username'};
38 return unless exists $self->userhash->{$id};
40 my $user = $self->userhash->{$id};
43 if ( Scalar::Util::blessed($user)
44 and $user->isa('Catalyst::Plugin::Authentication::User::Hash') )
48 if ( ref $user eq "HASH" ) {
50 return bless $user, "Catalyst::Plugin::Authentication::User::Hash";
53 Catalyst::Exception->throw( "The user '$id' is a reference of type "
55 . " but should be a HASH" );
59 Catalyst::Exception->throw(
60 "The user '$id' is has to be a hash reference or an object");
69 # choose a random user
70 scalar keys %{ $self->userhash };
71 ( undef, my $user ) = each %{ $self->userhash };
76 ## Backwards compatibility
78 # This is a backwards compatible routine. get_user is specifically for loading a user by it's unique id
79 # find_user is capable of doing the same by simply passing { id => $id }
80 # no new code should be written using get_user as it is deprecated.
82 my ( $self, $id ) = @_;
83 $self->find_user({id => $id});
86 ## backwards compatibility
90 $c->default_auth_store(
92 $c->config->{authentication}, $c
107 Catalyst::Plugin::Authentication::Store::Minimal - Minimal authentication store
111 # you probably just want Store::Minimal under most cases,
112 # but if you insist you can instantiate your own store:
114 use Catalyst::Plugin::Authentication::Store::Minimal;
120 __PACKAGE__->config->{authentication} =
122 default_realm => 'members',
127 password_field => 'password',
128 password_type => 'clear'
134 password => "s00p3r",
136 roles => [qw/edit delete/],
139 password => "s3cr3t",
140 roles => [qw/comment/],
151 This authentication store lets you create a very quick and dirty user
152 database in your application's config hash.
154 You will need to include the Authentication plugin, and at least one Credential
155 plugin to use this Store. Credential::Password is reccommended.
157 It's purpose is mainly for testing, and it should probably be replaced by a
158 more "serious" store for production.
160 The hash in the config, as well as the user objects/hashes are freely mutable
169 The classname used for the store. This is part of
170 L<Catalyst::Plugin::Authentication> and is the method by which
171 Catalyst::Plugin::Authentication::Store::Minimal is loaded as the
172 user store. For this module to be used, this must be set to
177 This is a simple hash of users, the keys are the usenames, and the values are
178 hashrefs containing a password key/value pair, and optionally, a roles/list
179 of role-names pair. If using roles, you will also need to add the
180 Authorization::Roles plugin.
182 See the SYNOPSIS for an example.
188 There are no publicly exported routines in the Minimal store (or indeed in
189 most authentication stores) However, below is a description of the routines
190 required by L<Catalyst::Plugin::Authentication> for all authentication stores.
192 =head2 new( $config, $app, $realm )
194 Constructs a new store object, which uses the user element of the supplied config
195 hash ref as it's backing structure.
197 =head2 find_user( $authinfo, $c )
199 Keys the hash by the 'id' or 'username' element in the authinfo hash and returns the user.
201 ... documentation fairy stopped here. ...
203 If the return value is unblessed it will be blessed as
204 L<Catalyst::Plugin::Authentication::User::Hash>.
206 =head2 from_session( $id )
208 Delegates to C<get_user>.
210 =head2 user_supports( )
212 Chooses a random user from the hash and delegates to it.