1 package Catalyst::Authentication::Store::DBIx::Class::User;
4 use namespace::autoclean;
5 extends 'Catalyst::Authentication::User';
7 use List::MoreUtils 'all';
10 has 'config' => (is => 'rw');
11 has 'resultset' => (is => 'rw');
12 has '_user' => (is => 'rw');
13 has '_roles' => (is => 'rw');
16 my ( $class, $config, $c) = @_;
18 $config->{user_model} = $config->{user_class}
19 unless defined $config->{user_model};
22 resultset => $c->model($config->{'user_model'}),
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};
35 $self->config->{'id_field'} = [$self->{'resultset'}->result_source->primary_columns]
36 unless exists $self->config->{'id_field'};
38 $self->config->{'id_field'} = [$self->config->{'id_field'}]
39 unless ref $self->config->{'id_field'} eq 'ARRAY';
41 Catalyst::Exception->throw(
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'} };
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
50 $self->config->{lazyload} = 0;
52 # if (!$self->config->{lazyload}) {
53 # return $self->load_user($authinfo, $c);
55 # ## what do we do with a lazyload?
56 # ## presumably this is coming out of session storage.
57 # ## use $authinfo to fill in the user in that case?
65 my ($self, $authinfo, $c) = @_;
67 my $dbix_class_config = 0;
69 if (exists($authinfo->{'dbix_class'})) {
70 $authinfo = $authinfo->{'dbix_class'};
71 $dbix_class_config = 1;
74 ## User can provide an arrayref containing the arguments to search on the user class.
75 ## or even provide a prepared resultset, allowing maximum flexibility for user retreival.
76 ## these options are only available when using the dbix_class authinfo hash.
77 if ($dbix_class_config && exists($authinfo->{'result'})) {
78 $self->_user($authinfo->{'result'});
79 } elsif ($dbix_class_config && exists($authinfo->{'resultset'})) {
80 $self->_user($authinfo->{'resultset'}->first);
81 } elsif ($dbix_class_config && exists($authinfo->{'searchargs'})) {
82 $self->_user($self->resultset->search(@{$authinfo->{'searchargs'}})->first);
84 ## merge the ignore fields array into a hash - so we can do an easy check while building the query
85 my %ignorefields = map { $_ => 1} @{$self->config->{'ignore_fields_in_find'}};
88 # now we walk all the fields passed in, and build up a search hash.
89 foreach my $key (grep {!$ignorefields{$_}} keys %{$authinfo}) {
90 if ($self->resultset->result_source->has_column($key)) {
91 $searchargs->{$key} = $authinfo->{$key};
94 if (keys %{$searchargs}) {
95 $self->_user($self->resultset->search($searchargs)->first);
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."
106 if ($self->get_object) {
114 sub supported_features {
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.
128 ## shortcut if we have already retrieved them
129 if (ref $self->_roles eq 'ARRAY') {
130 return(@{$self->_roles});
134 if (exists($self->config->{'role_column'})) {
135 my $role_data = $self->get($self->config->{'role_column'});
137 @roles = split /[\s,\|]+/, $self->get($self->config->{'role_column'});
139 $self->_roles(\@roles);
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'})) {
144 $_->get_column($self->config->{role_field})
145 } $self->_user->$relation->search(undef, {
146 columns => [ $self->config->{role_field} ]
148 $self->_roles(\@roles);
150 Catalyst::Exception->throw("role table does not have a column called " . $self->config->{'role_field'});
153 Catalyst::Exception->throw("user->roles accessed, but no role configuration found");
156 return @{$self->_roles};
162 #return $self->get($self->config->{'id_field'});
164 #my $frozenuser = $self->_user->result_source->schema->freeze( $self->_user );
167 my %userdata = $self->_user->get_columns();
172 my ($self, $frozenuser, $c) = @_;
174 #my $obj = $self->resultset->result_source->schema->thaw( $frozenuser );
177 #if (!exists($self->config->{'use_userdata_from_session'}) || $self->config->{'use_userdata_from_session'} == 0) {
178 # $self->_user->discard_changes();
183 ## if use_userdata_from_session is defined in the config, we fill in the user data from the session.
184 if (exists($self->config->{'use_userdata_from_session'}) && $self->config->{'use_userdata_from_session'} != 0) {
185 my $obj = $self->resultset->new_result({ %$frozenuser });
191 if (ref $frozenuser eq 'HASH') {
193 map { ($_ => $frozenuser->{$_}) }
194 @{ $self->config->{id_field} }
198 return $self->load( { $self->config->{'id_field'} => $frozenuser }, $c);
202 my ($self, $field) = @_;
204 if (my $code = $self->_user->can($field)) {
205 return $self->_user->$code;
207 elsif (my $accessor =
208 try { $self->_user->result_source->column_info($field)->{accessor} }) {
209 return $self->_user->$accessor;
211 # XXX this should probably throw
217 my ($self, $force) = @_;
220 $self->_user->discard_changes;
227 my ($self, $force) = @_;
229 return $self->get_object($force);
234 $self->_user( $self->resultset->auto_create( @_ ) );
240 $self->_user->auto_update( @_ );
245 return $self->SUPER::can(@_) || do {
247 if (my $code = $self->_user->can($method)) {
248 sub { shift->_user->$code(@_) }
249 } elsif (my $accessor =
250 try { $self->_user->result_source->column_info($method)->{accessor} }) {
251 sub { shift->_user->$accessor }
260 (my $method) = (our $AUTOLOAD =~ /([^:]+)$/);
261 return if $method eq "DESTROY";
263 if (my $code = $self->_user->can($method)) {
264 return $self->_user->$code(@_);
266 elsif (my $accessor =
267 try { $self->_user->result_source->column_info($method)->{accessor} }) {
268 return $self->_user->$accessor(@_);
270 # XXX this should also throw
275 __PACKAGE__->meta->make_immutable(inline_constructor => 0);
282 Catalyst::Authentication::Store::DBIx::Class::User - The backing user
283 class for the Catalyst::Authentication::Store::DBIx::Class storage
288 This documentation refers to version 0.1500.
292 Internal - not used directly, please see
293 L<Catalyst::Authentication::Store::DBIx::Class> for details on how to
294 use this module. If you need more information than is present there, read the
301 The Catalyst::Authentication::Store::DBIx::Class::User class implements user storage
302 connected to an underlying DBIx::Class schema object.
304 =head1 SUBROUTINES / METHODS
310 =head2 load ( $authinfo, $c )
312 Retrieves a user from storage using the information provided in $authinfo.
314 =head2 supported_features
316 Indicates the features supported by this class. These are currently Roles and Session.
320 Returns an array of roles associated with this user, if roles are configured for this user class.
324 Returns a serialized user for storage in the session.
328 Revives a serialized user from storage in the session.
330 =head2 get ( $fieldname )
332 Returns the value of $fieldname for the user in question. Roughly translates to a call to
333 the DBIx::Class::Row's get_column( $fieldname ) routine.
337 Retrieves the DBIx::Class object that corresponds to this user
341 Synonym for get_object
345 This is called when the auto_create_user option is turned on in
346 Catalyst::Plugin::Authentication and a user matching the authinfo provided is not found.
347 By default, this will call the C<auto_create()> method of the resultset associated
348 with this object. It is up to you to implement that method.
352 This is called when the auto_update_user option is turned on in
353 Catalyst::Plugin::Authentication. Note that by default the DBIx::Class store
354 uses every field in the authinfo hash to match the user. This means any
355 information you provide with the intent to update must be ignored during the
356 user search process. Otherwise the information will most likely cause the user
357 record to not be found. To ignore fields in the search process, you
358 have to add the fields you wish to update to the 'ignore_fields_in_find'
359 authinfo element. Alternately, you can use one of the advanced row retrieval
360 methods (searchargs or resultset).
362 By default, auto_update will call the C<auto_update()> method of the
363 DBIx::Class::Row object associated with the user. It is up to you to implement
364 that method (probably in your schema file)
368 Delegates method calls to the underlieing user row.
372 Delegates handling of the C<< can >> method to the underlieing user row.
374 =head1 BUGS AND LIMITATIONS
376 None known currently, please email the author if you find any.
380 Jason Kuri (jayk@cpan.org)
384 Matt S Trout (mst) <mst@shadowcat.co.uk>
386 (fixes wrt can/AUTOLOAD sponsored by L<http://reask.com/>)
390 Copyright (c) 2007-2010 the aforementioned authors. All rights
391 reserved. This program is free software; you can redistribute
392 it and/or modify it under the same terms as Perl itself.