Skip to content

Commit

Permalink
Add a parameter to configure the firewall name (issue #49)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vincent Composieux committed Oct 29, 2014
1 parent 391e19d commit 022d018
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 4 deletions.
16 changes: 16 additions & 0 deletions DependencyInjection/Configuration.php
Expand Up @@ -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');

Expand All @@ -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()
;

Expand Down
1 change: 1 addition & 0 deletions DependencyInjection/EkinoWordpressExtension.php
Expand Up @@ -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);
}
Expand Down
14 changes: 10 additions & 4 deletions Event/Hook/UserHookListener.php
Expand Up @@ -48,20 +48,27 @@ class UserHookListener
*/
protected $session;

/**
* @var string
*/
protected $firewall;

/**
* Constructor
*
* @param UserManager $userManager Wordpress bundle user manager
* @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;
}

/**
Expand All @@ -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));
}

/**
Expand Down
1 change: 1 addition & 0 deletions Resources/config/hooks.xml
Expand Up @@ -17,6 +17,7 @@
<argument type="service" id="logger" />
<argument type="service" id="security.context" />
<argument type="service" id="session" />
<argument>%ekino.wordpress.firewall_name%</argument>
</service>

<service id="ekino.wordpress.response_subscriber" class="%ekino.wordpress.response_subscriber.class%">
Expand Down
115 changes: 115 additions & 0 deletions Tests/Event/Hook/UserHookListenerTest.php
@@ -0,0 +1,115 @@
<?php
/*
* This file is part of the Ekino Wordpress package.
*
* (c) 2013 Ekino
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Ekino\WordpressBundle\Tests\Event\Hook;

use Ekino\WordpressBundle\Event\Hook\UserHookListener;

/**
* Class UserHookListenerTest
*
* @author Vincent Composieux <composieux@ekino.com>
*/
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);
}
}

0 comments on commit 022d018

Please sign in to comment.