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

ImpersonateUserController   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 0
Metric Value
wmc 5
c 0
b 0
f 0
lcom 1
cbo 10
dl 0
loc 91
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 1
A impersonateAction() 0 17 3
A addFlash() 0 6 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\AdminBundle\Controller;
13
14
use Sylius\Bundle\CoreBundle\Security\UserImpersonatorInterface;
15
use Sylius\Bundle\UserBundle\Provider\UserProviderInterface;
16
use Symfony\Component\HttpFoundation\RedirectResponse;
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Component\HttpFoundation\Response;
19
use Symfony\Component\HttpFoundation\Session\Session;
20
use Symfony\Component\HttpKernel\Exception\HttpException;
21
use Symfony\Component\Routing\RouterInterface;
22
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
23
use Symfony\Component\Translation\TranslatorInterface;
24
25
/**
26
 * @author Jan Góralski <[email protected]>
27
 */
28
class ImpersonateUserController
29
{
30
    /**
31
     * @var UserImpersonatorInterface
32
     */
33
    protected $impersonator;
34
35
    /**
36
     * @var AuthorizationCheckerInterface
37
     */
38
    protected $authorizationChecker;
39
40
    /**
41
     * @var UserProviderInterface
42
     */
43
    protected $userProvider;
44
45
    /**
46
     * @var RouterInterface
47
     */
48
    protected $router;
49
50
    /**
51
     * @var TranslatorInterface
52
     */
53
    protected $translator;
54
55
    /**
56
     * @var string
57
     */
58
    protected $authorizationRole;
59
60
    /**
61
     * @param UserImpersonatorInterface $impersonator
62
     * @param AuthorizationCheckerInterface $authorizationChecker
63
     * @param UserProviderInterface $userProvider
64
     * @param RouterInterface $router
65
     * @param TranslatorInterface $translator
66
     * @param string $authorizationRole
67
     */
68
    public function __construct(
69
        UserImpersonatorInterface $impersonator,
70
        AuthorizationCheckerInterface $authorizationChecker,
71
        UserProviderInterface $userProvider,
72
        RouterInterface $router,
73
        TranslatorInterface $translator,
74
        $authorizationRole
75
    ) {
76
        $this->impersonator = $impersonator;
77
        $this->authorizationChecker = $authorizationChecker;
78
        $this->userProvider = $userProvider;
79
        $this->router = $router;
80
        $this->translator = $translator;
81
        $this->authorizationRole = $authorizationRole;
82
    }
83
84
    /**
85
     * @param Request $request
86
     * @param string $username
87
     *
88
     * @return Response
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use RedirectResponse.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
89
     */
90
    public function impersonateAction(Request $request, $username)
91
    {
92
        if (!$this->authorizationChecker->isGranted($this->authorizationRole)) {
93
            throw new HttpException(Response::HTTP_UNAUTHORIZED);
94
        }
95
96
        $user = $this->userProvider->loadUserByUsername($username);
97
        if (null === $user) {
98
            throw new HttpException(Response::HTTP_NOT_FOUND);
99
        }
100
101
        $this->impersonator->impersonate($user);
102
103
        $this->addFlash($request, $username);
104
105
        return new RedirectResponse($request->headers->get('referer'));
106
    }
107
108
    /**
109
     * @param Request $request
110
     * @param string $username
111
     */
112
    private function addFlash(Request $request, $username)
113
    {
114
        /** @var Session $session */
115
        $session = $request->getSession();
116
        $session->getFlashBag()->add('success', $this->translator->trans('sylius.impersonation.success', ['%name%' => $username], 'flashes'));
117
    }
118
}
119