|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Services\Registration; |
|
4
|
|
|
|
|
5
|
|
|
use App\Entity\User; |
|
6
|
|
|
use Symfony\Component\EventDispatcher\Event; |
|
7
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
8
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
9
|
|
|
use Symfony\Component\HttpFoundation\Session\SessionInterface; |
|
10
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
|
11
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; |
|
12
|
|
|
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent; |
|
13
|
|
|
|
|
14
|
|
|
class RegistrationAutoLogon |
|
15
|
|
|
{ |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var EventDispatcherInterface |
|
19
|
|
|
*/ |
|
20
|
|
|
private $dispatcher; |
|
21
|
|
|
/** |
|
22
|
|
|
* @var TokenStorageInterface |
|
23
|
|
|
*/ |
|
24
|
|
|
private $tokenStorage; |
|
25
|
|
|
/** |
|
26
|
|
|
* @var SessionInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
private $session; |
|
29
|
|
|
|
|
30
|
|
|
public function __construct(EventDispatcherInterface $dispatcher, TokenStorageInterface $tokenStorage, SessionInterface $session) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->dispatcher = $dispatcher; |
|
33
|
|
|
$this->tokenStorage = $tokenStorage; |
|
34
|
|
|
$this->session = $session; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param User $user |
|
39
|
|
|
* @param Request $request |
|
40
|
|
|
* @return Event |
|
41
|
|
|
* Auto Logges on the passed user |
|
42
|
|
|
* |
|
43
|
|
|
*/ |
|
44
|
|
|
public function autoLogon(User $user, Request $request): Event |
|
45
|
|
|
{ |
|
46
|
|
|
//Login user |
|
47
|
|
|
$token = new UsernamePasswordToken($user, null, 'main', $user->getRoles()); |
|
48
|
|
|
$this->tokenStorage->setToken($token); |
|
49
|
|
|
$this->session->set('_security_main', serialize($token)); |
|
50
|
|
|
$event = new InteractiveLoginEvent($request, $token); |
|
51
|
|
|
return $this->dispatcher->dispatch("security.interactive_login", $event); |
|
52
|
|
|
} |
|
53
|
|
|
} |