Schema/Result/User.pm

filter: Statementstmt 100% 132 / 133 Branchbran 97.3% 36 / 38 Conditioncond 100% 14 / 14 MC/DCmcdc 100% 9 / 9 Subroutinesub 100% 22 / 22 Podpod 100% 12 / 12 totaltotal 99.5% 225 / 228 CC 25 SCAR 12.8
CC
25
Cov
99%
reset_token_generate
8.0
reset_token_verify
6.0
reset_token_checksum
3.0
CRAP 3.6
« Test/Role/MySQL.pm ↑ Summary Schema/ResultSet/User.pm »
1 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package Interchange6::Schema::Result::User;
4  
5 =head1 NAME
6  
7 Interchange6::Schema::Result::User
8  
9 =cut
10  
11 2 ▶use base 'Interchange6::Schema::Base::Attribute';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 use Interchange6::Schema::Candy -components =>
14 2 ▶ [qw(InflateColumn::DateTime PassphraseColumn TimeStamp)];
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 ▶use Class::Method::Modifiers;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use Data::UUID;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use Digest::MD5;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use Session::Token;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 =head1 ACCESSORS
24  
25 =head2 users_id
26  
27 Primary key.
28  
29 =cut
30  
31 2 primary_column users_id => {
32 data_type => "integer",
33 is_auto_increment => 1,
34 sequence => "users_users_id_seq",
35 };
36  
37 =head2 username
38  
39 The username is automatically converted to lowercase so
40 we make sure that the unique constraint on username works.
41  
42 =cut
43  
44 2 unique_column username => {
45 data_type => "varchar",
46 size => 255,
47 accessor => '_username',
48 };
49  
50 sub username {
51 9 ▶ my $self = shift;
Subroutine username: called CC=2 SCAR=6.9
Pod documented
52 9 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 3 6 9
53 3 my $value = shift;
54 3 $value = check_username($value);
55 1 $self->_username($value);
56 }
57 7 return $self->_username();
58 }
59  
60 =head2 nickname
61  
62 Unique nickname for user.
63  
64 =cut
65  
66 2 unique_column nickname => {
67 data_type => "varchar",
68 is_nullable => 1,
69 size => 255,
70 };
71  
72 =head2 email
73  
74 email address.
75  
76 =cut
77  
78 2 column email => {
79 data_type => "varchar",
80 default_value => "",
81 size => 255,
82 };
83  
84 =head2 password
85  
86 See L<DBIx::Class::PassphraseColumn> and its dependency
87 L<Authen::Passphrase> for full details of supported password encodings.
88  
89 New/changed passwords are currently encoded using
90 L<Authen::Passphrase::BlowfishCrypt> using 2^14 rounds and random salt.
91  
92 Check password method is C<check_password>.
93  
94 Default value is '{CRYPT}*' which causes C<check_password> to fail.
95  
96 =cut
97  
98 2 column password => {
99 data_type => "varchar",
100 size => 2048,
101 default_value => '*', # reject
102 passphrase => 'crypt',
103 passphrase_class => 'BlowfishCrypt',
104 passphrase_args => {
105 cost => 14,
106 key_nul => 1,
107 salt_random => 1,
108 },
109 passphrase_check_method => 'check_password',
110 };
111  
112 around 'check_password' => sub {
113 7 ▶ my $orig = shift;
Subroutine __ANON__: called CC=3 SCAR=11.0
114 7 my $self = shift;
115 7 my $ret;
116 # DBIx::Class::PassphraseColumn v0.02 can throw exceptions so use eval.
117 # Must investigate further why column inflation sometimes returns undef
118 # which cause ->match to fail.
119 7 eval { $ret = $orig->($self, @_) };
120 7 ▶ if ( $ret ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ret) { } 2 5 7
121 2 $self->update( { fail_count => 0, last_login => DateTime->now } );
122 }
123 else {
124 5 ▶ $self->update( { fail_count => ( $self->fail_count || 0 ) + 1 } );
Condition100%2/2
$self->fail_count || 0
l!l
23
MC/DC100%1/1
$self->fail_count || 0
$self->fail_count
Truth table100%2/2
A
A= $self->fail_count
Aresult
11
00
125 }
126 7 return $ret;
127 2 };
128  
129 =head2 first_name
130  
131 User's first name.
132  
133 =cut
134  
135 2 column first_name => {
136 data_type => "varchar",
137 default_value => "",
138 size => 255,
139 };
140  
141 =head2 last_name
142  
143 User's last name.
144  
145 =cut
146  
147 2 column last_name => {
148 data_type => "varchar",
149 default_value => "",
150 size => 255,
151 };
152  
153 =head2 last_login
154  
155 Last login returned as L<DateTime> object. Updated on successful call to
156 C<check_password>.
157  
158 =cut
159  
160 2 column last_login => {
161 data_type => "datetime",
162 is_nullable => 1,
163 };
164  
165 =head2 fail_count
166  
167 Count of failed logins since last successful login. On successful call to
168 C<check_password> gets reset to zero but on fail is incremented.
169  
170 =cut
171  
172 2 column fail_count => {
173 data_type => "integer",
174 default_value => 0,
175 };
176  
177 =head2 reset_expires
178  
179 Date and time when L</reset_token> expires.
180  
181 =cut
182  
183 2 column reset_expires => {
184 data_type => "datetime",
185 is_nullable => 1,
186 };
187  
188 =head2 reset_token
189  
190 Used to store password reset token.
191  
192 =cut
193  
194 2 column reset_token => {
195 data_type => "varchar",
196 size => 255,
197 is_nullable => 1,
198 };
199  
200 =head2 is_anonymous
201  
202 Boolean denoting an anonymous user. Defaults to 0 (false);
203  
204 =cut
205  
206 2 column is_anonymous => {
207 data_type => "boolean",
208 default_value => 0,
209 };
210  
211 =head2 created
212  
213 Date and time when this record was created returned as L<DateTime> object.
214 Value is auto-set on insert.
215  
216 =cut
217  
218 2 column created => {
219 data_type => "datetime",
220 set_on_create => 1,
221 };
222  
223 =head2 last_modified
224  
225 Date and time when this record was last modified returned as L<DateTime> object.
226 Value is auto-set on insert and update.
227  
228 =cut
229  
230 2 column last_modified => {
231 data_type => "datetime",
232 set_on_create => 1,
233 set_on_update => 1,
234 };
235  
236 =head2 active
237  
238 Is this user account active? Default is yes.
239  
240 =cut
241  
242 2 column active => {
243 data_type => "boolean",
244 default_value => 1,
245 };
246  
247 =head1 RELATIONS
248  
249 =head2 addresses
250  
251 Type: has_many
252  
253 Related object: L<Interchange6::Schema::Result::Address>
254  
255 =cut
256  
257 2 has_many
258 addresses => "Interchange6::Schema::Result::Address",
259 "users_id",
260 { cascade_copy => 0, cascade_delete => 0 };
261  
262 =head2 carts
263  
264 Type: has_many
265  
266 Related object: L<Interchange6::Schema::Result::Cart>
267  
268 =cut
269  
270 2 has_many
271 carts => "Interchange6::Schema::Result::Cart",
272 "users_id",
273 { cascade_copy => 0, cascade_delete => 0 };
274  
275 =head2 orders
276  
277 Type: has_many
278  
279 Related object: L<Interchange6::Schema::Result::Order>
280  
281 =cut
282  
283 2 has_many
284 orders => "Interchange6::Schema::Result::Order",
285 "users_id",
286 { cascade_copy => 0, cascade_delete => 0 };
287  
288 =head2 user_attributes
289  
290 Type: has_many
291  
292 Related object: L<Interchange6::Schema::Result::UserAttribute>
293  
294 =cut
295  
296 2 has_many
297 user_attributes => "Interchange6::Schema::Result::UserAttribute",
298 "users_id",
299 { cascade_copy => 0, cascade_delete => 0 };
300  
301 =head2 user_roles
302  
303 Type: has_many
304  
305 Related object: L<Interchange6::Schema::Result::UserRole>
306  
307 =cut
308  
309 2 has_many
310 user_roles => "Interchange6::Schema::Result::UserRole",
311 "users_id";
312  
313 =head2 roles
314  
315 Type: many_to_many
316  
317 Composing rels: L</user_roles> -> role
318  
319 =cut
320  
321 2 many_to_many roles => "user_roles", "role";
322  
323 =head2 approvals
324  
325 Type: has_many
326  
327 Related object: L<Interchange6::Schema::Result::Message> FK C<approved_by_users_id>
328  
329 =cut
330  
331 2 has_many
332 approvals => "Interchange6::Schema::Result::Message",
333 { 'foreign.approved_by_users_id' => 'self.users_id' };
334  
335 =head2 messages
336  
337 Type: has_many
338  
339 Related object: L<Interchange6::Schema::Result::Message> FK C<author_users_id>
340  
341 =cut
342  
343 2 has_many
344 messages => "Interchange6::Schema::Result::Message",
345 { 'foreign.author_users_id' => 'self.users_id' };
346  
347 =head1 METHODS
348  
349 Attribute methods are provided by the L<Interchange6::Schema::Base::Attribute> class.
350  
351 =head2 sqlt_deploy_hook
352  
353 Called during table creation to add indexes on the following columns:
354  
355 =over 4
356  
357 =item * reset_token
358  
359 =back
360  
361 =cut
362  
363 sub sqlt_deploy_hook {
364 1 ▶ my ( $self, $table ) = @_;
Subroutine sqlt_deploy_hook: called CC=1 SCAR=0.0
Pod documented
365  
366 1 $table->add_index(
367 name => 'users_idx_reset_token',
368 fields => ['reset_token']
369 );
370 }
371  
372 =head2 reset_token_generate( %args );
373  
374 Arguments should be a hash of the following key/value pairs:
375  
376 =over
377  
378 =item * duration => $datetime_duration_hashref
379  
380 Value should be a hash reference containing values that can be passed directly
381 to L<DateTime::Duration/new>. Passing an undef value to duration will lead
382 to the creation of a reset token that does not expire. Default duration is
383 24 hours.
384  
385 =item * entropy => $number_of_bits
386  
387 The number of bits of entropy to be used by L<Session::Token/new>. Defaults
388 to 128.
389  
390 =back
391  
392 This method sets L</reset_expires> and L</reset_token> and returns the value
393 of L</reset_token> with checksum added.
394  
395 =cut
396  
397 sub reset_token_generate {
398 14 ▶ my ( $self, %args ) = @_;
Subroutine reset_token_generate: called CC=8 SCAR=20.8
Pod documented
399  
400 14 ▶ if ( exists $args{duration} && !defined $args{duration} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{'duration'} and not defined $args{'duration'}) { } 1 13 14
Condition100%3/3
exists $args{"duration"} and not defined $args{"duration"}
!ll&&!rl&&r
1031
MC/DC100%2/2
exists $args{"duration"} and not defined $args{"duration"}
exists $args{"duration"} not defined $args{"duration"}
Truth table100%3/3
A and not(B)
A= exists $args{"duration"}B= not defined $args{"duration"}
ABresult
0X0
100
111
401  
402 # we got undef duration so clear reset_expires
403  
404 1 $self->reset_expires(undef);
405 }
406 else {
407  
408 # attempt to set reset_expires to appropriate value
409  
410 13 ▶ if ( $args{duration} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'duration'}) { } 3 10 13
411 $self->throw_exception(
412 "duration arg to reset_token_generate must be a hashref")
413 3 ▶ unless ref( $args{duration} ) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args{"duration"} eq "HASH" 1 2 3
414 }
415 else {
416 10 $args{duration} = { hours => 24 };
417 }
418  
419 12 my $dt = DateTime->now;
420 12 $dt->add( %{$args{duration}} );
421 12 $self->reset_expires($dt);
422 }
423  
424 # generate random token and store it in the DB
425  
426 # Session::Token->new assumes entropy is numeric but first check is '>'
427 # which causes a warning on non-numeric so we check here 1st
428 die "bad value for entropy"
429 13 ▶ if ( $args{entropy} && $args{entropy} !~ /^\d+$/ );
Branch: 1 branch
BranchTrueFalseTotal
if $args{"entropy"} and not $args{"entropy"} =~ /^\d+$/ 1 12 13
Condition100%3/3
$args{"entropy"} and not $args{"entropy"} =~ /^\d+$/
!ll&&!rl&&r
1111
MC/DC100%2/2
$args{"entropy"} and not $args{"entropy"} =~ /^\d+$/
$args{"entropy"} not $args{"entropy"} =~ /^\d+$/
Truth table100%3/3
A and not(B)
A= $args{"entropy"}B= not $args{"entropy"} =~ /^\d+$/
ABresult
0X0
100
111
430  
431 # set default entropy if not defined
432 12 ▶ $args{entropy} = 128 unless $args{entropy};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"entropy"} 11 1 12
433  
434 12 my $token = Session::Token->new( entropy => $args{entropy} )->get;
435 12 $self->reset_token( $token );
436  
437 # flush changes to DB
438  
439 12 $self->update;
440  
441 # return combined token and checksum
442  
443 12 return join( "_", $token, $self->reset_token_checksum($token) );
444 }
445  
446 =head2 reset_token_checksum
447  
448 Returns the checksum for the token stored in L</reset_token>.
449  
450 Checksum is a digest of L</password>, L</reset_token> and L</reset_expires>
451 (if this is defined). This ensures that a reset token is not valid if password
452 has changed or if a newer token has been generated.
453  
454 =cut
455  
456 sub reset_token_checksum {
457 23 ▶ my $self = shift;
Subroutine reset_token_checksum: called CC=3 SCAR=11.0
Pod documented
458 23 my $digest = Digest::MD5->new();
459 23 ▶ $digest->add( $self->password->as_crypt ) if $self->password;
Branch: 1 branch
BranchTrueFalseTotal
if $self->password 23 0 23
460 23 $digest->add( $self->reset_token );
461 23 ▶ $digest->add( $self->reset_expires->datetime ) if $self->reset_expires;
Branch: 1 branch
BranchTrueFalseTotal
if $self->reset_expires 19 4 23
462 23 return $digest->hexdigest();
463 }
464  
465 =head2 reset_token_verify
466  
467 When passed combined token and checksum as argument returns 1 if token
468 and checksum are correct and L</reset_expires> is not in the past (if
469 it is defined). Returns 0 on failure.
470  
471 =cut
472  
473 sub reset_token_verify {
474 16 ▶ my $self = shift;
Subroutine reset_token_verify: called CC=6 SCAR=17.9
Pod documented
475 16 my ( $token, $checksum ) = split(/_/, shift);
476  
477 16 ▶ $self->throw_exception("Bad argument to reset_token_verify")
Branch: 1 branch
BranchTrueFalseTotal
unless $checksum 2 14 16
478 unless $checksum;
479  
480 # check token and expiry against DB and compare checksum
481  
482 # The following should be combined in a single 'if' statement but sadly
483 # we then end up with Devel::Cover being unable to test the condition
484 # correctly so instead we break it out into ugly code. At least it
485 # means we know our tests are covering all condition. :(
486 14 ▶ if ( $self->reset_token eq $token ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->reset_token eq $token) 13 1 14
487 13 ▶ if ( !$self->reset_expires || DateTime->now <= $self->reset_expires ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->reset_expires or "DateTime"->now <= $self->reset_expires) 11 2 13
Condition100%3/3
not $self->reset_expires or "DateTime"->now <= $self->reset_expires
l!l&&r!l&&!r
382
MC/DC100%2/2
not $self->reset_expires or "DateTime"->now <= $self->reset_expires
not $self->reset_expires "DateTime"->now <= $self->reset_expires
Truth table100%3/3
not(A) or B
A= not $self->reset_expiresB= "DateTime"->now <= $self->reset_expires
ABresult
1X1
011
000
488 11 ▶ if ( $self->reset_token_checksum eq $checksum ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->reset_token_checksum eq $checksum) 8 3 11
489 8 return 1;
490 }
491 }
492 }
493 6 return 0;
494 }
495  
496 =head2 new
497  
498 Overloaded method.
499  
500 If L</username> is undefined and L</is_anonymous> is defined then create
501 a unique username for this anonymous user and set L</active> to false.
502 Otherwise check username using L</check_username>.
503  
504 =cut
505  
506 sub new {
507 46 ▶ my ( $class, $attrs ) = @_;
Subroutine new: called CC=3 SCAR=11.0
Pod documented
508  
509 46 ▶ if ( defined $attrs->{is_anonymous} && !defined $attrs->{username} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $attrs->{'is_anonymous'} and not defined $attrs->{'username'}) { } 1 45 46
Condition100%3/3
defined $attrs->{"is_anonymous"} and not defined $attrs->{"username"}
!ll&&!rl&&r
4411
MC/DC100%2/2
defined $attrs->{"is_anonymous"} and not defined $attrs->{"username"}
defined $attrs->{"is_anonymous"} not defined $attrs->{"username"}
Truth table100%3/3
A and not(B)
A= defined $attrs->{"is_anonymous"}B= not defined $attrs->{"username"}
ABresult
0X0
100
111
510 1 my $ug = Data::UUID->new;
511 1 $attrs->{username} = "anonymous-" . lc( $ug->create_str );
512 1 $attrs->{active} = 0;
513 }
514 else {
515 45 $attrs->{username} = check_username($attrs->{username});
516 }
517  
518 43 my $new = $class->next::method($attrs);
519 43 return $new;
520 }
521  
522 =head2 insert
523  
524 Overloaded method. Always add new users to Role with name 'user' unless user
525 has L</is_anonymous> set in which case add user to role 'anonymous';
526  
527 =cut
528  
529 sub insert {
530 43 ▶ my ($self, @args) = @_;
Subroutine insert: called CC=3 SCAR=11.0
Pod documented
531
532 43 my $guard = $self->result_source->schema->txn_scope_guard;
533  
534 43 $self->next::method(@args);
535  
536 42 ▶ my $role_name = $self->is_anonymous ? 'anonymous' : 'user';
Branch: 1 branch
BranchTrueFalseTotal
$self->is_anonymous ? : 2 40 42
537  
538 42 my $user_role = $self->result_source->schema->resultset('Role')
539 ->find( { name => $role_name } );
540  
541 # uncoverable branch false
542 42 ▶ if ( $user_role ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($user_role) { } 42 0 42
543 42 $self->create_related( 'user_roles', { roles_id => $user_role->id } );
544 }
545 else {
546 # we should never get here
547 # uncoverable statement
548 0 $self->throw_exception(
549 "Role with name 'user' must exist when creating a new user");
550 }
551
552 42 $guard->commit;
553 42 return $self;
554 }
555  
556 =head2 update
557  
558 Overloaded method. Check username using L</check_username> if supplied.
559  
560 =cut
561  
562 sub update {
563 21 ▶ my ( $self, $upd ) = @_;
Subroutine update: called CC=2 SCAR=6.9
Pod documented
564  
565 21 my $username;
566  
567 # username may have been passed as arg or previously set
568  
569 21 ▶ if ( exists $upd->{username} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $upd->{"username"}) 1 20 21
570 1 $upd->{username} = check_username($upd->{username});
571 }
572  
573 21 return $self->next::method($upd);
574 }
575  
576 =head2 check_username( $username )
577  
578 Die if C<$username> is undef or empty string. Otherwise return C<lc($username)>
579  
580 =cut
581  
582 sub check_username {
583 49 ▶ my $value = shift;
Subroutine check_username: called CC=3 SCAR=11.0
Pod documented
584 49 ▶ die "username cannot be undef" unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 3 46 49
585 46 $value =~ s/(^\s+|\s+$)//g;
586 46 ▶ die "username cannot be empty string" if $value eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $value eq "" 2 44 46
587 44 return lc($value);
588 }
589  
590 =head2 blog_posts
591  
592 Returns resultset of messages that are blog posts
593  
594 =cut
595  
596 sub blog_posts {
597 1 ▶ my $self = shift;
Subroutine blog_posts: called CC=1 SCAR=0.0
Pod documented
598 1 return $self->messages->search( { 'message_type.name' => 'blog_post' },
599 { join => 'message_type' } );
600 }
601  
602 =head2 name
603  
604 Returns L</first_name> and L</last_name> joined by a single space.
605  
606 =cut
607  
608 sub name {
609 5 ▶ my $self = shift;
Subroutine name: called CC=1 SCAR=0.0
Pod documented
610 5 return join( " ", $self->first_name, $self->last_name );
611 }
612  
613 =head2 reviews
614  
615 Returns resultset of messages that are reviews.
616  
617 =cut
618  
619 sub reviews {
620 1 ▶ my $self = shift;
Subroutine reviews: called CC=1 SCAR=0.0
Pod documented
621 1 return $self->messages->search( { 'message_type.name' => 'product_review' },
622 { join => 'message_type' } );
623 }
624  
625 2 1;
« Test/Role/MySQL.pm ↑ Summary Schema/ResultSet/User.pm »