From 94ef50350645ef15cb36f5507e7e466547011fc0 Mon Sep 17 00:00:00 2001 From: fancyweb Date: Thu, 10 Oct 2019 17:30:51 +0200 Subject: [PATCH] feat: compatibility with Symfony 3.4 --- DependencyInjection/Configuration.php | 1 - DependencyInjection/EkinoDrupalExtension.php | 3 -- Drupal/DrupalRequestListener.php | 1 - Entity/FosUser.php | 4 +-- Entity/HybridUser.php | 4 +-- Entity/User.php | 8 +++++ Event/Listener/UserEntityHookListener.php | 6 ++-- .../Listener/UserRegistrationHookListener.php | 20 ++++++++----- Listener/ExceptionListener.php | 1 - Log/LoggerWatchdog.php | 4 +-- Resources/config/user_hook.xml | 6 ++-- .../UserRegistrationHookListenerTest.php | 14 ++++++--- composer.json | 29 ++++++++++++++----- 13 files changed, 62 insertions(+), 39 deletions(-) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 06ca42a..c6dac03 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -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; /** diff --git a/DependencyInjection/EkinoDrupalExtension.php b/DependencyInjection/EkinoDrupalExtension.php index 1d2edd0..469bcc5 100644 --- a/DependencyInjection/EkinoDrupalExtension.php +++ b/DependencyInjection/EkinoDrupalExtension.php @@ -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; /** diff --git a/Drupal/DrupalRequestListener.php b/Drupal/DrupalRequestListener.php index 198b6bc..e7e7dee 100644 --- a/Drupal/DrupalRequestListener.php +++ b/Drupal/DrupalRequestListener.php @@ -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; diff --git a/Entity/FosUser.php b/Entity/FosUser.php index 8b5ce44..1efc651 100644 --- a/Entity/FosUser.php +++ b/Entity/FosUser.php @@ -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; } diff --git a/Entity/HybridUser.php b/Entity/HybridUser.php index 24f391d..41b76c3 100644 --- a/Entity/HybridUser.php +++ b/Entity/HybridUser.php @@ -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 { diff --git a/Entity/User.php b/Entity/User.php index cbf5801..6857000 100644 --- a/Entity/User.php +++ b/Entity/User.php @@ -19,4 +19,12 @@ public function getUid() { return $this->uid; } + + /** + * {@inheritdoc} + */ + public function setSalt($salt) + { + $this->salt = $salt; + } } diff --git a/Event/Listener/UserEntityHookListener.php b/Event/Listener/UserEntityHookListener.php index 37b6302..ec0bfee 100644 --- a/Event/Listener/UserEntityHookListener.php +++ b/Event/Listener/UserEntityHookListener.php @@ -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 @@ -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) { @@ -111,4 +111,4 @@ public function onPreSave(DrupalEntityEvent $event) $account->usernameCanonical = $account->name; } } -} \ No newline at end of file +} diff --git a/Event/Listener/UserRegistrationHookListener.php b/Event/Listener/UserRegistrationHookListener.php index 8c2c478..461b3d1 100644 --- a/Event/Listener/UserRegistrationHookListener.php +++ b/Event/Listener/UserRegistrationHookListener.php @@ -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; @@ -30,9 +30,9 @@ class UserRegistrationHookListener protected $logger; /** - * @var Request + * @var RequestStack */ - protected $request; + protected $requestStack; /** * @var array @@ -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; } @@ -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)); } } @@ -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); } } } diff --git a/Listener/ExceptionListener.php b/Listener/ExceptionListener.php index 8b27faf..ebb2ae3 100644 --- a/Listener/ExceptionListener.php +++ b/Listener/ExceptionListener.php @@ -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; diff --git a/Log/LoggerWatchdog.php b/Log/LoggerWatchdog.php index 01844c6..ab52b47 100644 --- a/Log/LoggerWatchdog.php +++ b/Log/LoggerWatchdog.php @@ -11,7 +11,7 @@ namespace Ekino\Bundle\DrupalBundle\Log; -use Symfony\Component\HttpKernel\Log\LoggerInterface; +use Psr\Log\LoggerInterface; /** * LoggerWatchdog. @@ -135,4 +135,4 @@ public function log($level, $message, array $context = array()) watchdog('Symfony2', $message, $context, $level); } } -} \ No newline at end of file +} diff --git a/Resources/config/user_hook.xml b/Resources/config/user_hook.xml index c33a48d..148a01c 100644 --- a/Resources/config/user_hook.xml +++ b/Resources/config/user_hook.xml @@ -6,12 +6,12 @@ - + - + @@ -27,4 +27,4 @@ - \ No newline at end of file + diff --git a/Tests/Event/Listener/UserRegistrationHookListenerTest.php b/Tests/Event/Listener/UserRegistrationHookListenerTest.php index d227be2..1fdd404 100644 --- a/Tests/Event/Listener/UserRegistrationHookListenerTest.php +++ b/Tests/Event/Listener/UserRegistrationHookListenerTest.php @@ -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); @@ -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); } @@ -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); } } diff --git a/composer.json b/composer.json index 63e146e..3d3df63 100644 --- a/composer.json +++ b/composer.json @@ -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", @@ -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 + } }