Repository navigation
Expand file tree
/
Copy pathDataCollector.php
More file actions
82 lines (67 loc) · 2.89 KB
/
Copy pathDataCollector.php
File metadata and controls
82 lines (67 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
declare(strict_types=1);
namespace MsgPhp\User\Infra\Security;
use MsgPhp\Domain\Exception\EntityNotFoundException;
use MsgPhp\User\Repository\UserRepositoryInterface;
use MsgPhp\User\UserIdInterface;
use Symfony\Bundle\SecurityBundle\DataCollector\SecurityDataCollector as BaseDataCollector;
use Symfony\Bundle\SecurityBundle\Debug\TraceableFirewallListener;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
use Symfony\Component\Security\Core\Role\RoleHierarchyInterface;
use Symfony\Component\Security\Core\Role\SwitchUserRole;
use Symfony\Component\Security\Http\FirewallMapInterface;
use Symfony\Component\Security\Http\Logout\LogoutUrlGenerator;
/**
* @author Roland Franssen <franssen.roland@gmail.com>
*
* @todo consider decorating instead
*
* @internal
*/
final class DataCollector extends BaseDataCollector
{
private $repository;
public function __construct(TokenStorageInterface $tokenStorage = null, RoleHierarchyInterface $roleHierarchy = null, LogoutUrlGenerator $logoutUrlGenerator = null, AccessDecisionManagerInterface $accessDecisionManager = null, FirewallMapInterface $firewallMap = null, TraceableFirewallListener $firewall = null, UserRepositoryInterface $repository = null)
{
parent::__construct($tokenStorage, $roleHierarchy, $logoutUrlGenerator, $accessDecisionManager, $firewallMap, $firewall);
$this->repository = $repository;
}
public function collect(Request $request, Response $response, \Exception $exception = null): void
{
parent::collect($request, $response, $exception);
if (!isset($this->data['token'])) {
return;
}
/** @var TokenInterface $token */
$token = $this->data['token'];
$user = $token->getUser();
if ($user instanceof SecurityUser) {
$this->data['user'] = $this->getUsername($user->getUserId());
}
foreach ($token->getRoles() as $role) {
if (!$role instanceof SwitchUserRole) {
continue;
}
$impersonatorUser = $role->getSource()->getUser();
if ($impersonatorUser instanceof SecurityUser) {
$this->data['impersonator_user'] = $this->getUsername($impersonatorUser->getUserId());
break;
}
}
}
private function getUsername(UserIdInterface $id): string
{
if (null === $this->repository) {
return $id->toString();
}
try {
return $this->repository->find($id)->getCredential()->getUsername();
} catch (EntityNotFoundException $e) {
return $id->toString();
}
}
}