Bug 17746: (QA follow-up) Make set_password.pl generate a password if required
authorTomas Cohen Arazi <tomascohen@theke.io>
Wed, 20 Feb 2019 14:26:40 +0000 (11:26 -0300)
committerNick Clemens <nick@bywatersolutions.com>
Thu, 28 Mar 2019 11:58:20 +0000 (11:58 +0000)
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 <tomascohen@theke.io>

Signed-off-by: Liz Rea <wizzyrea@gmail.com>

Signed-off-by: Josef Moravec <josef.moravec@gmail.com>

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>

debian/scripts/koha-reset-passwd
misc/admin/set_password.pl

index ac3c406..14cfe33 100755 (executable)
@@ -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
index 8ae1444..0df9be3 100755 (executable)
 # along with Koha; if not, see <http://www.gnu.org/licenses>.
 
 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>