1 package Catalyst::Authentication::Store::Minimal;
3 use Catalyst::Authentication::User::Hash;
4 use namespace::autoclean;
6 has userhash => ( is => 'rw');
9 my ( $class, $config, $app, $realm) = @_;
11 { userhash => $config->{'users'} };
15 my ( $self, $c, $id ) = @_;
17 return $id if ref $id;
19 $self->find_user( { id => $id } );
22 ## this is not necessarily a good example of what find_user can do, since all we do is
23 ## look up with the id anyway. find_user can be used to locate a user based on other
24 ## combinations of data. See C::P::Authentication::Store::DBIx::Class for a better example
26 my ( $self, $userinfo, $c ) = @_;
28 my $id = $userinfo->{'id'};
30 $id ||= $userinfo->{'username'};
32 return unless exists $self->userhash->{$id};
34 my $user = $self->userhash->{$id};
36 if ( ref($user) eq "HASH") {
38 return bless $user, "Catalyst::Authentication::User::Hash";
39 } elsif ( ref($user) && blessed($user) && $user->isa('Catalyst::Authentication::User::Hash')) {
42 Catalyst::Exception->throw( "The user '$id' must be a hash reference or an " .
43 "object of class Catalyst::Authentication::User::Hash");
51 # choose a random user
52 scalar keys %{ $self->userhash };
53 ( undef, my $user ) = each %{ $self->userhash };
58 ## Backwards compatibility
60 # This is a backwards compatible routine. get_user is specifically for loading a user by it's unique id
61 # find_user is capable of doing the same by simply passing { id => $id }
62 # no new code should be written using get_user as it is deprecated.
64 my ( $self, $id ) = @_;
65 $self->find_user({id => $id});
76 Catalyst::Authentication::Store::Minimal - Minimal authentication store
80 # you probably just want Store::Minimal under most cases,
81 # but if you insist you can instantiate your own store:
83 use Catalyst::Authentication::Store::Minimal;
89 __PACKAGE__->config( 'Plugin::Authentication' =>
91 default_realm => 'members',
96 password_field => 'password',
97 password_type => 'clear'
103 password => "s00p3r",
105 roles => [qw/edit delete/],
108 password => "s3cr3t",
109 roles => [qw/comment/],
121 This authentication store lets you create a very quick and dirty user
122 database in your application's config hash.
124 You will need to include the Authentication plugin, and at least one Credential
125 plugin to use this Store. Credential::Password is reccommended.
127 It's purpose is mainly for testing, and it should probably be replaced by a
128 more "serious" store for production.
130 The hash in the config, as well as the user objects/hashes are freely mutable
139 The classname used for the store. This is part of
140 L<Catalyst::Plugin::Authentication> and is the method by which
141 Catalyst::Authentication::Store::Minimal is loaded as the
142 user store. For this module to be used, this must be set to
147 This is a simple hash of users, the keys are the usenames, and the values are
148 hashrefs containing a password key/value pair, and optionally, a roles/list
149 of role-names pair. If using roles, you will also need to add the
150 Authorization::Roles plugin.
152 See the SYNOPSIS for an example.
158 There are no publicly exported routines in the Minimal store (or indeed in
159 most authentication stores) However, below is a description of the routines
160 required by L<Catalyst::Plugin::Authentication> for all authentication stores.
162 =head2 new( $config, $app, $realm )
164 Constructs a new store object, which uses the user element of the supplied config
165 hash ref as it's backing structure.
167 =head2 find_user( $authinfo, $c )
169 Keys the hash by the 'id' or 'username' element in the authinfo hash and returns the user.
171 ... documentation fairy stopped here. ...
173 If the return value is unblessed it will be blessed as
174 L<Catalyst::Authentication::User::Hash>.
176 =head2 from_session( $id )
178 Delegates to C<get_user>.
180 =head2 user_supports( )
182 Chooses a random user from the hash and delegates to it.