|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the login-cidadao project or it's bundles. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Guilherme Donato <guilhermednt on github> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace LoginCidadao\APIBundle\Event; |
|
12
|
|
|
|
|
13
|
|
|
use LoginCidadao\OAuthBundle\Entity\AccessTokenRepository; |
|
14
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
|
15
|
|
|
use FOS\OAuthServerBundle\Security\Authentication\Token\OAuthToken; |
|
16
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseEvent; |
|
17
|
|
|
use SimpleThings\EntityAudit\AuditConfiguration; |
|
18
|
|
|
use Symfony\Component\HttpKernel\HttpKernel; |
|
19
|
|
|
use LoginCidadao\OAuthBundle\Model\ClientUser; |
|
20
|
|
|
|
|
21
|
|
|
class LoggedInUserListener |
|
22
|
|
|
{ |
|
23
|
|
|
/** @var AccessTokenRepository */ |
|
24
|
|
|
private $accessTokenRepository; |
|
25
|
|
|
|
|
26
|
|
|
/** @var TokenStorageInterface */ |
|
27
|
|
|
private $tokenStorage; |
|
28
|
|
|
|
|
29
|
|
|
/** @var AuditConfiguration */ |
|
30
|
|
|
private $auditConfig; |
|
31
|
|
|
|
|
32
|
3 |
|
public function __construct( |
|
33
|
|
|
AccessTokenRepository $accessTokenRepository, |
|
34
|
|
|
TokenStorageInterface $tokenStorage, |
|
35
|
|
|
AuditConfiguration $auditConfig |
|
36
|
|
|
) { |
|
37
|
3 |
|
$this->accessTokenRepository = $accessTokenRepository; |
|
38
|
3 |
|
$this->tokenStorage = $tokenStorage; |
|
39
|
3 |
|
$this->auditConfig = $auditConfig; |
|
40
|
3 |
|
} |
|
41
|
|
|
|
|
42
|
3 |
|
public function onKernelRequest(GetResponseEvent $event) |
|
43
|
|
|
{ |
|
44
|
3 |
|
if (HttpKernel::MASTER_REQUEST !== $event->getRequestType()) { |
|
45
|
|
|
// don't do anything if it's not the master request |
|
46
|
1 |
|
return; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
2 |
|
$token = $this->tokenStorage->getToken(); |
|
50
|
2 |
|
if (!$token instanceof OAuthToken) { |
|
51
|
1 |
|
return; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
1 |
|
$this->auditConfig->setCurrentUsername("OAuthToken:{$token->getToken()}"); |
|
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
// Handling Client Credentials |
|
57
|
1 |
|
if ($token->getUser() === null) { |
|
58
|
1 |
|
$accessToken = $this->accessTokenRepository->findOneBy(['token' => $token->getToken()]); |
|
59
|
1 |
|
$client = $accessToken->getClient(); |
|
60
|
|
|
|
|
61
|
1 |
|
$token->setUser(new ClientUser($client)); |
|
62
|
|
|
} |
|
63
|
1 |
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|