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