Completed
Push — master ( 7fc704...cfa808 )
by Paweł
33:04 queued 17:47
created

SecurityService::restorePreviousSessionToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
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\Behat\Service;
13
14
use Sylius\Behat\Service\Setter\CookieSetterInterface;
15
use Sylius\Component\Core\Model\UserInterface;
16
use Sylius\Component\User\Repository\UserRepositoryInterface;
17
use Symfony\Component\HttpFoundation\Session\SessionInterface;
18
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
19
20
/**
21
 * @author Arkadiusz Krakowiak <[email protected]>
22
 * @author Kamil Kokot <[email protected]>
23
 */
24
final class SecurityService implements SecurityServiceInterface
25
{
26
    /**
27
     * @var UserRepositoryInterface
28
     */
29
    private $userRepository;
30
31
    /**
32
     * @var SessionInterface
33
     */
34
    private $session;
35
36
    /**
37
     * @var CookieSetterInterface
38
     */
39
    private $cookieSetter;
40
41
    /**
42
     * @var string
43
     */
44
    private $sessionTokenVariable;
45
46
    /**
47
     * @param UserRepositoryInterface $userRepository
48
     * @param SessionInterface $session
49
     * @param CookieSetterInterface $cookieSetter
50
     * @param string $contextKey
51
     */
52
    public function __construct(
53
        UserRepositoryInterface $userRepository,
54
        SessionInterface $session,
55
        CookieSetterInterface $cookieSetter,
56
        $contextKey
57
    ) {
58
        $this->userRepository = $userRepository;
59
        $this->session = $session;
60
        $this->cookieSetter = $cookieSetter;
61
        $this->sessionTokenVariable = sprintf('_security_%s', $contextKey);
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function logIn($email)
68
    {
69
        /** @var UserInterface $user */
70
        $user = $this->userRepository->findOneBy(['username' => $email]);
71
        if (null === $user) {
72
            throw new \InvalidArgumentException(sprintf('There is no user with email %s', $email));
73
        }
74
75
        $this->logUserIn($user);
76
    }
77
78
    public function logOut()
79
    {
80
        $this->setSerializedToken(null);
81
82
        $this->cookieSetter->setCookie($this->session->getName(), $this->session->getId());
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function performActionAs(UserInterface $user, callable $action)
89
    {
90
        $previousToken = $this->getToken();
91
        $this->logUserIn($user);
92
        $action();
93
94
        if (null !== $previousToken) {
95
            $this->restorePreviousSessionToken($previousToken);
96
97
            return;
98
        }
99
100
        $this->logOut();
101
    }
102
103
    /**
104
     * @param UserInterface $user
105
     */
106
    private function logUserIn(UserInterface $user)
107
    {
108
        $token = new UsernamePasswordToken($user, $user->getPassword(), 'randomstringbutnotnull', $user->getRoles());
109
        $serializedToken = serialize($token);
110
111
        $this->setSerializedToken($serializedToken);
112
113
        $this->cookieSetter->setCookie($this->session->getName(), $this->session->getId());
114
    }
115
116
    /**
117
     * @param string $token
118
     */
119
    private function restorePreviousSessionToken($token)
120
    {
121
        $this->setSerializedToken($token);
122
123
        $this->cookieSetter->setCookie($this->session->getName(), $this->session->getId());
124
    }
125
126
    /**
127
     * @param string $token
128
     */
129
    private function setSerializedToken($token)
130
    {
131
        $this->session->set($this->sessionTokenVariable, $token);
132
        $this->session->save();
133
    }
134
135
    /**
136
     * @return string
137
     */
138
    private function getToken()
139
    {
140
        return $this->session->get($this->sessionTokenVariable);
141
    }
142
}
143