Commit 0395f002 authored by Bertrand Gauthier's avatar Bertrand Gauthier
Browse files

Corrections pour passer à ZF3.

parent e0827096
...@@ -90,7 +90,7 @@ class Module implements AutoloaderProviderInterface, ConfigProviderInterface, Se ...@@ -90,7 +90,7 @@ class Module implements AutoloaderProviderInterface, ConfigProviderInterface, Se
*/ */
private function reconfigureRoutesForAuth(ServiceLocatorInterface $sl) private function reconfigureRoutesForAuth(ServiceLocatorInterface $sl)
{ {
/* @var $router \Zend\Mvc\Router\Http\TreeRouteStack */ /* @var $router \Zend\Router\Http\TreeRouteStack */
$router = $sl->get('router'); $router = $sl->get('router');
// si l'auth CAS est activée, modif de la route de connexion pour zapper le formulaire d'auth maison. // si l'auth CAS est activée, modif de la route de connexion pour zapper le formulaire d'auth maison.
......
<?php <?php
use UnicaenAuth\Authentication\Storage\DbFactory;
use UnicaenAuth\Authentication\Storage\LdapFactory;
use UnicaenAuth\Authentication\Storage\ShibFactory;
use UnicaenAuth\Controller\AuthControllerFactory; use UnicaenAuth\Controller\AuthControllerFactory;
use UnicaenAuth\Controller\DroitsControllerFactory;
use UnicaenAuth\Controller\UtilisateurControllerFactory;
use UnicaenAuth\Form\Droits\RoleFormFactory;
use UnicaenAuth\Guard\PrivilegeControllerFactory;
use UnicaenAuth\Guard\PrivilegeRouteFactory;
use UnicaenAuth\Service\ShibService; use UnicaenAuth\Service\ShibService;
use UnicaenAuth\Service\ShibServiceFactory; use UnicaenAuth\Service\ShibServiceFactory;
use UnicaenAuth\Service\UserContextFactory; use UnicaenAuth\Service\UserContextFactory;
use UnicaenAuth\Service\UserFactory;
use UnicaenAuth\Service\UserMapperFactory; use UnicaenAuth\Service\UserMapperFactory;
use UnicaenAuth\View\Helper\LdapConnectViewHelperFactory; use UnicaenAuth\View\Helper\LdapConnectViewHelperFactory;
use UnicaenAuth\View\Helper\LocalConnectViewHelperFactory; use UnicaenAuth\View\Helper\LocalConnectViewHelperFactory;
...@@ -420,11 +429,7 @@ return [ ...@@ -420,11 +429,7 @@ return [
'authUserContext' => 'UnicaenAuth\Service\UserContext', // pour la compatibilité 'authUserContext' => 'UnicaenAuth\Service\UserContext', // pour la compatibilité
], ],
'invokables' => [ 'invokables' => [
'UnicaenAuth\Authentication\Storage\Db' => 'UnicaenAuth\Authentication\Storage\Db',
'UnicaenAuth\Authentication\Storage\Ldap' => 'UnicaenAuth\Authentication\Storage\Ldap',
'UnicaenAuth\Authentication\Storage\Shib' => 'UnicaenAuth\Authentication\Storage\Shib',
'UnicaenAuth\View\RedirectionStrategy' => 'UnicaenAuth\View\RedirectionStrategy', 'UnicaenAuth\View\RedirectionStrategy' => 'UnicaenAuth\View\RedirectionStrategy',
'UnicaenAuth\Service\User' => 'UnicaenAuth\Service\User',
'UnicaenAuth\Service\CategoriePrivilege' => 'UnicaenAuth\Service\CategoriePrivilegeService', 'UnicaenAuth\Service\CategoriePrivilege' => 'UnicaenAuth\Service\CategoriePrivilegeService',
], ],
'abstract_factories' => [ 'abstract_factories' => [
...@@ -449,6 +454,12 @@ return [ ...@@ -449,6 +454,12 @@ return [
'UnicaenAuth\Service\UserContext' => UserContextFactory::class, 'UnicaenAuth\Service\UserContext' => UserContextFactory::class,
'zfcuser_user_mapper' => UserMapperFactory::class, 'zfcuser_user_mapper' => UserMapperFactory::class,
'MouchardCompleterAuth' => 'UnicaenAuth\Mouchard\MouchardCompleterAuthFactory', 'MouchardCompleterAuth' => 'UnicaenAuth\Mouchard\MouchardCompleterAuthFactory',
'UnicaenAuth\Authentication\Storage\Db' => DbFactory::class,
'UnicaenAuth\Authentication\Storage\Ldap' => LdapFactory::class,
'UnicaenAuth\Authentication\Storage\Shib' => ShibFactory::class,
'UnicaenAuth\Service\User' => UserFactory::class,
'UnicaenAuth\Guard\PrivilegeController' => PrivilegeControllerFactory::class,
'UnicaenAuth\Guard\PrivilegeRoute' => PrivilegeRouteFactory::class,
], ],
'shared' => [ 'shared' => [
'MouchardCompleterAuth' => false, 'MouchardCompleterAuth' => false,
...@@ -460,17 +471,19 @@ return [ ...@@ -460,17 +471,19 @@ return [
'controllers' => [ 'controllers' => [
'invokables' => [ 'invokables' => [
'UnicaenAuth\Controller\Utilisateur' => 'UnicaenAuth\Controller\UtilisateurController',
'UnicaenAuth\Controller\Droits' => 'UnicaenAuth\Controller\DroitsController',
], ],
'factories' => [ 'factories' => [
'UnicaenAuth\Controller\Auth' => AuthControllerFactory::class, 'UnicaenAuth\Controller\Auth' => AuthControllerFactory::class,
'UnicaenAuth\Controller\Utilisateur' => UtilisateurControllerFactory::class,
'UnicaenAuth\Controller\Droits' => DroitsControllerFactory::class,
], ],
], ],
'form_elements' => [ 'form_elements' => [
'invokables' => [ 'invokables' => [
'UnicaenAuth\Form\Droits\Role' => 'UnicaenAuth\Form\Droits\RoleForm', ],
'factories' => [
'UnicaenAuth\Form\Droits\Role' => RoleFormFactory::class,
], ],
], ],
......
...@@ -3,16 +3,15 @@ ...@@ -3,16 +3,15 @@
namespace UnicaenAuth\Assertion; namespace UnicaenAuth\Assertion;
use BjyAuthorize\Service\Authorize; use BjyAuthorize\Service\Authorize;
use UnicaenApp\ServiceManager\ServiceLocatorAwareInterface;
use UnicaenApp\ServiceManager\ServiceLocatorAwareTrait;
use UnicaenAuth\Service\Traits\UserContextServiceAwareTrait; use UnicaenAuth\Service\Traits\UserContextServiceAwareTrait;
use Zend\Mvc\Application;
use Zend\Mvc\Controller\Plugin\FlashMessenger;
use Zend\Mvc\MvcEvent; use Zend\Mvc\MvcEvent;
use Zend\Mvc\Plugin\FlashMessenger\FlashMessenger;
use Zend\Permissions\Acl\Acl; use Zend\Permissions\Acl\Acl;
use Zend\Permissions\Acl\Assertion\AssertionInterface; use Zend\Permissions\Acl\Assertion\AssertionInterface;
use Zend\Permissions\Acl\Resource\ResourceInterface; use Zend\Permissions\Acl\Resource\ResourceInterface;
use Zend\Permissions\Acl\Role\RoleInterface; use Zend\Permissions\Acl\Role\RoleInterface;
use UnicaenApp\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorAwareTrait;
/** /**
* Description of AbstractAssertion * Description of AbstractAssertion
...@@ -296,44 +295,63 @@ abstract class AbstractAssertion implements AssertionInterface, ServiceLocatorAw ...@@ -296,44 +295,63 @@ abstract class AbstractAssertion implements AssertionInterface, ServiceLocatorAw
return true; return true;
} }
/**
* @var MvcEvent
*/
private $mvcEvent;
/**
* @param MvcEvent $mvcEvent
*/
public function setMvcEvent(MvcEvent $mvcEvent)
{
$this->mvcEvent = $mvcEvent;
}
/** /**
* @return MvcEvent * @return MvcEvent
*/ */
protected function getMvcEvent() protected function getMvcEvent()
{ {
$application = $this->getServiceLocator()->get('Application'); return $this->mvcEvent;
/* @var $application Application */
return $application->getMvcEvent();
} }
/**
* @var Authorize
*/
private $serviceAuthorize;
/**
* @param Authorize $serviceAuthorize
*/
public function setServiceAuthorize(Authorize $serviceAuthorize)
{
$this->serviceAuthorize = $serviceAuthorize;
}
/** /**
* @return Authorize * @return Authorize
*/ */
private function getServiceAuthorize() private function getServiceAuthorize()
{ {
$serviceAuthorize = $this->getServiceLocator()->get('BjyAuthorize\Service\Authorize'); return $this->serviceAuthorize;
/* @var $serviceAuthorize Authorize */
return $serviceAuthorize;
} }
/**
* @param FlashMessenger $fm
* @deprecated Merci d'abandonner cette méthode : SoC violation !
*/
public function setFlashMessenger(FlashMessenger $fm)
{
$this->fm = $fm;
}
/** /**
* @return FlashMessenger * @return FlashMessenger
* @deprecated Merci d'abandonner cette méthode : SoC violation !
*/ */
protected function flashMessenger() protected function flashMessenger()
{ {
if (!$this->fm){
$this->fm = $this->getServiceLocator()->get('controllerpluginmanager')->get('flashmessenger');
}
return $this->fm; return $this->fm;
} }
} }
\ No newline at end of file
...@@ -2,11 +2,16 @@ ...@@ -2,11 +2,16 @@
namespace UnicaenAuth\Authentication\Adapter; namespace UnicaenAuth\Authentication\Adapter;
use Interop\Container\ContainerInterface;
use UnicaenApp\Exception\LogicException; use UnicaenApp\Exception\LogicException;
use UnicaenAuth\Options\ModuleOptions;
use UnicaenAuth\Service\User;
use Zend\EventManager\EventManager; use Zend\EventManager\EventManager;
use Zend\EventManager\EventManagerAwareInterface; use Zend\EventManager\EventManagerAwareInterface;
use Zend\Router\Http\TreeRouteStack;
use Zend\ServiceManager\AbstractFactoryInterface; use Zend\ServiceManager\AbstractFactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface; use Zend\ServiceManager\ServiceLocatorInterface;
use UnicaenApp\Mapper\Ldap\People as LdapPeopleMapper;
/** /**
* Description of AbstractFactory * Description of AbstractFactory
...@@ -15,28 +20,22 @@ use Zend\ServiceManager\ServiceLocatorInterface; ...@@ -15,28 +20,22 @@ use Zend\ServiceManager\ServiceLocatorInterface;
*/ */
class AbstractFactory implements AbstractFactoryInterface class AbstractFactory implements AbstractFactoryInterface
{ {
/**
* Determine if we can create a service with name
*
* @param ServiceLocatorInterface $serviceLocator
* @param $name
* @param $requestedName
* @return bool
*/
public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName) public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
{ {
return strpos($requestedName, __NAMESPACE__) === 0 && class_exists($requestedName); return $this->canCreate($serviceLocator, $requestedName);
} }
/**
* Create service with name
*
* @param ServiceLocatorInterface $serviceLocator
* @param $name
* @param $requestedName
* @return \ZfcUser\Authentication\Adapter\AbstractAdapter
*/
public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName) public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
{
return $this->__invoke($serviceLocator, $requestedName);
}
public function canCreate(ContainerInterface $container, $requestedName)
{
return strpos($requestedName, __NAMESPACE__) === 0 && class_exists($requestedName);
}
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{ {
switch ($requestedName) { switch ($requestedName) {
case __NAMESPACE__ . '\Ldap': case __NAMESPACE__ . '\Ldap':
...@@ -59,14 +58,69 @@ class AbstractFactory implements AbstractFactoryInterface ...@@ -59,14 +58,69 @@ class AbstractFactory implements AbstractFactoryInterface
break; break;
} }
$this->injectDependencies($adapter, $container);
if ($adapter instanceof EventManagerAwareInterface) { if ($adapter instanceof EventManagerAwareInterface) {
/** @var EventManager $eventManager */ /** @var EventManager $eventManager */
$eventManager = $serviceLocator->get('event_manager'); $eventManager = $container->get('EventManager');
$adapter->setEventManager($eventManager); $adapter->setEventManager($eventManager);
$userService = $serviceLocator->get('unicaen-auth_user_service'); /* @var $userService \UnicaenAuth\Service\User */ $userService = $container->get('unicaen-auth_user_service'); /* @var $userService \UnicaenAuth\Service\User */
$eventManager->attach('userAuthenticated', [$userService, 'userAuthenticated'], 100); $eventManager->attach('userAuthenticated', [$userService, 'userAuthenticated'], 100);
} }
return $adapter; return $adapter;
} }
/**
* @param Ldap|Db|Cas $adapter
* @param ContainerInterface $container
*/
private function injectDependencies($adapter, ContainerInterface $container)
{
switch (true) {
case $adapter instanceof Ldap:
/** @var User $userService */
$userService = $container->get('unicaen-auth_user_service');
$adapter->setUserService($userService);
/** @var LdapPeopleMapper $ldapPeopleMapper */
$ldapPeopleMapper = $container->get('ldap_people_mapper');
$adapter->setLdapPeopleMapper($ldapPeopleMapper);
$options = array_merge(
$container->get('zfcuser_module_options')->toArray(),
$container->get('unicaen-auth_module_options')->toArray());
$adapter->setOptions(new ModuleOptions($options));
/** @var \UnicaenApp\Options\ModuleOptions $appModuleOptions */
$appModuleOptions = $container->get('unicaen-app_module_options');
$adapter->setAppModuleOptions($appModuleOptions);
break;
case $adapter instanceof Cas:
/** @var User $userService */
$userService = $container->get('unicaen-auth_user_service');
$adapter->setUserService($userService);
/** @var TreeRouteStack $router */
$router = $container->get('router');
$adapter->setRouter($router);
$options = array_merge(
$container->get('zfcuser_module_options')->toArray(),
$container->get('unicaen-auth_module_options')->toArray());
$adapter->setOptions(new ModuleOptions($options));
/** @var LdapPeopleMapper $ldapPeopleMapper */
$ldapPeopleMapper = $container->get('ldap_people_mapper');
$adapter->setLdapPeopleMapper($ldapPeopleMapper);
break;
default:
break;
}
}
} }
\ No newline at end of file
...@@ -4,7 +4,6 @@ namespace UnicaenAuth\Authentication\Adapter; ...@@ -4,7 +4,6 @@ namespace UnicaenAuth\Authentication\Adapter;
use phpCAS; use phpCAS;
use UnicaenApp\Mapper\Ldap\People as LdapPeopleMapper; use UnicaenApp\Mapper\Ldap\People as LdapPeopleMapper;
use UnicaenApp\ServiceManager\ServiceLocatorAwareInterface;
use UnicaenAuth\Options\ModuleOptions; use UnicaenAuth\Options\ModuleOptions;
use UnicaenAuth\Service\User; use UnicaenAuth\Service\User;
use Zend\Authentication\Exception\UnexpectedValueException; use Zend\Authentication\Exception\UnexpectedValueException;
...@@ -12,8 +11,7 @@ use Zend\Authentication\Result as AuthenticationResult; ...@@ -12,8 +11,7 @@ use Zend\Authentication\Result as AuthenticationResult;
use Zend\EventManager\EventManager; use Zend\EventManager\EventManager;
use Zend\EventManager\EventManagerAwareInterface; use Zend\EventManager\EventManagerAwareInterface;
use Zend\EventManager\EventManagerInterface; use Zend\EventManager\EventManagerInterface;
use Zend\Mvc\Router\Http\TreeRouteStack; use Zend\Router\Http\TreeRouteStack;
use Zend\ServiceManager\ServiceLocatorAwareTrait;
use ZfcUser\Authentication\Adapter\AbstractAdapter; use ZfcUser\Authentication\Adapter\AbstractAdapter;
use ZfcUser\Authentication\Adapter\AdapterChainEvent as AuthEvent; use ZfcUser\Authentication\Adapter\AdapterChainEvent as AuthEvent;
use ZfcUser\Authentication\Adapter\ChainableAdapter; use ZfcUser\Authentication\Adapter\ChainableAdapter;
...@@ -23,10 +21,8 @@ use ZfcUser\Authentication\Adapter\ChainableAdapter; ...@@ -23,10 +21,8 @@ use ZfcUser\Authentication\Adapter\ChainableAdapter;
* *
* @author Bertrand GAUTHIER <bertrand.gauthier@unicaen.fr> * @author Bertrand GAUTHIER <bertrand.gauthier@unicaen.fr>
*/ */
class Cas extends AbstractAdapter implements EventManagerAwareInterface, ServiceLocatorAwareInterface class Cas extends AbstractAdapter implements EventManagerAwareInterface
{ {
use ServiceLocatorAwareTrait;
/** /**
* @var EventManager * @var EventManager
*/ */
...@@ -52,15 +48,40 @@ class Cas extends AbstractAdapter implements EventManagerAwareInterface, Service ...@@ -52,15 +48,40 @@ class Cas extends AbstractAdapter implements EventManagerAwareInterface, Service
*/ */
protected $ldapPeopleMapper; protected $ldapPeopleMapper;
/**
* @var User
*/
private $userService;
/**
* @param User $userService
*/
public function setUserService(User $userService)
{
$this->userService = $userService;
}
/**
* @var TreeRouteStack
*/
private $router;
/**
* @param TreeRouteStack $router
*/
public function setRouter(TreeRouteStack $router)
{
$this->router = $router;
}
/** /**
* Réalise l'authentification. * Réalise l'authentification.
* *
* @param AuthEvent $e * @param \Zend\EventManager\Event|AuthEvent $e
* @return boolean
* @throws UnexpectedValueException * @throws UnexpectedValueException
* @see ChainableAdapter * @see ChainableAdapter
*/ */
public function authenticate(AuthEvent $e) public function authenticate(\Zend\EventManager\Event $e)
{ {
// if ($e->getIdentity()) { // if ($e->getIdentity()) {
// return; // return;
...@@ -102,8 +123,7 @@ class Cas extends AbstractAdapter implements EventManagerAwareInterface, Service ...@@ -102,8 +123,7 @@ class Cas extends AbstractAdapter implements EventManagerAwareInterface, Service
$ldapPeople = $this->getLdapPeopleMapper()->findOneByUsername($identity); $ldapPeople = $this->getLdapPeopleMapper()->findOneByUsername($identity);
/* @var $userService User */ /* @var $userService User */
$userService = $this->serviceLocator->get('unicaen-auth_user_service'); $this->userService->userAuthenticated($ldapPeople);
$userService->userAuthenticated($ldapPeople);
} }
/** /**
...@@ -117,8 +137,7 @@ class Cas extends AbstractAdapter implements EventManagerAwareInterface, Service ...@@ -117,8 +137,7 @@ class Cas extends AbstractAdapter implements EventManagerAwareInterface, Service
return; // NB: l'authentification CAS est désactivée ssi le tableau des options est vide return; // NB: l'authentification CAS est désactivée ssi le tableau des options est vide
} }
$router = $this->serviceLocator->get('router'); /* @var $router TreeRouteStack */ $returnUrl = $this->router->getRequestUri()->setPath($this->router->getBaseUrl())->toString();
$returnUrl = $router->getRequestUri()->setPath($router->getBaseUrl())->toString();
$this->getCasClient()->logoutWithRedirectService($returnUrl); $this->getCasClient()->logoutWithRedirectService($returnUrl);
} }
...@@ -186,12 +205,12 @@ class Cas extends AbstractAdapter implements EventManagerAwareInterface, Service ...@@ -186,12 +205,12 @@ class Cas extends AbstractAdapter implements EventManagerAwareInterface, Service
*/ */
public function getOptions() public function getOptions()
{ {
if (!$this->options instanceof ModuleOptions) { // if (!$this->options instanceof ModuleOptions) {
$options = array_merge( // $options = array_merge(
$this->serviceLocator->get('zfcuser_module_options')->toArray(), // $this->serviceLocator->get('zfcuser_module_options')->toArray(),
$this->serviceLocator->get('unicaen-auth_module_options')->toArray()); // $this->serviceLocator->get('unicaen-auth_module_options')->toArray());
$this->setOptions(new ModuleOptions($options)); // $this->setOptions(new ModuleOptions($options));
} // }
return $this->options; return $this->options;
} }
...@@ -202,9 +221,6 @@ class Cas extends AbstractAdapter implements EventManagerAwareInterface, Service ...@@ -202,9 +221,6 @@ class Cas extends AbstractAdapter implements EventManagerAwareInterface, Service
*/ */
public function getLdapPeopleMapper() public function getLdapPeopleMapper()
{ {
if (null === $this->ldapPeopleMapper) {
$this->ldapPeopleMapper = $this->serviceLocator->get('ldap_people_mapper');
}
return $this->ldapPeopleMapper; return $this->ldapPeopleMapper;
} }
...@@ -217,6 +233,7 @@ class Cas extends AbstractAdapter implements EventManagerAwareInterface, Service ...@@ -217,6 +233,7 @@ class Cas extends AbstractAdapter implements EventManagerAwareInterface, Service
public function setLdapPeopleMapper(LdapPeopleMapper $mapper) public function setLdapPeopleMapper(LdapPeopleMapper $mapper)
{ {
$this->ldapPeopleMapper = $mapper; $this->ldapPeopleMapper = $mapper;
return $this; return $this;
} }
......
...@@ -2,11 +2,11 @@ ...@@ -2,11 +2,11 @@
namespace UnicaenAuth\Authentication\Adapter; namespace UnicaenAuth\Authentication\Adapter;
use Interop\Container\ContainerInterface;
use UnicaenApp\ServiceManager\ServiceLocatorAwareInterface; use UnicaenApp\ServiceManager\ServiceLocatorAwareInterface;
use UnicaenApp\ServiceManager\ServiceLocatorAwareTrait;
use UnicaenAuth\Options\ModuleOptions; use UnicaenAuth\Options\ModuleOptions;
use Zend\ServiceManager\Exception\ServiceNotFoundException; use Zend\ServiceManager\Exception\ServiceNotFoundException;
use Zend\ServiceManager\ServiceLocatorAwareTrait;
use Zend\ServiceManager\ServiceLocatorInterface;
use ZfcUser\Authentication\Adapter\AdapterChainEvent as AuthEvent; use ZfcUser\Authentication\Adapter\AdapterChainEvent as AuthEvent;
use ZfcUser\Options\AuthenticationOptionsInterface; use ZfcUser\Options\AuthenticationOptionsInterface;
...@@ -25,11 +25,11 @@ class Db extends \ZfcUser\Authentication\Adapter\Db implements ServiceLocatorAwa ...@@ -25,11 +25,11 @@ class Db extends \ZfcUser\Authentication\Adapter\Db implements ServiceLocatorAwa
/** /**
* Set service locator * Set service locator
* *
* @param ServiceLocatorInterface $serviceLocator * @param ContainerInterface $serviceLocator
* @return self * @return self
* @deprecated Abandonnez l'injection du service locator, svp * @deprecated Abandonnez l'injection du service locator, svp
*/ */