Silki/Schema/User.pm

filter: Statementstmt 96.0% 49 / 51 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 17 / 17 Podpod n/a totaltotal 97.0% 66 / 68 CC 1 SCAR 0.2
CC
1
Cov
96%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Silki/Schema/PageRevision.pm ↑ Summary Silki/I18N.pm »
1 package Silki::Schema::User;
2 {
3 $Silki::Schema::User::VERSION = '0.29';
4 }
5  
6 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use feature ':5.10';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 ▶use Authen::Passphrase::BlowfishCrypt;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Digest::SHA qw( sha1_hex );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Fey::Literal::Function;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use Fey::Object::Iterator::FromSelect;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use Fey::ORM::Exceptions qw( no_such_row );
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use Fey::Placeholder;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use List::AllUtils qw( all any none first first_index );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use Silki::Email qw( send_email );
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶use Silki::I18N qw( loc );
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶use Silki::Schema;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 0 ▶use Silki::Schema::Domain;
Subroutine BEGIN: called CC=1 SCAR=2.6
25 use Silki::Schema::Page;
26 use Silki::Schema::Permission;
27 use Silki::Schema::Role;
28 use Silki::Types qw( Int Str Bool );
29 use Silki::Util qw( string_is_empty );
30  
31 use Fey::ORM::Table;
32 use MooseX::ClassAttribute;
33 use MooseX::Params::Validate qw( pos_validated_list validated_list );
34  
35 my $Schema = Silki::Schema->Schema();
36  
37 with 'Silki::Role::Schema::URIMaker';
38  
39 with 'Silki::Role::Schema::SystemLogger' =>
40 { methods => [ 'insert', 'update', 'delete' ] };
41  
42 with 'Silki::Role::Schema::DataValidator' => {
43 steps => [
44 '_has_password_or_openid_uri',
45 '_email_address_is_unique',
46 '_normalize_and_validate_openid_uri',
47 '_openid_uri_is_unique',
48 ],
49 };
50  
51 has_policy 'Silki::Schema::Policy';
52  
53 has_table( $Schema->table('User') );
54  
55 has_one 'creator' => ( table => $Schema->table('User') );
56  
57 has_one 'image' => (
58 table => $Schema->table('UserImage'),
59 undef => 1,
60 );
61  
62 has_many 'pages' => ( table => $Schema->table('Page') );
63  
64 has best_name => (
65 is => 'ro',
66 isa => Str,
67 lazy => 1,
68 builder => '_build_best_name',
69 clearer => '_clear_best_name',
70 );
71  
72 has has_valid_password => (
73 is => 'ro',
74 isa => Bool,
75 lazy => 1,
76 builder => '_build_has_valid_password',
77 clearer => '_clear_has_valid_password',
78 );
79  
80 has has_login_credentials => (
81 is => 'ro',
82 isa => Bool,
83 lazy => 1,
84 builder => '_build_has_login_credentials',
85 clearer => '_clear_has_login_credentials',
86 );
87  
88 class_has _RoleInWikiSelect => (
89 is => 'ro',
90 does => 'Fey::Role::SQL::ReturnsData',
91 lazy => 1,
92 builder => '_BuildRoleInWikiSelect',
93 );
94  
95 class_has _MemberWikiCountSelect => (
96 is => 'ro',
97 does => 'Fey::Role::SQL::ReturnsData',
98 lazy => 1,
99 builder => '_BuildMemberWikiCountSelect',
100 );
101  
102 class_has _MemberWikiSelect => (
103 is => 'ro',
104 does => 'Fey::Role::SQL::ReturnsData',
105 lazy => 1,
106 builder => '_BuildMemberWikiSelect',
107 );
108  
109 class_has _AllWikiCountSelect => (
110 is => 'ro',
111 does => 'Fey::Role::SQL::ReturnsData',
112 lazy => 1,
113 builder => '_BuildAllWikiCountSelect',
114 );
115  
116 class_has _AllWikiSelect => (
117 is => 'ro',
118 does => 'Fey::Role::SQL::ReturnsData',
119 lazy => 1,
120 builder => '_BuildAllWikiSelect',
121 );
122  
123 class_has _SharedWikiSelect => (
124 is => 'ro',
125 isa => 'Fey::SQL::Union',
126 lazy => 1,
127 builder => '_BuildSharedWikiSelect',
128 );
129  
130 class_has _RecentlyViewedPagesSelect => (
131 is => 'ro',
132 isa => 'Fey::SQL::Select',
133 lazy => 1,
134 builder => '_BuildRecentlyViewedPagesSelect',
135 );
136  
137 class_has _AllPageCountSelect => (
138 is => 'ro',
139 does => 'Fey::Role::SQL::ReturnsData',
140 lazy => 1,
141 builder => '_BuildAllPageCountSelect',
142 );
143  
144 class_has _AllRevisionCountSelect => (
145 is => 'ro',
146 does => 'Fey::Role::SQL::ReturnsData',
147 lazy => 1,
148 builder => '_BuildAllRevisionCountSelect',
149 );
150  
151 class_has _AllFileCountSelect => (
152 is => 'ro',
153 does => 'Fey::Role::SQL::ReturnsData',
154 lazy => 1,
155 builder => '_BuildAllFileCountSelect',
156 );
157  
158 class_has 'SystemUser' => (
159 is => 'ro',
160 isa => __PACKAGE__,
161 lazy => 1,
162 default => sub { __PACKAGE__->_FindOrCreateSystemUser() },
163 );
164  
165 class_has 'GuestUser' => (
166 is => 'ro',
167 isa => __PACKAGE__,
168 lazy => 1,
169 default => sub { __PACKAGE__->_FindOrCreateGuestUser() },
170 );
171  
172 class_has _ActiveUserCountSelect => (
173 is => 'ro',
174 isa => 'Fey::SQL::Select',
175 lazy => 1,
176 builder => '_BuildActiveUserCountSelect',
177 );
178  
179 class_has _ActiveUsersSelect => (
180 is => 'ro',
181 isa => 'Fey::SQL::Select',
182 lazy => 1,
183 builder => '_BuildActiveUsersSelect',
184 );
185  
186 class_has _AllUsersSelect => (
187 is => 'ro',
188 isa => 'Fey::SQL::Select',
189 lazy => 1,
190 builder => '_BuildAllUsersSelect',
191 );
192  
193 class_has _AllRevisionsForDeleteSelect => (
194 is => 'ro',
195 isa => 'Fey::SQL::Select',
196 lazy => 1,
197 builder => '_BuildAllRevisionsForDeleteSelect',
198 );
199  
200 {
201 my $select = __PACKAGE__->_MemberWikiCountSelect();
202  
203 query member_wiki_count => (
204 select => $select,
205 bind_params => sub { $_[0]->user_id(), $select->bind_params() },
206 );
207 }
208  
209 {
210 my $select = __PACKAGE__->_MemberWikiSelect();
211  
212 has_many member_wikis => (
213 table => $Schema->table('Wiki'),
214 select => $select,
215 bind_params => sub { $_[0]->user_id(), $select->bind_params() },
216 );
217 }
218  
219 {
220 my $select = __PACKAGE__->_AllWikiCountSelect();
221  
222 query all_wiki_count => (
223 select => $select,
224 bind_params => sub { ( $_[0]->user_id() ) x 3 },
225 );
226 }
227  
228 {
229 my $select = __PACKAGE__->_AllWikiSelect();
230  
231 has_many all_wikis => (
232 table => $Schema->table('Wiki'),
233 select => $select,
234 bind_params => sub { ( $_[0]->user_id() ) x 3 },
235 );
236 }
237  
238 {
239 my $select = __PACKAGE__->_AllPageCountSelect();
240  
241 query page_count => (
242 select => $select,
243 bind_params => sub { ( $_[0]->user_id() ) },
244 );
245 }
246  
247 {
248 my $select = __PACKAGE__->_AllRevisionCountSelect();
249  
250 query revision_count => (
251 select => $select,
252 bind_params => sub { ( $_[0]->user_id() ) },
253 );
254 }
255  
256 {
257 my $select = __PACKAGE__->_AllFileCountSelect();
258  
259 query file_count => (
260 select => $select,
261 bind_params => sub { ( $_[0]->user_id() ) },
262 );
263 }
264  
265 with 'Silki::Role::Schema::Serializes' => {
266 skip => ['password'],
267 };
268  
269 my $UnusablePW = '*unusable*';
270 around insert => sub {
271 my $orig = shift;
272 my $class = shift;
273 my %p = @_;
274  
275 if ( $p{requires_activation} && $p{disable_login} ) {
276 die loc(
277 'Cannot pass requires_activation and disable_login when inserting a user'
278 );
279 }
280  
281 if ( delete $p{requires_activation} ) {
282 $p{confirmation_key}
283 = $class->_make_confirmation_key( $p{email_address} );
284  
285 $p{password} = $UnusablePW;
286 }
287 elsif ( delete $p{disable_login} ) {
288 $p{password} = $UnusablePW;
289 $p{openid_uri} = undef;
290 $p{confirmation_key} = undef;
291 }
292 elsif ( defined $p{password} ) {
293 $p{password} = $class->_password_as_rfc2307( $p{password} );
294 }
295  
296 $p{username} //= $p{email_address};
297  
298 $p{created_by_user_id} = $p{user}->user_id()
299 if $p{user};
300  
301 return $class->$orig(%p);
302 };
303  
304 around update => sub {
305 my $orig = shift;
306 my $self = shift;
307 my %p = @_;
308  
309 if ( !string_is_empty( $p{email_address} )
310 && string_is_empty( $p{username} )
311 && $self->username() eq $self->email_address() ) {
312 $p{username} = $p{email_address};
313 }
314  
315 unless ( string_is_empty( $p{password} ) ) {
316 $p{password} = $self->_password_as_rfc2307( $p{password} );
317 }
318  
319 if ( delete $p{disable_login} ) {
320 $p{password} = $UnusablePW;
321 $p{openid_uri} = undef;
322 $p{confirmation_key} = undef;
323 }
324  
325 $p{last_modified_datetime} = Fey::Literal::Function->new('NOW');
326  
327 return $self->$orig(%p);
328 };
329  
330 after update => sub {
331 $_[0]->_clear_best_name();
332 $_[0]->_clear_has_valid_password();
333 $_[0]->_clear_has_login_credentials();
334 };
335  
336 around delete => sub {
337 my $orig = shift;
338 my $self = shift;
339 my %p = @_;
340  
341 Silki::Schema->RunInTransaction(
342 sub {
343 my $revisions = $self->_all_revisions_for_delete();
344 while ( my $revision = $revisions->next() ) {
345 $revision->delete( user => $p{user} );
346 }
347  
348 $self->$orig(%p);
349 }
350 );
351 };
352  
353 sub _system_log_values_for_insert {
354 my $class = shift;
355 my %p = @_;
356  
357 my $msg = 'Created user: ' . $p{username};
358  
359 return (
360 message => $msg,
361 data_blob => \%p,
362 );
363 }
364  
365 sub _system_log_values_for_update {
366 my $self = shift;
367 my %p = @_;
368  
369 my $msg;
370 if ( exists $p{is_disabled}
371 && $p{is_disabled}
372 && !$self->is_disabled() ) {
373  
374 $msg = 'Disabled user';
375 }
376 elsif ($self->is_disabled()
377 && exists $p{is_disabled}
378 && !$p{is_disabled} ) {
379  
380 $msg = 'Enabled user';
381 }
382 else {
383 $msg = 'Updated user';
384 }
385  
386 $msg .= ': ' . $self->best_name();
387  
388 my %blob = %p;
389 delete $blob{last_modified_datetime};
390  
391 return (
392 message => $msg,
393 data_blob => \%blob,
394 );
395 }
396  
397 sub _system_log_values_for_delete {
398 my $self = shift;
399  
400 my $msg
401 = 'Deleted user '
402 . $self->best_name() . ' - '
403 . ( $self->email_address() || $self->openid_uri() );
404  
405 return (
406 message => $msg,
407 data_blob => {
408 map { $_ => $self->$_() }
409 qw(
410 email_address
411 username
412 openid_uri
413 display_name
414 time_zone
415 locale_code
416 )
417 }
418 );
419 }
420  
421 sub _make_confirmation_key {
422 shift;
423  
424 return sha1_hex(
425 shift, time, $$, rand(1_000_000_000),
426 Silki::Config->instance()->secret()
427 );
428 }
429  
430 sub _password_as_rfc2307 {
431 my $self = shift;
432 my $pw = shift;
433  
434 # XXX - require a certain length or complexity? make it
435 # configurable?
436 my $pass = Authen::Passphrase::BlowfishCrypt->new(
437 cost => 8,
438 salt_random => 1,
439 passphrase => $pw,
440 );
441  
442 return $pass->as_rfc2307();
443 }
444  
445 sub _has_password_or_openid_uri {
446 my $self = shift;
447 my $p = shift;
448 my $is_insert = shift;
449  
450 my $error = { message => loc('You must provide a password or OpenID.') };
451  
452 if ($is_insert) {
453  
454 # coverage - this is never going to be true if the around modifier on
455 # insert defined above runs first, because it deletes the
456 # disable_login parameter and sets a crypted password in the params
457 # hash.
458 return if $p->{disable_login};
459  
460 return $error
461 if all { string_is_empty( $p->{$_} ) } qw( password openid_uri );
462  
463 return;
464 }
465 else {
466 my $preserve_pw = delete $p->{preserve_password};
467  
468 # The preserve_password param will be set when a user is updated via a
469 # preferences form of some sort. If they already have a valid
470 # password, an empty password field in that form does not indicate
471 # that the password in the dbms should be set to NULL.
472 if ( exists $p->{password}
473 && string_is_empty( $p->{password} )
474 && $self->has_valid_password()
475 && $preserve_pw ) {
476 delete $p->{password};
477  
478 return;
479 }
480  
481 return $error
482 if all { exists $p->{$_} && string_is_empty( $p->{$_} ) }
483 qw( password openid_uri );
484  
485 return
486 if any { !string_is_empty( $p->{$_} ) } qw( password openid_uri );
487  
488 return if none { exists $p->{$_} } qw( password openid_uri );
489  
490 if ( exists $p->{password} && string_is_empty( $p->{password} ) ) {
491 return unless string_is_empty( $self->openid_uri() );
492 }
493 elsif ( exists $p->{openid_uri}
494 && string_is_empty( $p->{openid_uri} ) ) {
495 return unless string_is_empty( $self->password() );
496 }
497  
498 return $error;
499 }
500 }
501  
502 sub _email_address_is_unique {
503 my $self = shift;
504 my $p = shift;
505 my $is_insert = shift;
506  
507 return if string_is_empty( $p->{email_address} );
508  
509 return if !$is_insert && $self->email_address() eq $p->{email_address};
510  
511 return unless __PACKAGE__->new( email_address => $p->{email_address} );
512  
513 return {
514 field => 'email_address',
515 message => loc(
516 'The email address you provided is already in use by another user.'
517 ),
518 };
519 }
520  
521 sub _normalize_and_validate_openid_uri {
522 my $self = shift;
523 my $p = shift;
524 my $is_insert = shift;
525  
526 return if string_is_empty( $p->{openid_uri} );
527  
528 my $uri = URI->new( $p->{openid_uri} );
529  
530 unless ( defined $uri->scheme()
531 && $uri->scheme() =~ /^https?/ ) {
532 return {
533 field => 'openid_uri',
534 message => loc('The OpenID you provided is not a valid URI.'),
535 };
536 }
537  
538 $p->{openid_uri} = $uri->canonical() . q{};
539  
540 return;
541 }
542  
543 sub _openid_uri_is_unique {
544 my $self = shift;
545 my $p = shift;
546 my $is_insert = shift;
547  
548 return if string_is_empty( $p->{openid_uri} );
549  
550 return
551 if !$is_insert
552 && $self->openid_uri()
553 && $self->openid_uri() eq $p->{openid_uri};
554  
555 return unless __PACKAGE__->new( openid_uri => $p->{openid_uri} );
556  
557 return {
558 field => 'openid_uri',
559 message => loc(
560 'The OpenID URI you provided is already in use by another user.'
561 ),
562 };
563 }
564  
565 sub _base_uri_path {
566 my $self = shift;
567  
568 return '/user/' . $self->user_id();
569 }
570  
571 sub domain {
572 my $self = shift;
573  
574 my $wiki = $self->member_wikis()->next();
575 $wiki ||= $self->all_wikis()->next();
576  
577 return $wiki ? $wiki->domain() : Silki::Schema::Domain->DefaultDomain();
578 }
579  
580 sub EnsureRequiredUsersExist {
581 my $class = shift;
582  
583 $class->_FindOrCreateSystemUser();
584  
585 $class->_FindOrCreateGuestUser();
586 }
587  
588 {
589 my $SystemUsername = 'system-user';
590  
591 sub _FindOrCreateSystemUser {
592 my $class = shift;
593  
594 return $class->_FindOrCreateSpecialUser($SystemUsername);
595 }
596 }
597  
598 {
599 my $GuestUsername = 'guest-user';
600  
601 sub _FindOrCreateGuestUser {
602 my $class = shift;
603  
604 return $class->_FindOrCreateSpecialUser(
605 $GuestUsername,
606 Silki::Schema::User->SystemUser(),
607 );
608 }
609  
610 sub is_guest {
611 my $self = shift;
612  
613 return $self->username() eq $GuestUsername;
614 }
615 }
616  
617 sub _FindOrCreateSpecialUser {
618 my $class = shift;
619 my $username = shift;
620 my $creator = shift;
621  
622 my $user = eval { $class->new( username => $username ) };
623  
624 return $user if $user;
625  
626 return $class->_CreateSpecialUser( $username, $creator );
627 }
628  
629 sub _CreateSpecialUser {
630 my $class = shift;
631 my $username = shift;
632 my $creator = shift;
633  
634 my $domain = Silki::Schema::Domain->DefaultDomain();
635  
636 my $email = 'silki-' . $username . q{@} . $domain->email_hostname();
637  
638 my $display_name = join ' ', map {ucfirst} split /-/, $username;
639  
640 local $Silki::Role::Schema::SystemLogger::SkipLog = 1
641 unless $creator;
642  
643 return $class->insert(
644 display_name => $display_name,
645 username => $username,
646 email_address => $email,
647 password => q{},
648 disable_login => 1,
649 is_system_user => 1,
650 user => $creator,
651 );
652 }
653  
654 sub set_time_zone_for_dt {
655 my $self = shift;
656 my $dt = shift;
657  
658 return $dt->clone()->set_time_zone( $self->time_zone() );
659 }
660  
661 sub _build_best_name {
662 my $self = shift;
663  
664 return $self->display_name() if length $self->display_name;
665  
666 my $username = $self->username();
667  
668 if ( $username =~ /\@/ ) {
669 $username =~ s/\@.+$//;
670 }
671  
672 return $username;
673 }
674  
675 sub _build_has_valid_password {
676 my $self = shift;
677  
678 return 0 if string_is_empty( $self->password() );
679 return 0 unless $self->_password_is_encrypted();
680  
681 return 1;
682 }
683  
684 sub _build_has_login_credentials {
685 my $self = shift;
686  
687 return 1 if !string_is_empty( $self->openid_uri() );
688  
689 return 1 if $self->has_valid_password();
690  
691 return 0;
692 }
693  
694 sub _password_is_encrypted {
695 my $self = shift;
696  
697 return $self->password() =~ /^{CRYPT}/ ? 1 : 0;
698 }
699  
700 sub requires_activation {
701 my $self = shift;
702  
703 return defined $self->confirmation_key() && !$self->has_valid_password();
704 }
705  
706 sub confirmation_uri {
707 my $self = shift;
708 my %p = @_;
709  
710 die loc(
711 'Cannot make a confirmation uri for a user who does not have a confirmation key.'
712 ) unless defined $self->confirmation_key();
713  
714 my $view = $p{view} || 'preferences_form';
715  
716 $p{view} = 'confirmation/' . $self->confirmation_key() . q{/} . $view;
717  
718 return $self->uri(%p);
719 }
720  
721 sub check_password {
722 my $self = shift;
723 my $pw = shift;
724  
725 return if $self->is_system_user();
726  
727 return unless $self->has_valid_password();
728  
729 my $pass = Authen::Passphrase::BlowfishCrypt->from_rfc2307(
730 $self->password() );
731  
732 return $pass->match($pw);
733 }
734  
735 sub is_authenticated {
736 my $self = shift;
737  
738 return !$self->is_system_user();
739 }
740  
741 sub can_edit_user {
742 my $self = shift;
743 my $user = shift;
744  
745 return 0 if $user->is_system_user();
746  
747 return 1 if $self->is_admin();
748  
749 return 1 if $self->user_id() == $user->user_id();
750  
751 return 0;
752 }
753  
754 sub has_permission_in_wiki {
755 my $self = shift;
756  
757 return 1 if $self->is_admin();
758  
759 my ( $wiki, $perm ) = validated_list(
760 \@_,
761 wiki => { isa => 'Silki::Schema::Wiki' },
762 permission => { isa => 'Silki::Schema::Permission' },
763 );
764  
765 my $perms = $wiki->permissions();
766  
767 my $role = $self->role_in_wiki($wiki);
768  
769 return $perms->{ $role->name() }{ $perm->name() };
770 }
771  
772 sub is_wiki_member {
773 my $self = shift;
774 my ($wiki) = pos_validated_list( \@_, { isa => 'Silki::Schema::Wiki' } );
775  
776 my $role_name = $self->_role_name_in_wiki($wiki);
777  
778 return defined $role_name;
779 }
780  
781 sub role_in_wiki {
782 my $self = shift;
783 my ($wiki) = pos_validated_list( \@_, { isa => 'Silki::Schema::Wiki' } );
784  
785 return Silki::Schema::Role->Guest() if $self->is_guest();
786  
787 my $role_name = $self->_role_name_in_wiki($wiki);
788  
789 $role_name ||= 'Authenticated';
790  
791 return Silki::Schema::Role->$role_name();
792 }
793  
794 sub _role_name_in_wiki {
795 my $self = shift;
796 my $wiki = shift;
797  
798 my $select = $self->_RoleInWikiSelect();
799  
800 my $dbh = Silki::Schema->DBIManager()->source_for_sql($select)->dbh();
801  
802 my $row = $dbh->selectrow_arrayref(
803 $select->sql($dbh),
804 {},
805 $wiki->wiki_id(),
806 $self->user_id(),
807 );
808  
809 return unless $row;
810  
811 return $row->[0];
812 }
813  
814 sub _BuildRoleInWikiSelect {
815 my $select = Silki::Schema->SQLFactoryClass()->new_select();
816  
817 #<<<
818 $select
819 ->select( $Schema->table('Role')->column('name') )
820 ->from( $Schema->table('Role'), $Schema->table('UserWikiRole') )
821 ->where( $Schema->table('UserWikiRole')->column('wiki_id'),
822 '=', Fey::Placeholder->new() )
823 ->and( $Schema->table('UserWikiRole')->column('user_id'),
824 '=', Fey::Placeholder->new() );
825 #>>>
826 return $select;
827 }
828  
829 sub _BuildMemberWikiCountSelect {
830 my $class = shift;
831  
832 my $select = Silki::Schema->SQLFactoryClass()->new_select();
833  
834 my $distinct = Fey::Literal::Term->new(
835 'DISTINCT ',
836 $Schema->table('Wiki')->column('wiki_id')
837 );
838 my $count = Fey::Literal::Function->new( 'COUNT', $distinct );
839  
840 $select->select($count);
841 $class->_MemberWikiSelectBase($select);
842  
843 return $select;
844 }
845  
846 sub _BuildMemberWikiSelect {
847 my $class = shift;
848  
849 my $select = Silki::Schema->SQLFactoryClass()->new_select();
850  
851 $select->select( $Schema->table('Wiki') );
852 $class->_MemberWikiSelectBase($select);
853 $select->order_by( $Schema->table('Wiki')->column('title') );
854  
855 return $select;
856 }
857  
858 sub _MemberWikiSelectBase {
859 my $class = shift;
860 my $select = shift;
861  
862 my $guest = Silki::Schema::Role->Guest();
863 my $authed = Silki::Schema::Role->Authenticated();
864 my $read = Silki::Schema::Permission->Read();
865  
866 #<<<
867 $select
868 ->from( $Schema->table('Wiki'), $Schema->table('UserWikiRole') )
869 ->where( $Schema->table('UserWikiRole')->column('user_id'),
870 '=', Fey::Placeholder->new() );
871 #>>>
872 return;
873 }
874  
875 sub wikis_shared_with {
876 my $self = shift;
877 my $user = shift;
878  
879 my $select = $self->_SharedWikiSelect();
880  
881 return Fey::Object::Iterator::FromSelect->new(
882 classes => ['Silki::Schema::Wiki'],
883 select => $select,
884 dbh => Silki::Schema->DBIManager()->source_for_sql($select)->dbh(),
885 bind_params => [
886 $self->user_id(), $user->user_id(),
887 $self->user_id(), $self->user_id(),
888 $user->user_id(), $user->user_id(),
889 ],
890 );
891 }
892  
893 sub _BuildSharedWikiSelect {
894 my $class = shift;
895  
896 my $explicit_wiki_select = Silki::Schema->SQLFactoryClass()->new_select();
897  
898 $explicit_wiki_select->select( $Schema->table('Wiki') );
899 $class->_ExplicitWikiSelectBase($explicit_wiki_select);
900  
901 my $implicit_wiki_select = Silki::Schema->SQLFactoryClass()->new_select();
902  
903 $implicit_wiki_select->select( $Schema->table('Wiki') );
904 $class->_ImplicitWikiSelectBase($implicit_wiki_select);
905  
906 my $intersect1 = Silki::Schema->SQLFactoryClass()->new_intersect;
907 $intersect1->intersect( $explicit_wiki_select, $explicit_wiki_select );
908  
909 my $intersect2 = Silki::Schema->SQLFactoryClass()->new_intersect;
910 $intersect2->intersect( $implicit_wiki_select, $implicit_wiki_select );
911  
912 my $union = Silki::Schema->SQLFactoryClass()->new_union;
913  
914 # To use an ORDER BY with a UNION in Pg, you specify the column as a
915 # number (ORDER BY 5).
916 my $title_idx = first_index { $_->name() eq 'title' }
917 $Schema->table('Wiki')->columns();
918 #<<<
919 $union
920 ->union( $intersect1, $intersect2 )
921 ->order_by( Fey::Literal::Term->new($title_idx) );
922 #>>>
923 return $union;
924 }
925  
926 sub _BuildAllWikiCountSelect {
927 my $class = shift;
928  
929 my $explicit_wiki_select = Silki::Schema->SQLFactoryClass()->new_select();
930  
931 $explicit_wiki_select->select(
932 $Schema->table('Wiki')->column('wiki_id') );
933 $class->_ExplicitWikiSelectBase($explicit_wiki_select);
934  
935 my $implicit_wiki_select = Silki::Schema->SQLFactoryClass()->new_select();
936  
937 $implicit_wiki_select->select(
938 $Schema->table('Wiki')->column('wiki_id') );
939 $class->_ImplicitWikiSelectBase($implicit_wiki_select);
940  
941 my $select = Silki::Schema->SQLFactoryClass()->new_select();
942  
943 my $distinct = Fey::Literal::Term->new(
944 'DISTINCT ',
945 $Schema->table('Wiki')->column('wiki_id')
946 );
947 my $count = Fey::Literal::Function->new( 'COUNT', $distinct );
948  
949 #<<<
950 $select
951 ->select($count)->from( $Schema->table('Wiki') )
952 ->where( $Schema->table('Wiki')->column('wiki_id'),
953 'IN', $explicit_wiki_select )
954 ->where('or')
955 ->where( $Schema->table('Wiki')->column('wiki_id'),
956 'IN', $implicit_wiki_select );
957 #>>>
958 return $select;
959 }
960  
961 sub _BuildAllWikiSelect {
962 my $class = shift;
963  
964 my $explicit_wiki_select = Silki::Schema->SQLFactoryClass()->new_select();
965  
966 my $is_explicit1 = Fey::Literal::Term->new('1 AS is_explicit');
967 $is_explicit1->set_can_have_alias(0);
968 $explicit_wiki_select->select( $Schema->table('Wiki'), $is_explicit1 );
969 $class->_ExplicitWikiSelectBase($explicit_wiki_select);
970  
971 my $implicit_wiki_select = Silki::Schema->SQLFactoryClass()->new_select();
972  
973 my $is_explicit0 = Fey::Literal::Term->new('0 AS is_explicit');
974 $is_explicit0->set_can_have_alias(0);
975 $implicit_wiki_select->select( $Schema->table('Wiki'), $is_explicit0 );
976 $class->_ImplicitWikiSelectBase($implicit_wiki_select);
977  
978 my $union = Silki::Schema->SQLFactoryClass()->new_union;
979  
980 # To use an ORDER BY with a UNION in Pg, you specify the column as a
981 # number (ORDER BY 5).
982 my $is_explicit_idx = ( scalar $Schema->table('Wiki')->columns() ) + 1;
983  
984 my $title_idx = first_index { $_->name() eq 'title' }
985 $Schema->table('Wiki')->columns();
986  
987 $union->union( $explicit_wiki_select, $implicit_wiki_select )->order_by(
988 Fey::Literal::Term->new($is_explicit_idx),
989 'DESC',
990 Fey::Literal::Term->new($title_idx),
991 'ASC',
992 );
993  
994 return $union;
995 }
996  
997 sub _ExplicitWikiSelectBase {
998 my $class = shift;
999 my $select = shift;
1000  
1001 $select->from( $Schema->tables( 'Wiki', 'UserWikiRole' ) )->where(
1002 $Schema->table('UserWikiRole')->column('user_id'),
1003 '=', Fey::Placeholder->new()
1004 );
1005  
1006 return;
1007 }
1008  
1009 sub _ImplicitWikiSelectBase {
1010 my $class = shift;
1011 my $select = shift;
1012  
1013 my $explicit = Silki::Schema->SQLFactoryClass()->new_select();
1014 $explicit->select( $Schema->table('Wiki')->column('wiki_id') );
1015 $class->_ExplicitWikiSelectBase($explicit);
1016 #<<<
1017 $select
1018 ->from( $Schema->tables( 'Wiki', 'Page' ) )
1019 ->from( $Schema->tables( 'Page', 'PageRevision' ) )
1020 ->where( $Schema->table('PageRevision')->column('user_id'),
1021 '=', Fey::Placeholder->new() )
1022 ->and( $Schema->table('Wiki')->column('wiki_id'),
1023 'NOT IN', $explicit );
1024 #>>>
1025 return;
1026 }
1027  
1028 sub recently_viewed_pages {
1029 my $self = shift;
1030 my ( $limit, $offset ) = validated_list(
1031 \@_,
1032 limit => { isa => Int, optional => 1 },
1033 offset => { isa => Int, default => 0 },
1034 );
1035  
1036 my $select = $self->_RecentlyViewedPagesSelect()->clone();
1037 $select->limit( $limit, $offset );
1038  
1039 return Fey::Object::Iterator::FromSelect->new(
1040 classes => ['Silki::Schema::Page'],
1041 select => $select,
1042 dbh => Silki::Schema->DBIManager()->source_for_sql($select)->dbh(),
1043 bind_params => [ $self->user_id() ],
1044 );
1045 }
1046  
1047 sub _BuildRecentlyViewedPagesSelect {
1048 my $class = shift;
1049  
1050 my ( $page_t, $page_view_t ) = $Schema->tables( 'Page', 'PageView' );
1051  
1052 my $max_func = Fey::Literal::Function->new(
1053 'MAX',
1054 $page_view_t->column('view_datetime')
1055 );
1056  
1057 my $max_datetime = Silki::Schema->SQLFactoryClass()->new_select();
1058  
1059 #<<<
1060 $max_datetime
1061 ->select($max_func)
1062 ->from( $page_view_t )
1063 ->where( $page_view_t->column('page_id'),
1064 '=', $page_t->column('page_id') );
1065 #>>>
1066 my $viewed_select = Silki::Schema->SQLFactoryClass()->new_select();
1067 #<<<
1068 $viewed_select
1069 ->select($page_t)
1070 ->from( $page_t, $page_view_t )
1071 ->where( $page_view_t->column('user_id'), '=', Fey::Placeholder->new() )
1072 ->and ( $page_view_t->column('view_datetime') , '=', $max_datetime )
1073 ->order_by(
1074 $page_view_t->column('view_datetime'), 'DESC',
1075 $page_t->column('title'), 'ASC',
1076 );
1077 #>>>
1078 return $viewed_select;
1079 }
1080  
1081 sub send_invitation_email {
1082 my $self = shift;
1083  
1084 $self->_send_email( @_, template => 'invitation' );
1085 }
1086  
1087 sub send_activation_email {
1088 my $self = shift;
1089  
1090 $self->_send_email( @_, template => 'activation' );
1091 }
1092  
1093 sub forgot_password {
1094 my $self = shift;
1095  
1096 $self->update(
1097 confirmation_key =>
1098 $self->_make_confirmation_key( $self->email_address() ),
1099 user => Silki::Schema::User->SystemUser(),
1100 );
1101  
1102 $self->_send_email(
1103 @_,
1104 sender => $self,
1105 subject => loc('Password reset for Silki'),
1106 template => 'forgot-password',
1107 );
1108 }
1109  
1110 sub _send_email {
1111 my $self = shift;
1112 my ( $wiki, $sender, $domain, $message, $subject, $template )
1113 = validated_list(
1114 \@_,
1115 wiki => { isa => 'Silki::Schema::Wiki', optional => 1 },
1116 sender => { isa => 'Silki::Schema::User' },
1117 domain => {
1118 isa => 'Silki::Schema::Domain',
1119 default => Silki::Schema::Domain->DefaultDomain()
1120 },
1121 message => { isa => Str, optional => 1 },
1122 subject => { isa => Str, optional => 1 },
1123 template => { isa => Str },
1124 );
1125  
1126 die "Cannot send an invitation email without a wiki."
1127 if $template eq 'invitation' && !$wiki;
1128  
1129 $subject ||=
1130 $wiki
1131 ? loc(
1132 'You have been invited to join the %1 wiki at %2',
1133 $wiki->title(),
1134 $wiki->domain()->web_hostname(),
1135 )
1136 : loc(
1137 'Activate your user account on the %1 server',
1138 $self->domain()->web_hostname()
1139 );
1140  
1141 my $from = Email::Address->new(
1142 $sender->best_name(),
1143 $sender->email_address()
1144 )->format();
1145  
1146 my $to = Email::Address->new(
1147 $self->best_name(),
1148 $self->email_address(),
1149 )->format();
1150  
1151 send_email(
1152 from => $from,
1153 subject => $subject,
1154 to => $to,
1155 template => $template,
1156 template_params => {
1157 user => $self,
1158 wiki => $wiki,
1159 domain => $domain,
1160 sender => $sender,
1161 message => $message,
1162 },
1163 );
1164  
1165 return;
1166 }
1167  
1168 sub ActiveUsers {
1169 my $class = shift;
1170 my ( $limit, $offset ) = validated_list(
1171 \@_,
1172 limit => { isa => Int, optional => 1 },
1173 offset => { isa => Int, default => 0 },
1174 );
1175  
1176 my $select = $class->_ActiveUsersSelect()->clone();
1177 $select->limit( $limit, $offset );
1178  
1179 my $dbh = Silki::Schema->DBIManager()->source_for_sql($select)->dbh();
1180  
1181 return Fey::Object::Iterator::FromSelect->new(
1182 classes => 'Silki::Schema::User',
1183 select => $select,
1184 dbh => $dbh,
1185 bind_params => [ $select->bind_params() ],
1186 );
1187 }
1188  
1189 sub _BuildActiveUsersSelect {
1190 my $class = shift;
1191  
1192 my $select = $class->_AllUsersSelect()->clone();
1193  
1194 my $user_t = $Schema->table('User');
1195  
1196 $select->where( $user_t->column('is_disabled'), '=', 0 );
1197  
1198 return $select;
1199 }
1200  
1201 sub ActiveUserCount {
1202 my $class = shift;
1203  
1204 my $select = $class->_ActiveUserCountSelect();
1205  
1206 my $dbh = Silki::Schema->DBIManager()->source_for_sql($select)->dbh();
1207  
1208 my $vals = $dbh->selectrow_arrayref(
1209 $select->sql($dbh), {},
1210 $select->bind_params()
1211 );
1212  
1213 return $vals ? $vals->[0] : 0;
1214 }
1215  
1216 sub _BuildActiveUserCountSelect {
1217 my $class = shift;
1218  
1219 my $select = Silki::Schema->SQLFactoryClass()->new_select();
1220  
1221 my $user_t = $Schema->table('User');
1222  
1223 my $count
1224 = Fey::Literal::Function->new( 'COUNT', $user_t->column('user_id') );
1225  
1226 #<<<
1227 $select
1228 ->select($count)
1229 ->from($user_t)
1230 ->where( $user_t->column('is_disabled'), '=', 0 );
1231 #>>>
1232 return $select;
1233 }
1234  
1235 sub All {
1236 my $class = shift;
1237 my ( $limit, $offset ) = validated_list(
1238 \@_,
1239 limit => { isa => Int, optional => 1 },
1240 offset => { isa => Int, default => 0 },
1241 );
1242  
1243 my $select = $class->_AllUsersSelect()->clone();
1244 $select->limit( $limit, $offset );
1245  
1246 my $dbh = Silki::Schema->DBIManager()->source_for_sql($select)->dbh();
1247  
1248 return Fey::Object::Iterator::FromSelect->new(
1249 classes => 'Silki::Schema::User',
1250 select => $select,
1251 dbh => $dbh,
1252 bind_params => [ $select->bind_params() ],
1253 );
1254 }
1255  
1256 sub _BuildAllUsersSelect {
1257 my $class = shift;
1258  
1259 my $select = Silki::Schema->SQLFactoryClass()->new_select();
1260  
1261 my $user_t = $Schema->table('User');
1262  
1263 my $order_by = Fey::Literal::Term->new(
1264 q{CASE WHEN display_name = '' THEN username ELSE display_name END});
1265  
1266 #<<<
1267 $select
1268 ->select($user_t)
1269 ->from($user_t)
1270 ->order_by($order_by);
1271 #>>>
1272 return $select;
1273 }
1274  
1275 sub _all_revisions_for_delete {
1276 my $self = shift;
1277  
1278 my $select = $self->_AllRevisionsForDeleteSelect();
1279  
1280 my $dbh = Silki::Schema->DBIManager()->source_for_sql($select)->dbh();
1281  
1282 return Fey::Object::Iterator::FromSelect->new(
1283 classes => 'Silki::Schema::PageRevision',
1284 select => $select,
1285 dbh => $dbh,
1286 bind_params => [ $self->user_id() ],
1287 );
1288 }
1289  
1290 sub _BuildAllRevisionsForDeleteSelect {
1291 my $class = shift;
1292  
1293 my $select = Silki::Schema->SQLFactoryClass()->new_select();
1294  
1295 my $rev_t = $Schema->table('PageRevision');
1296  
1297 #<<<
1298 $select
1299 ->select($rev_t)
1300 ->from($rev_t)
1301 ->where( $rev_t->column('user_id'), '=', Fey::Placeholder->new() )
1302 ->order_by( $rev_t->column('page_id'), 'ASC',
1303 $rev_t->column('revision_number'), 'DESC',
1304 );
1305 #>>>
1306 return $select;
1307 }
1308  
1309 sub _BuildAllPageCountSelect {
1310 my $class = shift;
1311  
1312 my $select = Silki::Schema->SQLFactoryClass()->new_select();
1313  
1314 my $page_t = $Schema->table('Page');
1315  
1316 my $count
1317 = Fey::Literal::Function->new( 'COUNT', $page_t->column('page_id') );
1318  
1319 #<<<
1320 $select
1321 ->select($count)
1322 ->from($page_t)
1323 ->where( $page_t->column('user_id'), '=', Fey::Placeholder->new() );
1324 #>>>
1325 return $select;
1326 }
1327  
1328 sub _BuildAllRevisionCountSelect {
1329 my $class = shift;
1330  
1331 my $select = Silki::Schema->SQLFactoryClass()->new_select();
1332  
1333 my $rev_t = $Schema->table('PageRevision');
1334  
1335 my $count
1336 = Fey::Literal::Function->new( 'COUNT', $rev_t->column('page_id') );
1337  
1338 #<<<
1339 $select
1340 ->select($count)
1341 ->from($rev_t)
1342 ->where( $rev_t->column('user_id'), '=', Fey::Placeholder->new() );
1343 #>>>
1344 return $select;
1345 }
1346  
1347 sub _BuildAllFileCountSelect {
1348 my $class = shift;
1349  
1350 my $select = Silki::Schema->SQLFactoryClass()->new_select();
1351  
1352 my $file_t = $Schema->table('File');
1353  
1354 my $count
1355 = Fey::Literal::Function->new( 'COUNT', $file_t->column('file_id') );
1356  
1357 #<<<
1358 $select
1359 ->select($count)
1360 ->from($file_t)
1361 ->where( $file_t->column('user_id'), '=', Fey::Placeholder->new() );
1362 #>>>
1363 return $select;
1364 }
1365  
1366 __PACKAGE__->meta()->make_immutable();
1367  
1368 1;
1369  
1370 # ABSTRACT: Represents a user
1371  
1372 __END__
« Silki/Schema/PageRevision.pm ↑ Summary Silki/I18N.pm »