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 <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>
(cherry picked from commit 3ef48c8708)
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
(cherry picked from commit 3b42307122)

Signed-off-by: Lucas Gass <lucas@bywatersolutions.com>
This commit is contained in:
Tomas Cohen Arazi 2019-02-20 11:26:40 -03:00 committed by Lucas Gass
parent c6651b8724
commit 9fe361a491
2 changed files with 20 additions and 6 deletions

View File

@ -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

View File

@ -18,8 +18,11 @@
# 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>