diff --git a/fid/alpha/config/vufind/fid.ini b/fid/alpha/config/vufind/fid.ini new file mode 100644 index 0000000000000000000000000000000000000000..d084b63bd6ac9cefa78497783281ecfb3054ebb1 --- /dev/null +++ b/fid/alpha/config/vufind/fid.ini @@ -0,0 +1,5 @@ +[Parent_Config] +relative_path=../../../config/vufind/fid.ini + +[Client] +baseUrlOverrideAllowed = true \ No newline at end of file diff --git a/fid/config/vufind/fid.ini b/fid/config/vufind/fid.ini index a745c935baf3f5ef7a4e21f1e10b819c9a997007..f3c0cff28b32461ac8e7c34c9e7d06d19ae23132 100644 --- a/fid/config/vufind/fid.ini +++ b/fid/config/vufind/fid.ini @@ -1,5 +1,7 @@ [Client] -baseUrl = http://172.18.113.133/bbi-alpha.3 +baseUrlOverrideAllowed = false +baseUrlOverridePattern = "^https://fid-api.finc.info/" +baseUrl = https://fid-api.finc.info/staging/fid xdebug_trusted_proxies[] = 139.18.25.87 [Security] diff --git a/module/fid/config/module.config.php b/module/fid/config/module.config.php index 77f8d58bbe0bafcd1fdc34d859b33df8e1d6b47a..d32247d06d75a64348d6c26ed659a059595b0c8b 100644 --- a/module/fid/config/module.config.php +++ b/module/fid/config/module.config.php @@ -33,6 +33,7 @@ use fid\Helper\TranslatorDelegator; use fid\Hydrator\OrderHydrator; use fid\Hydrator\UserHydrator; use fid\Hydrator\UserHydratorDelegatorFactory; +use fid\Listener\BaseUrlListener; use fid\Listener\ErrorListener; use fid\Listener\ErrorListenerFactory; use fid\Listener\LocaleListener; @@ -61,15 +62,15 @@ use Zend\ServiceManager\Factory\InvokableFactory; $config = [ 'forms' => [ - 'user-init-form' => require 'user-init-form.php', - 'user-create-form' => require 'user-create-form.php', - 'user-update-form' => require 'user-update-form.php', - 'admin-edit-form' => require 'admin-edit-form.php', - 'fid-acquisition-form' => require 'fid-acquisition-form.php', + 'user-init-form' => require 'user-init-form.php', + 'user-create-form' => require 'user-create-form.php', + 'user-update-form' => require 'user-update-form.php', + 'admin-edit-form' => require 'admin-edit-form.php', + 'fid-acquisition-form' => require 'fid-acquisition-form.php', 'fid-acquisition-subito-partial-copy-form' => require 'fid-acquisition-subito-partial-copy-form.php', - PasswordResetModel::class => require 'password-reset-form.php', - PasswordChangeModel::class => require 'password-change-form.php', - UsernameChangeModel::class => require 'username-change-form.php', + PasswordResetModel::class => require 'password-reset-form.php', + PasswordChangeModel::class => require 'password-change-form.php', + UsernameChangeModel::class => require 'username-change-form.php', ], 'filters' => [ 'factories' => [ @@ -78,7 +79,7 @@ $config = [ ], 'hydrators' => [ 'factories' => [ - UserHydrator::class => InvokableFactory::class, + UserHydrator::class => InvokableFactory::class, OrderHydrator::class => InvokableFactory::class, ], 'delegators' => [ @@ -88,22 +89,23 @@ $config = [ ], ], 'controllers' => [ - 'factories' => [ + 'factories' => [ RecordController::class => AbstractBaseWithConfigFactory::class, - UserController::class => UserControllerFactory::class, + UserController::class => UserControllerFactory::class, ], - 'aliases' => [ + 'aliases' => [ \VuFind\Controller\RecordController::class => RecordController::class, ], 'delegators' => [ RecordController::class => [ - RecordControllerDelegatorFactory::class, + RecordControllerDelegatorFactory::class, ], ], ], 'listeners' => [ ErrorListener::class, LocaleListener::class, + BaseUrlListener::class, ], // TODO: issue PR to add alias to vufind core 'controller_plugins' => [ @@ -129,6 +131,7 @@ $config = [ ErrorListener::class => InvokableFactory:: class, ILSAuthenticator::class => ILSAuthenticatorFactory::class, LocaleListener::class => InvokableFactory::class, + BaseUrlListener::class => InvokableFactory::class ], ], 'view_helpers' => [ @@ -141,12 +144,12 @@ $config = [ ], 'vufind' => [ 'plugin_managers' => [ - 'ajaxhandler' => [ + 'ajaxhandler' => [ 'factories' => [ 'fid\AjaxHandler\GetResolverLinks' => 'fid\AjaxHandler\GetResolverLinksFactory' ], - 'aliases' => [ + 'aliases' => [ 'getResolverLinks' => 'fid\AjaxHandler\GetResolverLinks' ] @@ -320,7 +323,7 @@ $config = [ ], ], ], - 'orders' => [ + 'orders' => [ 'type' => 'literal', 'options' => [ 'route' => '/orders', @@ -339,7 +342,7 @@ $config = [ 'route' => '/admin' ], 'child_routes' => [ - 'list' => [ + 'list' => [ 'type' => 'literal', 'options' => [ 'route' => '/list', @@ -349,7 +352,7 @@ $config = [ ], ], ], - 'exportList' => [ + 'exportList' => [ 'type' => 'literal', 'options' => [ 'route' => '/exportList', @@ -359,7 +362,7 @@ $config = [ ], ], ], - 'edit' => [ + 'edit' => [ 'type' => 'Zend\Router\Http\Segment', 'options' => [ 'route' => '/edit/[:userid]', @@ -370,7 +373,7 @@ $config = [ ], ], ], - 'orders' => [ + 'orders' => [ 'type' => 'literal', 'options' => [ 'route' => '/orders', @@ -412,7 +415,9 @@ $config = [ ]; $nonTabRecordActions = [ - 'fidPDA', 'fidSubitoArticle', 'fidSubitoPartialCopy' + 'fidPDA', + 'fidSubitoArticle', + 'fidSubitoPartialCopy' ]; // Define record view routes -- route name => controller diff --git a/module/fid/src/Listener/BaseUrlListener.php b/module/fid/src/Listener/BaseUrlListener.php new file mode 100644 index 0000000000000000000000000000000000000000..3fb05b4216f38c4f6f4362667cd21335637dfc33 --- /dev/null +++ b/module/fid/src/Listener/BaseUrlListener.php @@ -0,0 +1,99 @@ +<?php +/** + * Copyright (C) 2020 Leipzig University Library + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * 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 this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * @author Sebastian Kehr <kehr@ub.uni-leipzig.de> + * @license http://opensource.org/licenses/gpl-2.0.php GNU GPLv2 + */ + +namespace fid\Listener; + +use fid\Service\ClientException; +use fid\Service\ClientFactory; +use Psr\Container\ContainerInterface; +use VuFind\Config\PluginManager; +use VuFind\Cookie\CookieManager; +use Zend\Config\Config; +use Zend\EventManager\AbstractListenerAggregate; +use Zend\EventManager\EventManagerInterface; +use Zend\Mvc\MvcEvent; +use Zend\View\Model\ViewModel; + +class BaseUrlListener extends AbstractListenerAggregate +{ + protected const PARAM = 'fidis_base_url'; + + protected const EVENT = MvcEvent::EVENT_ROUTE; + + protected const STATUS = '%sFidis: %s'; + + /** + * @var string + */ + protected $baseUrl; + + /** + * @return string + */ + public function getBaseUrl(): string + { + return $this->baseUrl; + } + + public function attach(EventManagerInterface $events, $priority = 9000) + { + $events->attach(static::EVENT, [$this, 'onDispatch'], $priority); + } + + /** + * @param MvcEvent $event + * + * @throws ClientException + */ + public function onDispatch(MvcEvent $event) + { + /** @var Config $config */ + /** @var PluginManager $configManager */ + /** @var ContainerInterface $container */ + $request = $event->getRequest(); + $container = $event->getApplication()->getServiceManager(); + $configManager = $container->get(PluginManager::class); + $config = $configManager->get(ClientFactory::CONFIG_KEY)->Client; + + if (!$config instanceof Config || !$config->baseUrlOverrideAllowed) { + return; + } + + $this->baseUrl = $request->getQuery(self::PARAM) + ?? $request->getCookie()[self::PARAM] + ?? $config->baseUrl; + + $valid = $this->baseUrl === $config->baseUrl + || preg_match("#$config->baseUrlOverridePattern#", $this->baseUrl); + + if (!$valid) { + $this->baseUrl = $config->baseUrl; + } + + /** @var ViewModel $viewModel */ + /** @var CookieManager $cookieManager */ + $cookieManager = $container->get(CookieManager::class); + $viewModel = $container->get('ViewManager')->getViewModel(); + $cookieManager->set(self::PARAM, $this->baseUrl); + $fidisStatus = sprintf(self::STATUS, $valid ? '' : '!', $this->baseUrl); + $viewModel->setVariable('fidisStatus', $fidisStatus); + } +} \ No newline at end of file diff --git a/module/fid/src/Service/ClientFactory.php b/module/fid/src/Service/ClientFactory.php index 7ec59f8d69e35568ee04a82605d82dee5d5f6e87..ef621cd0aa8ae846e016b05d4183778e2941ec97 100644 --- a/module/fid/src/Service/ClientFactory.php +++ b/module/fid/src/Service/ClientFactory.php @@ -1,6 +1,6 @@ <?php /** - * Copyright (C) 2019 Leipzig University Library + * Copyright (C) 2020 Leipzig University Library * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as @@ -21,6 +21,7 @@ namespace fid\Service; +use fid\Listener\BaseUrlListener; use Psr\Container\ContainerInterface; use Psr\Http\Client\ClientInterface as HttpClientInterface; use Psr\Http\Message\RequestFactoryInterface; @@ -35,7 +36,7 @@ use Zend\Session\SessionManager; class ClientFactory { - protected const CONFIG_KEY = 'fid'; + public const CONFIG_KEY = 'fid'; protected const ERROR_CONFIG_FILE = 'Invalid configuration file "%s.ini".'; @@ -55,6 +56,9 @@ class ClientFactory throw new ClientException($message); } + $baseUrl = $container->get(BaseUrlListener::class)->getBaseUrl(); + $configArray = compact('baseUrl') + $config->toArray(); + /** @var SessionManager $sessionManager */ $sessionManager = $container->get(SessionManager::class); $session = new Session(Client::class, $sessionManager); @@ -71,7 +75,9 @@ class ClientFactory /** @var RequestFactoryInterface $requestFactory */ $requestFactory = $container->get(RequestFactoryInterface::class); - return new Client($config->toArray(), $session, $cookies, $serializer, - $httpClient, $uriFactory, $streamFactory, $requestFactory); + return new Client( + $configArray, $session, $cookies, $serializer, $httpClient, + $uriFactory, $streamFactory, $requestFactory + ); } } \ No newline at end of file