From 022d0184c5f897b427d3a1472a318190fce453c6 Mon Sep 17 00:00:00 2001 From: Vincent Composieux Date: Wed, 29 Oct 2014 13:20:17 -0400 Subject: [PATCH] Add a parameter to configure the firewall name (issue #49) --- DependencyInjection/Configuration.php | 16 +++ .../EkinoWordpressExtension.php | 1 + Event/Hook/UserHookListener.php | 14 ++- Resources/config/hooks.xml | 1 + Tests/Event/Hook/UserHookListenerTest.php | 115 ++++++++++++++++++ 5 files changed, 143 insertions(+), 4 deletions(-) create mode 100644 Tests/Event/Hook/UserHookListenerTest.php diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index df408fd..1763a45 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -29,6 +29,15 @@ class Configuration implements ConfigurationInterface */ public function getConfigTreeBuilder() { + /** + ->arrayNode('profiler') + ->addDefaultsIfNotSet() + ->fixXmlConfig('container_type', 'container_types') + ->children() + ->scalarNode('enabled')->defaultValue('%kernel.debug%')->end() + ->end() + ->end() + */ $treeBuilder = new TreeBuilder(); $rootNode = $treeBuilder->root('ekino_wordpress'); @@ -40,6 +49,13 @@ public function getConfigTreeBuilder() ->booleanNode('load_twig_extension')->defaultFalse()->end() ->booleanNode('cookie_hash')->defaultNull()->end() ->scalarNode('i18n_cookie_name')->defaultFalse()->end() + + ->arrayNode('security') + ->addDefaultsIfNotSet() + ->children() + ->scalarNode('firewall_name')->defaultValue('secured_area')->end() + ->end() + ->end() ->end() ; diff --git a/DependencyInjection/EkinoWordpressExtension.php b/DependencyInjection/EkinoWordpressExtension.php index b86277b..bb88e8d 100644 --- a/DependencyInjection/EkinoWordpressExtension.php +++ b/DependencyInjection/EkinoWordpressExtension.php @@ -63,6 +63,7 @@ public function load(array $configs, ContainerBuilder $container) } $container->setParameter('ekino.wordpress.cookie_hash', $config['cookie_hash']); + $container->setParameter('ekino.wordpress.firewall_name', $config['security']['firewall_name']); $container->setParameter($this->getAlias() . '.backend_type_orm', true); } diff --git a/Event/Hook/UserHookListener.php b/Event/Hook/UserHookListener.php index 86ef382..3353c26 100644 --- a/Event/Hook/UserHookListener.php +++ b/Event/Hook/UserHookListener.php @@ -48,6 +48,11 @@ class UserHookListener */ protected $session; + /** + * @var string + */ + protected $firewall; + /** * Constructor * @@ -55,13 +60,15 @@ class UserHookListener * @param LoggerInterface $logger Symfony PSR logger * @param SecurityContextInterface $securityContext Symfony security context * @param SessionInterface $session Symfony session service + * @param string $firewall EkinoWordpressBundle firewall name */ - public function __construct(UserManager $userManager, LoggerInterface $logger, SecurityContextInterface $securityContext, SessionInterface $session) + public function __construct(UserManager $userManager, LoggerInterface $logger, SecurityContextInterface $securityContext, SessionInterface $session, $firewall) { $this->userManager = $userManager; $this->logger = $logger; $this->securityContext = $securityContext; $this->session = $session; + $this->firewall = $firewall; } /** @@ -78,11 +85,10 @@ public function onLogin(WordpressEvent $event) $user = $this->userManager->find($wpUser->data->ID); $user->setWordpressRoles($wpUser->roles); - $firewall = 'secured_area'; - $token = new UsernamePasswordToken($user, $user->getPass(), $firewall, $user->getRoles()); + $token = new UsernamePasswordToken($user, $user->getPass(), $this->firewall, $user->getRoles()); $this->securityContext->setToken($token); - $this->session->set('_security_' . $firewall, serialize($token)); + $this->session->set('_security_' . $this->firewall, serialize($token)); } /** diff --git a/Resources/config/hooks.xml b/Resources/config/hooks.xml index 725a7b7..6349a48 100644 --- a/Resources/config/hooks.xml +++ b/Resources/config/hooks.xml @@ -17,6 +17,7 @@ + %ekino.wordpress.firewall_name% diff --git a/Tests/Event/Hook/UserHookListenerTest.php b/Tests/Event/Hook/UserHookListenerTest.php new file mode 100644 index 0000000..5a40586 --- /dev/null +++ b/Tests/Event/Hook/UserHookListenerTest.php @@ -0,0 +1,115 @@ + + */ +class UserHookListenerTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var \Ekino\WordpressBundle\Manager\UserManager + */ + protected $userManager; + + /** + * @var \Psr\Log\LoggerInterface + */ + protected $logger; + + /** + * @var \Symfony\Component\Security\Core\SecurityContextInterface + */ + protected $securityContext; + + /** + * @var \Symfony\Component\HttpFoundation\Session\SessionInterface + */ + protected $session; + + /** + * @var string + */ + protected $firewall; + + /** + * @var UserHookListener + */ + protected $listener; + + /** + * Sets up a UserHookListener instance + */ + protected function setUp() + { + $this->userManager = $this->getMockBuilder('Ekino\WordpressBundle\Manager\UserManager') + ->disableOriginalConstructor() + ->getMock(); + + $this->logger = $this->getMock('Psr\Log\LoggerInterface'); + + $this->securityContext = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface'); + + $this->session = $this->getMock('Symfony\Component\HttpFoundation\Session\SessionInterface'); + + $this->firewall = 'secured_area'; + + $this->listener = new UserHookListener($this->userManager, $this->logger, $this->securityContext, $this->session, $this->firewall); + } + + /** + * Tests onLogin() method + */ + public function testOnLogin() + { + // Given + $wpUserData = new \stdClass(); + $wpUserData->ID = 1; + + $wpUser = new \stdClass(); + $wpUser->data = $wpUserData; + $wpUser->roles = array('administrator'); + + $event = $this->getMock('Ekino\WordpressBundle\Event\WordpressEvent'); + $event->expects($this->once())->method('getParameter')->will($this->returnValue($wpUser)); + + $user = $this->getMock('Ekino\WordpressBundle\Entity\User'); + $user->expects($this->once())->method('setWordpressRoles')->with($wpUser->roles); + $user->expects($this->once())->method('getPass')->will($this->returnValue(1234)); + $user->expects($this->once())->method('getRoles')->will($this->returnValue(array('ROLE_WP_ADMINISTRATOR'))); + + $this->userManager->expects($this->once())->method('find')->will($this->returnValue($user)); + + $this->securityContext->expects($this->once())->method('setToken'); + + // When - Then + $this->listener->onLogin($event); + } + + /** + * Tests onLogout() method + */ + public function testOnLogout() + { + // Given + $event = $this->getMock('Ekino\WordpressBundle\Event\WordpressEvent'); + + // When - Then + $this->session->expects($this->once())->method('clear'); + $this->securityContext->expects($this->once())->method('setToken')->with(null); + + $this->listener->onLogout($event); + } +}