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