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