UserAwareComponent::getUser()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 5
c 2
b 0
f 0
dl 0
loc 11
rs 10
cc 3
nc 3
nop 0
1
<?php
2
3
/*
4
 * This file is part of the XiideaEasyAuditBundle package.
5
 *
6
 * (c) Xiidea <http://www.xiidea.net>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Xiidea\EasyAuditBundle\Common;
13
14
use Symfony\Component\HttpFoundation\RequestStack;
15
use Symfony\Component\Security\Core\Authentication\Token\SwitchUserToken;
16
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
17
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
18
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
19
20
class UserAwareComponent
21
{
22
    /**
23
     * @var TokenStorageInterface
24
     */
25
    private $tokenStorage;
26
27
    /**
28
     * @var AuthorizationCheckerInterface
29
     */
30
    private $authChecker;
31
32
    /**
33
     * @var RequestStack
34
     */
35
    private $requestStack;
36
37
    /**
38
     * @param TokenStorageInterface $tokenStorage
39
     */
40
    public function setTokenStorage($tokenStorage)
41
    {
42
        $this->tokenStorage = $tokenStorage;
43
    }
44
45
    /**
46
     * Get a user from the Security Context.
47
     *
48
     * @return mixed
49
     *
50
     * @throws \LogicException If SecurityBundle is not available
51
     */
52
    public function getUser()
53
    {
54
        if (null === $token = $this->tokenStorage->getToken()) {
55
            return null;
56
        }
57
58
        if (!is_object($user = $token->getUser())) {
59
            return null;
60
        }
61
62
        return $user;
63
    }
64
65
    /**
66
     * @param AuthorizationCheckerInterface $authChecker
67
     */
68
    public function setAuthChecker($authChecker)
69
    {
70
        $this->authChecker = $authChecker;
71
    }
72
73
    /**
74
     * @param RequestStack $requestStack
75
     */
76
    public function setRequestStack($requestStack)
77
    {
78
        $this->requestStack = $requestStack;
79
    }
80
81
    /**
82
     * @return mixed
83
     */
84
    final protected function getImpersonatingUser()
85
    {
86
        if (null === $token = $this->tokenStorage->getToken()) {
87
            return null;
88
        }
89
90
        if ($this->authChecker->isGranted('IS_IMPERSONATOR')) {
91
            return $this->getImpersonatingUserFromRole($token);
92
        }
93
94
        return null;
95
    }
96
97
    /**
98
     * @return string
99
     */
100
    public function getUsername()
101
    {
102
        $user = $this->getUser();
103
104
        if (empty($user)) {
105
            return $this->getAnonymousUserName();
106
        }
107
108
        return $user->getUsername();
0 ignored issues
show
Bug introduced by
The method getUsername() does not exist on Symfony\Component\Security\Core\User\UserInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

108
        return $user->/** @scrutinizer ignore-call */ getUsername();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
109
    }
110
111
    /**
112
     * @return string
113
     */
114
    protected function getAnonymousUserName()
115
    {
116
        $request = $this->getRequest();
117
118
        if ($request && $request->getClientIp()) {
119
            return 'Anonymous';
120
        }
121
122
        return 'By Command';
123
    }
124
125
    /**
126
     * @param TokenInterface $token
127
     * @param null           $user
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $user is correct as it would always require null to be passed?
Loading history...
128
     *
129
     * @return mixed
130
     */
131
    protected function getImpersonatingUserFromRole($token, $user = null)
132
    {
133
        if ($token instanceof SwitchUserToken) {
134
            $user = $token->getOriginalToken()->getUser();
135
        }
136
137
        return $user;
138
    }
139
140
    protected function getRequest()
141
    {
142
        if (null === $this->requestStack) {
143
            return false;
144
        }
145
146
        return $this->requestStack->getCurrentRequest();
147
    }
148
}
149