Commit | Line | Data |
5000f545 |
1 | package Catalyst::Plugin::Authentication::Store::DBIx::Class::User; |
2 | |
3 | use strict; |
4 | use warnings; |
5 | use base qw/Catalyst::Plugin::Authentication::User/; |
ff7203cb |
6 | use base qw/Class::Accessor::Fast/; |
7 | |
8 | BEGIN { |
9 | __PACKAGE__->mk_accessors(qw/config resultset _user _roles/); |
10 | } |
5000f545 |
11 | |
12 | sub new { |
ff7203cb |
13 | my ( $class, $config, $c) = @_; |
5000f545 |
14 | |
ff7203cb |
15 | my $self = { |
16 | resultset => $c->model($config->{'user_class'}), |
17 | config => $config, |
18 | _user => undef |
19 | }; |
5000f545 |
20 | |
21 | bless $self, $class; |
22 | |
ff7203cb |
23 | |
24 | |
5000f545 |
25 | ## if we have lazyloading turned on - we should not query the DB unless something gets read. |
26 | ## that's the idea anyway - still have to work out how to manage that - so for now we always force |
27 | ## lazyload to off. |
ff7203cb |
28 | $self->config->{lazyload} = 0; |
5000f545 |
29 | |
ff7203cb |
30 | # if (!$self->config->{lazyload}) { |
31 | # return $self->load_user($authinfo, $c); |
32 | # } else { |
33 | # ## what do we do with a lazyload? |
34 | # ## presumably this is coming out of session storage. |
35 | # ## use $authinfo to fill in the user in that case? |
36 | # } |
37 | |
5000f545 |
38 | return $self; |
39 | } |
40 | |
41 | |
ff7203cb |
42 | sub load { |
5000f545 |
43 | my ($self, $authinfo, $c) = @_; |
44 | |
ff7203cb |
45 | my $dbix_class_config = 0; |
46 | |
47 | if (exists($authinfo->{'dbix_class'})) { |
48 | $authinfo = $authinfo->{'dbix_class'}; |
49 | $dbix_class_config = 1; |
50 | } |
51 | |
5000f545 |
52 | ## User can provide an arrayref containing the arguments to search on the user class. |
ff7203cb |
53 | ## or even provide a prepared resultset, allowing maximum flexibility for user retreival. |
54 | ## these options are only available when using the dbix_class authinfo hash. |
55 | if ($dbix_class_config && exists($authinfo->{'resultset'})) { |
56 | $self->_user($authinfo->{'resultset'}->first); |
57 | } elsif ($dbix_class_config && exists($authinfo->{'searchargs'})) { |
58 | $self->_user($self->resultset->search(@{$authinfo->{'searchargs'}})->first); |
5000f545 |
59 | } else { |
60 | ## merge the ignore fields array into a hash - so we can do an easy check while building the query |
ff7203cb |
61 | my %ignorefields = map { $_ => 1} @{$self->config->{'ignore_fields_in_find'}}; |
5000f545 |
62 | my $searchargs = {}; |
63 | |
64 | # now we walk all the fields passed in, and build up a search hash. |
65 | foreach my $key (grep {!$ignorefields{$_}} keys %{$authinfo}) { |
ff7203cb |
66 | if ($self->resultset->result_source->has_column($key)) { |
5000f545 |
67 | $searchargs->{$key} = $authinfo->{$key}; |
68 | } |
ff7203cb |
69 | } |
70 | $self->_user($self->resultset->search($searchargs)->first); |
71 | } |
72 | |
73 | if ($self->get_object) { |
74 | return $self |
75 | } else { |
76 | return undef; |
5000f545 |
77 | } |
ff7203cb |
78 | #$c->log->debug(dumper($self->{'user'})); |
5000f545 |
79 | |
80 | } |
81 | |
82 | sub supported_features { |
83 | my $self = shift; |
5000f545 |
84 | |
85 | return { |
5000f545 |
86 | session => 1, |
87 | roles => 1, |
88 | }; |
89 | } |
90 | |
91 | |
92 | sub roles { |
93 | my ( $self, @wanted_roles ) = @_; |
94 | |
95 | ## shortcut if we have already retrieved them |
ff7203cb |
96 | if (ref $self->_roles eq 'ARRAY') { |
97 | return(@{$self->_roles}); |
5000f545 |
98 | } |
99 | |
100 | my @roles = (); |
ff7203cb |
101 | if (exists($self->config->{'role_column'})) { |
102 | @roles = split /[ ,\|]/, $self->get($self->config->{'role_column'}); |
103 | $self->_roles = \@roles; |
104 | } elsif (exists($self->config->{'role_relation'})) { |
105 | my $relation = $self->config->{'role_relation'}; |
106 | if ($self->_user->$relation->result_source->has_column($self->config->{'role_field'})) { |
107 | @roles = $self->_user->$relation->search(undef, { columns => [ $self->config->{'role_field'}]})->all(); |
5000f545 |
108 | } else { |
ff7203cb |
109 | Catalyst::Exception->throw("role table does not have a column called " . $self->config->{'role_field'}); |
5000f545 |
110 | } |
ff7203cb |
111 | my $rolefield = $self->config->{'role_field'}; |
112 | @{$self->_roles} = map { $_->get_column($self->config->{'role_field'}) } @roles; |
5000f545 |
113 | } else { |
114 | Catalyst::Exception->throw("user->roles accessed, but no role configuration found"); |
115 | } |
116 | |
ff7203cb |
117 | return @{$self->_roles}; |
5000f545 |
118 | } |
119 | |
120 | sub for_session { |
ff7203cb |
121 | my $self = shift; |
122 | |
123 | return $self->get('id'); |
124 | } |
125 | |
126 | sub from_session { |
127 | my ($self, $frozenuser, $c) = @_; |
128 | |
129 | # this could be a lot better. But for now it just assumes $frozenuser is an id and uses find_user |
130 | # XXX: hits the database on every request? Not good... |
131 | return $self->load( { id => $frozenuser }, $c); |
5000f545 |
132 | } |
133 | |
134 | sub get { |
135 | my ($self, $field) = @_; |
136 | |
ff7203cb |
137 | if ($self->_user->can($field)) { |
138 | return $self->_user->$field; |
5000f545 |
139 | } else { |
140 | return undef; |
141 | } |
142 | } |
143 | |
144 | sub obj { |
145 | my $self = shift; |
ff7203cb |
146 | |
5000f545 |
147 | return $self->get_object; |
148 | } |
149 | |
150 | sub get_object { |
151 | my $self = shift; |
152 | |
ff7203cb |
153 | return $self->_user; |
5000f545 |
154 | } |
155 | |
156 | sub AUTOLOAD { |
157 | my $self = shift; |
158 | (my $method) = (our $AUTOLOAD =~ /([^:]+)$/); |
159 | return if $method eq "DESTROY"; |
160 | |
ff7203cb |
161 | $self->_user->$method(@_); |
5000f545 |
162 | } |
163 | |
164 | 1; |
165 | __END__ |
166 | |
167 | =head1 NAME |
168 | |
169 | Catalyst::Plugin::Authentication::Store::DBIx::Class::User - A class to ... |
170 | |
171 | =head1 VERSION |
172 | |
173 | This documentation refers to version 0.01. |
174 | |
175 | =head1 SYNOPSIS |
176 | |
177 | use Catalyst::Plugin::Authentication::Store::DBIx::Class::User; |
178 | |
179 | =head1 DESCRIPTION |
180 | |
181 | The Catalyst::Plugin::Authentication::Store::DBIx::Class::User class implements ... |
182 | |
183 | =head1 SUBROUTINES / METHODS |
184 | |
185 | =head2 new (constructor) |
186 | |
187 | Parameters: |
188 | class |
189 | authinfo |
190 | config |
191 | c |
192 | lazyload |
193 | |
194 | Insert description of constructor here... |
195 | |
196 | =head2 load_user (method) |
197 | |
198 | Parameters: |
199 | authinfo |
200 | c |
201 | |
202 | Insert description of method here... |
203 | |
204 | =head2 supported_features (method) |
205 | |
206 | Parameters: |
207 | none |
208 | |
209 | Insert description of method here... |
210 | |
211 | =head2 roles |
212 | |
213 | Parameters: |
214 | none |
215 | |
216 | Insert description of subroutine here... |
217 | |
218 | =head2 for_session |
219 | |
220 | Parameters: |
221 | none |
222 | |
223 | Insert description of subroutine here... |
224 | |
225 | =head2 get (method) |
226 | |
227 | Parameters: |
228 | field |
229 | |
230 | Insert description of method here... |
231 | |
232 | =head2 obj (method) |
233 | |
234 | Parameters: |
235 | none |
236 | |
237 | Insert description of method here... |
238 | |
239 | =head2 get_object (method) |
240 | |
241 | Parameters: |
242 | none |
243 | |
244 | Insert description of method here... |
245 | |
246 | =head2 AUTOLOAD (method) |
247 | |
248 | Parameters: |
249 | none |
250 | |
251 | Insert description of method here... |
252 | |
253 | =head1 DEPENDENCIES |
254 | |
255 | Modules used, version dependencies, core yes/no |
256 | |
257 | strict |
258 | |
259 | warnings |
260 | |
261 | =head1 NOTES |
262 | |
263 | ... |
264 | |
265 | =head1 BUGS AND LIMITATIONS |
266 | |
267 | None known currently, please email the author if you find any. |
268 | |
269 | =head1 AUTHOR |
270 | |
271 | Jason Kuri (jk@domain.tld) |
272 | |
273 | =head1 LICENCE |
274 | |
275 | Copyright 2006 by Jason Kuri. |
276 | |
277 | This software is free. It is licensed under the same terms as Perl itself. |
278 | |
279 | =cut |