diff --git a/library/Class/DigitalResource/Config.php b/library/Class/DigitalResource/Config.php index 59319aa4994773b2804a0a37586aed805d95d39f..74cd9358373754d5f655f668f56ceb84bfecdf39 100644 --- a/library/Class/DigitalResource/Config.php +++ b/library/Class/DigitalResource/Config.php @@ -197,7 +197,7 @@ class Class_DigitalResource_Config extends Class_Entity { } - public function urlFor($user, $params=[]) { + public function urlFor($user, $params = []) { if(!$user) return; diff --git a/library/digital_resources/Cvs/Config.php b/library/digital_resources/Cvs/Config.php index b28c65ded5ae266b4f95f9ad6fec0d499fa81e6e..c56edd7e1ced4671abdc76d6b94e3baa6332e5a7 100644 --- a/library/digital_resources/Cvs/Config.php +++ b/library/digital_resources/Cvs/Config.php @@ -52,7 +52,8 @@ class Cvs_Config extends Class_DigitalResource_Config { 'SOURCEKEY' => Class_AdminVar_Meta::newDefault($this->_('Paramétrage CVS'))->bePrivate(), 'SOURCEPASSWORD' => Class_AdminVar_Meta::newDefault($this->_('Paramétrage CVS'))->bePrivate(), 'LOGINTEST' => Class_AdminVar_Meta::newDefault($this->_('Paramétrage CVS'))->bePrivate(), - 'API_URL' => Class_AdminVar_Meta::newDefault($this->_('Url de l\'API'), ['value' => 'http://stream.cvs-mediatheques.com/api/partners.php'])->bePrivate() + 'API_URL' => Class_AdminVar_Meta::newDefault($this->_('Url de l\'API'), ['value' => 'http://stream.cvs-mediatheques.com/api/partners.php'])->bePrivate(), + 'ANONYMOUS_ACCESS' => Class_AdminVar_Meta::newOnOff($this->_('Authoriser l\'accès aux visiteurs anonymes'))->bePrivate() ], ]; } @@ -74,6 +75,9 @@ class Cvs_Config extends Class_DigitalResource_Config { public function hasRightAccess($user) { + if ($this->getAdminVar('ANONYMOUS_ACCESS')) + return true; + if (!$user) return true == (boolean) $this->getAdminVar('LOGINTEST'); diff --git a/library/digital_resources/Cvs/Service.php b/library/digital_resources/Cvs/Service.php index b80e763c6afe178bc05ff1bc812ec2b4d52ff232..d5999866928a4104adb6826c93b7fe132656d785 100644 --- a/library/digital_resources/Cvs/Service.php +++ b/library/digital_resources/Cvs/Service.php @@ -129,14 +129,14 @@ class Cvs_Service { } - protected function _getUser() { + public function getUser() { $user = $this->_user ? $this->_user : Class_Users::getIdentity(); return $user ? $user - : Class_Users::newInstance(['login' => $this->_config->getAdminVar('LOGINTEST')]); + : Class_Users::newInstance(['login' => $this->_var('LOGINTEST')]); } @@ -166,7 +166,7 @@ class Cvs_Service { protected function _getLogin() { - $user = $this->_getUser(); + $user = $this->getUser(); return $user->getIdabon() ? $user->getIdabon() : $user->getLogin(); } @@ -190,7 +190,6 @@ class Cvs_Service { $header->appendChild($xml->createElement($key, $value)); $closure($xml, $params); -// : $this->_searchAction($xml, $params); $this->_appendUser($xml); @@ -226,7 +225,7 @@ class Cvs_Service { protected function _appendUser($xml) { - $user = $this->_getUser(); + $user = $this->getUser(); $user_data = array_filter(['login' => $user->getLogin(), 'nom' => $user->getNom(), diff --git a/library/digital_resources/Cvs/controllers/IndexController.php b/library/digital_resources/Cvs/controllers/IndexController.php index 34b0816281b7509f89cacdab690103523bc63579..ff7df9e7574c361ff4bac488df21f4fa269b00f5 100644 --- a/library/digital_resources/Cvs/controllers/IndexController.php +++ b/library/digital_resources/Cvs/controllers/IndexController.php @@ -20,4 +20,19 @@ */ -class Cvs_Plugin_IndexController extends Class_DigitalResource_Controller {} \ No newline at end of file +class Cvs_Plugin_IndexController extends Class_DigitalResource_Controller { + public function ssoAction() { + if((!$this->_config->getAdminVar('ANONYMOUS_ACCESS'))) + return parent::ssoAction(); + + $user = ($user = Class_Users::getIdentity()) + ? $user + : (new Cvs_Service)->getUser(); + + $url = $this->_config->urlFor($user, $this->_request->getParams()); + + return $url + ? $this->_javascriptRedirectTo($url) + : $this->_redirectToIndex($this->_config->getNotAllowedMessage()); + } +} \ No newline at end of file diff --git a/library/digital_resources/Cvs/tests/CvsTest.php b/library/digital_resources/Cvs/tests/CvsTest.php index 0452cd63a0be29d83afe96a37bc02569f08ebb7e..8bfe3a7058cf19b05535b90d6a9fcf464248a808 100644 --- a/library/digital_resources/Cvs/tests/CvsTest.php +++ b/library/digital_resources/Cvs/tests/CvsTest.php @@ -941,4 +941,102 @@ class CvsServiceWithBorrowerAndLibraryLabelTest extends CvsActivatedTestCase { $this->assertEquals('http://montauban.cvs-mediatheques.com/?ln=musique&con=01255712bd8d68e9e08fe3ff1dadc57a&if=0', $this->_cvs->getSiteAccessFor(Class_Users::getIdentity())); } -} \ No newline at end of file +} + + + + +class CvsSearchWithAnonymousAccessTest extends CvsActivatedTestCase { + + protected $_cvs; + + + public function setUp() { + parent::setUp(); + ZendAfi_Auth::getInstance()->clearIdentity(); + + Class_AdminVar::set('Cvs_ANONYMOUS_ACCESS', 1); + + $this->_cvs = new Cvs_Service; + + $http_client = $this + ->mock() + ->whenCalled('postData') + ->with(Class_AdminVar::get('Cvs_API_URL'), + ['xml' => $this->_expectedXML()]) + ->answers($this->_returnedXML()) + ->beStrict(); + + Class_WebService_Abstract::setHttpClient($http_client); + } + + + protected function _expectedXML() { + $xml = '<?xml version="1.0" encoding="utf-8"?> +<albums> + <header> + <bmid>22223</bmid> + <sourceid>22225</sourceid> + <key>73844495168ecc1aa59ba96c8cea6e82</key> + <time>1369640315</time> + <adhid>cvs_login_test</adhid> + <action>search_document</action> + </header> + <body> + <q><![CDATA[ecole]]></q> + <espace><![CDATA[]]></espace> + <classement><![CDATA[consultes]]></classement> + <page><![CDATA[1]]></page> + <nombre_par_page><![CDATA[5]]></nombre_par_page> + <login>cvs_login_test</login> + </body> +</albums>'; + + $dom = $this->_cvs->createDomDocument(); + $dom->loadXML($xml); + return $this->_cvs->getEncodedXML($dom->saveXML()); + } + + + protected function _returnedXML() { + return file_get_contents(__DIR__ . '/cvs_search_ecole.xml'); + } + + + /** @test */ + public function searchEcoleShouldReturnTitleForFirstNoticeEtreEtAvoir() { + $result = $this->_cvs->find('ecole', 1, 5); + $this->assertEquals('Etre et avoir', + $result->getRecords()[0]->getTitrePrincipal()); + } +} + + + + + +class CvsSsoAnonymousAccessTest extends CvsActivatedTestCase { + public function setUp() { + parent::setUp(); + Class_AdminVar::set('Cvs_ANONYMOUS_ACCESS', 1); + ZendAfi_Auth::getInstance()->clearIdentity(); + } + + + /** @test */ + public function shouldRedirectToCvs() { + $this->dispatch('/opac/modules/cvs', true); + $this->assertXPathContentContains('//script', + 'document.location.href="https://rayonnages', + $this->_response->getBody()); + } + + + /** @test */ + public function shouldRedirectToAlbum() { + $this->dispatch('/opac/modules/cvs?docid=135', true); + $this->assertXPathContentContains('//script', + '#album&docid=135', + $this->_response->getBody()); + } +}