Commit b1d90cdf authored by Bertrand Gauthier's avatar Bertrand Gauthier
Browse files

Ajout possibilité de choisir une clé préférée pour l'obtention des données...

Ajout possibilité de choisir une clé préférée pour l'obtention des données d'identité ('ldap' ou 'db').
Tests unitaires.
parent fe826533
<?php <?php
namespace UnicaenAuth\View\Helper; namespace UnicaenAuth\View\Helper;
use Zend\I18n\View\Helper\AbstractTranslatorHelper;
use Zend\Authentication\AuthenticationService;
use Zend\View\Exception\InvalidArgumentException;
/** /**
* Classe mère des aides de vue concernant l'utilisateur connecté. * Classe mère des aides de vue concernant l'utilisateur connecté.
* *
* @author Bertrand GAUTHIER <bertrand.gauthier at unicaen.fr> * @author Bertrand GAUTHIER <bertrand.gauthier at unicaen.fr>
*/ */
abstract class UserAbstract extends \Zend\I18n\View\Helper\AbstractTranslatorHelper abstract class UserAbstract extends AbstractTranslatorHelper
{ {
protected $authService; protected $authService;
/** /**
* Constructeur. * Constructeur.
*
* @param AuthenticationService $authService
*/ */
public function __construct(\Zend\Authentication\AuthenticationService $authService = null) public function __construct(AuthenticationService $authService = null)
{ {
if (null !== $authService) { if (null !== $authService) {
$this->setAuthService($authService); $this->setAuthService($authService);
...@@ -48,7 +54,7 @@ abstract class UserAbstract extends \Zend\I18n\View\Helper\AbstractTranslatorHel ...@@ -48,7 +54,7 @@ abstract class UserAbstract extends \Zend\I18n\View\Helper\AbstractTranslatorHel
* @param string $preferedKey * @param string $preferedKey
* @return mixed * @return mixed
*/ */
public function getIdentity($preferedKey = 'ldap') public function getIdentity($preferedKey = null)
{ {
if (!$this->getAuthService() || !$this->getAuthService()->hasIdentity()) { if (!$this->getAuthService() || !$this->getAuthService()->hasIdentity()) {
return null; return null;
...@@ -57,19 +63,23 @@ abstract class UserAbstract extends \Zend\I18n\View\Helper\AbstractTranslatorHel ...@@ -57,19 +63,23 @@ abstract class UserAbstract extends \Zend\I18n\View\Helper\AbstractTranslatorHel
$identity = $this->getAuthService()->getIdentity(); $identity = $this->getAuthService()->getIdentity();
if (is_array($identity)) { if (is_array($identity)) {
$preferedKey = $preferedKey ?: 'ldap'; $keys = array('ldap', 'db');
if (isset($identity[$preferedKey])) { if ($preferedKey) {
$identity = $identity[$preferedKey]; // on met la clé spécifiée en tête de liste
$keys = array_merge(($tmp = array($preferedKey)), array_diff($keys, $tmp));
} }
elseif (isset($identity['ldap'])) { $found = null;
$identity = $identity['ldap']; foreach ($keys as $key) {
if (isset($identity[$key])) {
$found = $identity[$key];
break;
} }
elseif (isset($identity['db'])) {
$identity = $identity['db'];
} }
else { if (null === $found) {
throw new \InvalidArgumentException("Données d'identité invalides."); throw new InvalidArgumentException(
"Aucune des clés suivantes n'a été trouvée dans les données d'identité: " . implode(", ", $keys) . ".");
} }
$identity = $found;
} }
return $identity; return $identity;
......
...@@ -27,6 +27,12 @@ class UserAbstractTest extends PHPUnit_Framework_TestCase ...@@ -27,6 +27,12 @@ class UserAbstractTest extends PHPUnit_Framework_TestCase
$this->helper->setAuthService($this->authService); $this->helper->setAuthService($this->authService);
} }
public function testCanConstructWithAuthServiceSpecified()
{
$helper = $this->getMockForAbstractClass('UnicaenAuth\View\Helper\UserAbstract', array($this->authService));
$this->assertSame($this->authService, $helper->getAuthService());
}
public function testCanSetAuthService() public function testCanSetAuthService()
{ {
$this->assertSame($this->authService, $this->helper->getAuthService()); $this->assertSame($this->authService, $this->helper->getAuthService());
...@@ -61,9 +67,18 @@ class UserAbstractTest extends PHPUnit_Framework_TestCase ...@@ -61,9 +67,18 @@ class UserAbstractTest extends PHPUnit_Framework_TestCase
public function provideValidArrayIdentity() public function provideValidArrayIdentity()
{ {
return array( return array(
array(array('db' => 'Db Identity'), 'Db Identity'), 'db-only' => array(
array(array('ldap' => 'Ldap Identity'), 'Ldap Identity'), array('db' => 'Db Identity'),
array(array('db' => 'Db Identity', 'ldap' => 'Ldap Identity'), 'Db Identity'), 'Db Identity',
),
'ldap-only' => array(
array('ldap' => 'Ldap Identity'),
'Ldap Identity',
),
'db-ldap' => array(
array('db' => 'Db Identity', 'ldap' => 'Ldap Identity'),
'Ldap Identity',
),
); );
} }
...@@ -83,6 +98,54 @@ class UserAbstractTest extends PHPUnit_Framework_TestCase ...@@ -83,6 +98,54 @@ class UserAbstractTest extends PHPUnit_Framework_TestCase
$this->assertEquals($expected, $this->helper->getIdentity()); $this->assertEquals($expected, $this->helper->getIdentity());
} }
public function provideValidArrayIdentityWithPreferedKey()
{
return array(
'db-only-ldap-prefered' => array(
array('db' => 'Db Identity'),
'ldap', // clé absente
'Db Identity',
),
'ldap-only-db-prefered' => array(
array('ldap' => 'Ldap Identity'),
'db', // clé absente
'Ldap Identity',
),
'db-ldap-none-prefered' => array(
array('db' => 'Db Identity', 'ldap' => 'Ldap Identity'),
null, // équivaut à 'ldap'
'Ldap Identity',
),
'db-ldap-ldap-prefered' => array(
array('db' => 'Db Identity', 'ldap' => 'Ldap Identity'),
'ldap',
'Ldap Identity',
),
'db-ldap-db-prefered' => array(
array('db' => 'Db Identity', 'ldap' => 'Ldap Identity'),
'db',
'Db Identity',
),
);
}
/**
* @dataProvider provideValidArrayIdentityWithPreferedKey
* @param array $identity
* @param string $preferedKey
* @param string $expected
*/
public function testGettingIdentityReturnsAuthServiceIdentityFromValidArrayIdentityWithPreferedKey($identity, $preferedKey, $expected)
{
$this->authService->expects($this->once())
->method('hasIdentity')
->will($this->returnValue(true));
$this->authService->expects($this->once())
->method('getIdentity')
->will($this->returnValue($identity));
$this->assertEquals($expected, $this->helper->getIdentity($preferedKey));
}
public function provideInvalidArrayIdentity() public function provideInvalidArrayIdentity()
{ {
return array( return array(
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment