From 1736fb9e8ff8d872dffc08267fcc842116b70d0a Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Wed, 20 Feb 2019 11:26:40 -0300 Subject: [PATCH] Bug 17746: (QA follow-up) Make set_password.pl generate a password if required This patch makes the set_password.pl script generate the password if it is not passed as an argument. It also changes the behaviour of set_password.pl: it will now print the userid and password (generated or not) to mimick the behaviour from koha-reset-passwd. The koha-reset-passwd gets simplified as it doesn't generate the random password anymore, it passes the responsability to set_password.pl To test: - Follow the steps from the original platch - Verify all behaves as expected Signed-off-by: Tomas Cohen Arazi --- debian/scripts/koha-reset-passwd | 4 +--- misc/admin/set_password.pl | 22 +++++++++++++++++++--- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/debian/scripts/koha-reset-passwd b/debian/scripts/koha-reset-passwd index ac3c40673f..14cfe33f03 100755 --- a/debian/scripts/koha-reset-passwd +++ b/debian/scripts/koha-reset-passwd @@ -44,7 +44,6 @@ set_password() { local instancename=$1 local userid=$2 - local password=$(pwgen 12 1) # Optionally use alternative paths for a dev install adjust_paths_dev_install $1 @@ -58,9 +57,8 @@ set_password() if sudo -u "$instancename-koha" -H \ env PERL5LIB=$PERL5LIB \ KOHA_CONF="/etc/koha/sites/$instancename/koha-conf.xml" \ - $KOHA_BINDIR/admin/set_password.pl --userid $userid --password $password ; then + $KOHA_BINDIR/admin/set_password.pl --userid $userid ; then - echo "$userid $password" return 0 else return 1 diff --git a/misc/admin/set_password.pl b/misc/admin/set_password.pl index 8ae1444bd9..0df9be318b 100755 --- a/misc/admin/set_password.pl +++ b/misc/admin/set_password.pl @@ -18,8 +18,11 @@ # along with Koha; if not, see . use Modern::Perl; + +use Bytes::Random::Secure; use Getopt::Long; use Pod::Usage; +use String::Random; use Koha::Patrons; @@ -33,7 +36,6 @@ GetOptions( ); pod2usage(1) if $help; -pod2usage("password is mandatory") unless $password; unless ( $userid or $patron_id or $cardnumber ) { pod2usage("userid is mandatory") unless $userid; @@ -41,6 +43,11 @@ unless ( $userid or $patron_id or $cardnumber ) { pod2usage("cardnumber is mandatory") unless $cardnumber; } +unless ($password) { + my $generator = String::Random->new( rand_gen => \&alt_rand ); + $password = $generator->randregex('[A-Za-z][A-Za-z0-9_]{6}.[A-Za-z][A-Za-z0-9_]{6}\d'); +} + my $filter; if ( $userid ) { @@ -64,6 +71,15 @@ unless ( $patrons->count > 0 ) { my $patron = $patrons->next; $patron->set_password({ password => $password, skip_validation => 1 }); +print $patron->userid . " " . $password . "\n"; + +sub alt_rand { # Alternative randomizer + my ($max) = @_; + my $random = Bytes::Random::Secure->new( NonBlocking => 1 ); + my $r = $random->irand / 2**32; + return int( $r * $max ); +} + =head1 NAME set_password.pl - Set the specified password for the user in Koha @@ -75,7 +91,7 @@ set_password.pl Options: -?|--help brief help message - --password the password to be set + --password the password to be set (optional) --userid the userid to be used to find the patron --patron_id the borrowernumber for the patron --cardnumber the cardnumber for the patron @@ -94,7 +110,7 @@ The patron's userid (for finding the patron) =item B<--password> -The password to be set in the database +The password to be set in the database. If no password is passed, a random one is generated. =item B<--patron_id> -- 2.20.1