b82261f58695c3e0e1573bf97208a99f7b839c61
[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, $hash ) = @_;
13
14     bless { hash => $hash }, $class;
15 }
16
17 sub from_session {
18         my ( $self, $c, $id ) = @_;
19
20         return $id if ref $id;
21
22         $self->get_user( $id );
23 }
24
25 sub get_user {
26     my ( $self, $id ) = @_;
27
28     return unless exists $self->{hash}{$id};
29
30     my $user = $self->{hash}{$id};
31
32     if ( ref $user ) {
33         if ( Scalar::Util::blessed($user) ) {
34                         $user->store( $self );
35                         $user->id( $id );
36             return $user;
37         }
38         elsif ( ref $user eq "HASH" ) {
39             return bless $user, "Catalyst::Plugin::Authentication::User::Hash";
40         }
41         else {
42             Catalyst::Exception->throw( "The user '$id' is a reference of type "
43                   . ref($user)
44                   . " but should be a HASH" );
45         }
46     }
47     else {
48         Catalyst::Exception->throw(
49             "The user '$id' is has to be a hash reference or an object");
50     }
51
52     return $user;
53 }
54
55 sub user_supports {
56     my $self = shift;
57
58     # choose a random user
59     scalar keys %{ $self->{hash} };
60     ( undef, my $user ) = each %{ $self->{hash} };
61
62     $user->supports(@_);
63 }
64
65 __PACKAGE__;
66
67 __END__
68
69 =pod
70
71 =head1 NAME
72
73 Catalyst::Plugin::Authentication::Store::Minimal::Backend - Minimal
74 authentication storage backend.
75
76 =head1 SYNOPSIS
77
78     # you probably just want Store::Minimal under most cases,
79     # but if you insist you can instantiate your own store:
80
81     use Catalyst::Plugin::Authentication::Store::Minimal::Backend;
82
83     use Catalyst qw/
84         Authentication
85         Authentication::Credential::Password
86     /;
87
88     my %users = (
89         user => { password => "s3cr3t" },
90     );
91     
92     our $users = Catalyst::Plugin::Authentication::Store::Minimal::Backend->new(\%users);
93
94     sub action : Local {
95         my ( $self, $c ) = @_;
96
97         $c->login( $users->get_user( $c->req->param("login") ),
98             $c->req->param("password") );
99     }
100
101 =head1 DESCRIPTION
102
103 You probably want L<Catalyst::Plugin::Authentication::Store::Minimal>, unless
104 you are mixing several stores in a single app and one of them is Minimal.
105
106 Otherwise, this lets you create a store manually.
107
108 =head1 METHODS
109
110 =over 4
111
112 =item new $hash_ref
113
114 Constructs a new store object, which uses the supplied hash ref as it's backing
115 structure.
116
117 =item get_user $id
118
119 Keys the hash by $id and returns the value.
120
121 If the return value is unblessed it will be blessed as
122 L<Catalyst::Plugin::Authentication::User::Hash>.
123
124 =item user_supports
125
126 Chooses a random user from the hash and delegates to it.
127
128 =back
129
130 =cut
131
132