Bug 32336: (QA follow-up) Use $metadata->schema
[srvgit] / 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::Context;
25 use C4::Output qw( output_html_with_http_headers );
26 use C4::Letters qw( GetPreparedLetter EnqueueLetter SendQueuedMessages );
27 use C4::Members;
28 use C4::Form::MessagingPreferences;
29 use Koha::AuthUtils;
30 use Koha::Patrons;
31 use Koha::Patron::Consent;
32 use Koha::Patron::Modifications;
33 use Koha::Patron::Categories;
34
35 my $cgi = CGI->new;
36 my $dbh = C4::Context->dbh;
37
38 unless ( C4::Context->preference('PatronSelfRegistration') ) {
39     print $cgi->redirect("/cgi-bin/koha/opac-main.pl");
40     exit;
41 }
42
43 my $token = $cgi->param('token');
44 my $m = Koha::Patron::Modifications->find( { verification_token => $token } );
45
46 my ( $template, $borrowernumber, $cookie );
47 my ( $error_type, $error_info );
48
49 if (
50     $m # The token exists and the email is unique if requested
51     and not(
52             C4::Context->preference('PatronSelfRegistrationEmailMustBeUnique')
53         and Koha::Patrons->search( { email => $m->email } )->count
54     )
55   )
56 {
57     my $patron_attrs = $m->unblessed;
58     $patron_attrs->{password} ||= Koha::AuthUtils::generate_password(Koha::Patron::Categories->find($patron_attrs->{categorycode}));
59     my $consent_dt = delete $patron_attrs->{gdpr_proc_consent};
60     $patron_attrs->{categorycode} ||= C4::Context->preference('PatronSelfRegistrationDefaultCategory');
61     delete $patron_attrs->{timestamp};
62     delete $patron_attrs->{verification_token};
63     delete $patron_attrs->{changed_fields};
64     delete $patron_attrs->{extended_attributes};
65
66     my $patron;
67     try {
68         $patron = Koha::Patron->new( $patron_attrs )->store;
69         Koha::Patron::Consent->new({ borrowernumber => $patron->borrowernumber, type => 'GDPR_PROCESSING', given_on => $consent_dt })->store if $patron && $consent_dt;
70     } catch {
71         $error_type = ref($_);
72         $error_info = "$_";
73     };
74
75     if ($patron) {
76         if( $m->extended_attributes ){
77             $m->borrowernumber( $patron->borrowernumber);
78             $m->changed_fields(['extended_attributes']);
79             $m->approve();
80         } else {
81             $m->delete();
82         }
83         ( $template, $borrowernumber, $cookie ) = get_template_and_user(
84             {
85                 template_name   => "opac-registration-confirmation.tt",
86                 type            => "opac",
87                 query           => $cgi,
88                 authnotrequired => C4::Context->preference("OpacPublic") ? 1 : 0,
89             }
90         );
91         C4::Form::MessagingPreferences::handle_form_action($cgi, { borrowernumber => $patron->borrowernumber }, $template, 1, C4::Context->preference('PatronSelfRegistrationDefaultCategory') ) if C4::Context->preference('EnhancedMessagingPreferences');
92
93         $template->param( password_cleartext => $patron->plain_text_password );
94         $template->param( borrower => $patron );
95
96         # If 'AutoEmailNewUser' syspref is on, email user their account details from the 'notice' that matches the user's branchcode.
97         if ( C4::Context->preference("AutoEmailNewUser") ) {
98             # Look up correct email address taking AutoEmailPrimaryAddress into account
99             my $emailaddr = $patron->notice_email_address;
100             # if we manage to find a valid email address, send notice
101             if ($emailaddr) {
102                 eval {
103                     my $letter = GetPreparedLetter(
104                         module      => 'members',
105                         letter_code => 'WELCOME',
106                         branchcode  => $patron->branchcode,,
107                         lang        => $patron->lang || 'default',
108                         tables      => {
109                             'branches'  => $patron->branchcode,
110                             'borrowers' => $patron->borrowernumber,
111                         },
112                         want_librarian => 1,
113                     ) or return;
114
115                     my $message_id = EnqueueLetter(
116                         {
117                             letter                 => $letter,
118                             borrowernumber         => $patron->id,
119                             to_address             => $emailaddr,
120                             message_transport_type => 'email'
121                         }
122                     );
123                     SendQueuedMessages({ message_id => $message_id });
124                 };
125             }
126         }
127
128         # Notify library of new patron registration
129         my $notify_library = C4::Context->preference("EmailPatronRegistrations");
130         if ($notify_library) {
131             $patron->notify_library_of_registration($notify_library);
132         }
133
134         $template->param(
135             PatronSelfRegistrationAdditionalInstructions =>
136               C4::Context->preference(
137                 'PatronSelfRegistrationAdditionalInstructions')
138         );
139
140         my ($theme, $news_lang, $availablethemes) = C4::Templates::themelanguage(C4::Context->config('opachtdocs'),'opac-registration-confirmation.tt','opac',$cgi);
141         $template->param( news_lang => $news_lang );
142     }
143 }
144
145 if( !$template ) { # Missing token, patron exception, etc.
146     ( $template, $borrowernumber, $cookie ) = get_template_and_user({
147         template_name   => "opac-registration-invalid.tt",
148         type            => "opac",
149         query           => $cgi,
150         authnotrequired => C4::Context->preference("OpacPublic") ? 1 : 0,
151     });
152     $template->param( error_type => $error_type, error_info => $error_info );
153 }
154
155 output_html_with_http_headers $cgi, $cookie, $template->output;