Bug 17746: (QA follow-up) Make set_password.pl generate a password if required
[koha.git] / misc / admin / set_password.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Copyright 2019 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 Bytes::Random::Secure;
23 use Getopt::Long;
24 use Pod::Usage;
25 use String::Random;
26
27 use Koha::Patrons;
28
29 my ( $help, $password, $cardnumber, $patron_id, $userid );
30 GetOptions(
31     'help|?'         => \$help,
32     'userid=s'       => \$userid,
33     'password=s'     => \$password,
34     'patron_id=s'    => \$patron_id,
35     'cardnumber=s'   => \$cardnumber,
36 );
37
38 pod2usage(1) if $help;
39
40 unless ( $userid or $patron_id or $cardnumber ) {
41     pod2usage("userid is mandatory")       unless $userid;
42     pod2usage("patron_id is mandatory")    unless $patron_id;
43     pod2usage("cardnumber is mandatory")   unless $cardnumber;
44 }
45
46 unless ($password) {
47     my $generator  = String::Random->new( rand_gen => \&alt_rand );
48     $password      = $generator->randregex('[A-Za-z][A-Za-z0-9_]{6}.[A-Za-z][A-Za-z0-9_]{6}\d');
49 }
50
51 my $filter;
52
53 if ( $userid ) {
54     $filter->{userid} = $userid;
55 }
56
57 if ( $cardnumber ) {
58     $filter->{cardnumber} = $cardnumber;
59 }
60
61 if ( $patron_id ) {
62     $filter->{borrowernumber} = $patron_id;
63 }
64
65 my $patrons = Koha::Patrons->search( $filter );
66
67 unless ( $patrons->count > 0 ) {
68     pod2usage( "No patron found matching the specified criteria" );
69 }
70
71 my $patron = $patrons->next;
72 $patron->set_password({ password => $password, skip_validation => 1 });
73
74 print $patron->userid . " " . $password . "\n";
75
76 sub alt_rand { # Alternative randomizer
77     my ($max) = @_;
78     my $random = Bytes::Random::Secure->new( NonBlocking => 1 );
79     my $r = $random->irand / 2**32;
80     return int( $r * $max );
81 }
82
83 =head1 NAME
84
85 set_password.pl - Set the specified password for the user in Koha
86
87 =head1 SYNOPSIS
88
89 set_password.pl
90   --userid <userid> --password <password> --patron_id <patron_id> --cardnumber <cardnumber>
91
92  Options:
93    -?|--help        brief help message
94    --password       the password to be set (optional)
95    --userid         the userid to be used to find the patron
96    --patron_id      the borrowernumber for the patron
97    --cardnumber     the cardnumber for the patron
98
99 =head1 OPTIONS
100
101 =over 8
102
103 =item B<--help|-?>
104
105 Print a brief help message and exits
106
107 =item B<--userid>
108
109 The patron's userid (for finding the patron)
110
111 =item B<--password>
112
113 The password to be set in the database. If no password is passed, a random one is generated.
114
115 =item B<--patron_id>
116
117 The patron's internal id (for finding the patron)
118
119 =item B<--cardnumber>
120
121 Patron's cardnumber (for finding the patron)
122
123 =back
124
125 =head1 DESCRIPTION
126
127 A simple script to change an existing's user password in the Koha database
128
129 =cut