|
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\OAuthBundle\Model\AccessTokenManager; |
|
18
|
|
|
use LoginCidadao\CoreBundle\Model\PersonInterface; |
|
19
|
|
|
use LoginCidadao\RemoteClaimsBundle\Model\RemoteClaimAuthorizationInterface; |
|
20
|
|
|
use LoginCidadao\RemoteClaimsBundle\Model\RemoteClaimFetcherInterface; |
|
21
|
|
|
use LoginCidadao\RemoteClaimsBundle\Model\RemoteClaimInterface; |
|
22
|
|
|
use LoginCidadao\RemoteClaimsBundle\Model\RemoteClaimManagerInterface; |
|
23
|
|
|
|
|
24
|
|
|
class PersonSerializeEventSubscriber implements EventSubscriberInterface |
|
25
|
|
|
{ |
|
26
|
|
|
/** @var RemoteClaimManagerInterface */ |
|
27
|
|
|
private $remoteClaimManager; |
|
28
|
|
|
|
|
29
|
|
|
/** @var AccessTokenManager */ |
|
30
|
|
|
private $accessTokenManager; |
|
31
|
|
|
|
|
32
|
|
|
/** @var RemoteClaimFetcherInterface */ |
|
33
|
|
|
private $fetcher; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* PersonSerializeEventSubscriber constructor. |
|
37
|
|
|
* @param AccessTokenManager $accessTokenManager |
|
38
|
|
|
* @param RemoteClaimManagerInterface $remoteClaimManager |
|
39
|
|
|
* @param RemoteClaimFetcherInterface $fetcher |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __construct( |
|
42
|
|
|
AccessTokenManager $accessTokenManager, |
|
43
|
|
|
RemoteClaimManagerInterface $remoteClaimManager, |
|
44
|
|
|
RemoteClaimFetcherInterface $fetcher |
|
45
|
|
|
) { |
|
46
|
|
|
$this->accessTokenManager = $accessTokenManager; |
|
47
|
|
|
$this->remoteClaimManager = $remoteClaimManager; |
|
48
|
|
|
$this->fetcher = $fetcher; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public static function getSubscribedEvents() |
|
52
|
|
|
{ |
|
53
|
|
|
return [ |
|
54
|
|
|
[ |
|
55
|
|
|
'event' => Events::POST_SERIALIZE, |
|
56
|
|
|
'method' => 'onPostSerialize', |
|
57
|
|
|
'class' => 'LoginCidadao\CoreBundle\Model\PersonInterface', |
|
58
|
|
|
], |
|
59
|
|
|
]; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function onPostSerialize(ObjectEvent $event) |
|
63
|
|
|
{ |
|
64
|
|
|
if (!$event->getObject() instanceof PersonInterface) { |
|
65
|
|
|
return; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$this->addDistributedAndAggregatedClaims($event); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
private function addDistributedAndAggregatedClaims(ObjectEvent $event) |
|
72
|
|
|
{ |
|
73
|
|
|
$visitor = $event->getVisitor(); |
|
74
|
|
|
if (!$visitor instanceof GenericSerializationVisitor) { |
|
75
|
|
|
return; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
$person = $event->getObject(); |
|
79
|
|
|
if (!$person instanceof PersonInterface) { |
|
80
|
|
|
return; |
|
81
|
|
|
} |
|
82
|
|
|
$client = $this->accessTokenManager->getTokenClient(); |
|
83
|
|
|
|
|
84
|
|
|
$remoteClaims = $this->remoteClaimManager->getRemoteClaimsWithTokens($client, $person); |
|
85
|
|
|
|
|
86
|
|
|
$claimNames = []; |
|
87
|
|
|
$claimSources = []; |
|
88
|
|
|
foreach ($remoteClaims as $remoteClaim) { |
|
89
|
|
|
/** @var RemoteClaimInterface $claim */ |
|
90
|
|
|
$claim = $remoteClaim['remoteClaim']; |
|
91
|
|
|
|
|
92
|
|
|
/** @var RemoteClaimAuthorizationInterface $claimAuthorization */ |
|
93
|
|
|
$claimAuthorization = $remoteClaim['authorization']; |
|
94
|
|
|
|
|
95
|
|
|
$name = (string)$claim->getName(); |
|
96
|
|
|
$endpoint = $this->fetcher->discoverClaimUri($claim->getName()); |
|
97
|
|
|
|
|
98
|
|
|
$claimNames[$name] = $name; |
|
99
|
|
|
$claimSources[$name] = [ |
|
100
|
|
|
'endpoint' => $endpoint, |
|
101
|
|
|
'access_token' => $claimAuthorization->getAccessToken(), |
|
102
|
|
|
]; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
$visitor->addData('_claim_names', $claimNames); |
|
106
|
|
|
$visitor->addData('_claim_sources', $claimSources); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|