lib/OpusVL/AppKit/RolesFor/Schema/AppKitAuthDB/Result/User.pm

filter: Statementstmt 50.5% 49 / 97 Branchbran 40.6% 13 / 32 Conditioncond 55.5% 5 / 9 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 47.0% 8 / 17 Podpod 66.6% 10 / 15 totaltotal 49.4% 87 / 176 CC 22 SCAR 18.6
CC
22
Cov
49%
params_hash
20.0
check_password
7.0
getdata
6.0
CRAP 6.4
« blib/lib/OpusVL/AppKit/RolesFor/Controller/GUI.pm ↑ Summary lib/OpusVL/AppKit/Action/AppKitForm.pm »
1 package OpusVL::AppKit::RolesFor::Schema::AppKitAuthDB::Result::User;
2  
3 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 14 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6  
7 sub setup_authdb
8 {
9 13 ▶ my $class = shift;
Subroutine setup_authdb: called CC=1 SCAR=0.0
Pod documented
10  
11 13 $class->load_components("EncodedColumn");
12
13 # Alter the password column to enable encoded password..
14 13 $class->add_columns
15 (
16 "+password",
17 {
18 encode_column => 1,
19 encode_class => 'Crypt::Eksblowfish::Bcrypt',
20 encode_args => { key_nul => 0, cost => 8 },
21 encode_check_method => '_local_check_password',
22 }
23 );
24  
25 13 $class->many_to_many( roles => 'users_roles', 'role' );
26 13 $class->many_to_many( parameters => 'users_parameters', 'parameter' );
27 }
28  
29  
30 sub check_password
31 {
32 11 ▶ my $self = shift;
Subroutine check_password: called CC=6 SCAR=19.5
Pod documented
33 # FIXME: is our database inconsistent?
34 11 ▶ return 0 unless $self->status eq 'active' || $self->status eq 'enabled';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->status eq "active" or $self->status eq "enabled" 0 11 11
Condition33%1/3
$self->status eq "active" or $self->status eq "enabled"
l!l&&r!l&&!r
0110
MC/DC0%0/2
$self->status eq "active" or $self->status eq "enabled"
$self->status eq "active" $self->status eq "enabled"
Truth table33%1/3
A or B
A= $self->status eq "active"B= $self->status eq "enabled"
ABresult
1X1
011
000
35 11 my $schema = $self->result_source->schema;
36 # see if the schema has been given a method for
37 # checking the password
38 11 my $result;
39 11 ▶ if($schema->can('password_check') && $schema->password_check)
Branch: 1 branch
BranchTrueFalseTotal
if ($schema->can('password_check') and $schema->password_check) { } 0 11 11
Condition33%1/3
$schema->can("password_check") and $schema->password_check
!ll&&!rl&&r
1100
MC/DC0%0/2
$schema->can("password_check") and $schema->password_check
$schema->can("password_check") $schema->password_check
Truth table33%1/3
A and B
A= $schema->can("password_check")B= $schema->password_check
ABresult
0X0
100
111
40 {
41 # look up ldap password.
42 0 $result = $schema->password_check->check_password($self->username, @_);
43 }
44 else
45 {
46 11 $result = $self->_local_check_password(@_);
47 }
48 11 ▶ if($result)
Branch: 1 branch
BranchTrueFalseTotal
if ($result) { } 9 2 11
49 {
50 9 $self->successful_login;
51 }
52 else
53 {
54 2 $self->failed_login;
55 }
56 11 return $result;
57 }
58  
59 sub failed_login
60 {
61 2 ▶ my $self = shift;
Subroutine failed_login: called CC=1 SCAR=0.0
Pod undocumented
62 2 $self->update({ last_failed_login => DateTime->now() });
63 }
64  
65 sub successful_login
66 {
67 9 ▶ my $self = shift;
Subroutine successful_login: called CC=1 SCAR=0.0
Pod undocumented
68 9 $self->update({ last_login => DateTime->now() });
69 }
70  
71  
72  
73 sub getdata
74 {
75 0 ▶ my $self = shift;
Subroutine getdata: not called CC=2 SCAR=17.9
Pod documented
76 0 my ($key) = @_;
77 0 my $data = $self->find_related( 'users_data', { key => $key } );
78 0 ▶ return undef unless $data;
Branch: 1 branch
BranchTrueFalseTotal
unless $data 0 0 0
79 0 return $data->value;
80 }
81  
82  
83 sub setdata
84 {
85 0 ▶ my $self = shift;
Subroutine setdata: not called CC=1 SCAR=6.9
Pod documented
86 0 my ($key, $value) = @_;
87 0 my $data = $self->find_or_create_related( 'users_data', { key => $key } );
88 0 $data->update( { value => $value } );
89 0 return 1;
90 }
91  
92  
93 sub disable
94 {
95 0 ▶ my $self = shift;
Subroutine disable: not called CC=2 SCAR=17.9
Pod documented
96  
97 0 ▶ if ( $self->status )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->status) 0 0 0
98 {
99 0 $self->update( { status => 'disabled' } );
100 0 return 1;
101 }
102 0 return 0;
103 }
104  
105  
106 sub enable
107 {
108 0 ▶ my $self = shift;
Subroutine enable: not called CC=2 SCAR=17.9
Pod documented
109  
110 0 ▶ if ( $self->status )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->status) 0 0 0
111 {
112 0 $self->update( { status => 'enabled' } );
113 0 return 1;
114 }
115 0 return 0;
116 }
117  
118  
119 sub params_hash
120 {
121 0 ▶ my $self = shift;
Subroutine params_hash: not called CC=4 SCAR=30.0
Pod documented
122  
123 0 my %hash;
124 0 foreach my $rp ( $self->users_parameters )
125 {
126 0 ▶ next unless defined $rp;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rp 0 0 0
127 0 ▶ next unless defined $rp->parameter;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rp->parameter 0 0 0
128 0 $hash{ $rp->parameter->parameter } = $rp->value;
129 }
130  
131 0 return \%hash;
132 }
133  
134  
135 sub set_param_by_name
136 {
137 0 ▶ my $self = shift;
Subroutine set_param_by_name: not called CC=2 SCAR=17.9
Pod documented
138 0 my ( $param_name, $param_value ) = @_;
139  
140 # find the param..
141 0 my $param = $self->result_source->schema->resultset('Parameter')->find( { parameter => $param_name } );
142  
143 # return undef, if we could find the param..
144 0 ▶ return undef unless $param;
Branch: 1 branch
BranchTrueFalseTotal
unless $param 0 0 0
145  
146 # add to users parameter...
147 0 $self->update_or_create_related( 'users_parameters', { value => $param_value, parameter_id => $param->id } );
148  
149 0 return 1;
150 }
151  
152 sub get_parameter_value_by_name
153 {
154 0 ▶ my $self = shift;
Subroutine get_parameter_value_by_name: not called CC=2 SCAR=17.9
Pod undocumented
155 0 my $val = $self->get_parameter_by_name(@_);
156 0 ▶ return unless $val;
Branch: 1 branch
BranchTrueFalseTotal
unless $val 0 0 0
157 0 return $val->value;
158 }
159  
160 sub get_parameter_by_name
161 {
162 0 ▶ my $self = shift;
Subroutine get_parameter_by_name: not called CC=1 SCAR=6.9
Pod undocumented
163 0 my $name = shift;
164  
165 0 my $param = $self->users_parameters->find({ 'parameter.parameter' => $name }, { join => [ 'parameter' ] });
166 0 return $param;
167 }
168  
169  
170 sub delete_param_by_name
171 {
172 0 ▶ my $self = shift;
Subroutine delete_param_by_name: not called CC=2 SCAR=17.9
Pod documented
173 0 my ( $param_name ) = @_;
174  
175 # FIXME: this code blows, use get_parameter_by_name instead.
176 # find the param..
177 0 my $param = $self->result_source->schema->resultset('Parameter')->find( { parameter => $param_name } );
178  
179 # return undef, if we could find the param..
180 0 ▶ return undef unless $param;
Branch: 1 branch
BranchTrueFalseTotal
unless $param 0 0 0
181  
182 # delete to users parameter...
183 0 $self->delete_related( 'users_parameters', { parameter_id => $param->id } );
184  
185 0 return 1;
186 }
187  
188  
189 sub roles_modifiable
190 {
191 27 ▶ my $self = shift;
Subroutine roles_modifiable: called CC=5 SCAR=16.1
Pod undocumented
192 27 my $schema = $self->result_source->schema;
193  
194 # check to see if any of the current roles allow access to all
195 27 ▶ if (grep { $_ } map { $_->can_change_any_role } $self->roles->all)
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_;} map({$_->can_change_any_role;} $self->roles->all)) 2 25 27
196 {
197 2 return $schema->resultset('Role');
198 }
199 25 my $allowed_roles = $self->roles->search_related('roles_allowed_roles');
200 25 ▶ if($allowed_roles->count == 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($allowed_roles->count == 0) 19 6 25
201 {
202 # check to see if any allowed roles are setup
203 # if not return all roles.
204 19 ▶ if($schema->resultset('RoleAllowed')->count == 0
Branch: 1 branch
BranchTrueFalseTotal
if ($schema->resultset("RoleAllowed")->count == 0 and $schema->resultset("RoleAdmin")->count == 0) 17 2 19
Condition100%3/3
$schema->resultset("RoleAllowed")->count == 0 and $schema->resultset("RoleAdmin")->count == 0
!ll&&!rl&&r
1117
MC/DC100%2/2
$schema->resultset("RoleAllowed")->count == 0 and $schema->resultset("RoleAdmin")->count == 0
$schema->resultset("RoleAllowed")->count == 0 $schema->resultset("RoleAdmin")->count == 0
Truth table100%3/3
A and B
A= $schema->resultset("RoleAllowed")->count == 0B= $schema->resultset("RoleAdmin")->count == 0
ABresult
0X0
100
111
205 && $schema->resultset('RoleAdmin')->count == 0)
206 {
207 17 return $schema->resultset('Role');
208 }
209 }
210 8 my $roles = $schema->resultset('Role')->search({ id => { in => $allowed_roles->get_column('role_allowed')->as_query }});
211  
212 8 return $roles;
213 }
214  
215  
216 sub can_modify_user
217 {
218 2 ▶ my ($self, $username) = @_;
Subroutine can_modify_user: called CC=4 SCAR=13.9
Pod documented
219 2 my $schema = $self->result_source->schema;
220 2 my $other_user = $schema->resultset('User')->find({ username => $username});
221 2 ▶ die "Unable to find user '$username'" unless $other_user;
Branch: 1 branch
BranchTrueFalseTotal
unless $other_user 0 2 2
222 2 my @roles = $other_user->roles->all;
223 2 my @allowed = $self->roles_modifiable->all;
224 2 my %allowed_hash = map { $_->role => 1 } @allowed;
225 2 for my $role (@roles)
226 {
227 3 ▶ return 0 unless $allowed_hash{$role->role};
Branch: 1 branch
BranchTrueFalseTotal
unless $allowed_hash{$role->role} 1 2 3
228 }
229 1 return 1;
230 }
231  
232 14 1;
233  
234 __END__
235  
236 =pod
237  
238 =encoding UTF-8
239  
240 =head1 NAME
241  
242 OpusVL::AppKit::RolesFor::Schema::AppKitAuthDB::Result::User
243  
244 =head1 VERSION
245  
246 version 2.22
247  
248 =head2 setup_authdb
249  
250 This need to be called as the User result class is being setup to
251 finish the table setup.
252  
253 =head2 check_password
254  
255 The check_password function is usually called by Catalyst to determine
256 if the password is correct for a user. It returns 0 for false and 1 for
257 true. If the database schema has a check_password function that is used,
258 otherwise the standard Bcrypt function is used to check the hash stored
259 in the database.
260  
261 =head2 getdata
262  
263 =head2 setdata
264  
265 =head2 disable
266  
267 Disables a users account.
268  
269 =head2 enable
270  
271 Enables a users account.
272  
273 =head2 params_hash
274  
275 Finds all a users parameters, matches them with the value and returns a nice Hash ref.
276  
277 =head2 set_param_by_name
278  
279 Sets a users parameter by the parameter name.
280 Returns:
281 undef - if the param could be found by name.
282 1 - if the param was set successfully.
283  
284 =head2 delete_param_by_name
285  
286 Deltes a users parameter by the parameter name.
287 Returns:
288 undef - if the param could be found by name.
289 1 - if the param was deleted successfully.
290  
291 =head2 roles_allowed
292  
293 Returns the list of roles this user is allowed to modify.
294  
295 =head2 can_modify_user
296  
297 This method returns true if the user is allowed to modify the user in question.
298  
299 It determines this by checking the roles the current user is allowed to modify
300 to the roles the other user has. If it's not allowed to modify a role that user
301 has then it will return false.
302  
303 $user->can_modify_user('colin');
304  
305 =head1 AUTHOR
306  
307 OpusVL - www.opusvl.com
308  
309 =head1 COPYRIGHT AND LICENSE
310  
311 This software is copyright (c) 2010 by OpusVL - www.opusvl.com.
312  
313 This is free software; you can redistribute it and/or modify it under
314 the same terms as the Perl 5 programming language system itself.
315  
316 =cut
« blib/lib/OpusVL/AppKit/RolesFor/Controller/GUI.pm ↑ Summary lib/OpusVL/AppKit/Action/AppKitForm.pm »