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
*/
private function reconfigureRoutesForAuth(ServiceLocatorInterface $sl)
{
/* @var $router \Zend\Mvc\Router\Http\TreeRouteStack */
/* @var $router \Zend\Router\Http\TreeRouteStack */
$router = $sl->get('router');
// si l'auth CAS est activée, modif de la route de connexion pour zapper le formulaire d'auth maison.
......
<?php
use UnicaenAuth\Authentication\Storage\DbFactory;
use UnicaenAuth\Authentication\Storage\LdapFactory;
use UnicaenAuth\Authentication\Storage\ShibFactory;
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\ShibServiceFactory;
use UnicaenAuth\Service\UserContextFactory;
use UnicaenAuth\Service\UserFactory;
use UnicaenAuth\Service\UserMapperFactory;
use UnicaenAuth\View\Helper\LdapConnectViewHelperFactory;
use UnicaenAuth\View\Helper\LocalConnectViewHelperFactory;
......@@ -420,11 +429,7 @@ return [
'authUserContext' => 'UnicaenAuth\Service\UserContext', // pour la compatibilité
],
'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\Service\User' => 'UnicaenAuth\Service\User',
'UnicaenAuth\Service\CategoriePrivilege' => 'UnicaenAuth\Service\CategoriePrivilegeService',
],
'abstract_factories' => [
......@@ -449,6 +454,12 @@ return [
'UnicaenAuth\Service\UserContext' => UserContextFactory::class,
'zfcuser_user_mapper' => UserMapperFactory::class,
'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' => [
'MouchardCompleterAuth' => false,
......@@ -460,17 +471,19 @@ return [
'controllers' => [
'invokables' => [
'UnicaenAuth\Controller\Utilisateur' => 'UnicaenAuth\Controller\UtilisateurController',
'UnicaenAuth\Controller\Droits' => 'UnicaenAuth\Controller\DroitsController',
],
'factories' => [
'UnicaenAuth\Controller\Auth' => AuthControllerFactory::class,
'UnicaenAuth\Controller\Utilisateur' => UtilisateurControllerFactory::class,
'UnicaenAuth\Controller\Droits' => DroitsControllerFactory::class,
],
],
'form_elements' => [
'invokables' => [
'UnicaenAuth\Form\Droits\Role' => 'UnicaenAuth\Form\Droits\RoleForm',
],
'factories' => [
'UnicaenAuth\Form\Droits\Role' => RoleFormFactory::class,
],
],
......
......@@ -3,16 +3,15 @@
namespace UnicaenAuth\Assertion;
use BjyAuthorize\Service\Authorize;
use UnicaenApp\ServiceManager\ServiceLocatorAwareInterface;
use UnicaenApp\ServiceManager\ServiceLocatorAwareTrait;
use UnicaenAuth\Service\Traits\UserContextServiceAwareTrait;
use Zend\Mvc\Application;
use Zend\Mvc\Controller\Plugin\FlashMessenger;
use Zend\Mvc\MvcEvent;
use Zend\Mvc\Plugin\FlashMessenger\FlashMessenger;
use Zend\Permissions\Acl\Acl;
use Zend\Permissions\Acl\Assertion\AssertionInterface;
use Zend\Permissions\Acl\Resource\ResourceInterface;
use Zend\Permissions\Acl\Role\RoleInterface;
use UnicaenApp\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorAwareTrait;
/**
* Description of AbstractAssertion
......@@ -296,44 +295,63 @@ abstract class AbstractAssertion implements AssertionInterface, ServiceLocatorAw
return true;
}
/**
* @var MvcEvent
*/
private $mvcEvent;
/**
* @param MvcEvent $mvcEvent
*/
public function setMvcEvent(MvcEvent $mvcEvent)
{
$this->mvcEvent = $mvcEvent;
}
/**
* @return MvcEvent
*/
protected function getMvcEvent()
{
$application = $this->getServiceLocator()->get('Application');
/* @var $application Application */
return $application->getMvcEvent();
return $this->mvcEvent;
}
/**
* @var Authorize
*/
private $serviceAuthorize;
/**
* @param Authorize $serviceAuthorize
*/
public function setServiceAuthorize(Authorize $serviceAuthorize)
{
$this->serviceAuthorize = $serviceAuthorize;
}
/**
* @return Authorize
*/
private function getServiceAuthorize()
{
$serviceAuthorize = $this->getServiceLocator()->get('BjyAuthorize\Service\Authorize');
/* @var $serviceAuthorize Authorize */
return $serviceAuthorize;
return $this->serviceAuthorize;
}
/**
* @param FlashMessenger $fm
* @deprecated Merci d'abandonner cette méthode : SoC violation !
*/
public function setFlashMessenger(FlashMessenger $fm)
{
$this->fm = $fm;
}
/**
* @return FlashMessenger
* @deprecated Merci d'abandonner cette méthode : SoC violation !
*/
protected function flashMessenger()
{
if (!$this->fm){
$this->fm = $this->getServiceLocator()->get('controllerpluginmanager')->get('flashmessenger');
}
return $this->fm;
}
}
\ No newline at end of file
......@@ -2,11 +2,16 @@
namespace UnicaenAuth\Authentication\Adapter;
use Interop\Container\ContainerInterface;
use UnicaenApp\Exception\LogicException;
use UnicaenAuth\Options\ModuleOptions;
use UnicaenAuth\Service\User;
use Zend\EventManager\EventManager;
use Zend\EventManager\EventManagerAwareInterface;
use Zend\Router\Http\TreeRouteStack;
use Zend\ServiceManager\AbstractFactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use UnicaenApp\Mapper\Ldap\People as LdapPeopleMapper;
/**
* Description of AbstractFactory
......@@ -15,28 +20,22 @@ use Zend\ServiceManager\ServiceLocatorInterface;
*/
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)
{
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)
{
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) {
case __NAMESPACE__ . '\Ldap':
......@@ -59,14 +58,69 @@ class AbstractFactory implements AbstractFactoryInterface
break;
}
$this->injectDependencies($adapter, $container);
if ($adapter instanceof EventManagerAwareInterface) {
/** @var EventManager $eventManager */
$eventManager = $serviceLocator->get('event_manager');
$eventManager = $container->get('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);
}
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;
use phpCAS;
use UnicaenApp\Mapper\Ldap\People as LdapPeopleMapper;
use UnicaenApp\ServiceManager\ServiceLocatorAwareInterface;
use UnicaenAuth\Options\ModuleOptions;
use UnicaenAuth\Service\User;
use Zend\Authentication\Exception\UnexpectedValueException;
......@@ -12,8 +11,7 @@ use Zend\Authentication\Result as AuthenticationResult;
use Zend\EventManager\EventManager;
use Zend\EventManager\EventManagerAwareInterface;
use Zend\EventManager\EventManagerInterface;
use Zend\Mvc\Router\Http\TreeRouteStack;
use Zend\ServiceManager\ServiceLocatorAwareTrait;
use Zend\Router\Http\TreeRouteStack;
use ZfcUser\Authentication\Adapter\AbstractAdapter;
use ZfcUser\Authentication\Adapter\AdapterChainEvent as AuthEvent;
use ZfcUser\Authentication\Adapter\ChainableAdapter;
......@@ -23,10 +21,8 @@ use ZfcUser\Authentication\Adapter\ChainableAdapter;
*
* @author Bertrand GAUTHIER <bertrand.gauthier@unicaen.fr>
*/
class Cas extends AbstractAdapter implements EventManagerAwareInterface, ServiceLocatorAwareInterface
class Cas extends AbstractAdapter implements EventManagerAwareInterface
{
use ServiceLocatorAwareTrait;
/**
* @var EventManager
*/
......@@ -52,15 +48,40 @@ class Cas extends AbstractAdapter implements EventManagerAwareInterface, Service
*/
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.
*
* @param AuthEvent $e
* @return boolean
* @param \Zend\EventManager\Event|AuthEvent $e
* @throws UnexpectedValueException
* @see ChainableAdapter
*/
public function authenticate(AuthEvent $e)
public function authenticate(\Zend\EventManager\Event $e)
{
// if ($e->getIdentity()) {
// return;
......@@ -102,8 +123,7 @@ class Cas extends AbstractAdapter implements EventManagerAwareInterface, Service
$ldapPeople = $this->getLdapPeopleMapper()->findOneByUsername($identity);
/* @var $userService User */
$userService = $this->serviceLocator->get('unicaen-auth_user_service');
$userService->userAuthenticated($ldapPeople);
$this->userService->userAuthenticated($ldapPeople);
}
/**
......@@ -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
}
$router = $this->serviceLocator->get('router'); /* @var $router TreeRouteStack */
$returnUrl = $router->getRequestUri()->setPath($router->getBaseUrl())->toString();
$returnUrl = $this->router->getRequestUri()->setPath($this->router->getBaseUrl())->toString();
$this->getCasClient()->logoutWithRedirectService($returnUrl);
}
......@@ -186,12 +205,12 @@ class Cas extends AbstractAdapter implements EventManagerAwareInterface, Service
*/
public function getOptions()
{
if (!$this->options instanceof ModuleOptions) {
$options = array_merge(
$this->serviceLocator->get('zfcuser_module_options')->toArray(),
$this->serviceLocator->get('unicaen-auth_module_options')->toArray());
$this->setOptions(new ModuleOptions($options));
}
// if (!$this->options instanceof ModuleOptions) {
// $options = array_merge(
// $this->serviceLocator->get('zfcuser_module_options')->toArray(),
// $this->serviceLocator->get('unicaen-auth_module_options')->toArray());
// $this->setOptions(new ModuleOptions($options));
// }
return $this->options;
}
......@@ -202,9 +221,6 @@ class Cas extends AbstractAdapter implements EventManagerAwareInterface, Service
*/
public function getLdapPeopleMapper()
{
if (null === $this->ldapPeopleMapper) {
$this->ldapPeopleMapper = $this->serviceLocator->get('ldap_people_mapper');
}
return $this->ldapPeopleMapper;
}
......@@ -217,6 +233,7 @@ class Cas extends AbstractAdapter implements EventManagerAwareInterface, Service
public function setLdapPeopleMapper(LdapPeopleMapper $mapper)
{
$this->ldapPeopleMapper = $mapper;
return $this;
}
......
......@@ -2,11 +2,11 @@
namespace UnicaenAuth\Authentication\Adapter;
use Interop\Container\ContainerInterface;
use UnicaenApp\ServiceManager\ServiceLocatorAwareInterface;
use UnicaenApp\ServiceManager\ServiceLocatorAwareTrait;
use UnicaenAuth\Options\ModuleOptions;
use Zend\ServiceManager\Exception\ServiceNotFoundException;
use Zend\ServiceManager\ServiceLocatorAwareTrait;
use Zend\ServiceManager\ServiceLocatorInterface;
use ZfcUser\Authentication\Adapter\AdapterChainEvent as AuthEvent;
use ZfcUser\Options\AuthenticationOptionsInterface;
......@@ -25,11 +25,11 @@ class Db extends \ZfcUser\Authentication\Adapter\Db implements ServiceLocatorAwa
/**
* Set service locator
*
* @param ServiceLocatorInterface $serviceLocator
* @param ContainerInterface $serviceLocator
* @return self
* @deprecated Abandonnez l'injection du service locator, svp
*/
public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
public function setServiceLocator(ContainerInterface $serviceLocator)
{
$this->serviceLocator = $serviceLocator;
$this->serviceManager = $serviceLocator;
......@@ -40,10 +40,10 @@ class Db extends \ZfcUser\Authentication\Adapter\Db implements ServiceLocatorAwa
/**
* Authentification.
*
* @param AuthEvent $e
* @param AuthEvent|\Zend\EventManager\Event $e
* @return boolean
*/
public function authenticate(AuthEvent $e)
public function authenticate(\Zend\EventManager\Event $e)
{
if ($e->getIdentity()) {
return;
......
......@@ -4,7 +4,6 @@ namespace UnicaenAuth\Authentication\Adapter;
use UnicaenApp\Exception\RuntimeException;
use UnicaenApp\Mapper\Ldap\People as LdapPeopleMapper;
use UnicaenApp\ServiceManager\ServiceLocatorAwareInterface;
use UnicaenAuth\Options\ModuleOptions;
use UnicaenAuth\Service\User;
use Zend\Authentication\Adapter\Ldap as LdapAuthAdapter;
......@@ -14,7 +13,6 @@ use Zend\EventManager\Event;
use Zend\EventManager\EventManager;
use Zend\EventManager\EventManagerAwareInterface;
use Zend\EventManager\EventManagerInterface;
use Zend\ServiceManager\ServiceLocatorAwareTrait;
use ZfcUser\Authentication\Adapter\AbstractAdapter;
use ZfcUser\Authentication\Adapter\AdapterChainEvent as AuthEvent;
use ZfcUser\Authentication\Adapter\ChainableAdapter;
......@@ -24,10 +22,8 @@ use ZfcUser\Authentication\Adapter\ChainableAdapter;
*
* @author Bertrand GAUTHIER <bertrand.gauthier@unicaen.fr>
*/
class Ldap extends AbstractAdapter implements ServiceLocatorAwareInterface, EventManagerAwareInterface
class Ldap extends AbstractAdapter implements EventManagerAwareInterface
{
use ServiceLocatorAwareTrait;
const USURPATION_USERNAMES_SEP = '=';
/**
......@@ -55,15 +51,41 @@ class Ldap extends AbstractAdapter implements ServiceLocatorAwareInterface, Even
*/
protected $usernameUsurpe;
/**
* @var User
*/
private $userService;
/**
* @param User $userService
*/
public function setUserService(User $userService)
{
$this->userService = $userService;
}
/**
* @var \UnicaenApp\Options\ModuleOptions
*/
private $appModuleOptions;
/**
* @param \UnicaenApp\Options\ModuleOptions $appModuleOptions
*/
public function setAppModuleOptions(\UnicaenApp\Options\ModuleOptions $appModuleOptions)
{
$this->appModuleOptions = $appModuleOptions;
}
/**
*
* @param AuthEvent $e
* @param AuthEvent|\Zend\EventManager\Event $e
* @return boolean
* @throws \Zend\Authentication\Adapter\Exception\ExceptionInterface
* @throws \Zend\Ldap\Exception\LdapException
* @see ChainableAdapter
*/
public function authenticate(AuthEvent $e)
public function authenticate(\Zend\EventManager\Event $e)
{
if ($this->isSatisfied()) {
try {
......@@ -119,8 +141,7 @@ class Ldap extends AbstractAdapter implements ServiceLocatorAwareInterface, Even
->setMessages(['Authentication successful.']);
/* @var $userService User */
$userService = $this->serviceLocator->get('unicaen-auth_user_service');
$userService->userAuthenticated($ldapPeople);
$this->userService->userAuthenticated($ldapPeople);
}
/**
......@@ -203,9 +224,6 @@ class Ldap extends AbstractAdapter implements ServiceLocatorAwareInterface, Even
*/
public function getLdapPeopleMapper()
{
if (null === $this->ldapPeopleMapper) {
$this->ldapPeopleMapper = $this->serviceLocator->get('ldap_people_mapper');
}
return $this->ldapPeopleMapper;
}
......@@ -234,12 +252,6 @@ class Ldap extends AbstractAdapter implements ServiceLocatorAwareInterface, Even
*/
public function getOptions()
{
if (!$this->options instanceof ModuleOptions) {
$options = array_merge(
$this->serviceLocator->get('zfcuser_module_options')->toArray(),
$this->serviceLocator->get('unicaen-auth_module_options')->toArray());
$this->setOptions(new ModuleOptions($options));
}
return $this->options;
}
......@@ -248,7 +260,7 @@ class Ldap extends AbstractAdapter implements ServiceLocatorAwareInterface, Even
*/
public function getAppModuleOptions()
{
return $this->serviceLocator->get('unicaen-app_module_options');
return $this->appModuleOptions;
}
/**
......
......@@ -2,9 +2,10 @@
namespace UnicaenAuth\Authentication;
use Interop\Container\ContainerInterface;
use Zend\Authentication\AuthenticationService;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\Authentication\AuthenticationService;
/**
* Description of AuthenticationServiceFactory
......@@ -13,17 +14,16 @@ use Zend\Authentication\AuthenticationService;
*/
class AuthenticationServiceFactory implements FactoryInterface
{
/**
* Create service
*
* @param ServiceLocatorInterface $serviceLocator
* @return AuthenticationService
*/
public function createService(ServiceLocatorInterface $serviceLocator)
{
return $this->__invoke($serviceLocator, '?');
}