3 package Catalyst::Plugin::Authentication::Store::Minimal::Backend;
8 use Catalyst::Plugin::Authentication::User::Hash;
12 my ( $class, $config, $app) = @_;
14 bless { hash => $config->{'users'} }, $class;
18 my ( $self, $c, $id ) = @_;
20 return $id if ref $id;
22 $self->find_user( { id => $id } );
25 ## this is not necessarily a good example of what find_user can do, since all we do is
26 ## look up with the id anyway. find_user can be used to locate a user based on other
27 ## combinations of data. See C::P::Authentication::Store::DBIx::Class for a better example
29 my ( $self, $userinfo, $c ) = @_;
31 my $id = $userinfo->{'id'};
33 $id ||= $userinfo->{'username'};
35 return unless exists $self->{'hash'}{$id};
37 my $user = $self->{'hash'}{$id};
40 if ( Scalar::Util::blessed($user) ) {
44 elsif ( ref $user eq "HASH" ) {
46 return bless $user, "Catalyst::Plugin::Authentication::User::Hash";
49 Catalyst::Exception->throw( "The user '$id' is a reference of type "
51 . " but should be a HASH" );
55 Catalyst::Exception->throw(
56 "The user '$id' is has to be a hash reference or an object");
65 # choose a random user
66 scalar keys %{ $self->{hash} };
67 ( undef, my $user ) = each %{ $self->{hash} };
72 ## Backwards compatibility
74 # This is a backwards compatible routine. get_user is specifically for loading a user by it's unique id
75 # find_user is capable of doing the same by simply passing { id => $id }
76 # no new code should be written using get_user as it is deprecated.
78 my ( $self, $id ) = @_;
79 $self->find_user({id => $id});
92 Catalyst::Plugin::Authentication::Store::Minimal::Backend - Minimal
93 authentication storage backend.
97 # you probably just want Store::Minimal under most cases,
98 # but if you insist you can instantiate your own store:
100 use Catalyst::Plugin::Authentication::Store::Minimal::Backend;
106 __PACKAGE__->config->{authentication} =
108 default_realm => 'members',
118 password => "s00p3r",
120 roles => [qw/edit delete/],
123 password => "s3cr3t",
124 roles => [qw/comment/],
135 This authentication store backend lets you create a very quick and dirty user
136 database in your application's config hash.
138 You will need to include the Authentication plugin, and at least one Credential
139 plugin to use this Store. Credential::Password is reccommended.
141 It's purpose is mainly for testing, and it should probably be replaced by a
142 more "serious" store for production.
144 The hash in the config, as well as the user objects/hashes are freely mutable
153 The classname used for the store. This is part of
154 L<Catalyst::Plugin::Authentication> and is the method by which
155 Catalyst::Plugin::Authentication::Store::Minimal::Backend is loaded as the
156 user store. For this module to be used, this must be set to
161 This is a simple hash of users, the keys are the usenames, and the values are
162 hashrefs containing a password key/value pair, and optionally, a roles/list
163 of role-names pair. If using roles, you will also need to add the
164 Authorization::Roles plugin.
166 See the SYNOPSIS for an example.
172 There are no publicly exported routines in the Minimal store (or indeed in
173 most authentication stores) However, below is a description of the routines
174 required by L<Catalyst::Plugin::Authentication> for all authentication stores.
178 =item new ( $config, $app )
180 Constructs a new store object, which uses the user element of the supplied config
181 hash ref as it's backing structure.
183 =item find_user ( $authinfo, $c )
185 Keys the hash by the 'id' or 'username' element in the authinfo hash and returns the user.
187 If the return value is unblessed it will be blessed as
188 L<Catalyst::Plugin::Authentication::User::Hash>.
190 =item from_session $id
192 Delegates to C<get_user>.
196 Chooses a random user from the hash and delegates to it.