"Fossies" - the Fresh Open Source Software Archive

Member "koha-19.11.15/misc/admin/set_password.pl" (23 Feb 2021, 3221 Bytes) of package /linux/misc/koha-19.11.15.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Perl source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "set_password.pl" see the Fossies "Dox" file reference documentation.

    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 
   22 use Bytes::Random::Secure;
   23 use Getopt::Long;
   24 use Pod::Usage;
   25 use String::Random;
   26 
   27 use Koha::Patrons;
   28 use Koha::Script;
   29 
   30 my ( $help, $password, $cardnumber, $patron_id, $userid );
   31 GetOptions(
   32     'help|?'         => \$help,
   33     'userid=s'       => \$userid,
   34     'password=s'     => \$password,
   35     'patron_id=s'    => \$patron_id,
   36     'cardnumber=s'   => \$cardnumber,
   37 );
   38 
   39 pod2usage(1) if $help;
   40 
   41 unless ( $userid or $patron_id or $cardnumber ) {
   42     pod2usage("userid is mandatory")       unless $userid;
   43     pod2usage("patron_id is mandatory")    unless $patron_id;
   44     pod2usage("cardnumber is mandatory")   unless $cardnumber;
   45 }
   46 
   47 unless ($password) {
   48     my $generator  = String::Random->new( rand_gen => \&alt_rand );
   49     $password      = $generator->randregex('[A-Za-z][A-Za-z0-9_]{6}.[A-Za-z][A-Za-z0-9_]{6}\d');
   50 }
   51 
   52 my $filter;
   53 
   54 if ( $userid ) {
   55     $filter->{userid} = $userid;
   56 }
   57 
   58 if ( $cardnumber ) {
   59     $filter->{cardnumber} = $cardnumber;
   60 }
   61 
   62 if ( $patron_id ) {
   63     $filter->{borrowernumber} = $patron_id;
   64 }
   65 
   66 my $patrons = Koha::Patrons->search( $filter );
   67 
   68 unless ( $patrons->count > 0 ) {
   69     pod2usage( "No patron found matching the specified criteria" );
   70 }
   71 
   72 my $patron = $patrons->next;
   73 $patron->set_password({ password => $password, skip_validation => 1 });
   74 
   75 print $patron->userid . " " . $password . "\n";
   76 
   77 sub alt_rand { # Alternative randomizer
   78     my ($max) = @_;
   79     my $random = Bytes::Random::Secure->new( NonBlocking => 1 );
   80     my $r = $random->irand / 2**32;
   81     return int( $r * $max );
   82 }
   83 
   84 =head1 NAME
   85 
   86 set_password.pl - Set the specified password for the user in Koha
   87 
   88 =head1 SYNOPSIS
   89 
   90 set_password.pl
   91   --userid <userid> --password <password> --patron_id <patron_id> --cardnumber <cardnumber>
   92 
   93  Options:
   94    -?|--help        brief help message
   95    --password       the password to be set (optional)
   96    --userid         the userid to be used to find the patron
   97    --patron_id      the borrowernumber for the patron
   98    --cardnumber     the cardnumber for the patron
   99 
  100 =head1 OPTIONS
  101 
  102 =over 8
  103 
  104 =item B<--help|-?>
  105 
  106 Print a brief help message and exits
  107 
  108 =item B<--userid>
  109 
  110 The patron's userid (for finding the patron)
  111 
  112 =item B<--password>
  113 
  114 The password to be set in the database. If no password is passed, a random one is generated.
  115 
  116 =item B<--patron_id>
  117 
  118 The patron's internal id (for finding the patron)
  119 
  120 =item B<--cardnumber>
  121 
  122 Patron's cardnumber (for finding the patron)
  123 
  124 =back
  125 
  126 =head1 DESCRIPTION
  127 
  128 A simple script to change an existing's user password in the Koha database
  129 
  130 =cut