Line 0
Link Here
|
0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
3 |
# This file is part of Koha. |
4 |
# |
5 |
# Copyright 2025 Koha Development Team |
6 |
# |
7 |
# Koha is free software; you can redistribute it and/or modify it |
8 |
# under the terms of the GNU General Public License as published by |
9 |
# the Free Software Foundation; either version 3 of the License, or |
10 |
# (at your option) any later version. |
11 |
# |
12 |
# Koha is distributed in the hope that it will be useful, but |
13 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
# GNU General Public License for more details. |
16 |
# |
17 |
# You should have received a copy of the GNU General Public License |
18 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
19 |
|
20 |
use Modern::Perl; |
21 |
|
22 |
use Getopt::Long qw( GetOptions ); |
23 |
use Pod::Usage qw( pod2usage ); |
24 |
|
25 |
use Koha::Patrons; |
26 |
use Koha::Script; |
27 |
|
28 |
my ( $help, $cardnumber, $patron_id, $userid ); |
29 |
GetOptions( |
30 |
'help|?' => \$help, |
31 |
'userid=s' => \$userid, |
32 |
'patron_id=s' => \$patron_id, |
33 |
'cardnumber=s' => \$cardnumber, |
34 |
); |
35 |
|
36 |
pod2usage(1) if $help; |
37 |
|
38 |
# Validate that exactly one parameter is provided (mutually exclusive) |
39 |
my $param_count = grep { defined } ( $userid, $patron_id, $cardnumber ); |
40 |
|
41 |
if ( $param_count == 0 ) { |
42 |
pod2usage("Error: One of --userid, --patron_id, or --cardnumber is required"); |
43 |
} elsif ( $param_count > 1 ) { |
44 |
pod2usage("Error: Only one of --userid, --patron_id, or --cardnumber can be specified"); |
45 |
} |
46 |
|
47 |
# Build search filter based on the provided parameter |
48 |
my $filter = {}; |
49 |
my $search_type; |
50 |
|
51 |
if ($userid) { |
52 |
$filter->{userid} = $userid; |
53 |
$search_type = "userid '$userid'"; |
54 |
} elsif ($cardnumber) { |
55 |
$filter->{cardnumber} = $cardnumber; |
56 |
$search_type = "cardnumber '$cardnumber'"; |
57 |
} elsif ($patron_id) { |
58 |
$filter->{borrowernumber} = $patron_id; |
59 |
$search_type = "patron_id '$patron_id'"; |
60 |
} |
61 |
|
62 |
# Find the patron |
63 |
my $patrons = Koha::Patrons->search($filter); |
64 |
|
65 |
unless ( $patrons->count > 0 ) { |
66 |
say "Error: No patron found with $search_type"; |
67 |
exit 1; |
68 |
} |
69 |
|
70 |
my $patron = $patrons->next; |
71 |
|
72 |
# Check if patron actually has 2FA enabled |
73 |
unless ( $patron->has_2fa_enabled ) { |
74 |
say "Patron " |
75 |
. $patron->cardnumber . " (" |
76 |
. $patron->firstname . " " |
77 |
. $patron->surname |
78 |
. ") does not have 2FA enabled."; |
79 |
exit 0; |
80 |
} |
81 |
|
82 |
# Display patron information and current status |
83 |
say "Resetting 2FA for patron:"; |
84 |
say " Card number: " . $patron->cardnumber; |
85 |
say " Name: " . $patron->firstname . " " . $patron->surname; |
86 |
say " Current auth method: " . ( $patron->auth_method || 'password' ); |
87 |
|
88 |
# Reset 2FA settings using the new method |
89 |
eval { $patron->reset_2fa; }; |
90 |
|
91 |
if ($@) { |
92 |
say "Error: Failed to reset 2FA settings: $@"; |
93 |
exit 1; |
94 |
} |
95 |
|
96 |
say "Success: 2FA has been reset for patron " . $patron->cardnumber; |
97 |
|
98 |
=head1 NAME |
99 |
|
100 |
reset_2fa.pl - Reset the 2FA setting for the Koha user |
101 |
|
102 |
=head1 SYNOPSIS |
103 |
|
104 |
reset_2fa.pl --userid <userid> |
105 |
reset_2fa.pl --patron_id <patron_id> |
106 |
reset_2fa.pl --cardnumber <cardnumber> |
107 |
|
108 |
Options: |
109 |
-?|--help brief help message |
110 |
--userid the userid to be used to find the patron |
111 |
--patron_id the borrowernumber for the patron |
112 |
--cardnumber the cardnumber for the patron |
113 |
|
114 |
Note: Only one of --userid, --patron_id, or --cardnumber should be specified. |
115 |
|
116 |
=head1 OPTIONS |
117 |
|
118 |
=over 8 |
119 |
|
120 |
=item B<--help|-?> |
121 |
|
122 |
Print a brief help message and exits |
123 |
|
124 |
=item B<--userid> |
125 |
|
126 |
The patron's userid (for finding the patron). Mutually exclusive with other options. |
127 |
|
128 |
=item B<--patron_id> |
129 |
|
130 |
The patron's internal id (borrowernumber) for finding the patron. Mutually exclusive with other options. |
131 |
|
132 |
=item B<--cardnumber> |
133 |
|
134 |
Patron's cardnumber (for finding the patron). Mutually exclusive with other options. |
135 |
|
136 |
=back |
137 |
|
138 |
=head1 DESCRIPTION |
139 |
|
140 |
A simple script to reset an existing user's 2FA authentication settings. |
141 |
|
142 |
This script will: |
143 |
- Find the patron using the specified identifier |
144 |
- Check if the patron has 2FA enabled |
145 |
- Reset the patron's 2FA setting |
146 |
|
147 |
The script requires exactly one of the patron identification parameters and will |
148 |
exit with an error if none or multiple parameters are provided. |
149 |
|
150 |
=cut |