RDBO/User.pm untested

filter: Statementstmt 0.0% 0 / 101 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 175 CC 19 SCAR 32.2
CC
19
Cov
0%
roles
56.0
load
42.0
new
30.0
CRAP 25.0
1 package Catalyst::Authentication::Store::RDBO::User;
2 use strict;
3 use warnings;
4  
5 use base qw/Catalyst::Authentication::User/;
6 use base qw/Class::Accessor::Fast/;
7  
8 BEGIN {
9 __PACKAGE__->mk_accessors(qw/config user_class manager_class _user _roles/);
10 }
11  
12 sub new
13 {
14 my ($class, $config, $c) = @_;
15  
16 my $self = {
17 user_class => $config->{'user_class'},
18 manager_class => $config->{'manager_class'}
19 ? $config->{'manager_class'}
20 : $config->{'user_class'} . '::Manager',
21 config => $config,
22 _roles => undef,
23 _user => undef
24 };
25  
26 bless $self, $class;
27  
28 ## Note to self- add handling of multiple-column primary keys.
29 if(!exists($self->config->{'id_field'})) {
30 my @pks = $self->user_class->meta->primary_key->column_names;
31 if($#pks == 0) {
32 $self->config->{'id_field'} = $pks[0];
33 } else {
34 Catalyst::Exception->throw("user table does not contain a single primary key column - please specify 'id_field' in config!");
35 }
36 }
37  
38 if(!$self->user_class->meta->column($self->config->{'id_field'})) {
39 Catalyst::Exception->throw("id_field set to " . $self->config->{'id_field'} . " but user table has no column by that name!");
40 }
41  
42 ## if we have lazyloading turned on - we should not query the DB unless something gets read.
43 ## that's the idea anyway - still have to work out how to manage that - so for now we always force
44 ## lazyload to off.
45 $self->config->{lazyload} = 0;
46  
47 return $self;
48 }
49  
50 sub _fetch_first
51 {
52 my ($self, $query ) = @_;
53 my $results = $self->manager_class->get_objects(
54 query => $query,
55 object_class => $self->user_class,
56 limit => 1,
57 );
58 if( ! $results || ! @$results) {
59 return undef;
60 }
61  
62 return $results->[0];
63 }
64  
65 sub load
66 {
67 my ($self, $authinfo, $c) = @_;
68  
69 my $rdbo_config = 0;
70  
71 if(exists($authinfo->{'rdbo'})) {
72 $authinfo = $authinfo->{'rdbo'};
73 $rdbo_config = 1;
74 }
75  
76 # User can provide an arrayref containing the arguments to search on
77 # the user class by providing a 'rdbo' authinfo hash.
78 if($rdbo_config && exists($authinfo->{'searchargs'})) {
79 $self->_user( $self->_fetch_first( $authinfo->{'searchargs'}));
80 } else {
81 # merge the ignore fields array into a hash - so we can do an
82 # easy check while building the query
83 my %ignorefields = map { $_ => 1 } @{ $self->config->{'ignore_fields_in_find'} };
84 my $searchargs = {};
85  
86 # now we walk all the fields passed in, and build up a search hash.
87 foreach my $key (grep { !$ignorefields{$_} } keys %{$authinfo}) {
88 if($self->user_class->meta->column($key)) {
89 $searchargs->{$key} = $authinfo->{$key};
90 }
91 }
92 if(keys %{$searchargs}) {
93 $self->_user($self->_fetch_first( [ %$searchargs ]));
94 } else {
95 Catalyst::Exception->throw("User retrieval failed: no columns from " . $self->config->{'user_model'} . " were provided");
96 }
97 }
98  
99 if($self->get_object) {
100 return $self;
101 } else {
102 return undef;
103 }
104  
105 }
106  
107 sub supported_features
108 {
109 my $self = shift;
110  
111 return {
112 session => 1,
113 roles => 1,
114 };
115 }
116  
117 sub roles
118 {
119 my ($self) = shift;
120  
121 ## shortcut if we have already retrieved them
122 if(ref $self->_roles eq 'ARRAY') {
123 return (@{ $self->_roles });
124 }
125  
126 my @roles = ();
127 if(exists($self->config->{'role_column'})) {
128 my $role_data = $self->get($self->config->{'role_column'});
129 if($role_data) {
130 @roles = split /[\s,\|]+/, $self->get($self->config->{'role_column'});
131 }
132 $self->_roles(\@roles);
133 } elsif(exists($self->config->{'role_relation'})) {
134 my $relation = $self->config->{'role_relation'};
135 if(!$self->_user->meta->relationship($relation)) {
136 Catalyst::Exception->throw('User object does not have a relation matching role_relation config');
137 }
138 my $role_field = $self->config->{'role_field'};
139 if(!$self->_user->meta->relationship($relation)->foreign_class->meta->column($role_field)) {
140 Catalyst::Exception->throw("role table does not have a column called " . $self->config->{'role_field'});
141 }
142 @roles = map { $_->$role_field } $self->_user->$relation;
143 $self->_roles(\@roles);
144 } else {
145 Catalyst::Exception->throw("user->roles accessed, but no role configuration found");
146 }
147  
148 return @{ $self->_roles };
149 }
150  
151 sub for_session
152 {
153 my $self = shift;
154  
155 return $self->get($self->config->{'id_field'});
156 }
157  
158 sub from_session
159 {
160 my ($self, $frozenuser, $c) = @_;
161  
162 my $id = $frozenuser;
163  
164 return $self->load({ $self->config->{'id_field'} => $id }, $c);
165 }
166  
167 sub get
168 {
169 my ($self, $field) = @_;
170  
171 if($self->_user->can($field)) {
172 return $self->_user->$field;
173 } else {
174 return undef;
175 }
176 }
177  
178 sub get_object
179 {
180 my ($self, $force) = @_;
181  
182 return $self->_user;
183 }
184  
185 sub obj
186 {
187 my ($self, $force) = @_;
188  
189 return $self->get_object($force);
190 }
191  
192 sub AUTOLOAD
193 {
194 my $self = shift;
195 (my $method) = (our $AUTOLOAD =~ /([^:]+)$/);
196 return if $method eq "DESTROY";
197  
198 $self->_user->$method(@_);
199 }
200  
201 1;
202  
203 __END__