Bug 32426: Changes for opac-registration-verify
[koha.git] / opac / opac-registration-verify.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use CGI qw ( -utf8 );
21 use Try::Tiny;
22
23 use C4::Auth qw( get_template_and_user );
24 use C4::Output qw( output_html_with_http_headers );
25 use C4::Letters qw( GetPreparedLetter EnqueueLetter SendQueuedMessages );
26 use C4::Members;
27 use C4::Form::MessagingPreferences;
28 use Koha::AuthUtils;
29 use Koha::Patrons;
30 use Koha::Patron::Consent;
31 use Koha::Patron::Modifications;
32 use Koha::Patron::Categories;
33
34 my $cgi = CGI->new;
35 my $dbh = C4::Context->dbh;
36
37 unless ( C4::Context->preference('PatronSelfRegistration') ) {
38     print $cgi->redirect("/cgi-bin/koha/opac-main.pl");
39     exit;
40 }
41
42 my $token = $cgi->param('token');
43 my $m = Koha::Patron::Modifications->find( { verification_token => $token } );
44
45 my ( $template, $borrowernumber, $cookie );
46 my ( $error_type, $error_info );
47
48 if (
49     $m # The token exists and the email is unique if requested
50     and not(
51             C4::Context->preference('PatronSelfRegistrationEmailMustBeUnique')
52         and Koha::Patrons->search( { email => $m->email } )->count
53     )
54   )
55 {
56     my $patron_attrs = $m->unblessed;
57     $patron_attrs->{password} ||= Koha::AuthUtils::generate_password(Koha::Patron::Categories->find($patron_attrs->{categorycode}));
58     my $consent_dt = delete $patron_attrs->{gdpr_proc_consent};
59     $patron_attrs->{categorycode} ||= C4::Context->preference('PatronSelfRegistrationDefaultCategory');
60     delete $patron_attrs->{timestamp};
61     delete $patron_attrs->{verification_token};
62     delete $patron_attrs->{changed_fields};
63     delete $patron_attrs->{extended_attributes};
64
65     my $patron;
66     try {
67         $patron = Koha::Patron->new( $patron_attrs )->store;
68         Koha::Patron::Consent->new({ borrowernumber => $patron->borrowernumber, type => 'GDPR_PROCESSING', given_on => $consent_dt })->store if $patron && $consent_dt;
69     } catch {
70         $error_type = ref($_);
71         $error_info = "$_";
72     };
73
74     if ($patron) {
75         if( $m->extended_attributes ){
76             $m->borrowernumber( $patron->borrowernumber);
77             $m->changed_fields(['extended_attributes']);
78             $m->approve();
79         } else {
80             $m->delete();
81         }
82         ( $template, $borrowernumber, $cookie ) = get_template_and_user(
83             {
84                 template_name   => "opac-registration-confirmation.tt",
85                 type            => "opac",
86                 query           => $cgi,
87                 authnotrequired => 1,
88             }
89         );
90         C4::Form::MessagingPreferences::handle_form_action($cgi, { borrowernumber => $patron->borrowernumber }, $template, 1, C4::Context->preference('PatronSelfRegistrationDefaultCategory') ) if C4::Context->preference('EnhancedMessagingPreferences');
91
92         $template->param( password_cleartext => $patron->plain_text_password );
93         $template->param( borrower => $patron );
94
95         # If 'AutoEmailNewUser' syspref is on, email user their account details from the 'notice' that matches the user's branchcode.
96         if ( C4::Context->preference("AutoEmailNewUser") ) {
97             # Look up correct email address taking AutoEmailPrimaryAddress into account
98             my $emailaddr = $patron->notice_email_address;
99             # if we manage to find a valid email address, send notice
100             if ($emailaddr) {
101                 eval {
102                     my $letter = GetPreparedLetter(
103                         module      => 'members',
104                         letter_code => 'WELCOME',
105                         branchcode  => $patron->branchcode,,
106                         lang        => $patron->lang || 'default',
107                         tables      => {
108                             'branches'  => $patron->branchcode,
109                             'borrowers' => $patron->borrowernumber,
110                         },
111                         want_librarian => 1,
112                     ) or return;
113
114                     my $message_id = EnqueueLetter(
115                         {
116                             letter                 => $letter,
117                             borrowernumber         => $patron->id,
118                             to_address             => $emailaddr,
119                             message_transport_type => 'email'
120                         }
121                     );
122                     SendQueuedMessages({ message_id => $message_id });
123                 };
124             }
125         }
126
127         # Notify library of new patron registration
128         my $notify_library = C4::Context->preference("EmailPatronRegistrations");
129         if ($notify_library) {
130             $patron->notify_library_of_registration($notify_library);
131         }
132
133         $template->param(
134             PatronSelfRegistrationAdditionalInstructions =>
135               C4::Context->preference(
136                 'PatronSelfRegistrationAdditionalInstructions')
137         );
138
139         my ($theme, $news_lang, $availablethemes) = C4::Templates::themelanguage(C4::Context->config('opachtdocs'),'opac-registration-confirmation.tt','opac',$cgi);
140         $template->param( news_lang => $news_lang );
141     }
142 }
143
144 if( !$template ) { # Missing token, patron exception, etc.
145     ( $template, $borrowernumber, $cookie ) = get_template_and_user({
146         template_name   => "opac-registration-invalid.tt",
147         type            => "opac",
148         query           => $cgi,
149         authnotrequired => 1,
150     });
151     $template->param( error_type => $error_type, error_info => $error_info );
152 }
153
154 output_html_with_http_headers $cgi, $cookie, $template->output;