Issues (11)

src/User/UserProvider.php (2 issues)

1
<?php
2
3
declare(strict_types=1);
4
5
namespace DH\AuditorBundle\User;
6
7
use DH\Auditor\Provider\Doctrine\Configuration;
8
use DH\Auditor\User\User;
9
use DH\Auditor\User\UserInterface as AuditorUserInterface;
10
use DH\Auditor\User\UserProviderInterface;
11
use Exception;
12
use Symfony\Component\Security\Core\Authentication\Token\SwitchUserToken;
13
use Symfony\Component\Security\Core\Security;
14
use Symfony\Component\Security\Core\User\UserInterface;
15
16
class UserProvider implements UserProviderInterface
17
{
18
    private Security $security;
19
20
    private Configuration $configuration;
21
22
    public function __construct(Security $security, Configuration $configuration)
23
    {
24
        $this->security = $security;
25
        $this->configuration = $configuration;
26
    }
27
28
    public function __invoke(): ?AuditorUserInterface
29
    {
30
        $tokenUser = $this->getTokenUser();
31
        $impersonatorUser = $this->getImpersonatorUser();
32
33
        $identifier = null;
34
        $username = null;
35
36
        if (null !== $tokenUser && $tokenUser instanceof UserInterface) {
37
            if (method_exists($tokenUser, 'getId')) {
38
                $identifier = $tokenUser->getId();
39
            }
40
41
            $username = '';
42
            if (method_exists($tokenUser, 'getUserIdentifier')) {
43
                $username = $tokenUser->getUserIdentifier();
44
            } elseif (method_exists($tokenUser, 'getUsername')) {
45
                $username = $tokenUser->getUsername();
0 ignored issues
show
Deprecated Code introduced by Damien Harper
The function Symfony\Component\Securi...nterface::getUsername() has been deprecated: since Symfony 5.3, use getUserIdentifier() instead ( Ignorable by Annotation )

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

45
                $username = /** @scrutinizer ignore-deprecated */ $tokenUser->getUsername();

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...
46
            }
47
        }
48
49
        if ($impersonatorUser instanceof UserInterface) {
50
            $impersonatorUsername = '';
51
            if (method_exists($impersonatorUser, 'getUserIdentifier')) {
52
                $impersonatorUsername = $impersonatorUser->getUserIdentifier();
53
            } elseif (method_exists($impersonatorUser, 'getUsername')) {
54
                $impersonatorUsername = $impersonatorUser->getUsername();
0 ignored issues
show
Deprecated Code introduced by Damien Harper
The function Symfony\Component\Securi...nterface::getUsername() has been deprecated: since Symfony 5.3, use getUserIdentifier() instead ( Ignorable by Annotation )

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

54
                $impersonatorUsername = /** @scrutinizer ignore-deprecated */ $impersonatorUser->getUsername();

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...
55
            }
56
            $username .= '[impersonator '.$impersonatorUsername.']';
57
        }
58
59
        if (null === $identifier && null === $username) {
60
            return null;
61
        }
62
63
        return new User($identifier, $username);
64
    }
65
66
    private function getTokenUser(): ?UserInterface
67
    {
68
        try {
69
            $token = $this->security->getToken();
70
        } catch (Exception $e) {
71
            $token = null;
72
        }
73
74
        if (null === $token) {
75
            return null;
76
        }
77
78
        $tokenUser = $token->getUser();
79
        if ($tokenUser instanceof UserInterface) {
80
            return $tokenUser;
81
        }
82
83
        return null;
84
    }
85
86
    private function getImpersonatorUser()
87
    {
88
        $token = $this->security->getToken();
89
90
        if (null !== $token && $token instanceof SwitchUserToken) {
91
            return $token->getOriginalToken()->getUser();
92
        }
93
94
        if (null !== $token) {
95
            return $token->getUser();
96
        }
97
98
        return null;
99
    }
100
}
101