-
Notifications
You must be signed in to change notification settings - Fork 4
/
create_superlibrarian.pl
executable file
·103 lines (74 loc) · 2.99 KB
/
create_superlibrarian.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/perl
# This program 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.
#
# This program 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>.
#
# This program comes with ABSOLUTELY NO WARRANTY;
use Modern::Perl;
use Getopt::Long;
use Pod::Usage;
use C4::Context;
use Koha::AuthUtils;
use Koha::Patrons;
my $dbh = C4::Context->dbh;
my ( $branchcode ) = $dbh->selectrow_array(q|SELECT IF( EXISTS( SELECT branchcode FROM branches WHERE branchcode="CPL"), "CPL", ( SELECT branchcode FROM branches LIMIT 1 ) )|);
my ( $categorycode ) = $dbh->selectrow_array(q|SELECT IF( EXISTS( SELECT categorycode FROM categories WHERE categorycode="S"), "S", ( SELECT categorycode FROM categories LIMIT 1 ) )|);
die
"Not enough data in the database, library and/or patron category does not exist"
unless $branchcode and $categorycode;
my $patron_exists = $dbh->selectrow_array(q|SELECT COUNT(*) FROM borrowers WHERE userid = "koha"|);
die "A patron with userid 'koha' already exists" if $patron_exists;
$patron_exists = $dbh->selectrow_array(q|SELECT COUNT(*) FROM borrowers WHERE cardnumber = "koha"|);
die "A patron with cardnumber '42' already exists" if $patron_exists;
my $userid = 'koha';
my $password = 'koha';
my $help;
GetOptions(
'help|?' => \$help,
'userid=s' => \$userid,
'password=s' => \$password
);
pod2usage(1) if $help;
my $patron = Koha::Patron->new({
surname => 'koha',
userid => $userid,
cardnumber => '42',
branchcode => $branchcode,
categorycode => $categorycode,
flags => 1,
})->store;
my $digest = Koha::AuthUtils::hash_password($password);
# We must not call Koha::Patron->store here
# Ideally we should use Koha::Patron->set_password, but it does not exist for all versions
$patron->password($digest);
$patron->_result->update_or_insert;
=head1 NAME
create_superlibrarian.pl - create a user in Koha with superlibrarian permissions
=head1 SYNOPSIS
create_superlibrarian.pl
[ --userid <userid> ] [ --password <password> ]
Options:
-?|--help brief help message
--userid specify the userid to be set (defaults to koha)
--password specify the password to be set (defaults to koha)
=head1 OPTIONS
=over 8
=item B<--help|-?>
Print a brief help message and exits
=item B<--userid>
Allows you to specify the userid to be set in the database
=item B<--password>
Allows you to specify the password to be set in the database
=back
=head1 DESCRIPTION
A simple script to create a user in the Koha database with superlibrarian permissions
=cut