Bug 17746: Make koha-reset-passwd user set_password.pl
[koha.git] / debian / scripts / koha-reset-passwd
1 #!/bin/sh
2 #
3 # koha-reset-passwd -- reset password for a user in a Koha instance
4 # Copyright 2010  Catalyst IT, Ltd
5 # Copyright 2019  Theke Solutions
6
7 # This program is free software: you can redistribute it and/or modify
8 # it 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 # This program is distributed in the hope that it will be useful,
13 # but 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 this program.  If not, see <http://www.gnu.org/licenses/>.
19
20
21 set -e
22
23 # include helper functions
24 if [ -f "/usr/share/koha/bin/koha-functions.sh" ]; then
25     . "/usr/share/koha/bin/koha-functions.sh"
26 else
27     echo "Error: /usr/share/koha/bin/koha-functions.sh not present." 1>&2
28     exit 1
29 fi
30
31 usage()
32 {
33     local scriptname=$0
34     cat <<EOF
35 Resets the password for the specified user on the Koha instance.
36
37 Usage: $scriptname instancename userid
38
39 Note: The generated password will be printed.
40 EOF
41 }
42
43 set_password()
44 {
45     local instancename=$1
46     local userid=$2
47     local password=$(pwgen 12 1)
48
49     # Optionally use alternative paths for a dev install
50     adjust_paths_dev_install $1
51
52     if [ "$DEV_INSTALL" = "" ]; then
53         KOHA_BINDIR=$KOHA_HOME/bin
54     else
55         KOHA_BINDIR=$KOHA_HOME/misc
56     fi
57
58     if sudo -u "$instancename-koha" -H \
59         env PERL5LIB=$PERL5LIB \
60         KOHA_CONF="/etc/koha/sites/$instancename/koha-conf.xml" \
61         $KOHA_BINDIR/admin/set_password.pl --userid $userid --password $password ; then
62
63         echo "$userid $password"
64         return 0
65     else
66         return 1
67     fi
68 }
69
70 if [ $# -lt 2 ]; then
71     usage
72     die "Wrong parameters"
73 fi
74
75 instance="$1"
76 shift
77
78 for userid in "$@"
79 do
80     set_password $instance $userid
81 done
82
83 exit 0