Koha/misc/import_patrons.pl
Nick Clemens 6464ada6c6
Bug 23473: Allow overwrite of passwords during import
To test:
 1 - Have some patrons in your system
 2 - Export some of their info via reports
    SELECT cardnumber, userid, surname, firstname, password, branchcode, categorycode
 3 - Edit the file from above, changing all the password lines
 4 - Import the file with overwrite
 5 - Confirm passwords have not changed (run the report again and confirm the hashes are the same)
 6 - Apply patch
 7 - Restart all the things
 8 - Check the new box on import screen to overwrite passwrods
 9 - Import file again
10 - Confirm passwords have changed
11 - Signin using new password to verify the hash is the password as supplied
12 - Repeat via commandline import supplying --overwrite_passwords option
13 - Verify works as expected
14 - Prove -v t/db_dependent/Koha/Patrons/Import.t

Sponsored-by: ByWater Solutions
Signed-off-by: Ron Marion <ron.marion@goddard.edu>
Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
2020-03-27 12:21:59 +00:00

152 lines
3.9 KiB
Perl
Executable file

#!/usr/bin/perl
# Parts copyright 2014 ByWater Solutions
#
# This file is part of Koha.
#
# Koha is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Koha is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Koha; if not, see <http://www.gnu.org/licenses>.
use Modern::Perl;
use Getopt::Long;
use Pod::Usage;
use Koha::Script;
use Koha::Patrons::Import;
my $Import = Koha::Patrons::Import->new();
my $csv_file;
my $matchpoint;
my $overwrite_cardnumber;
my $overwrite_passwords;
my %defaults;
my $ext_preserve = 0;
my $confirm;
my $verbose = 0;
my $help;
GetOptions(
'c|confirm' => \$confirm,
'f|file=s' => \$csv_file,
'm|matchpoint=s' => \$matchpoint,
'd|default=s' => \%defaults,
'o|overwrite' => \$overwrite_cardnumber,
'op|overwrite_passwords' => \$overwrite_passwords,
'p|preserve-extended-attributes' => \$ext_preserve,
'v|verbose+' => \$verbose,
'h|help|?' => \$help,
) or pod2usage(2);
pod2usage(1) if $help;
pod2usage(q|--file is required|) unless $csv_file;
pod2usage(q|--matchpoint is required|) unless $matchpoint;
warn "Running in dry-run mode, provide --confirm to apply the changes\n" unless $confirm;
my $handle;
open( $handle, "<", $csv_file ) or die $!;
my $return = $Import->import_patrons(
{
file => $handle,
defaults => \%defaults,
matchpoint => $matchpoint,
overwrite_cardnumber => $overwrite_cardnumber,
overwrite_passwords => $overwrite_passwords,
preserve_extended_attributes => $ext_preserve,
}
);
my $feedback = $return->{feedback};
my $errors = $return->{errors};
my $imported = $return->{imported};
my $overwritten = $return->{overwritten};
my $alreadyindb = $return->{already_in_db};
my $invalid = $return->{invalid};
if ($verbose) {
my $total = $imported + $alreadyindb + $invalid + $overwritten;
say q{};
say "Import complete:";
say "Imported: $imported";
say "Overwritten: $overwritten";
say "Skipped: $alreadyindb";
say "Invalid: $invalid";
say "Total: $total";
say q{};
}
if ($verbose > 1 ) {
say "Errors:";
say Data::Dumper::Dumper( $errors );
}
if ($verbose > 2 ) {
say "Feedback:";
say Data::Dumper::Dumper( $feedback );
}
=head1 NAME
import_patrons.pl - CLI script to import patrons data into Koha
=head1 SYNOPSIS
import_patrons.pl --file /path/to/patrons.csv --matchpoint cardnumber --confirm [--default branchcode=MPL] [--overwrite] [--preserve-extended-attributes] [--verbose]
=head1 OPTIONS
=over 8
=item B<-h|--help>
Prints a brief help message and exits
=item B<-c|--confirm>
Confirms you really want to import these patrons, otherwise prints this help
=item B<-f|--file>
Path to the CSV file of patrons to import
=item B<-c|--matchpoint>
Field on which to match incoming patrons to existing patrons
=item B<-d|--default>
Set defaults to patron fields, repeatable e.g. --default branchcode=MPL --default categorycode=PT
=item B<-o|--overwrite>
Overwrite existing patrons with new data if a match is found
=item B<-p|--preserve-extended-attributes>
Retain extended patron attributes for existing patrons being overwritten
=item B<-v|--verbose>
Be verbose
Multiple -v options increase the verbosity
2 repetitions or above will report lines in error
3 repetitions or above will report feedback
=back
=cut