diff --git a/FEATURES/116562 b/FEATURES/116562 new file mode 100644 index 0000000000000000000000000000000000000000..b9bed85a8506c8b5ec7efb504cf2539e0c7367fa --- /dev/null +++ b/FEATURES/116562 @@ -0,0 +1,10 @@ + '116562' => + ['Label' => $this->_('Fournisseur de vignettes'), + 'Desc' => $this->_('Bokeh permet de spécifier les informations permettant l\'accès au service Electre REST API version 2'), + 'Image' => '', + 'Video' => '', + 'Category' => $this->_('Enrichissements'), + 'Right' => function($feature_description, $user) {return true;}, + 'Wiki' => 'http://wiki.bokeh-library-portal.org/index.php?title=Vignette#Fournisseur_de_vignettes', + 'Test' => '', + 'Date' => '2021-01-28'], \ No newline at end of file diff --git a/VERSIONS_WIP/116562 b/VERSIONS_WIP/116562 new file mode 100644 index 0000000000000000000000000000000000000000..6c5659d5b8883ce754ff297bff4416b7ceab306f --- /dev/null +++ b/VERSIONS_WIP/116562 @@ -0,0 +1 @@ + - ticket #116562 : Enrichissements : Ajout de la possibilité de définir les accès aux fournisseur de notices Electre Rest APIv2 pour récupération des vignettes \ No newline at end of file diff --git a/library/Class/AdminVar.php b/library/Class/AdminVar.php index 13d5f4684a0d86d53cdf3091b17a4349633a539e..035608a11fb550852bac25ec9c2fbdd5cda673b4 100644 --- a/library/Class/AdminVar.php +++ b/library/Class/AdminVar.php @@ -140,6 +140,7 @@ class Class_AdminVarLoader extends Storm_Model_Loader { 'drive-checkout' => $this->_getDriveCheckoutVars(), 'journal' => $this->_getJournalVars(), 'activity' => $this->_getActivityVars(), + 'pellicule' => $this->_getPelliculeVars(), ]; } @@ -571,6 +572,10 @@ class Class_AdminVarLoader extends Storm_Model_Loader { ]; } + protected function _getPelliculeVars() { + return ['PELLICULE_SETTINGS' => (new Class_AdminVar_PelliculeSettings)->meta()]; + } + protected function _getJournalVars() { $stormModels = new Class_AdminVar_JournalStormModels(); diff --git a/library/Class/AdminVar/PelliculeSettings.php b/library/Class/AdminVar/PelliculeSettings.php new file mode 100644 index 0000000000000000000000000000000000000000..73a6d3f3bb7f1c31d9862aa42e4784d343cc6650 --- /dev/null +++ b/library/Class/AdminVar/PelliculeSettings.php @@ -0,0 +1,90 @@ +<?php +/** + * Copyright (c) 2012-2020, Agence Française Informatique (AFI). All rights reserved. + * + * BOKEH is free software; you can redistribute it and/or modify + * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by + * the Free Software Foundation. + * + * There are special exceptions to the terms and conditions of the AGPL as it + * is applied to this software (see README file). + * + * BOKEH 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 AFFERO GENERAL PUBLIC LICENSE for more details. + * + * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE + * along with BOKEH; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + + +class Class_AdminVar_PelliculeSettings { + use Trait_Translator; + const + PROVIDER_KEY = 'Provider', + LOGIN_KEY = 'Login', + PASSWORD_KEY = 'Password', + + ELECTRE_REST_2 = 'electre_rest_2'; + + protected $_settings; + + public function headers() { + if ((!$settings = json_decode(Class_AdminVar::get('PELLICULE_SETTINGS'), true)) + || !isset($settings[static::PROVIDER_KEY]) + || !is_array($settings[static::PROVIDER_KEY])) + return []; + + $this->_settings = $settings; + + return array_filter(array_map([$this, '_header'], + $settings[static::PROVIDER_KEY], + array_keys($settings[static::PROVIDER_KEY]))); + } + + + public function meta() { + $options = ['fields' => [ + ['name' => static::PROVIDER_KEY, + 'label' => $this->_('Fournisseur'), + 'type' => 'select', + 'options' => [static::ELECTRE_REST_2 => $this->_('Electre v2 REST')], + ], + ['name' => static::LOGIN_KEY, + 'label' => $this->_('Identifiant')], + ['name' => static::PASSWORD_KEY, + 'label' => $this->_('Mot de passe') + ]], + + 'fixed' => true]; + + $label = $this->_('Identifiant auprès d\'un fournisseur de vignettes'); + + return Class_AdminVar_Meta::newMultiInput($label, ['options' => $options, 'value' => '']); + } + + + protected function _header($provider, $position) { + if ($provider + && ($credentials = $this->_credentialsFor($provider, $position))) + return ['Authorization'=> 'Pellicule ' . base64_encode(json_encode($credentials))]; + } + + + protected function _credentialsFor($provider, $position) { + if (($id = $this->_getAtPosition(static::LOGIN_KEY, $position)) + && ($secret = $this->_getAtPosition(static::PASSWORD_KEY, $position))) + return ['provider' => $provider, + 'client_id' => $id, + 'client_secret' => $secret]; + } + + + protected function _getAtPosition($key, $position) { + return isset($this->_settings[$key][$position]) + ? $this->_settings[$key][$position] + : null; + } +} diff --git a/library/Class/WebService/AllServices.php b/library/Class/WebService/AllServices.php index a64a48b9c07d55ebb9281e3aaa1140d2fa859e1f..e7d4f9934114f5c01304f6c458b6400421c75f5d 100644 --- a/library/Class/WebService/AllServices.php +++ b/library/Class/WebService/AllServices.php @@ -84,41 +84,45 @@ class Class_WebService_AllServices { public static function runServiceAfiBiographie($args) { - return self::runServiceAfi(static::SVC_GET_BIOGRAPHY, $args); + return static::runServiceAfi(static::SVC_GET_BIOGRAPHY, $args); } public static function runServiceAfiVideo($args) { - return self::runServiceAfi(static::SVC_GET_VIDEO, $args); + return static::runServiceAfi(static::SVC_GET_VIDEO, $args); } public static function runServiceAfiInterviews($args) { - return self::runServiceAfi(static::SVC_GET_INTERVIEW, $args); + return static::runServiceAfi(static::SVC_GET_INTERVIEW, $args); } public static function runServiceAfiUploadVignette($args) { - return self::runServiceAfi(static::SVC_UPLOAD_THUMBNAIL, $args); + return static::runServiceAfi(static::SVC_UPLOAD_THUMBNAIL, $args); } public static function runServiceAfiUploadBiographie($args) { - return self::runServiceAfi(static::SVC_SET_BIOGRAPHY, $args); + return static::runServiceAfi(static::SVC_SET_BIOGRAPHY, $args); } + public static function runServiceAfiUploadTrailer($args) { - return self::runServiceAfi(static::SVC_SET_TRAILER, $args); + return static::runServiceAfi(static::SVC_SET_TRAILER, $args); } public static function runServiceGetUrlVignette($args) { - return self::runServiceAfi(static::SVC_GET_THUMBNAIL, $args); + if ($headers = (new Class_AdminVar_PelliculeSettings)->headers()) + $args['headers'] = $headers; + + return static::runServiceAfi(static::SVC_GET_THUMBNAIL, $args); } public static function setHttpClient($client) { - self::$_http_client = $client; + static::$_http_client = $client; } @@ -132,7 +136,7 @@ class Class_WebService_AllServices { 'image' => $url, 'numero' => $notice->getTomeAlpha(), 'clef_chapeau' => $notice->getClefChapeau()])); - if (self::RETOUR_SERVICE_OK != $result['statut_recherche']) + if (static::RETOUR_SERVICE_OK != $result['statut_recherche']) return $result['erreur']; $notice @@ -150,7 +154,7 @@ class Class_WebService_AllServices { 'auteur' => $auteur, 'clef_oeuvre' => Class_Notice_WorkKey::legacy()->keyString($notice)]); $result = static::runServiceAfiUploadBiographie($args); - if (self::RETOUR_SERVICE_OK != $result['statut_recherche']) + if (static::RETOUR_SERVICE_OK != $result['statut_recherche']) return $result['message']; } @@ -171,7 +175,7 @@ class Class_WebService_AllServices { 'clef_oeuvre' => Class_Notice_WorkKey::legacy()->keyString($record), 'language' => $lang, 'biography_disabled' => ($status == static::BIO_DISABLED) ? 1 : 0]); - if (self::RETOUR_SERVICE_OK != $result['statut_recherche']) + if (static::RETOUR_SERVICE_OK != $result['statut_recherche']) return $result['message']; } @@ -188,31 +192,41 @@ class Class_WebService_AllServices { if (!isset($result['statut_recherche'])) return $this->_('Le service n\'a pas répondu'); - if (self::RETOUR_SERVICE_OK != $result['statut_recherche']) + if (static::RETOUR_SERVICE_OK != $result['statut_recherche']) return $result['message']; } public static function httpGet($url, $args) { - if (!isset(self::$_http_client)) - self::$_http_client = new Class_WebService_SimpleWebClient(); - return self::$_http_client->open_url($url.'?'.http_build_query($args)); + if (!isset(static::$_http_client)) + static::$_http_client = new Class_WebService_SimpleWebClient(); + + $options = isset($args['headers']) + ? ['headers' => $args['headers']] + : []; + unset($args['headers']); + + $call_params = [$url . '?' . http_build_query($args)]; + if ($options) + $call_params[] = $options; + + return call_user_func_array([static::$_http_client, 'open_url'], $call_params); } - public static function runServiceAfi($service,$args) { + public static function runServiceAfi($service, $args) { if (!$url_service = Class_CosmoVar::get('url_services')) return false; if (!$args) $args = array(); - $args['src'] = self::createSecurityKey(); - $args['api'] = self::SVC_API; + $args['src'] = static::createSecurityKey(); + $args['api'] = static::SVC_API; $args['action'] = $service; - $response = json_decode(self::httpGet($url_service, $args), true); + $response = json_decode(static::httpGet($url_service, $args), true); if ($ig = Zend_Controller_Front::getInstance() ->getPlugin('ZendAfi_Controller_Plugin_InspectorGadget')) diff --git a/library/ZendAfi/Form/Admin/AdminVar/MultiInput.php b/library/ZendAfi/Form/Admin/AdminVar/MultiInput.php index 6318a64c4085cd1e29e9373365574c19e4992021..318da985aa4ca385f7f2b6956c74e806f3a9ac7b 100644 --- a/library/ZendAfi/Form/Admin/AdminVar/MultiInput.php +++ b/library/ZendAfi/Form/Admin/AdminVar/MultiInput.php @@ -21,7 +21,9 @@ class ZendAfi_Form_Admin_AdminVar_MultiInput extends ZendAfi_Form_Admin_AdminVar { - protected $_fields = []; + protected + $_fields = [], + $_fixed = false; public function setFields($fields) { @@ -30,11 +32,18 @@ class ZendAfi_Form_Admin_AdminVar_MultiInput extends ZendAfi_Form_Admin_AdminVar } + public function setFixed($flag) { + $this->_fixed = $flag; + return $this; + } + + public function addVariableEditElement() { $this->addElement('multiInput', 'valeur', ['label' => $this->_('Liste de valeurs'), - 'fields' => $this->_fields]); + 'fields' => $this->_fields, + 'fixed' => $this->_fixed]); } diff --git a/tests/application/modules/opac/controllers/NoticeAjaxControllerPelliculeTest.php b/tests/application/modules/opac/controllers/NoticeAjaxControllerPelliculeTest.php new file mode 100644 index 0000000000000000000000000000000000000000..b7a5b703b7dc95d6efd3c17c118c4be68b41745e --- /dev/null +++ b/tests/application/modules/opac/controllers/NoticeAjaxControllerPelliculeTest.php @@ -0,0 +1,73 @@ +<?php +/** + * Copyright (c) 2012-2021, Agence Française Informatique (AFI). All rights reserved. + * + * BOKEH is free software; you can redistribute it and/or modify + * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by + * the Free Software Foundation. + * + * There are special exceptions to the terms and conditions of the AGPL as it + * is applied to this software (see README file). + * + * BOKEH 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 AFFERO GENERAL PUBLIC LICENSE for more details. + * + * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE + * along with BOKEH; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + + +class NoticeAjaxControllerPelliculeActiveTest extends AbstractControllerTestCase { + protected + $_storm_default_to_volatile = true, + $_http_client; + + public function setUp() { + parent::setUp(); + Class_CosmoVar::set('url_services', 'http://cache.org'); + Class_AdminVar::set('PELLICULE_SETTINGS', + json_encode(['Provider' => [Class_AdminVar_PelliculeSettings::ELECTRE_REST_2], + 'Login' => ['mylogin'], + 'Password' => ['mys3cr3t']])); + + Class_Exemplaire::clearCache(); + Class_Notice::clearCache(); + + $this->fixture('Class_Notice', + ['id' => 1, + 'titre' => 'Pomme et Ananas', + 'titre_principal' => 'Journey in Satchidananda', + 'auteur_principal' => 'Jacques Audiard', + 'isbn' => '978225316403610', + 'ean' => '978225316403610', + 'clef_alpha' => 'POMMEETANANAS', + 'facettes' => 'A1 Q9', + 'date_maj' => '01/01/2015', + 'type_doc' => Class_TypeDoc::LIVRE, + 'url_vignette' => '', + 'url_image' => '', + 'exemplaires' => [$this->fixture('Class_Exemplaire', + ['id' => 2, + 'id_origine' => '9939_SSIRTE'])] + ]); + + $this->_http_client = $this->mock() + ->whenCalled('open_url') + ->answers(null); + + Class_WebService_AllServices::setHttpClient($this->_http_client); + $this->dispatch('/noticeajax/notice/id/1'); + } + + + /** @test */ + public function requestShouldContainsAuthorizationHeader() { + $headers = [['Authorization' => "Pellicule eyJwcm92aWRlciI6ImVsZWN0cmVfcmVzdF8yIiwiY2xpZW50X2lkIjoibXlsb2dpbiIsImNsaWVudF9zZWNyZXQiOiJteXMzY3IzdCJ9"]]; + + $call_params = $this->_http_client->getAttributesForLastCallOn('open_url'); + $this->assertEquals($headers, $call_params[1]['headers']); + } +} diff --git a/tests/application/modules/opac/controllers/NoticeAjaxControllerTest.php b/tests/application/modules/opac/controllers/NoticeAjaxControllerTest.php index 52b1515cf5f64b03fc98f834d6508cd1b8fa65cf..3e20626f6b840a2e6efe164cb6505486cbb42983 100644 --- a/tests/application/modules/opac/controllers/NoticeAjaxControllerTest.php +++ b/tests/application/modules/opac/controllers/NoticeAjaxControllerTest.php @@ -2906,4 +2906,4 @@ class NoticeAjaxControllerRecordWithSerieAnd995ZeroSameAsIdOrigineTest extends A utf8_encode('Voir ce numéro de '), $this->_response->getBody()); } -} \ No newline at end of file +}