• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

xiidea / EasyAuditBundle / 26936826981

04 Jun 2026 07:13AM UTC coverage: 99.8% (-0.2%) from 100.0%
26936826981

Pull #68

github

web-flow
Merge 3e9ade3e1 into 98e85550a
Pull Request #68: Add Symfony 8 support

2 of 2 new or added lines in 1 file covered. (100.0%)

1 existing line in 1 file now uncovered.

498 of 499 relevant lines covered (99.8%)

15.73 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.55
/Common/UserAwareComponent.php
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;
80✔
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()) {
48✔
55
            return null;
14✔
56
        }
57

58
        if (!is_object($user = $token->getUser())) {
34✔
59
            return null;
4✔
60
        }
61

62
        return $user;
30✔
63
    }
64

65
    /**
66
     * @param AuthorizationCheckerInterface $authChecker
67
     */
68
    public function setAuthChecker($authChecker)
69
    {
70
        $this->authChecker = $authChecker;
42✔
71
    }
72

73
    /**
74
     * @param RequestStack $requestStack
75
     */
76
    public function setRequestStack($requestStack)
77
    {
78
        $this->requestStack = $requestStack;
36✔
79
    }
80

81
    /**
82
     * @return mixed
83
     */
84
    final protected function getImpersonatingUser()
85
    {
86
        if (null === $token = $this->tokenStorage->getToken()) {
30✔
87
            return null;
2✔
88
        }
89

90
        if ($this->authChecker->isGranted('IS_IMPERSONATOR')) {
28✔
91
            return $this->getImpersonatingUserFromRole($token);
6✔
92
        }
93

94
        return null;
24✔
95
    }
96

97
    /**
98
     * @return string
99
     */
100
    public function getUsername()
101
    {
102
        $user = $this->getUser();
4✔
103
        if (empty($user)) {
4✔
104
            return $this->getAnonymousUserName();
2✔
105
        }
106
        if (method_exists($user, 'getUserIdentifier')) {
2✔
107
            return $user->getUserIdentifier();
2✔
108
        }
109

UNCOV
110
        return $user->getUsername();
×
111
    }
112

113
    /**
114
     * @return string
115
     */
116
    protected function getAnonymousUserName()
117
    {
118
        $request = $this->getRequest();
14✔
119

120
        if ($request && $request->getClientIp()) {
14✔
121
            return 'Anonymous';
6✔
122
        }
123

124
        return 'By Command';
8✔
125
    }
126

127
    /**
128
     * @param TokenInterface $token
129
     * @param null           $user
130
     *
131
     * @return mixed
132
     */
133
    protected function getImpersonatingUserFromRole($token, $user = null)
134
    {
135
        if ($token instanceof SwitchUserToken) {
6✔
136
            $user = $token->getOriginalToken()->getUser();
6✔
137
        }
138

139
        return $user;
6✔
140
    }
141

142
    protected function getRequest()
143
    {
144
        if (null === $this->requestStack) {
40✔
145
            return false;
6✔
146
        }
147

148
        return $this->requestStack->getCurrentRequest();
34✔
149
    }
150
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc