Fix calling User->can() as a class method. RT#90715
[catagits/Catalyst-Authentication-Store-DBIx-Class.git] / lib / Catalyst / Authentication / Store / DBIx / Class / User.pm
CommitLineData
6727afe2 1package Catalyst::Authentication::Store::DBIx::Class::User;
5000f545 2
6d4bea88 3use Moose;
4use namespace::autoclean;
5extends 'Catalyst::Authentication::User';
6
7use List::MoreUtils 'all';
51f40056 8use Try::Tiny;
6d4bea88 9
10has 'config' => (is => 'rw');
11has 'resultset' => (is => 'rw');
12has '_user' => (is => 'rw');
13has '_roles' => (is => 'rw');
5000f545 14
15sub new {
ff7203cb 16 my ( $class, $config, $c) = @_;
5000f545 17
9064f42f 18 $config->{user_model} = $config->{user_class}
19 unless defined $config->{user_model};
f55cb81e 20
ff7203cb 21 my $self = {
f55cb81e 22 resultset => $c->model($config->{'user_model'}),
ff7203cb 23 config => $config,
078727e0 24 _roles => undef,
ff7203cb 25 _user => undef
26 };
4bebb973 27
5000f545 28 bless $self, $class;
f55cb81e 29
9064f42f 30 Catalyst::Exception->throw(
31 "\$c->model('${ \$self->config->{user_model} }') did not return a resultset."
32 . " Did you set user_model correctly?"
33 ) unless $self->{resultset};
f55cb81e 34
70462180 35 $self->config->{'id_field'} = [$self->{'resultset'}->result_source->primary_columns]
36 unless exists $self->config->{'id_field'};
f55cb81e 37
70462180 38 $self->config->{'id_field'} = [$self->config->{'id_field'}]
39 unless ref $self->config->{'id_field'} eq 'ARRAY';
40
9064f42f 41 Catalyst::Exception->throw(
42 "id_field set to "
43 . join(q{,} => @{ $self->config->{'id_field'} })
44 . " but user table has no column by that name!"
45 ) unless all { $self->{'resultset'}->result_source->has_column($_) } @{ $self->config->{'id_field'} };
4bebb973 46
5000f545 47 ## if we have lazyloading turned on - we should not query the DB unless something gets read.
48 ## that's the idea anyway - still have to work out how to manage that - so for now we always force
49 ## lazyload to off.
ff7203cb 50 $self->config->{lazyload} = 0;
4bebb973 51
ff7203cb 52# if (!$self->config->{lazyload}) {
53# return $self->load_user($authinfo, $c);
54# } else {
55# ## what do we do with a lazyload?
4bebb973 56# ## presumably this is coming out of session storage.
ff7203cb 57# ## use $authinfo to fill in the user in that case?
58# }
59
5000f545 60 return $self;
61}
62
63
ff7203cb 64sub load {
5000f545 65 my ($self, $authinfo, $c) = @_;
4bebb973 66
ff7203cb 67 my $dbix_class_config = 0;
4bebb973 68
ff7203cb 69 if (exists($authinfo->{'dbix_class'})) {
70 $authinfo = $authinfo->{'dbix_class'};
71 $dbix_class_config = 1;
72 }
4bebb973 73
5000f545 74 ## User can provide an arrayref containing the arguments to search on the user class.
e2ecb860 75 ## or even provide a prepared resultset, allowing maximum flexibility for user retrieval.
4bebb973 76 ## these options are only available when using the dbix_class authinfo hash.
6bd97524 77 if ($dbix_class_config && exists($authinfo->{'result'})) {
78 $self->_user($authinfo->{'result'});
79 } elsif ($dbix_class_config && exists($authinfo->{'resultset'})) {
ff7203cb 80 $self->_user($authinfo->{'resultset'}->first);
81 } elsif ($dbix_class_config && exists($authinfo->{'searchargs'})) {
4bebb973 82 $self->_user($self->resultset->search(@{$authinfo->{'searchargs'}})->first);
5000f545 83 } else {
84 ## merge the ignore fields array into a hash - so we can do an easy check while building the query
4bebb973 85 my %ignorefields = map { $_ => 1} @{$self->config->{'ignore_fields_in_find'}};
5000f545 86 my $searchargs = {};
4bebb973 87
5000f545 88 # now we walk all the fields passed in, and build up a search hash.
89 foreach my $key (grep {!$ignorefields{$_}} keys %{$authinfo}) {
ff7203cb 90 if ($self->resultset->result_source->has_column($key)) {
5000f545 91 $searchargs->{$key} = $authinfo->{$key};
92 }
ff7203cb 93 }
87920e64 94 if (keys %{$searchargs}) {
95 $self->_user($self->resultset->search($searchargs)->first);
96 } else {
9064f42f 97 Catalyst::Exception->throw(
98 "Failed to load user data. You passed [" . join(',', keys %{$authinfo}) . "]"
99 . " to authenticate() but your user source (" . $self->config->{'user_model'} . ")"
100 . " only has these columns: [" . join( ",", $self->resultset->result_source->columns ) . "]"
101 . " Check your authenticate() call."
102 );
87920e64 103 }
ff7203cb 104 }
105
106 if ($self->get_object) {
93102ff5 107 return $self;
ff7203cb 108 } else {
109 return undef;
5000f545 110 }
5000f545 111
112}
113
114sub supported_features {
115 my $self = shift;
5000f545 116
117 return {
5000f545 118 session => 1,
119 roles => 1,
120 };
121}
122
123
124sub roles {
b5c13b47 125 my ( $self ) = shift;
126 ## this used to load @wantedroles - but that doesn't seem to be used by the roles plugin, so I dropped it.
5000f545 127
128 ## shortcut if we have already retrieved them
ff7203cb 129 if (ref $self->_roles eq 'ARRAY') {
130 return(@{$self->_roles});
5000f545 131 }
4bebb973 132
5000f545 133 my @roles = ();
ff7203cb 134 if (exists($self->config->{'role_column'})) {
ad93b3e9 135 my $role_data = $self->get($self->config->{'role_column'});
4bebb973 136 if ($role_data) {
87920e64 137 @roles = split /[\s,\|]+/, $self->get($self->config->{'role_column'});
ad93b3e9 138 }
078727e0 139 $self->_roles(\@roles);
ff7203cb 140 } elsif (exists($self->config->{'role_relation'})) {
141 my $relation = $self->config->{'role_relation'};
142 if ($self->_user->$relation->result_source->has_column($self->config->{'role_field'})) {
9064f42f 143 @roles = map {
144 $_->get_column($self->config->{role_field})
145 } $self->_user->$relation->search(undef, {
146 columns => [ $self->config->{role_field} ]
147 })->all;
078727e0 148 $self->_roles(\@roles);
5000f545 149 } else {
ff7203cb 150 Catalyst::Exception->throw("role table does not have a column called " . $self->config->{'role_field'});
5000f545 151 }
5000f545 152 } else {
153 Catalyst::Exception->throw("user->roles accessed, but no role configuration found");
154 }
155
ff7203cb 156 return @{$self->_roles};
5000f545 157}
158
159sub for_session {
ff7203cb 160 my $self = shift;
4bebb973 161
f26005a7 162 #return $self->get($self->config->{'id_field'});
4bebb973 163
be7c0c30 164 #my $frozenuser = $self->_user->result_source->schema->freeze( $self->_user );
165 #return $frozenuser;
4bebb973 166
f26005a7 167 my %userdata = $self->_user->get_columns();
e746d363 168
169 # If use_userdata_from_session is set, then store all of the columns of the user obj in the session
170 if (exists($self->config->{'use_userdata_from_session'}) && $self->config->{'use_userdata_from_session'} != 0) {
171 return \%userdata;
172 } else { # Otherwise, we just need the PKs for load() to use.
173 my %pk_fields = map { ($_ => $userdata{$_}) } @{ $self->config->{id_field} };
174 return \%pk_fields;
175 }
ff7203cb 176}
177
178sub from_session {
179 my ($self, $frozenuser, $c) = @_;
4bebb973 180
be7c0c30 181 #my $obj = $self->resultset->result_source->schema->thaw( $frozenuser );
182 #$self->_user($obj);
4bebb973 183
be7c0c30 184 #if (!exists($self->config->{'use_userdata_from_session'}) || $self->config->{'use_userdata_from_session'} == 0) {
185# $self->_user->discard_changes();
186# }
4bebb973 187#
be7c0c30 188# return $self;
4bebb973 189#
be7c0c30 190## if use_userdata_from_session is defined in the config, we fill in the user data from the session.
70462180 191 if (exists($self->config->{'use_userdata_from_session'}) && $self->config->{'use_userdata_from_session'} != 0) {
48ca4fcf 192
193 # We need to use inflate_result here since we -are- inflating a
194 # result object from cached data, not creating a fresh one.
195 # Components such as EncodedColumn wrap new() to ensure that a
196 # provided password is hashed on the way in, and re-running the
197 # hash function on data being restored is expensive and incorrect.
198
199 my $class = $self->resultset->result_class;
200 my $source = $self->resultset->result_source;
201 my $obj = $class->inflate_result($source, { %$frozenuser });
202
f26005a7 203 $obj->in_storage(1);
204 $self->_user($obj);
205 return $self;
f26005a7 206 }
70462180 207
208 if (ref $frozenuser eq 'HASH') {
209 return $self->load({
210 map { ($_ => $frozenuser->{$_}) }
211 @{ $self->config->{id_field} }
9db54bcf 212 }, $c);
70462180 213 }
214
215 return $self->load( { $self->config->{'id_field'} => $frozenuser }, $c);
5000f545 216}
217
218sub get {
219 my ($self, $field) = @_;
4bebb973 220
c305eeac 221 if (my $code = $self->_user->can($field)) {
222 return $self->_user->$code;
6d4bea88 223 }
51f40056 224 elsif (my $accessor =
225 try { $self->_user->result_source->column_info($field)->{accessor} }) {
6d4bea88 226 return $self->_user->$accessor;
5000f545 227 } else {
51f40056 228 # XXX this should probably throw
5000f545 229 return undef;
230 }
231}
232
c1d29ab7 233sub get_object {
f26005a7 234 my ($self, $force) = @_;
4bebb973 235
f26005a7 236 if ($force) {
237 $self->_user->discard_changes;
238 }
239
c1d29ab7 240 return $self->_user;
5000f545 241}
242
c1d29ab7 243sub obj {
f26005a7 244 my ($self, $force) = @_;
4bebb973 245
f26005a7 246 return $self->get_object($force);
5000f545 247}
248
69100364 249sub auto_create {
250 my $self = shift;
251 $self->_user( $self->resultset->auto_create( @_ ) );
252 return $self;
253}
254
255sub auto_update {
256 my $self = shift;
257 $self->_user->auto_update( @_ );
258}
259
28db23cf 260sub can {
261 my $self = shift;
262 return $self->SUPER::can(@_) || do {
a4523c16 263 my ($method) = @_;
c30ad9df 264 if (not ref $self) {
265 undef;
266 } elsif (not $self->_user) {
9821fc6a 267 undef;
268 } elsif (my $code = $self->_user->can($method)) {
28db23cf 269 sub { shift->_user->$code(@_) }
270 } elsif (my $accessor =
271 try { $self->_user->result_source->column_info($method)->{accessor} }) {
272 sub { shift->_user->$accessor }
273 } else {
274 undef;
275 }
276 };
277}
278
5000f545 279sub AUTOLOAD {
280 my $self = shift;
281 (my $method) = (our $AUTOLOAD =~ /([^:]+)$/);
282 return if $method eq "DESTROY";
283
c30ad9df 284 return unless ref $self;
285
8e553a46 286 if (my $code = $self->_user->can($method)) {
287 return $self->_user->$code(@_);
288 }
289 elsif (my $accessor =
290 try { $self->_user->result_source->column_info($method)->{accessor} }) {
291 return $self->_user->$accessor(@_);
292 } else {
293 # XXX this should also throw
294 return undef;
295 }
5000f545 296}
297
6d4bea88 298__PACKAGE__->meta->make_immutable(inline_constructor => 0);
299
5000f545 3001;
301__END__
302
303=head1 NAME
304
6727afe2 305Catalyst::Authentication::Store::DBIx::Class::User - The backing user
306class for the Catalyst::Authentication::Store::DBIx::Class storage
c1d29ab7 307module.
5000f545 308
309=head1 VERSION
310
a1c47a34 311This documentation refers to version 0.1503.
5000f545 312
313=head1 SYNOPSIS
314
c1d29ab7 315Internal - not used directly, please see
6727afe2 316L<Catalyst::Authentication::Store::DBIx::Class> for details on how to
c1d29ab7 317use this module. If you need more information than is present there, read the
318source.
93102ff5 319
4bebb973 320
5000f545 321
322=head1 DESCRIPTION
323
6727afe2 324The Catalyst::Authentication::Store::DBIx::Class::User class implements user storage
c1d29ab7 325connected to an underlying DBIx::Class schema object.
5000f545 326
327=head1 SUBROUTINES / METHODS
328
4bebb973 329=head2 new
5000f545 330
c1d29ab7 331Constructor.
5000f545 332
4bebb973 333=head2 load ( $authinfo, $c )
5000f545 334
c1d29ab7 335Retrieves a user from storage using the information provided in $authinfo.
5000f545 336
c1d29ab7 337=head2 supported_features
5000f545 338
c1d29ab7 339Indicates the features supported by this class. These are currently Roles and Session.
5000f545 340
341=head2 roles
342
c1d29ab7 343Returns an array of roles associated with this user, if roles are configured for this user class.
5000f545 344
345=head2 for_session
346
4bebb973 347Returns a serialized user for storage in the session.
5000f545 348
fbe76043 349=head2 from_session
350
4bebb973 351Revives a serialized user from storage in the session.
fbe76043 352
c1d29ab7 353=head2 get ( $fieldname )
5000f545 354
4bebb973 355Returns the value of $fieldname for the user in question. Roughly translates to a call to
c1d29ab7 356the DBIx::Class::Row's get_column( $fieldname ) routine.
5000f545 357
4bebb973 358=head2 get_object
5000f545 359
c1d29ab7 360Retrieves the DBIx::Class object that corresponds to this user
5000f545 361
362=head2 obj (method)
363
c1d29ab7 364Synonym for get_object
5000f545 365
69100364 366=head2 auto_create
367
4bebb973 368This is called when the auto_create_user option is turned on in
369Catalyst::Plugin::Authentication and a user matching the authinfo provided is not found.
4117c46f 370By default, this will call the C<auto_create()> method of the resultset associated
69100364 371with this object. It is up to you to implement that method.
372
373=head2 auto_update
374
4117c46f 375This is called when the auto_update_user option is turned on in
cccbdd0a 376Catalyst::Plugin::Authentication. Note that by default the DBIx::Class store
4117c46f 377uses every field in the authinfo hash to match the user. This means any
50631330 378information you provide with the intent to update must be ignored during the
379user search process. Otherwise the information will most likely cause the user
380record to not be found. To ignore fields in the search process, you
381have to add the fields you wish to update to the 'ignore_fields_in_find'
382authinfo element. Alternately, you can use one of the advanced row retrieval
383methods (searchargs or resultset).
4117c46f 384
385By default, auto_update will call the C<auto_update()> method of the
386DBIx::Class::Row object associated with the user. It is up to you to implement
387that method (probably in your schema file)
69100364 388
9791ac85 389=head2 AUTOLOAD
390
e2ecb860 391Delegates method calls to the underlying user row.
9791ac85 392
393=head2 can
394
e2ecb860 395Delegates handling of the C<< can >> method to the underlying user row.
9791ac85 396
5000f545 397=head1 BUGS AND LIMITATIONS
398
399None known currently, please email the author if you find any.
400
401=head1 AUTHOR
402
fbe76043 403Jason Kuri (jayk@cpan.org)
5000f545 404
28db23cf 405=head1 CONTRIBUTORS
406
407Matt S Trout (mst) <mst@shadowcat.co.uk>
408
a4523c16 409(fixes wrt can/AUTOLOAD sponsored by L<http://reask.com/>)
410
c1d29ab7 411=head1 LICENSE
5000f545 412
28db23cf 413Copyright (c) 2007-2010 the aforementioned authors. All rights
c1d29ab7 414reserved. This program is free software; you can redistribute
415it and/or modify it under the same terms as Perl itself.
5000f545 416
417=cut