r36178@cain (orig r6059): jayk | 2007-02-06 05:45:22 +0000
[catagits/Catalyst-Plugin-Authentication.git] / lib / Catalyst / Plugin / Authentication / Store / Minimal.pm
CommitLineData
07e49bc7 1#!/usr/bin/perl
2
3package Catalyst::Plugin::Authentication::Store::Minimal;
07e49bc7 4
5use strict;
6use warnings;
7
808fc188 8use Catalyst::Plugin::Authentication::User::Hash;
9use Scalar::Util ();
10use base qw/Class::Accessor::Fast/;
07e49bc7 11
808fc188 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
07e49bc7 84sub setup {
85 my $c = shift;
86
dde93f12 87 $c->default_auth_store(
808fc188 88 __PACKAGE__->new(
c955e5dc 89 $c->config->{authentication}, $c
dde93f12 90 )
91 );
07e49bc7 92
dde93f12 93 $c->NEXT::setup(@_);
07e49bc7 94}
95
96__PACKAGE__;
97
98__END__
99
100=pod
101
102=head1 NAME
103
808fc188 104Catalyst::Plugin::Authentication::Store::Minimal - Minimal
105authentication store.
07e49bc7 106
107=head1 SYNOPSIS
108
808fc188 109 # you probably just want Store::Minimal under most cases,
110 # but if you insist you can instantiate your own store:
07e49bc7 111
808fc188 112 use Catalyst::Plugin::Authentication::Store::Minimal;
07e49bc7 113
808fc188 114 use Catalyst qw/
115 Authentication
116 /;
117
118 __PACKAGE__->config->{authentication} =
119 {
120 default_realm => 'members',
121 realms => {
122 members => {
123 credential => {
66b2264f 124 class => 'Password',
125 password_field => 'password',
126 password_type => 'clear'
808fc188 127 },
128 store => {
129 class => 'Minimal',
130 users = {
131 bob => {
132 password => "s00p3r",
133 editor => 'yes',
134 roles => [qw/edit delete/],
135 },
136 william => {
137 password => "s3cr3t",
138 roles => [qw/comment/],
139 }
140 }
141 }
142 }
143 }
144 };
145
146
07e49bc7 147=head1 DESCRIPTION
148
808fc188 149This authentication store lets you create a very quick and dirty user
07e49bc7 150database in your application's config hash.
151
02c54a71 152You will need to include the Authentication plugin, and at least one Credential
153plugin to use this Store. Credential::Password is reccommended.
154
07e49bc7 155It's purpose is mainly for testing, and it should probably be replaced by a
156more "serious" store for production.
157
158The hash in the config, as well as the user objects/hashes are freely mutable
159at runtime.
160
02c54a71 161=head1 CONFIGURATION
162
163=over 4
164
808fc188 165=item class
166
167The classname used for the store. This is part of
168L<Catalyst::Plugin::Authentication> and is the method by which
169Catalyst::Plugin::Authentication::Store::Minimal is loaded as the
170user store. For this module to be used, this must be set to
171'Minimal'.
172
02c54a71 173=item users
174
175This is a simple hash of users, the keys are the usenames, and the values are
176hashrefs containing a password key/value pair, and optionally, a roles/list
177of role-names pair. If using roles, you will also need to add the
178Authorization::Roles plugin.
179
180See the SYNOPSIS for an example.
181
51111c81 182=back
183
808fc188 184=head1 METHODS
185
186There are no publicly exported routines in the Minimal store (or indeed in
187most authentication stores) However, below is a description of the routines
188required by L<Catalyst::Plugin::Authentication> for all authentication stores.
07e49bc7 189
190=over 4
191
808fc188 192=item new ( $config, $app )
193
194Constructs a new store object, which uses the user element of the supplied config
195hash ref as it's backing structure.
196
197=item find_user ( $authinfo, $c )
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
206=item from_session $id
207
208Delegates to C<get_user>.
209
210=item user_supports
07e49bc7 211
808fc188 212Chooses a random user from the hash and delegates to it.
07e49bc7 213
214=back
215
216=cut
217
218