|
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\RemoteClaimsBundle\EventSubscriber; |
|
12
|
|
|
|
|
13
|
|
|
use JMS\Serializer\EventDispatcher\EventSubscriberInterface; |
|
14
|
|
|
use JMS\Serializer\EventDispatcher\ObjectEvent; |
|
15
|
|
|
use JMS\Serializer\EventDispatcher\Events; |
|
16
|
|
|
use JMS\Serializer\GenericSerializationVisitor; |
|
17
|
|
|
use LoginCidadao\APIBundle\Service\VersionService; |
|
18
|
|
|
use LoginCidadao\OAuthBundle\Model\AccessTokenManager; |
|
19
|
|
|
use LoginCidadao\CoreBundle\Model\PersonInterface; |
|
20
|
|
|
use LoginCidadao\RemoteClaimsBundle\Exception\ClaimUriUnavailableException; |
|
21
|
|
|
use LoginCidadao\RemoteClaimsBundle\Model\RemoteClaimAuthorizationInterface; |
|
22
|
|
|
use LoginCidadao\RemoteClaimsBundle\Model\RemoteClaimFetcherInterface; |
|
23
|
|
|
use LoginCidadao\RemoteClaimsBundle\Model\RemoteClaimInterface; |
|
24
|
|
|
use LoginCidadao\RemoteClaimsBundle\Model\RemoteClaimManagerInterface; |
|
25
|
|
|
|
|
26
|
|
|
class PersonSerializeEventSubscriber implements EventSubscriberInterface |
|
27
|
|
|
{ |
|
28
|
|
|
/** @var RemoteClaimManagerInterface */ |
|
29
|
|
|
private $remoteClaimManager; |
|
30
|
|
|
|
|
31
|
|
|
/** @var AccessTokenManager */ |
|
32
|
|
|
private $accessTokenManager; |
|
33
|
|
|
|
|
34
|
|
|
/** @var RemoteClaimFetcherInterface */ |
|
35
|
|
|
private $fetcher; |
|
36
|
|
|
|
|
37
|
|
|
/** @var VersionService */ |
|
38
|
|
|
private $versionService; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* PersonSerializeEventSubscriber constructor. |
|
42
|
|
|
* @param AccessTokenManager $accessTokenManager |
|
43
|
|
|
* @param RemoteClaimManagerInterface $remoteClaimManager |
|
44
|
|
|
* @param RemoteClaimFetcherInterface $fetcher |
|
45
|
|
|
* @param VersionService $versionService |
|
46
|
|
|
*/ |
|
47
|
7 |
|
public function __construct( |
|
48
|
|
|
AccessTokenManager $accessTokenManager, |
|
49
|
|
|
RemoteClaimManagerInterface $remoteClaimManager, |
|
50
|
|
|
RemoteClaimFetcherInterface $fetcher, |
|
51
|
|
|
VersionService $versionService |
|
52
|
|
|
) { |
|
53
|
7 |
|
$this->accessTokenManager = $accessTokenManager; |
|
54
|
7 |
|
$this->remoteClaimManager = $remoteClaimManager; |
|
55
|
7 |
|
$this->fetcher = $fetcher; |
|
56
|
7 |
|
$this->versionService = $versionService; |
|
57
|
7 |
|
} |
|
58
|
|
|
|
|
59
|
1 |
|
public static function getSubscribedEvents() |
|
60
|
|
|
{ |
|
61
|
|
|
return [ |
|
62
|
|
|
[ |
|
63
|
1 |
|
'event' => Events::POST_SERIALIZE, |
|
64
|
|
|
'method' => 'onPostSerialize', |
|
65
|
|
|
'class' => 'LoginCidadao\CoreBundle\Model\PersonInterface', |
|
66
|
|
|
], |
|
67
|
|
|
]; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
7 |
|
public function onPostSerialize(ObjectEvent $event) |
|
71
|
|
|
{ |
|
72
|
7 |
|
if (!$event->getObject() instanceof PersonInterface) { |
|
73
|
2 |
|
return; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
5 |
|
if ($this->checkVersion()) { |
|
77
|
4 |
|
$this->addDistributedAndAggregatedClaims($event); |
|
78
|
|
|
} |
|
79
|
5 |
|
} |
|
80
|
|
|
|
|
81
|
4 |
|
private function addDistributedAndAggregatedClaims(ObjectEvent $event) |
|
82
|
|
|
{ |
|
83
|
4 |
|
$visitor = $event->getVisitor(); |
|
84
|
4 |
|
if (!$visitor instanceof GenericSerializationVisitor) { |
|
85
|
2 |
|
return; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
2 |
|
$person = $event->getObject(); |
|
89
|
2 |
|
$client = $this->accessTokenManager->getTokenClient(); |
|
90
|
|
|
|
|
91
|
2 |
|
$remoteClaims = $this->remoteClaimManager->getRemoteClaimsWithTokens($client, $person); |
|
92
|
|
|
|
|
93
|
2 |
|
$claimNames = []; |
|
94
|
2 |
|
$claimSources = []; |
|
95
|
2 |
|
foreach ($remoteClaims as $remoteClaim) { |
|
96
|
|
|
/** @var RemoteClaimInterface $claim */ |
|
97
|
2 |
|
$claim = $remoteClaim['remoteClaim']; |
|
98
|
|
|
|
|
99
|
|
|
/** @var RemoteClaimAuthorizationInterface $claimAuthorization */ |
|
100
|
2 |
|
$claimAuthorization = $remoteClaim['authorization']; |
|
101
|
|
|
|
|
102
|
2 |
|
$name = (string)$claim->getName(); |
|
103
|
|
|
try { |
|
104
|
2 |
|
$endpoint = $this->fetcher->discoverClaimUri($claim->getName()); |
|
105
|
1 |
|
} catch (ClaimUriUnavailableException $e) { |
|
106
|
|
|
// TODO: log error |
|
107
|
1 |
|
continue; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
1 |
|
$claimNames[$name] = $name; |
|
111
|
1 |
|
$claimSources[$name] = [ |
|
112
|
1 |
|
'endpoint' => $endpoint, |
|
113
|
1 |
|
'access_token' => $claimAuthorization->getAccessToken(), |
|
114
|
|
|
]; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
2 |
|
$visitor->addData('_claim_names', $claimNames); |
|
118
|
2 |
|
$visitor->addData('_claim_sources', $claimSources); |
|
119
|
2 |
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @return bool |
|
123
|
|
|
*/ |
|
124
|
5 |
|
private function checkVersion() |
|
125
|
|
|
{ |
|
126
|
5 |
|
$version = $this->versionService->getVersionFromRequest(); |
|
127
|
5 |
|
$version = $this->versionService->getString($version); |
|
128
|
|
|
|
|
129
|
5 |
|
return version_compare($version, '2.0.0', '>='); |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|