Bug 17746: Add misc/admin/set_password.pl script
[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 use Getopt::Long;
22 use Pod::Usage;
23
24 use Koha::Patrons;
25
26 my ( $help, $password, $cardnumber, $patron_id, $userid );
27 GetOptions(
28     'help|?'         => \$help,
29     'userid=s'       => \$userid,
30     'password=s'     => \$password,
31     'patron_id=s'    => \$patron_id,
32     'cardnumber=s'   => \$cardnumber,
33 );
34
35 pod2usage(1) if $help;
36 pod2usage("password is mandatory")     unless $password;
37
38 unless ( $userid or $patron_id or $cardnumber ) {
39     pod2usage("userid is mandatory")       unless $userid;
40     pod2usage("patron_id is mandatory")    unless $patron_id;
41     pod2usage("cardnumber is mandatory")   unless $cardnumber;
42 }
43
44 my $filter;
45
46 if ( $userid ) {
47     $filter->{userid} = $userid;
48 }
49
50 if ( $cardnumber ) {
51     $filter->{cardnumber} = $cardnumber;
52 }
53
54 if ( $patron_id ) {
55     $filter->{borrowernumber} = $patron_id;
56 }
57
58 my $patrons = Koha::Patrons->search( $filter );
59
60 unless ( $patrons->count > 0 ) {
61     pod2usage( "No patron found matching the specified criteria" );
62 }
63
64 my $patron = $patrons->next;
65 $patron->set_password({ password => $password, skip_validation => 1 });
66
67 =head1 NAME
68
69 set_password.pl - Set the specified password for the user in Koha
70
71 =head1 SYNOPSIS
72
73 set_password.pl
74   --userid <userid> --password <password> --patron_id <patron_id> --cardnumber <cardnumber>
75
76  Options:
77    -?|--help        brief help message
78    --password       the password to be set
79    --userid         the userid to be used to find the patron
80    --patron_id      the borrowernumber for the patron
81    --cardnumber     the cardnumber for the patron
82
83 =head1 OPTIONS
84
85 =over 8
86
87 =item B<--help|-?>
88
89 Print a brief help message and exits
90
91 =item B<--userid>
92
93 The patron's userid (for finding the patron)
94
95 =item B<--password>
96
97 The password to be set in the database
98
99 =item B<--patron_id>
100
101 The patron's internal id (for finding the patron)
102
103 =item B<--cardnumber>
104
105 Patron's cardnumber (for finding the patron)
106
107 =back
108
109 =head1 DESCRIPTION
110
111 A simple script to change an existing's user password in the Koha database
112
113 =cut