Skip to content
This repository was archived by the owner on Jan 2, 2025. It is now read-only.

Commit a7e592c

Browse files
author
Louis Courvoisier
committed
fix(session): fix the reset password process
1 parent f7b7df9 commit a7e592c

4 files changed

Lines changed: 104 additions & 18 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ A [BC BREAK] means the update will break the project for many reasons:
88
* class refactoring
99

1010

11+
### 2017-03-17
12+
13+
* Prevents to close the user session too quickly in order to allow Drupal to write in.
14+
It fixes the reset password process.
15+
* Adds psr/log to fix the deprecation of LoggerInterface.
16+
1117
### 2014-05-19
1218

1319
* [BC BREAK] Third argument of EntityRepository::findBy method is now $fieldConditions.

Event/Listener/UserRegistrationHookListener.php

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
namespace Ekino\Bundle\DrupalBundle\Event\Listener;
1313

1414
use Ekino\Bundle\DrupalBundle\Event\DrupalEvent;
15-
use Symfony\Component\HttpKernel\Log\LoggerInterface;
15+
use Psr\Log\LoggerInterface;
1616
use Symfony\Component\HttpFoundation\Request;
1717
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
1818
use Symfony\Component\Security\Core\User\UserInterface;
@@ -24,35 +24,40 @@
2424
*/
2525
class UserRegistrationHookListener
2626
{
27-
protected $userManager;
28-
27+
/**
28+
* @var LoggerInterface
29+
*/
2930
protected $logger;
3031

32+
/**
33+
* @var Request
34+
*/
3135
protected $request;
3236

37+
/**
38+
* @var array
39+
*/
3340
protected $providerKeys;
3441

3542
/**
36-
* @param \Symfony\Component\HttpKernel\Log\LoggerInterface $logger
37-
* @param \Symfony\Component\HttpFoundation\Request $request
38-
* @param array $providerKeys
43+
* @param LoggerInterface $logger
44+
* @param Request $request
45+
* @param array $providerKeys
3946
*/
4047
public function __construct(LoggerInterface $logger, Request $request, array $providerKeys)
4148
{
42-
$this->logger = $logger;
43-
$this->request = $request;
49+
$this->logger = $logger;
50+
$this->request = $request;
4451
$this->providerKeys = $providerKeys;
4552
}
4653

4754
/**
4855
* http://api.drupal.org/api/drupal/modules--user--user.api.php/function/hook_user_login/7
4956
*
50-
* @param \Ekino\Bundle\DrupalBundle\Event\DrupalEvent $event
51-
* @return void
57+
* @param DrupalEvent $event
5258
*/
5359
public function onLogin(DrupalEvent $event)
5460
{
55-
$edit = $event->getParameter(0);
5661
$user = $event->getParameter(1);
5762

5863
if (!$user instanceof UserInterface) {
@@ -64,21 +69,17 @@ public function onLogin(DrupalEvent $event)
6469
// will be used
6570
foreach ($this->providerKeys as $providerKey) {
6671
$token = new UsernamePasswordToken($user, null, $providerKey, $user->getRoles());
67-
6872
$this->request->getSession()->set('_security_'.$providerKey, serialize($token));
6973
}
70-
71-
$this->request->getSession()->save();
7274
}
7375

7476
/**
75-
* @param \Ekino\Bundle\DrupalBundle\Event\DrupalEvent $event
76-
* @return void
77+
* @param DrupalEvent $event
7778
*/
7879
public function onLogout(DrupalEvent $event)
7980
{
8081
foreach ($this->providerKeys as $providerKey) {
8182
$this->request->getSession()->set('_security_'.$providerKey, null);
8283
}
8384
}
84-
}
85+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Ekino Drupal package.
5+
*
6+
* (c) 2011 Ekino
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Ekino\Bundle\DrupalBundle\Event\Listener;
13+
14+
/**
15+
* Tests UserRegistrationHookListener
16+
*
17+
* @author Louis Courvoisier <louis.courvoisier@ekino.com>
18+
*/
19+
class UserRegistrationHookListenerTest extends \PHPUnit_Framework_TestCase
20+
{
21+
/**
22+
* Asserts the onLogin method throws an exception if user not instance of UserInterface
23+
*
24+
* @expectedException \RuntimeException
25+
* @expectedExceptionMessage An instance of UserInterface is expected
26+
*/
27+
public function testOnLoginThrowsException()
28+
{
29+
$logger = $this->getMock('Psr\Log\LoggerInterface');
30+
$request = $this->getMock('Symfony\Component\HttpFoundation\Request');
31+
$listener = new UserRegistrationHookListener($logger, $request, array());
32+
33+
$event = $this->getMock('Ekino\Bundle\DrupalBundle\Event\DrupalEvent');
34+
$event->expects($this->once())->method('getParameter')->with($this->equalTo(1))->willReturn(null);
35+
36+
$listener->onLogin($event);
37+
}
38+
39+
/**
40+
* Test onLogin method with provider keys
41+
*/
42+
public function testOnLoginUserWithProviderKeys()
43+
{
44+
$logger = $this->getMock('Psr\Log\LoggerInterface');
45+
$session = $this->getMock('Symfony\Component\HttpFoundation\Session\SessionInterface');
46+
47+
$request = $this->getMock('Symfony\Component\HttpFoundation\Request');
48+
$request->expects($this->exactly(3))->method('getSession')->willReturn($session);
49+
50+
$user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
51+
$user->expects($this->any())->method('getRoles')->willReturn(array('ROLE_USER'));
52+
53+
$event = $this->getMock('Ekino\Bundle\DrupalBundle\Event\DrupalEvent');
54+
$event->expects($this->once())->method('getParameter')->with($this->equalTo(1))->willReturn($user);
55+
56+
$listener = new UserRegistrationHookListener($logger, $request, array('1', '2', '3'));
57+
$listener->onLogin($event);
58+
}
59+
60+
/**
61+
* Test onLogin method with no provider keys
62+
*/
63+
public function testOnLoginUserWithoutProviderKeys()
64+
{
65+
$logger = $this->getMock('Psr\Log\LoggerInterface');
66+
67+
$request = $this->getMock('Symfony\Component\HttpFoundation\Request');
68+
$request->expects($this->never())->method('getSession');
69+
70+
$user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
71+
72+
$event = $this->getMock('Ekino\Bundle\DrupalBundle\Event\DrupalEvent');
73+
$event->expects($this->once())->method('getParameter')->with($this->equalTo(1))->willReturn($user);
74+
75+
$listener = new UserRegistrationHookListener($logger, $request, array());
76+
$listener->onLogin($event);
77+
}
78+
}

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"friendsofsymfony/user-bundle": "*",
1717
"symfony/http-foundation": ">=2.1,<3.0-dev",
1818
"symfony/http-kernel": ">=2.1,<3.0-dev",
19-
"twig/extensions": "1.0.*"
19+
"twig/extensions": "1.0.*",
20+
"psr/log": "1.0.*"
2021
},
2122
"autoload": {
2223
"psr-0": { "Ekino\\Bundle\\DrupalBundle": "" }

0 commit comments

Comments
 (0)