1 package Catalyst::Authentication::Store::Minimal;
3 use namespace::autoclean;
5 with 'MooseX::Emulate::Class::Accessor::Fast';
6 use Scalar::Util qw( blessed );
8 __PACKAGE__->mk_accessors(qw/userhash userclass/);
11 my ( $class, $config, $app, $realm) = @_;
14 userhash => $config->{'users'},
15 userclass => $config->{'user_class'} || "Catalyst::Authentication::User::Hash",
18 Catalyst::Utils::ensure_class_loaded( $self->userclass );
24 my ( $self, $c, $id ) = @_;
26 return $id if ref $id;
28 $self->find_user( { id => $id } );
31 ## this is not necessarily a good example of what find_user can do, since all we do is
32 ## look up with the id anyway. find_user can be used to locate a user based on other
33 ## combinations of data. See C::P::Authentication::Store::DBIx::Class for a better example
35 my ( $self, $userinfo, $c ) = @_;
37 my $id = $userinfo->{'id'};
39 $id ||= $userinfo->{'username'};
41 return unless exists $self->userhash->{$id};
43 my $user = $self->userhash->{$id};
45 if ( ref($user) eq "HASH") {
47 return bless $user, $self->userclass;
48 } elsif ( ref($user) && blessed($user) && $user->isa('Catalyst::Authentication::User::Hash')) {
51 Catalyst::Exception->throw( "The user '$id' must be a hash reference or an " .
52 "object of class Catalyst::Authentication::User::Hash");
60 # choose a random user
61 scalar keys %{ $self->userhash };
62 ( undef, my $user ) = each %{ $self->userhash };
67 ## Backwards compatibility
69 # This is a backwards compatible routine. get_user is specifically for loading a user by it's unique id
70 # find_user is capable of doing the same by simply passing { id => $id }
71 # no new code should be written using get_user as it is deprecated.
73 my ( $self, $id ) = @_;
74 $self->find_user({id => $id});
85 Catalyst::Authentication::Store::Minimal - Minimal authentication store
89 # you probably just want Store::Minimal under most cases,
90 # but if you insist you can instantiate your own store:
92 use Catalyst::Authentication::Store::Minimal;
98 __PACKAGE__->config( 'Plugin::Authentication' =>
100 default_realm => 'members',
105 password_field => 'password',
106 password_type => 'clear'
112 password => "s00p3r",
114 roles => [qw/edit delete/],
117 password => "s3cr3t",
118 roles => [qw/comment/],
130 This authentication store lets you create a very quick and dirty user
131 database in your application's config hash.
133 You will need to include the Authentication plugin, and at least one Credential
134 plugin to use this Store. Credential::Password is reccommended.
136 It's purpose is mainly for testing, and it should probably be replaced by a
137 more "serious" store for production.
139 The hash in the config, as well as the user objects/hashes are freely mutable
148 The classname used for the store. This is part of
149 L<Catalyst::Plugin::Authentication> and is the method by which
150 Catalyst::Authentication::Store::Minimal is loaded as the
151 user store. For this module to be used, this must be set to
156 The class used for the user object. If you don't specify a class name, the
157 default L<Catalyst::Authentication::User::Hash> will be used. If you define your
158 own class, it must inherit from L<Catalyst::Authentication::User::Hash>.
162 This is a simple hash of users, the keys are the usenames, and the values are
163 hashrefs containing a password key/value pair, and optionally, a roles/list
164 of role-names pair. If using roles, you will also need to add the
165 Authorization::Roles plugin.
167 See the SYNOPSIS for an example.
173 There are no publicly exported routines in the Minimal store (or indeed in
174 most authentication stores) However, below is a description of the routines
175 required by L<Catalyst::Plugin::Authentication> for all authentication stores.
177 =head2 new( $config, $app, $realm )
179 Constructs a new store object, which uses the user element of the supplied config
180 hash ref as it's backing structure.
182 =head2 find_user( $authinfo, $c )
184 Keys the hash by the 'id' or 'username' element in the authinfo hash and returns the user.
186 ... documentation fairy stopped here. ...
188 If the return value is unblessed it will be blessed as
189 L<Catalyst::Authentication::User::Hash>.
191 =head2 from_session( $id )
193 Delegates to C<get_user>.
195 =head2 user_supports( )
197 Chooses a random user from the hash and delegates to it.