1 package Catalyst::Authentication::Store::DBIx::Class::User;
6 use base qw/Catalyst::Authentication::User/;
7 use base qw/Class::Accessor::Fast/;
10 __PACKAGE__->mk_accessors(qw/config resultset _user _roles/);
14 my ( $class, $config, $c) = @_;
17 resultset => $c->model($config->{'user_class'}),
25 ## Note to self- add handling of multiple-column primary keys.
26 if (!exists($self->config->{'id_field'})) {
27 my @pks = $self->{'resultset'}->result_source->primary_columns;
29 $self->config->{'id_field'} = $pks[0];
31 Catalyst::Exception->throw("user table does not contain a single primary key column - please specify 'id_field' in config!");
34 if (not $self->{'resultset'}) {
35 Catalyst::Exception->throw("\$c->model('${ \$self->config->{user_class} }') did not return a resultset. Did you set user_class correctly?");
37 if (!$self->{'resultset'}->result_source->has_column($self->config->{'id_field'})) {
38 Catalyst::Exception->throw("id_field set to " . $self->config->{'id_field'} . " but user table has no column by that name!");
41 ## if we have lazyloading turned on - we should not query the DB unless something gets read.
42 ## that's the idea anyway - still have to work out how to manage that - so for now we always force
44 $self->config->{lazyload} = 0;
46 # if (!$self->config->{lazyload}) {
47 # return $self->load_user($authinfo, $c);
49 # ## what do we do with a lazyload?
50 # ## presumably this is coming out of session storage.
51 # ## use $authinfo to fill in the user in that case?
59 my ($self, $authinfo, $c) = @_;
61 my $dbix_class_config = 0;
63 if (exists($authinfo->{'dbix_class'})) {
64 $authinfo = $authinfo->{'dbix_class'};
65 $dbix_class_config = 1;
68 ## User can provide an arrayref containing the arguments to search on the user class.
69 ## or even provide a prepared resultset, allowing maximum flexibility for user retreival.
70 ## these options are only available when using the dbix_class authinfo hash.
71 if ($dbix_class_config && exists($authinfo->{'resultset'})) {
72 $self->_user($authinfo->{'resultset'}->first);
73 } elsif ($dbix_class_config && exists($authinfo->{'searchargs'})) {
74 $self->_user($self->resultset->search(@{$authinfo->{'searchargs'}})->first);
76 ## merge the ignore fields array into a hash - so we can do an easy check while building the query
77 my %ignorefields = map { $_ => 1} @{$self->config->{'ignore_fields_in_find'}};
80 # now we walk all the fields passed in, and build up a search hash.
81 foreach my $key (grep {!$ignorefields{$_}} keys %{$authinfo}) {
82 if ($self->resultset->result_source->has_column($key)) {
83 $searchargs->{$key} = $authinfo->{$key};
86 if (keys %{$searchargs}) {
87 $self->_user($self->resultset->search($searchargs)->first);
89 Catalyst::Exception->throw("User retrieval failed: no columns from " . $self->config->{'user_class'} . " were provided");
93 if ($self->get_object) {
98 #$c->log->debug(dumper($self->{'user'}));
102 sub supported_features {
113 my ( $self ) = shift;
114 ## this used to load @wantedroles - but that doesn't seem to be used by the roles plugin, so I dropped it.
116 ## shortcut if we have already retrieved them
117 if (ref $self->_roles eq 'ARRAY') {
118 return(@{$self->_roles});
122 if (exists($self->config->{'role_column'})) {
123 my $role_data = $self->get($self->config->{'role_column'});
125 @roles = split /[\s,\|]+/, $self->get($self->config->{'role_column'});
127 $self->_roles(\@roles);
128 } elsif (exists($self->config->{'role_relation'})) {
129 my $relation = $self->config->{'role_relation'};
130 if ($self->_user->$relation->result_source->has_column($self->config->{'role_field'})) {
131 @roles = map { $_->get_column($self->config->{'role_field'}) } $self->_user->$relation->search(undef, { columns => [ $self->config->{'role_field'}]})->all();
132 $self->_roles(\@roles);
134 Catalyst::Exception->throw("role table does not have a column called " . $self->config->{'role_field'});
137 Catalyst::Exception->throw("user->roles accessed, but no role configuration found");
140 return @{$self->_roles};
146 #return $self->get($self->config->{'id_field'});
148 #my $frozenuser = $self->_user->result_source->schema->freeze( $self->_user );
151 my %userdata = $self->_user->get_columns();
156 my ($self, $frozenuser, $c) = @_;
158 #my $obj = $self->resultset->result_source->schema->thaw( $frozenuser );
161 #if (!exists($self->config->{'use_userdata_from_session'}) || $self->config->{'use_userdata_from_session'} == 0) {
162 # $self->_user->discard_changes();
167 ## if use_userdata_from_session is defined in the config, we fill in the user data from the session.
168 if (exists($self->config->{'use_userdata_from_session'}) && $self->config->{'use_userdata_from_session'} != 0)
170 my $obj = $self->resultset->new_result({ %$frozenuser });
176 if (ref($frozenuser) eq 'HASH') {
177 $id = $frozenuser->{$self->config->{'id_field'}};
181 return $self->load( { $self->config->{'id_field'} => $id }, $c);
186 my ($self, $field) = @_;
188 if ($self->_user->can($field)) {
189 return $self->_user->$field;
196 my ($self, $force) = @_;
199 $self->_user->discard_changes;
206 my ($self, $force) = @_;
208 return $self->get_object($force);
213 $self->_user( $self->resultset->auto_create( @_ ) );
219 $self->_user->auto_update( @_ );
224 (my $method) = (our $AUTOLOAD =~ /([^:]+)$/);
225 return if $method eq "DESTROY";
227 $self->_user->$method(@_);
235 Catalyst::Authentication::Store::DBIx::Class::User - The backing user
236 class for the Catalyst::Authentication::Store::DBIx::Class storage
241 This documentation refers to version 0.10.
245 Internal - not used directly, please see
246 L<Catalyst::Authentication::Store::DBIx::Class> for details on how to
247 use this module. If you need more information than is present there, read the
254 The Catalyst::Authentication::Store::DBIx::Class::User class implements user storage
255 connected to an underlying DBIx::Class schema object.
257 =head1 SUBROUTINES / METHODS
263 =head2 load ( $authinfo, $c )
265 Retrieves a user from storage using the information provided in $authinfo.
267 =head2 supported_features
269 Indicates the features supported by this class. These are currently Roles and Session.
273 Returns an array of roles associated with this user, if roles are configured for this user class.
277 Returns a serialized user for storage in the session.
281 Revives a serialized user from storage in the session.
283 =head2 get ( $fieldname )
285 Returns the value of $fieldname for the user in question. Roughly translates to a call to
286 the DBIx::Class::Row's get_column( $fieldname ) routine.
290 Retrieves the DBIx::Class object that corresponds to this user
294 Synonym for get_object
298 This is called when the auto_create_user option is turned on in
299 Catalyst::Plugin::Authentication and a user matching the authinfo provided is not found.
300 By default, this will call the C<auto_create()> method of the resultset associated
301 with this object. It is up to you to implement that method.
305 This is called when the auto_update_user option is turned on in
306 Catalyst::Plugin::Authentication. Note that by default the DBIx::Class store
307 uses every field in the authinfo hash to match the user. This means any
308 information you provide with the intent to update must be ignored during the
309 user search process. Otherwise the information will most likely cause the user
310 record to not be found. To ignore fields in the search process, you
311 have to add the fields you wish to update to the 'ignore_fields_in_find'
312 authinfo element. Alternately, you can use one of the advanced row retrieval
313 methods (searchargs or resultset).
315 By default, auto_update will call the C<auto_update()> method of the
316 DBIx::Class::Row object associated with the user. It is up to you to implement
317 that method (probably in your schema file)
319 =head1 BUGS AND LIMITATIONS
321 None known currently, please email the author if you find any.
325 Jason Kuri (jayk@cpan.org)
329 Copyright (c) 2007 the aforementioned authors. All rights
330 reserved. This program is free software; you can redistribute
331 it and/or modify it under the same terms as Perl itself.