Bug 33568: Restore host records
[koha.git] / members / two_factor_auth.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
22 use C4::Auth qw( get_template_and_user );
23 use C4::Output qw( output_and_exit output_html_with_http_headers );
24
25 use Koha::Patrons;
26 use Koha::Auth::TwoFactorAuth;
27
28 my $cgi = CGI->new;
29
30 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
31     {
32         template_name => 'members/two_factor_auth.tt',
33         query         => $cgi,
34         type          => 'intranet',
35         flagsrequired => { catalogue => 1 },
36     }
37 );
38
39 my $TwoFactorAuthentication = C4::Context->preference('TwoFactorAuthentication');
40 if ( $TwoFactorAuthentication ne 'enabled' && $TwoFactorAuthentication ne 'enforced' ) {
41     print $cgi->redirect("/cgi-bin/koha/errors/404.pl");
42     exit;
43 }
44
45 my $logged_in_user = Koha::Patrons->find($loggedinuser);
46 my $op             = $cgi->param('op') // '';
47
48 if ( !C4::Context->config('encryption_key') ) {
49     $template->param( missing_key => 1 );
50 }
51 else {
52
53     my $csrf_pars = {
54         session_id => scalar $cgi->cookie('CGISESSID'),
55         token      => scalar $cgi->param('csrf_token'),
56     };
57
58     if ( $op eq 'cud-disable-2FA' ) {
59         my $auth =
60           Koha::Auth::TwoFactorAuth->new( { patron => $logged_in_user } );
61         $logged_in_user->secret(undef);
62         $logged_in_user->auth_method('password')->store;
63         if ( $logged_in_user->notice_email_address ) {
64             $logged_in_user->queue_notice(
65                 {
66                     letter_params => {
67                         module      => 'members',
68                         letter_code => '2FA_DISABLE',
69                         branchcode  => $logged_in_user->branchcode,
70                         lang        => $logged_in_user->lang,
71                         tables      => {
72                             branches  => $logged_in_user->branchcode,
73                             borrowers => $logged_in_user->id
74                         },
75                     },
76                     message_transports => ['email'],
77                 }
78             );
79         }
80     }
81 }
82
83 $template->param(
84     patron => $logged_in_user,
85     op     => $op,
86 );
87
88 output_html_with_http_headers $cgi, $cookie, $template->output;