Updates to authentication system. Initial import of modifications.
[catagits/Catalyst-Plugin-Authentication.git] / lib / Catalyst / Plugin / Authentication / Store / Minimal / Backend.pm
1 #!/usr/bin/perl
2
3 package Catalyst::Plugin::Authentication::Store::Minimal::Backend;
4
5 use strict;
6 use warnings;
7
8 use Catalyst::Plugin::Authentication::User::Hash;
9 use Scalar::Util ();
10
11 sub new {
12     my ( $class, $config, $app) = @_;
13
14     bless { hash => $config }, $class;
15 }
16
17 sub from_session {
18         my ( $self, $c, $id ) = @_;
19
20         return $id if ref $id;
21
22         $self->find_user( { id => $id } );
23 }
24
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
28 sub find_user {
29     my ( $self, $userinfo, $c ) = @_;
30
31     my $id = $userinfo->{'id'};
32     
33     return unless exists $self->{'hash'}{$id};
34
35     my $user = $self->{'hash'}{$id};
36
37     if ( ref $user ) {
38         if ( Scalar::Util::blessed($user) ) {
39                         $user->id( $id );
40             return $user;
41         }
42         elsif ( ref $user eq "HASH" ) {
43             $user->{id} ||= $id;
44             return bless $user, "Catalyst::Plugin::Authentication::User::Hash";
45         }
46         else {
47             Catalyst::Exception->throw( "The user '$id' is a reference of type "
48                   . ref($user)
49                   . " but should be a HASH" );
50         }
51     }
52     else {
53         Catalyst::Exception->throw(
54             "The user '$id' is has to be a hash reference or an object");
55     }
56
57     return $user;
58 }
59
60 sub user_supports {
61     my $self = shift;
62
63     # choose a random user
64     scalar keys %{ $self->{hash} };
65     ( undef, my $user ) = each %{ $self->{hash} };
66
67     $user->supports(@_);
68 }
69
70 ## Backwards compatibility
71 #
72 # This is a backwards compatible routine.  get_user is specifically for loading a user by it's unique id
73 # find_user is capable of doing the same by simply passing { id => $id }  
74 # no new code should be written using get_user as it is deprecated.
75 sub get_user {
76     my ( $self, $id ) = @_;
77     $self->find_user({id => $id});
78 }
79
80
81
82 __PACKAGE__;
83
84 __END__
85
86 =pod
87
88 =head1 NAME
89
90 Catalyst::Plugin::Authentication::Store::Minimal::Backend - Minimal
91 authentication storage backend.
92
93 =head1 SYNOPSIS
94
95     # you probably just want Store::Minimal under most cases,
96     # but if you insist you can instantiate your own store:
97
98     use Catalyst::Plugin::Authentication::Store::Minimal::Backend;
99
100     use Catalyst qw/
101         Authentication
102         Authentication::Credential::Password
103     /;
104
105     my %users = (
106         user => { password => "s3cr3t" },
107     );
108     
109     our $users = Catalyst::Plugin::Authentication::Store::Minimal::Backend->new(\%users);
110
111     sub action : Local {
112         my ( $self, $c ) = @_;
113
114         $c->login( $users->get_user( $c->req->param("login") ),
115             $c->req->param("password") );
116     }
117
118 =head1 DESCRIPTION
119
120 You probably want L<Catalyst::Plugin::Authentication::Store::Minimal>, unless
121 you are mixing several stores in a single app and one of them is Minimal.
122
123 Otherwise, this lets you create a store manually.
124
125 =head1 METHODS
126
127 =over 4
128
129 =item new $hash_ref
130
131 Constructs a new store object, which uses the supplied hash ref as it's backing
132 structure.
133
134 =item get_user $id
135
136 Keys the hash by $id and returns the value.
137
138 If the return value is unblessed it will be blessed as
139 L<Catalyst::Plugin::Authentication::User::Hash>.
140
141 =item from_session $id
142
143 Delegates to C<get_user>.
144
145 =item user_supports
146
147 Chooses a random user from the hash and delegates to it.
148
149 =back
150
151 =cut
152
153