Commit | Line | Data |
06675d2e |
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 { |
54c8dc06 |
12 | my ( $class, $config, $app) = @_; |
06675d2e |
13 | |
54c8dc06 |
14 | bless { hash => $config }, $class; |
06675d2e |
15 | } |
16 | |
96777f3a |
17 | sub 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 |
28 | sub find_user { |
29 | my ( $self, $userinfo, $c ) = @_; |
06675d2e |
30 | |
54c8dc06 |
31 | my $id = $userinfo->{'id'}; |
32 | |
33 | return unless exists $self->{'hash'}{$id}; |
0c4ddd06 |
34 | |
54c8dc06 |
35 | my $user = $self->{'hash'}{$id}; |
0c4ddd06 |
36 | |
37 | if ( ref $user ) { |
38 | if ( Scalar::Util::blessed($user) ) { |
96777f3a |
39 | $user->id( $id ); |
0c4ddd06 |
40 | return $user; |
41 | } |
42 | elsif ( ref $user eq "HASH" ) { |
87ca1824 |
43 | $user->{id} ||= $id; |
0c4ddd06 |
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 | } |
06675d2e |
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 | |
54c8dc06 |
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 | |
06675d2e |
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 | |
4fbe2e14 |
141 | =item from_session $id |
142 | |
143 | Delegates to C<get_user>. |
144 | |
06675d2e |
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 | |