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