View | Details | Raw Unified | Return to bug 16306
Collapse All | Expand All

(-)a/C4/Context.pm (+35 lines)
Lines 102-107 use ZOOM; Link Here
102
use XML::Simple;
102
use XML::Simple;
103
use C4::Boolean;
103
use C4::Boolean;
104
use C4::Debug;
104
use C4::Debug;
105
use C4::Members;
105
use POSIX ();
106
use POSIX ();
106
use DateTime::TimeZone;
107
use DateTime::TimeZone;
107
use Module::Load::Conditional qw(can_load);
108
use Module::Load::Conditional qw(can_load);
Lines 1293-1298 sub IsSuperLibrarian { Link Here
1293
    return ($userenv->{flags}//0) % 2;
1294
    return ($userenv->{flags}//0) % 2;
1294
}
1295
}
1295
1296
1297
=head2 setCommandlineEnvironment
1298
1299
Sets the Koha environment for command line scripts.
1300
1301
=cut
1302
1303
sub setCommandlineEnvironment {
1304
	my ($class) = @_;
1305
1306
	C4::Context->interface('commandline'); #Set interface for logger and friends
1307
	C4::Context->_new_userenv('commandline');
1308
	my $clisu = _enforceCommandlineSuperuserBorrowerExists();
1309
	C4::Context::set_userenv($clisu->{borrowernumber},$clisu->{userid},$clisu->{cardnumber},$clisu->{firstname},$clisu->{surname}, $clisu->{branchcode}, '', {}, '', '', '');
1310
}
1311
1312
sub _enforceCommandlineSuperuserBorrowerExists {
1313
	my $commandlineSuperuser = C4::Members::GetMember(userid => 'commandlineadmin');
1314
	unless ($commandlineSuperuser) {
1315
		my $dbh = C4::Context->dbh();
1316
		my $ctgr = $dbh->selectrow_array("SELECT categorycode FROM categories WHERE category_type = 'I' LIMIT 1;");
1317
		my $brnchcd = $dbh->selectrow_array("SELECT branchcode FROM borrowers GROUP BY branchcode ORDER BY COUNT(*) DESC LIMIT 1;");
1318
		C4::Members::AddMember(cardnumber => 'commandlineadmin',
1319
							   userid => 'commandlineadmin',
1320
							   surname => 'Admin',
1321
							   firstname => 'Koha',
1322
							   dateexpiry => '2099-12-31',
1323
							   categorycode => $ctgr,
1324
							   branchcode => $brnchcd,
1325
		);
1326
		$commandlineSuperuser = C4::Members::GetMember(userid => 'commandlineadmin');
1327
	}
1328
	return $commandlineSuperuser;
1329
}
1330
1296
=head2 interface
1331
=head2 interface
1297
1332
1298
Sets the current interface for later retrieval in any Perl module
1333
Sets the current interface for later retrieval in any Perl module
(-)a/t/db_dependent/Context_commandline.t (-1 / +30 lines)
Line 0 Link Here
0
- 
1
# Copyright 2016 KohaSuomi
2
#
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it under the
6
# terms of the GNU General Public License as published by the Free Software
7
# Foundation; either version 3 of the License, or (at your option) any later
8
# version.
9
#
10
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License along
15
# with Koha; if not, write to the Free Software Foundation, Inc.,
16
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18
use Modern::Perl;
19
use Test::More;
20
21
use C4::Context;
22
23
my $commandlineSuperuser = C4::Context::_enforceCommandlineSuperuserBorrowerExists();
24
is($commandlineSuperuser->{cardnumber}, "commandlineadmin", "_enforceCommandlineSuperuserBorrowerExists() enforced");
25
26
C4::Context->setCommandlineEnvironment();
27
my $env = C4::Context->userenv();
28
is($env->{id}, $commandlineSuperuser->{userid}, "setCommandlineEnvironment userenv set with 'commandlineadmin'");
29
30
done_testing();

Return to bug 16306