Schema/Result/User.pm

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