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
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é.
*
* @author Bertrand GAUTHIER <bertrand.gauthier at unicaen.fr>
*/
abstract class UserAbstract extends \Zend\I18n\View\Helper\AbstractTranslatorHelper
abstract class UserAbstract extends AbstractTranslatorHelper
{
protected $authService;
/**
* Constructeur.
*
* @param AuthenticationService $authService
*/
public function __construct(\Zend\Authentication\AuthenticationService $authService = null)
public function __construct(AuthenticationService $authService = null)
{
if (null !== $authService) {
$this->setAuthService($authService);
......@@ -48,7 +54,7 @@ abstract class UserAbstract extends \Zend\I18n\View\Helper\AbstractTranslatorHel
* @param string $preferedKey
* @return mixed
*/
public function getIdentity($preferedKey = 'ldap')
public function getIdentity($preferedKey = null)
{
if (!$this->getAuthService() || !$this->getAuthService()->hasIdentity()) {
return null;
......@@ -57,21 +63,25 @@ abstract class UserAbstract extends \Zend\I18n\View\Helper\AbstractTranslatorHel
$identity = $this->getAuthService()->getIdentity();
if (is_array($identity)) {
$preferedKey = $preferedKey ?: 'ldap';
if (isset($identity[$preferedKey])) {
$identity = $identity[$preferedKey];
}
elseif (isset($identity['ldap'])) {
$identity = $identity['ldap'];
$keys = array('ldap', 'db');
if ($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['db'])) {
$identity = $identity['db'];
$found = null;
foreach ($keys as $key) {
if (isset($identity[$key])) {
$found = $identity[$key];
break;
}
}
else {
throw new \InvalidArgumentException("Données d'identité invalides.");
if (null === $found) {
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;
}
}
}
\ No newline at end of file
......@@ -27,6 +27,12 @@ class UserAbstractTest extends PHPUnit_Framework_TestCase
$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()
{
$this->assertSame($this->authService, $this->helper->getAuthService());
......@@ -61,9 +67,18 @@ class UserAbstractTest extends PHPUnit_Framework_TestCase
public function provideValidArrayIdentity()
{
return array(
array(array('db' => 'Db Identity'), 'Db Identity'),
array(array('ldap' => 'Ldap Identity'), 'Ldap Identity'),
array(array('db' => 'Db Identity', 'ldap' => 'Ldap Identity'), 'Db Identity'),
'db-only' => array(
array('db' => '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
$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()
{
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