Skip to content

Commit

Permalink
feat: compatibility with Symfony 3.4
Browse files Browse the repository at this point in the history
  • Loading branch information
fancyweb committed Oct 11, 2019
1 parent 1182409 commit 94ef503
Show file tree
Hide file tree
Showing 13 changed files with 62 additions and 39 deletions.
1 change: 0 additions & 1 deletion DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace Ekino\Bundle\DrupalBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\ConfigurationInterface;

/**
Expand Down
3 changes: 0 additions & 3 deletions DependencyInjection/EkinoDrupalExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,8 @@
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Config\Definition\Processor;

/**
Expand Down
1 change: 0 additions & 1 deletion Drupal/DrupalRequestListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

use Ekino\Bundle\DrupalBundle\Delivery\DeliveryStrategyInterface;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;

Expand Down
4 changes: 2 additions & 2 deletions Entity/FosUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -607,9 +607,9 @@ public function setPlainPassword($password)
/**
* Sets the last login time
*
* @param \DateTime $time
* @param \DateTime|null $time
*/
public function setLastLogin(\DateTime $time)
public function setLastLogin(\DateTime $time = null)
{
$this->lastLogin = $time;
}
Expand Down
4 changes: 1 addition & 3 deletions Entity/HybridUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@

namespace Ekino\Bundle\DrupalBundle\Entity;

use Symfony\Component\Security\Core\User\UserInterface as SecurityUserInterface;
use FOS\UserBundle\Model\UserInterface as FOSUserInterface;

/**
* This class provide convenient proxies methods between FosUser class and a DrupalUser class
*
* This class provide convenient proxies methods between FosUser class and a DrupalUser class.
*/
abstract class HybridUser extends FosUser
{
Expand Down
8 changes: 8 additions & 0 deletions Entity/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,12 @@ public function getUid()
{
return $this->uid;
}

/**
* {@inheritdoc}
*/
public function setSalt($salt)
{
$this->salt = $salt;
}
}
6 changes: 3 additions & 3 deletions Event/Listener/UserEntityHookListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use Ekino\Bundle\DrupalBundle\Event\DrupalEntityEvent;
use Ekino\Bundle\DrupalBundle\Event\DrupalEntitiesEvent;
use FOS\UserBundle\Model\UserManagerInterface;
use Symfony\Component\HttpKernel\Log\LoggerInterface;
use Psr\Log\LoggerInterface;

/**
* These methods are called by the drupal user hook
Expand All @@ -29,7 +29,7 @@ class UserEntityHookListener

/**
* @param \FOS\UserBundle\Model\UserManagerInterface $userManager
* @param \Symfony\Component\HttpKernel\Log\LoggerInterface $logger
* @param \Psr\Log\LoggerInterface $logger
*/
public function __construct(UserManagerInterface $userManager, LoggerInterface $logger)
{
Expand Down Expand Up @@ -111,4 +111,4 @@ public function onPreSave(DrupalEntityEvent $event)
$account->usernameCanonical = $account->name;
}
}
}
}
20 changes: 12 additions & 8 deletions Event/Listener/UserRegistrationHookListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

use Ekino\Bundle\DrupalBundle\Event\DrupalEvent;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Core\User\UserInterface;

Expand All @@ -30,9 +30,9 @@ class UserRegistrationHookListener
protected $logger;

/**
* @var Request
* @var RequestStack
*/
protected $request;
protected $requestStack;

/**
* @var array
Expand All @@ -41,13 +41,13 @@ class UserRegistrationHookListener

/**
* @param LoggerInterface $logger
* @param Request $request
* @param RequestStack $requestStack
* @param array $providerKeys
*/
public function __construct(LoggerInterface $logger, Request $request, array $providerKeys)
public function __construct(LoggerInterface $logger, RequestStack $requestStack, array $providerKeys)
{
$this->logger = $logger;
$this->request = $request;
$this->requestStack = $requestStack;
$this->providerKeys = $providerKeys;
}

Expand All @@ -64,12 +64,14 @@ public function onLogin(DrupalEvent $event)
throw new \RuntimeException('An instance of UserInterface is expected');
}

$request = $this->requestStack->getMasterRequest();

// The ContextListener from the Security component is hijacked to insert a valid token into session
// so next time the user go to a valid symfony2 url with a proper security context, then the following token
// will be used
foreach ($this->providerKeys as $providerKey) {
$token = new UsernamePasswordToken($user, null, $providerKey, $user->getRoles());
$this->request->getSession()->set('_security_'.$providerKey, serialize($token));
$request->getSession()->set('_security_'.$providerKey, serialize($token));
}
}

Expand All @@ -78,8 +80,10 @@ public function onLogin(DrupalEvent $event)
*/
public function onLogout(DrupalEvent $event)
{
$request = $this->requestStack->getMasterRequest();

foreach ($this->providerKeys as $providerKey) {
$this->request->getSession()->set('_security_'.$providerKey, null);
$request->getSession()->set('_security_'.$providerKey, null);
}
}
}
1 change: 0 additions & 1 deletion Listener/ExceptionListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Ekino\Bundle\DrupalBundle\Listener;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

Expand Down
4 changes: 2 additions & 2 deletions Log/LoggerWatchdog.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Ekino\Bundle\DrupalBundle\Log;

