From e8394e038971cf5a0c1a6a734fd28cf44fe9b78e Mon Sep 17 00:00:00 2001 From: Olli-Antti Kivilahti Date: Wed, 20 Apr 2016 12:25:19 +0300 Subject: [PATCH] Bug 16306 - C4::Context->setCommandlineEnvironment() convenience function There is no unified way to set the environment for commandline scripts, even if the C4::Context->interface('commandline') explicitly allows setting the cli interface. This function wraps the complexity of environment setting in the command line. Also a default user environment must be set. To set the default user environment we need a default user. This patch also makes sure that a default 'commandlineadmin' always exists in DB. Usage: use C4::Context; BEGIN { C4::Context->setCommandlineEnvironment(); } ... #Load other dependencies that might depend on the environment or just: ... use C4::Context; C4::Context->setCommandlineEnvironment(); ... --- C4/Context.pm | 35 +++++++++++++++++++++++++++++++++++ t/db_dependent/Context_commandline.t | 30 ++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 t/db_dependent/Context_commandline.t diff --git a/C4/Context.pm b/C4/Context.pm index 05d761e..10034b3 100644 --- a/C4/Context.pm +++ b/C4/Context.pm @@ -102,6 +102,7 @@ use ZOOM; use XML::Simple; use C4::Boolean; use C4::Debug; +use C4::Members; use POSIX (); use DateTime::TimeZone; use Module::Load::Conditional qw(can_load); @@ -1293,6 +1294,40 @@ sub IsSuperLibrarian { return ($userenv->{flags}//0) % 2; } +=head2 setCommandlineEnvironment + +Sets the Koha environment for command line scripts. + +=cut + +sub setCommandlineEnvironment { + my ($class) = @_; + + C4::Context->interface('commandline'); #Set interface for logger and friends + C4::Context->_new_userenv('commandline'); + my $clisu = _enforceCommandlineSuperuserBorrowerExists(); + C4::Context::set_userenv($clisu->{borrowernumber},$clisu->{userid},$clisu->{cardnumber},$clisu->{firstname},$clisu->{surname}, $clisu->{branchcode}, '', {}, '', '', ''); +} + +sub _enforceCommandlineSuperuserBorrowerExists { + my $commandlineSuperuser = C4::Members::GetMember(userid => 'commandlineadmin'); + unless ($commandlineSuperuser) { + my $dbh = C4::Context->dbh(); + my $ctgr = $dbh->selectrow_array("SELECT categorycode FROM categories WHERE category_type = 'I' LIMIT 1;"); + my $brnchcd = $dbh->selectrow_array("SELECT branchcode FROM borrowers GROUP BY branchcode ORDER BY COUNT(*) DESC LIMIT 1;"); + C4::Members::AddMember(cardnumber => 'commandlineadmin', + userid => 'commandlineadmin', + surname => 'Admin', + firstname => 'Koha', + dateexpiry => '2099-12-31', + categorycode => $ctgr, + branchcode => $brnchcd, + ); + $commandlineSuperuser = C4::Members::GetMember(userid => 'commandlineadmin'); + } + return $commandlineSuperuser; +} + =head2 interface Sets the current interface for later retrieval in any Perl module diff --git a/t/db_dependent/Context_commandline.t b/t/db_dependent/Context_commandline.t new file mode 100644 index 0000000..0e585eb --- /dev/null +++ b/t/db_dependent/Context_commandline.t @@ -0,0 +1,30 @@ +# Copyright 2016 KohaSuomi +# +# 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, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use Modern::Perl; +use Test::More; + +use C4::Context; + +my $commandlineSuperuser = C4::Context::_enforceCommandlineSuperuserBorrowerExists(); +is($commandlineSuperuser->{cardnumber}, "commandlineadmin", "_enforceCommandlineSuperuserBorrowerExists() enforced"); + +C4::Context->setCommandlineEnvironment(); +my $env = C4::Context->userenv(); +is($env->{id}, $commandlineSuperuser->{userid}, "setCommandlineEnvironment userenv set with 'commandlineadmin'"); + +done_testing(); \ No newline at end of file -- 1.9.1