1 package Catalyst::Authentication::Store::DBIx::Class::User;
5 use base qw/Catalyst::Authentication::User/;
6 use base qw/Class::Accessor::Fast/;
9 __PACKAGE__->mk_accessors(qw/config resultset _user _roles/);
13 my ( $class, $config, $c) = @_;
16 resultset => $c->model($config->{'user_class'}),
25 if (!exists($self->config->{'id_field'})) {
26 my @pks = $self->{'resultset'}->result_source->primary_columns;
28 $self->config->{'id_field'} = $pks[0];
30 Catalyst::Exception->throw("user table does not contain a single primary key column - please specify 'id_field' in config!");
33 if (!$self->{'resultset'}->result_source->has_column($self->config->{'id_field'})) {
34 Catalyst::Exception->throw("id_field set to " . $self->config->{'id_field'} . " but user table has no column by that name!");
37 ## if we have lazyloading turned on - we should not query the DB unless something gets read.
38 ## that's the idea anyway - still have to work out how to manage that - so for now we always force
40 $self->config->{lazyload} = 0;
42 # if (!$self->config->{lazyload}) {
43 # return $self->load_user($authinfo, $c);
45 # ## what do we do with a lazyload?
46 # ## presumably this is coming out of session storage.
47 # ## use $authinfo to fill in the user in that case?
55 my ($self, $authinfo, $c) = @_;
57 my $dbix_class_config = 0;
59 if (exists($authinfo->{'dbix_class'})) {
60 $authinfo = $authinfo->{'dbix_class'};
61 $dbix_class_config = 1;
64 ## User can provide an arrayref containing the arguments to search on the user class.
65 ## or even provide a prepared resultset, allowing maximum flexibility for user retreival.
66 ## these options are only available when using the dbix_class authinfo hash.
67 if ($dbix_class_config && exists($authinfo->{'resultset'})) {
68 $self->_user($authinfo->{'resultset'}->first);
69 } elsif ($dbix_class_config && exists($authinfo->{'searchargs'})) {
70 $self->_user($self->resultset->search(@{$authinfo->{'searchargs'}})->first);
72 ## merge the ignore fields array into a hash - so we can do an easy check while building the query
73 my %ignorefields = map { $_ => 1} @{$self->config->{'ignore_fields_in_find'}};
76 # now we walk all the fields passed in, and build up a search hash.
77 foreach my $key (grep {!$ignorefields{$_}} keys %{$authinfo}) {
78 if ($self->resultset->result_source->has_column($key)) {
79 $searchargs->{$key} = $authinfo->{$key};
82 $self->_user($self->resultset->search($searchargs)->first);
85 if ($self->get_object) {
90 #$c->log->debug(dumper($self->{'user'}));
94 sub supported_features {
105 my ( $self ) = shift;
106 ## this used to load @wantedroles - but that doesn't seem to be used by the roles plugin, so I dropped it.
108 ## shortcut if we have already retrieved them
109 if (ref $self->_roles eq 'ARRAY') {
110 return(@{$self->_roles});
114 if (exists($self->config->{'role_column'})) {
115 my $role_data = $self->get($self->config->{'role_column'});
117 @roles = split /[ ,\|]+/, $self->get($self->config->{'role_column'});
119 $self->_roles(\@roles);
120 } elsif (exists($self->config->{'role_relation'})) {
121 my $relation = $self->config->{'role_relation'};
122 if ($self->_user->$relation->result_source->has_column($self->config->{'role_field'})) {
123 @roles = map { $_->get_column($self->config->{'role_field'}) } $self->_user->$relation->search(undef, { columns => [ $self->config->{'role_field'}]})->all();
124 $self->_roles(\@roles);
126 Catalyst::Exception->throw("role table does not have a column called " . $self->config->{'role_field'});
129 Catalyst::Exception->throw("user->roles accessed, but no role configuration found");
132 return @{$self->_roles};
138 return $self->get($self->config->{'id_field'});
142 my ($self, $frozenuser, $c) = @_;
144 # this could be a lot better. But for now it just assumes $frozenuser is an id and uses find_user
145 # XXX: hits the database on every request? Not good...
146 return $self->load( { $self->config->{'id_field'} => $frozenuser }, $c);
150 my ($self, $field) = @_;
152 if ($self->_user->can($field)) {
153 return $self->_user->$field;
168 return $self->get_object;
173 $self->_user( $self->resultset->auto_create( @_ ) );
179 $self->_user->auto_update( @_ );
184 (my $method) = (our $AUTOLOAD =~ /([^:]+)$/);
185 return if $method eq "DESTROY";
187 $self->_user->$method(@_);
195 Catalyst::Authentication::Store::DBIx::Class::User - The backing user
196 class for the Catalyst::Authentication::Store::DBIx::Class storage
201 This documentation refers to version 0.10.
205 Internal - not used directly, please see
206 L<Catalyst::Authentication::Store::DBIx::Class> for details on how to
207 use this module. If you need more information than is present there, read the
214 The Catalyst::Authentication::Store::DBIx::Class::User class implements user storage
215 connected to an underlying DBIx::Class schema object.
217 =head1 SUBROUTINES / METHODS
223 =head2 load ( $authinfo, $c )
225 Retrieves a user from storage using the information provided in $authinfo.
227 =head2 supported_features
229 Indicates the features supported by this class. These are currently Roles and Session.
233 Returns an array of roles associated with this user, if roles are configured for this user class.
237 Returns a serialized user for storage in the session. Currently, this is the value of the field
238 specified by the 'id_field' config variable.
242 Revives a serialized user from storage in the session. Currently, this uses the serialized data as the
243 value of the 'id_field' config variable.
245 =head2 get ( $fieldname )
247 Returns the value of $fieldname for the user in question. Roughly translates to a call to
248 the DBIx::Class::Row's get_column( $fieldname ) routine.
252 Retrieves the DBIx::Class object that corresponds to this user
256 Synonym for get_object
260 This is called when the auto_create_user option is turned on in
261 Catalyst::Plugin::Authentication and a user matching the authinfo provided is not found.
262 By default, this will call the C<auto_create()> method of the resultset associated
263 with this object. It is up to you to implement that method.
267 This is called when the auto_update_user option is turned on in
268 Catalyst::Plugin::Authentication. Note that by default the DBIx::Class store
269 uses every field in the authinfo hash to match the user. This means any
270 information that is provided that does not match the user record will cause
271 the user record to not be found. In order for the auto_update routine to be
272 useful, you have to add the fields you wish to update to the
273 'ignore_fields_in_find' authinfo element, or you must be using one of the advanced row
274 retrieval methods (searchargs or resultset).
276 By default, auto_update will call the C<auto_update()> method of the
277 DBIx::Class::Row object associated with the user. It is up to you to implement
278 that method (probably in your schema file)
280 =head1 BUGS AND LIMITATIONS
282 None known currently, please email the author if you find any.
286 Jason Kuri (jayk@cpan.org)
290 Copyright (c) 2007 the aforementioned authors. All rights
291 reserved. This program is free software; you can redistribute
292 it and/or modify it under the same terms as Perl itself.