Completed
Push — symfony3-validation-fail ( 1a4398 )
by Kamil
26:23 queued 08:51
created

UserImpersonator::impersonate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
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 Sylius\Bundle\CoreBundle\Security;
13
14
use Symfony\Component\HttpFoundation\Session\Session;
15
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
16
use Symfony\Component\Security\Core\User\UserInterface;
17
18
/**
19
 * @author Jan Góralski <[email protected]>
20
 */
21
final class UserImpersonator implements UserImpersonatorInterface
22
{
23
    /**
24
     * @var Session
25
     */
26
    private $session;
27
28
    /**
29
     * @var string
30
     */
31
    private $sessionTokenParameter;
32
33
    /**
34
     * @param Session $session
35
     * @param string $firewallContextName
36
     */
37
    public function __construct(Session $session, $firewallContextName)
38
    {
39
        $this->session = $session;
40
        $this->sessionTokenParameter = sprintf('_security_%s', $firewallContextName);
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function impersonate(UserInterface $user)
47
    {
48
        $token = new UsernamePasswordToken($user, $user->getPassword(), $this->sessionTokenParameter, $user->getRoles());
49
50
        $this->session->set($this->sessionTokenParameter, serialize($token));
51
        $this->session->save();
52
    }
53
}
54