Issues (12)

src/User/UserProvider.php (3 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\Role\SwitchUserRole;
14
use Symfony\Component\Security\Core\Security;
15
use Symfony\Component\Security\Core\User\UserInterface;
16
17
class UserProvider implements UserProviderInterface
18
{
19
    private Security $security;
20
21
    private Configuration $configuration;
22
23
    public function __construct(Security $security, Configuration $configuration)
24
    {
25
        $this->security = $security;
26
        $this->configuration = $configuration;
27
    }
28
29
    public function __invoke(): ?AuditorUserInterface
30
    {
31
        $tokenUser = $this->getTokenUser();
32
        $impersonatorUser = $this->getImpersonatorUser();
33
34
        $identifier = null;
35
        $username = null;
36
37
        if (null !== $tokenUser && $tokenUser instanceof UserInterface) {
38
            if (method_exists($tokenUser, 'getId')) {
39
                $identifier = $tokenUser->getId();
40
            }
41
42
            $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

42
            $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...
43
        }
44
45
        if (null !== $impersonatorUser && $impersonatorUser instanceof UserInterface) {
46
            $username .= sprintf('[impersonator %s]', $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

46
            $username .= sprintf('[impersonator %s]', /** @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...
47
        }
48
49
        if (null === $identifier && null === $username) {
50
            return null;
51
        }
52
53
        return new User($identifier, $username);
54
    }
55
56
    private function getTokenUser(): ?UserInterface
57
    {
58
        try {
59
            $token = $this->security->getToken();
60
        } catch (Exception $e) {
61
            $token = null;
62
        }
63
64
        if (null === $token) {
65
            return null;
66
        }
67
68
        $tokenUser = $token->getUser();
69
        if ($tokenUser instanceof UserInterface) {
70
            return $tokenUser;
71
        }
72
73
        return null;
74
    }
75
76
    /**
77
     * @return null|string|UserInterface
78
     */
79
    private function getImpersonatorUser()
80
    {
81
        $token = $this->security->getToken();
82
83
        // Symfony >= 5
84
        if (class_exists(SwitchUserToken::class) && $token instanceof SwitchUserToken) {
85
            return $token->getOriginalToken()->getUser();
86
        }
87
88
        // Symfony < 5
89
        $roles = [];
90
        if (null !== $token) {
91
            $roles = method_exists($token, 'getRoleNames') ? $token->getRoleNames() : $token->getRoles();
92
        }
93
94
        foreach ($roles as $role) {
95
            if ($role instanceof SwitchUserRole) {
96
                return $role->getSource()->getUser();
0 ignored issues
show
The method getSource() does not exist on Symfony\Component\Securi...ore\Role\SwitchUserRole. ( Ignorable by Annotation )

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

96
                return $role->/** @scrutinizer ignore-call */ getSource()->getUser();

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...
97
            }
98
        }
99
100
        return null;
101
    }
102
}
103