ORM/User.pm untested

filter: Statementstmt 0.0% 0 / 89 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 153 CC 16 SCAR 29.7
CC
16
Cov
0%
roles
56.0
new
12.0
from_session
12.0
CRAP 19.6
1 package Catalyst::Authentication::Store::Fey::ORM::User;
2 BEGIN {
3 $Catalyst::Authentication::Store::Fey::ORM::User::VERSION = '0.001';
4 }
5 # ABSTRACT: The backing user class for the L<Catalyst::Authentication::Store::Fey::ORM> storage module.
6  
7 use Moose;
8 use namespace::autoclean;
9 extends 'Catalyst::Authentication::User';
10  
11 has 'config' => ( is => 'rw' );
12 has '_user' => ( is => 'rw' );
13 has '_roles' => ( is => 'rw' );
14  
15 sub new {
16 my ( $class, $config, $c ) = @_;
17  
18 my $self = {
19 config => $config,
20 _roles => undef,
21 _user => undef,
22 };
23 bless $self, $class;
24  
25 Catalyst::Exception->throw(
26 'Did you set user_model correctly?'
27 ) unless $self->config->{user_model};
28  
29 Catalyst::Exception->throw(
30 'Did you set id_field correctly?'
31 ) unless $self->config->{id_field};
32  
33 return $self;
34 }
35  
36 sub supported_features {
37 my $self = shift;
38  
39 return {
40 session => 1,
41 roles => 1,
42 };
43 }
44  
45 sub load {
46 my ( $self, $authinfo, $c ) = @_;
47  
48 #
49 # search with $authinfo then set $self->_user
50 #
51 my $class = $self->config->{user_model};
52 my $user = $class->new( %$authinfo );
53 $self->_user($user);
54  
55 return $self->get_object ? $self : undef;
56 }
57  
58 sub roles {
59 my $self = shift;
60  
61 return @{$self->_roles} if ref $self->_roles eq 'ARRAY';
62  
63 my @roles;
64 if (exists($self->config->{role_column})) {
65 my $role_data = $self->get( $self->config->{role_column} );
66 @roles = split /[\s,\|]+/, $role_data if $role_data;
67 $self->_roles(\@roles);
68 }
69 elsif (exists($self->config->{role_relation})) {
70 my $relation = $self->config->{role_relation};
71 my $iter = $self->_user->$relation;
72 if ($iter) {
73 while ( my $role = $iter->next ) {
74 if (my $method = $role->can($self->config->{role_field})) {
75 push @roles, $role->$method;
76 }
77 else {
78 Catalyst::Exception->throw(
79 'Did you set role_field correctly?'
80 );
81 }
82 }
83 }
84 else {
85 Catalyst::Exception->throw(
86 'Did you set role_relation correctly?'
87 );
88 }
89 $self->_roles(\@roles);
90 }
91 else {
92 Catalyst::Exception->throw(
93 "user->roles accessed, but no role configuration found"
94 );
95 }
96  
97 return @{$self->_roles};
98 }
99  
100 sub for_session {
101 my $self = shift;
102  
103 my %userdata;
104 for my $column ( $self->_user->Table->columns ) {
105 my $method = $column->name;
106 $userdata{$column->name} = $self->_user->$method;
107 }
108  
109 return \%userdata;
110 }
111  
112 sub from_session {
113 my ( $self, $frozenuser, $c ) = @_;
114  
115 if ( exists( $self->config->{'use_userdata_from_session'} )
116 && $self->config->{'use_userdata_from_session'} != 0 )
117 {
118 return $self->load( $frozenuser, $c );
119 }
120  
121 if (ref $frozenuser eq 'HASH') {
122 return $self->load(
123 { map { ($_ => $frozenuser->{$_}) } $self->config->{id_field} },
124 $c,
125 );
126 }
127  
128 return $self->load( { $self->config->{id_field} => $frozenuser }, $c );
129 }
130  
131 sub get {
132 my ( $self, $field ) = @_;
133  
134 if (my $method = $self->_user->can($field)) {
135 return $self->_user->$method;
136 }
137 else {
138 return undef;
139 }
140 }
141  
142 sub get_object {
143 my $self = shift;
144  
145 return $self->_user;
146 }
147  
148 sub can {
149 my $self = shift;
150  
151 return $self->SUPER::can(@_) || do {
152 my ($method) = @_;
153 if (my $method = $self->_user->can($method)) {
154 return sub { shift->_user->$method(@_) };
155 }
156 else {
157 return;
158 }
159 };
160 }
161  
162 sub AUTOLOAD {
163 my $self = shift;
164  
165 (my $method) = (our $AUTOLOAD =~ /([^:]+)$/);
166 return if $method eq "DESTROY";
167  
168 if (my $method = $self->_user->can($method)) {
169 return $self->_user->$method(@_);
170 }
171 else {
172 return;
173 }
174 }
175  
176 __PACKAGE__->meta->make_immutable(inline_constructor => 0);
177  
178 1;
179  
180  
181 =pod
182  
183 =encoding utf-8
184  
185 =head1 NAME
186  
187 Catalyst::Authentication::Store::Fey::ORM::User - The backing user class for the L<Catalyst::Authentication::Store::Fey::ORM> storage module.
188  
189 =head1 VERSION
190  
191 version 0.001
192  
193 =head1 SYNOPSIS
194  
195 Internal - not used directly, please see
196 L<Catalyst::Authentication::Store::Fey::ORM> for details
197 on how to use this module.
198 If you need more information than is present there, read the source.
199  
200 =head1 DESCRIPTION
201  
202 The L<Catalyst::Authentication::Store::Fey::ORM::User> class
203 implements user storage connected to an underlying
204 L<Fey::ORM> schema object.
205  
206 =head1 METHODS
207  
208 =head2 new ( $config, $c )
209  
210 Constructor.
211  
212 =head2 supported_features
213  
214 Indicates the features supported by this class.
215 These are currently Roles and Session.
216  
217 =head2 load ( $authinfo, $c )
218  
219 Retrieves a user from storage using the information
220 provided in C<$authinfo>.
221  
222 =head2 roles
223  
224 Returns an array of roles associated with this user,
225 if roles are configured for this user class.
226  
227 =head2 for_session
228  
229 Returns a serialized user for storage in the session.
230  
231 =head2 from_session ( $frozenuser, $c )
232  
233 Revives a serialized user from storage in the session.
234  
235 =head2 get ( $field )
236  
237 Returns the value of $field for the user in question.
238  
239 =head2 get_object
240  
241 Retrieves the L<Fey::ORM> object that corresponds to this user
242  
243 =head2 can
244  
245 Delegates handling of the can method to the underlieing user row.
246  
247 =head2 AUTOLOAD
248  
249 Delegates method calls to the underlieing user row.
250  
251 =head1 AUTHOR
252  
253 Keedi Kim - κΉ€λ„ν˜• <keedi@cpan.org>
254  
255 =head1 COPYRIGHT AND LICENSE
256  
257 This software is copyright (c) 2011 by Keedi Kim.
258  
259 This is free software; you can redistribute it and/or modify it under
260 the same terms as the Perl 5 programming language system itself.
261  
262 =cut
263  
264  
265 __END__