|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the awurth/silex-user package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Alexis Wurth <[email protected]> |
|
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 AWurth\Silex\User\EventListener; |
|
13
|
|
|
|
|
14
|
|
|
use AWurth\Silex\User\Event\Events; |
|
15
|
|
|
use AWurth\Silex\User\Event\FilterUserResponseEvent; |
|
16
|
|
|
use AWurth\Silex\User\Event\UserEvent; |
|
17
|
|
|
use AWurth\Silex\User\Security\LoginManagerInterface; |
|
18
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
19
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
20
|
|
|
use Symfony\Component\Security\Core\Exception\AccountStatusException; |
|
21
|
|
|
|
|
22
|
|
|
class AuthenticationListener implements EventSubscriberInterface |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var LoginManagerInterface |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $loginManager; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $firewallName; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Constructor. |
|
36
|
|
|
* |
|
37
|
|
|
* @param LoginManagerInterface $loginManager |
|
38
|
|
|
* @param string $firewallName |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct(LoginManagerInterface $loginManager, $firewallName) |
|
41
|
|
|
{ |
|
42
|
|
|
$this->loginManager = $loginManager; |
|
43
|
|
|
$this->firewallName = $firewallName; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* {@inheritdoc} |
|
48
|
|
|
*/ |
|
49
|
|
|
public static function getSubscribedEvents() |
|
50
|
|
|
{ |
|
51
|
|
|
return [ |
|
52
|
|
|
Events::REGISTRATION_COMPLETED => 'authenticate', |
|
53
|
|
|
Events::REGISTRATION_CONFIRMED => 'authenticate' |
|
54
|
|
|
]; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Authenticates the user. |
|
59
|
|
|
* |
|
60
|
|
|
* @param FilterUserResponseEvent $event |
|
61
|
|
|
* @param string $eventName |
|
62
|
|
|
* @param EventDispatcherInterface $dispatcher |
|
63
|
|
|
*/ |
|
64
|
|
|
public function authenticate(FilterUserResponseEvent $event, $eventName, EventDispatcherInterface $dispatcher) |
|
|
|
|
|
|
65
|
|
|
{ |
|
66
|
|
|
try { |
|
67
|
|
|
$this->loginManager->logInUser($this->firewallName, $event->getUser(), $event->getResponse()); |
|
68
|
|
|
|
|
69
|
|
|
$dispatcher->dispatch(Events::SECURITY_IMPLICIT_LOGIN, new UserEvent($event->getUser(), $event->getRequest())); |
|
70
|
|
|
} catch (AccountStatusException $e) { |
|
71
|
|
|
// We simply do not authenticate users which do not pass the user |
|
72
|
|
|
// checker (not enabled, expired, etc.). |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.