Passed
Pull Request — master (#84)
by Maxime
02:53
created

TokenStorageUserProvider   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
eloc 29
c 6
b 0
f 0
dl 0
loc 52
rs 10
wmc 13

2 Methods

Rating   Name   Duplication   Size   Complexity  
C getUser() 0 43 12
A __construct() 0 3 1
1
<?php
2
3
namespace DH\DoctrineAuditBundle\User;
4
5
use Symfony\Component\Security\Core\Authentication\Token\SwitchUserToken;
6
use Symfony\Component\Security\Core\Role\SwitchUserRole;
7
use Symfony\Component\Security\Core\Security;
8
use Symfony\Component\Security\Core\User\UserInterface as BaseUserInterface;
9
10
class TokenStorageUserProvider implements UserProviderInterface
11
{
12
    private $security;
13
14
    public function __construct(Security $security)
15
    {
16
        $this->security = $security;
17
    }
18
19
    public function getUser(): ?UserInterface
20
    {
21
        $user = null;
22
23
        try {
24
            $token = $this->security->getToken();
25
        } catch (\Exception $e) {
26
            $token = null;
27
        }
28
29
        if (null === $token) {
30
            return null;
31
        }
32
33
        $tokenUser = $token->getUser();
34
        if ($tokenUser instanceof BaseUserInterface) {
35
            $impersonation = '';
36
            if ($this->security->isGranted('ROLE_PREVIOUS_ADMIN')) {
37
                $impersonatorUser = null;
38
                // Symfony > 4.3
39
                if ($token instanceof SwitchUserToken) {
40
                    $impersonatorUser = $token->getOriginalToken()->getUser();
41
                } else {
42
                    foreach ($this->security->getToken()->getRoles() as $role) {
0 ignored issues
show
Deprecated Code introduced by
The function Symfony\Component\Securi...enInterface::getRoles() has been deprecated: since Symfony 4.3, use the getRoleNames() method instead ( Ignorable by Annotation )

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

42
                    foreach (/** @scrutinizer ignore-deprecated */ $this->security->getToken()->getRoles() as $role) {

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
43
                        if ($role instanceof SwitchUserRole) {
44
                            $impersonatorUser = $role->getSource()->getUser();
45
46
                            break;
47
                        }
48
                    }
49
                }
50
51
                if (\is_object($impersonatorUser)) {
52
                    $id = method_exists($impersonatorUser, 'getId') ? $impersonatorUser->getId() : null;
53
                    $username = method_exists($impersonatorUser, 'getUsername') ? $impersonatorUser->getUsername() : (string) $impersonatorUser;
54
                    $impersonation = ' [impersonator '.$username.':'.$id.']';
55
                }
56
            }
57
            $id = method_exists($tokenUser, 'getId') ? $tokenUser->getId() : null;
58
            $user = new User($id, $tokenUser->getUsername().$impersonation);
59
        }
60
61
        return $user;
62
    }
63
}
64