Fix all the ->config invocations in docs to be more correct
[catagits/Catalyst-Plugin-Authentication.git] / lib / Catalyst / Authentication / Store / Minimal.pm
CommitLineData
5c5af345 1package Catalyst::Authentication::Store::Minimal;
2
3use strict;
4use warnings;
5
6use Catalyst::Authentication::User::Hash;
7use Scalar::Util qw( blessed );
8use base qw/Class::Accessor::Fast/;
9
10BEGIN {
11 __PACKAGE__->mk_accessors(qw/userhash/);
12}
13
14sub new {
15 my ( $class, $config, $app, $realm) = @_;
16
17 bless { userhash => $config->{'users'} }, $class;
18}
19
20sub from_session {
21 my ( $self, $c, $id ) = @_;
22
23 return $id if ref $id;
24
25 $self->find_user( { id => $id } );
26}
27
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
31sub find_user {
32 my ( $self, $userinfo, $c ) = @_;
33
34 my $id = $userinfo->{'id'};
35
36 $id ||= $userinfo->{'username'};
37
38 return unless exists $self->userhash->{$id};
39
40 my $user = $self->userhash->{$id};
41
5c5af345 42 if ( ref($user) eq "HASH") {
43 $user->{id} ||= $id;
44 return bless $user, "Catalyst::Authentication::User::Hash";
45 } elsif ( ref($user) && blessed($user) && $user->isa('Catalyst::Authentication::User::Hash')) {
46 return $user;
47 } else {
48 Catalyst::Exception->throw( "The user '$id' must be a hash reference or an " .
49 "object of class Catalyst::Authentication::User::Hash");
50 }
51 return $user;
52}
53
54sub user_supports {
55 my $self = shift;
56
57 # choose a random user
58 scalar keys %{ $self->userhash };
59 ( undef, my $user ) = each %{ $self->userhash };
60
61 $user->supports(@_);
62}
63
64## Backwards compatibility
65#
66# This is a backwards compatible routine. get_user is specifically for loading a user by it's unique id
67# find_user is capable of doing the same by simply passing { id => $id }
68# no new code should be written using get_user as it is deprecated.
69sub get_user {
70 my ( $self, $id ) = @_;
71 $self->find_user({id => $id});
72}
73
5c5af345 74__PACKAGE__;
75
76__END__
77
78=pod
79
80=head1 NAME
81
82Catalyst::Authentication::Store::Minimal - Minimal authentication store
83
84=head1 SYNOPSIS
85
86 # you probably just want Store::Minimal under most cases,
87 # but if you insist you can instantiate your own store:
88
89 use Catalyst::Authentication::Store::Minimal;
90
91 use Catalyst qw/
92 Authentication
93 /;
94
c83ea211 95 __PACKAGE__->config( 'Plugin::Authentication' =>
5c5af345 96 {
97 default_realm => 'members',
98 realms => {
99 members => {
100 credential => {
101 class => 'Password',
102 password_field => 'password',
103 password_type => 'clear'
104 },
105 store => {
106 class => 'Minimal',
107 users = {
108 bob => {
109 password => "s00p3r",
110 editor => 'yes',
111 roles => [qw/edit delete/],
112 },
113 william => {
114 password => "s3cr3t",
115 roles => [qw/comment/],
116 }
117 }
118 }
119 }
120 }
c83ea211 121 }
122 );
5c5af345 123
124
125=head1 DESCRIPTION
126
127This authentication store lets you create a very quick and dirty user
128database in your application's config hash.
129
130You will need to include the Authentication plugin, and at least one Credential
131plugin to use this Store. Credential::Password is reccommended.
132
133It's purpose is mainly for testing, and it should probably be replaced by a
134more "serious" store for production.
135
136The hash in the config, as well as the user objects/hashes are freely mutable
137at runtime.
138
139=head1 CONFIGURATION
140
141=over 4
142
143=item class
144
145The classname used for the store. This is part of
146L<Catalyst::Plugin::Authentication> and is the method by which
147Catalyst::Authentication::Store::Minimal is loaded as the
148user store. For this module to be used, this must be set to
149'Minimal'.
150
151=item users
152
153This is a simple hash of users, the keys are the usenames, and the values are
154hashrefs containing a password key/value pair, and optionally, a roles/list
155of role-names pair. If using roles, you will also need to add the
156Authorization::Roles plugin.
157
158See the SYNOPSIS for an example.
159
160=back
161
162=head1 METHODS
163
164There are no publicly exported routines in the Minimal store (or indeed in
165most authentication stores) However, below is a description of the routines
166required by L<Catalyst::Plugin::Authentication> for all authentication stores.
167
168=head2 new( $config, $app, $realm )
169
170Constructs a new store object, which uses the user element of the supplied config
171hash ref as it's backing structure.
172
173=head2 find_user( $authinfo, $c )
174
175Keys the hash by the 'id' or 'username' element in the authinfo hash and returns the user.
176
177... documentation fairy stopped here. ...
178
179If the return value is unblessed it will be blessed as
180L<Catalyst::Authentication::User::Hash>.
181
182=head2 from_session( $id )
183
184Delegates to C<get_user>.
185
186=head2 user_supports( )
187
188Chooses a random user from the hash and delegates to it.
189
190=head2 get_user( )
191
290e5a7e 192Deprecated
193
5c5af345 194=head2 setup( )
195
196=cut
197
198