use Symfony\Component\HttpKernel\Log\LoggerInterface;
use Psr\Log\LoggerInterface;

/**
* LoggerWatchdog.
Expand Down Expand Up @@ -135,4 +135,4 @@ public function log($level, $message, array $context = array())
watchdog('Symfony2', $message, $context, $level);
}
}
}
}
6 changes: 3 additions & 3 deletions Resources/config/user_hook.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

<services>

<service id="ekino.drupal.user_registration_hook" class="Ekino\Bundle\DrupalBundle\Event\Listener\UserRegistrationHookListener" scope="request">
<service id="ekino.drupal.user_registration_hook" class="Ekino\Bundle\DrupalBundle\Event\Listener\UserRegistrationHookListener">
<tag name="kernel.event_listener" event="drupal.user_login" method="onLogin" priority="64"/>
<tag name="kernel.event_listener" event="drupal.user_logout" method="onLogout" priority="64"/>

<argument type="service" id="logger" />
<argument type="service" id="request" />
<argument type="service" id="request_stack" />
<argument />
</service>

Expand All @@ -27,4 +27,4 @@

</services>

</container>
</container>
14 changes: 10 additions & 4 deletions Tests/Event/Listener/UserRegistrationHookListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ class UserRegistrationHookListenerTest extends \PHPUnit_Framework_TestCase
public function testOnLoginThrowsException()
{
$logger = $this->getMock('Psr\Log\LoggerInterface');
$request = $this->getMock('Symfony\Component\HttpFoundation\Request');
$listener = new UserRegistrationHookListener($logger, $request, array());
$requestStack = $this->getMock('Symfony\Component\HttpFoundation\RequestStack');
$listener = new UserRegistrationHookListener($logger, $requestStack, array());

$event = $this->getMock('Ekino\Bundle\DrupalBundle\Event\DrupalEvent');
$event->expects($this->once())->method('getParameter')->with($this->equalTo(1))->willReturn(null);
Expand All @@ -47,13 +47,16 @@ public function testOnLoginUserWithProviderKeys()
$request = $this->getMock('Symfony\Component\HttpFoundation\Request');
$request->expects($this->exactly(3))->method('getSession')->willReturn($session);

$requestStack = $this->getMock('Symfony\Component\HttpFoundation\RequestStack');
$requestStack->expects($this->once())->method('getMasterRequest')->willReturn($request);

$user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
$user->expects($this->any())->method('getRoles')->willReturn(array('ROLE_USER'));

$event = $this->getMock('Ekino\Bundle\DrupalBundle\Event\DrupalEvent');
$event->expects($this->once())->method('getParameter')->with($this->equalTo(1))->willReturn($user);

$listener = new UserRegistrationHookListener($logger, $request, array('1', '2', '3'));
$listener = new UserRegistrationHookListener($logger, $requestStack, array('1', '2', '3'));
$listener->onLogin($event);
}

Expand All @@ -67,12 +70,15 @@ public function testOnLoginUserWithoutProviderKeys()
$request = $this->getMock('Symfony\Component\HttpFoundation\Request');
$request->expects($this->never())->method('getSession');

$requestStack = $this->getMock('Symfony\Component\HttpFoundation\RequestStack');
$requestStack->expects($this->once())->method('getMasterRequest')->willReturn($request);

$user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');

$event = $this->getMock('Ekino\Bundle\DrupalBundle\Event\DrupalEvent');
$event->expects($this->once())->method('getParameter')->with($this->equalTo(1))->willReturn($user);

$listener = new UserRegistrationHookListener($logger, $request, array());
$listener = new UserRegistrationHookListener($logger, $requestStack, array());
$listener->onLogin($event);
}
}
29 changes: 21 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "ekino/drupal-bundle",
"type": "symfony-bundle",
"description": "Integrate Symfony2 with Drupal ",
"description": "Integrate Symfony2 with Drupal",
"keywords": ["components", "drupal"],
"homepage": "https://github.com/ekino/EkinoDrupalBundle",
"license": "MIT",
Expand All @@ -12,15 +12,28 @@
}
],
"require": {
"php": ">=5.3.2",
"friendsofsymfony/user-bundle": "*",
"symfony/http-foundation": ">=2.1,<3.0-dev",
"symfony/http-kernel": ">=2.1,<3.0-dev",
"twig/extensions": "1.0.*",
"psr/log": "1.0.*"
"php": ">=5.5.9",
"doctrine/common": "^2.6",
"doctrine/orm": "^2.5",
"friendsofsymfony/user-bundle": "^2.1",
"psr/log": "^1.1",
"symfony/config": "^3.4",
"symfony/dependency-injection": "^3.4",
"symfony/event-dispatcher": "^3.4",
"symfony/http-foundation": "^3.4",
"symfony/http-kernel": "^3.4",
"symfony/security-core": "^3.4",
"twig/twig": "^1.42"
},
"autoload": {
"psr-0": { "Ekino\\Bundle\\DrupalBundle": "" }
},
"target-dir": "Ekino/Bundle/DrupalBundle"
"target-dir": "Ekino/Bundle/DrupalBundle",
"require-dev": {
"phpunit/phpunit": "^4.8",
"symfony/phpunit-bridge": "^4.3"
},
"config": {
"sort-packages": true
}
}

0 comments on commit 94ef503

Please sign in to comment.