1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2014 SURFnet bv |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
* See the License for the specific language governing permissions and |
16
|
|
|
* limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace Surfnet\StepupRa\RaBundle\Service; |
20
|
|
|
|
21
|
|
|
use Exception; |
22
|
|
|
use Psr\Log\LoggerInterface; |
23
|
|
|
use Surfnet\StepupBundle\Command\SwitchLocaleCommand; |
24
|
|
|
use Surfnet\StepupMiddlewareClient\Identity\Dto\IdentitySearchQuery; |
25
|
|
|
use Surfnet\StepupMiddlewareClientBundle\Identity\Command\ExpressLocalePreferenceCommand; |
26
|
|
|
use Surfnet\StepupMiddlewareClientBundle\Identity\Dto\Identity; |
27
|
|
|
use Surfnet\StepupMiddlewareClientBundle\Identity\Service\IdentityService as ApiIdentityService; |
28
|
|
|
use Surfnet\StepupRa\RaBundle\Exception\RuntimeException; |
29
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
30
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
31
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
32
|
|
|
use Symfony\Component\Security\Core\User\UserProviderInterface; |
33
|
|
|
|
34
|
|
|
class IdentityService implements UserProviderInterface |
35
|
|
|
{ |
36
|
|
|
/** |
37
|
|
|
* @var \Surfnet\StepupMiddlewareClientBundle\Identity\Service\IdentityService |
38
|
|
|
*/ |
39
|
|
|
private $apiIdentityService; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var \Surfnet\StepupRa\RaBundle\Service\CommandService |
43
|
|
|
*/ |
44
|
|
|
private $commandService; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var \Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface |
48
|
|
|
*/ |
49
|
|
|
private $tokenStorage; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var \Psr\Log\LoggerInterface |
53
|
|
|
*/ |
54
|
|
|
private $logger; |
55
|
|
|
|
56
|
|
|
public function __construct( |
57
|
|
|
ApiIdentityService $apiIdentityService, |
58
|
|
|
CommandService $commandService, |
59
|
|
|
TokenStorageInterface $tokenStorage, |
60
|
|
|
LoggerInterface $logger |
61
|
|
|
) { |
62
|
|
|
$this->apiIdentityService = $apiIdentityService; |
63
|
|
|
$this->commandService = $commandService; |
64
|
|
|
$this->tokenStorage = $tokenStorage; |
65
|
|
|
$this->logger = $logger; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* For now this functionality is disabled, unsure if actually needed |
70
|
|
|
* |
71
|
|
|
* If needed, the username is the UUID of the identity so it can be fetched rather easy |
72
|
|
|
*/ |
73
|
|
|
public function loadUserByUsername($username) |
74
|
|
|
{ |
75
|
|
|
throw new RuntimeException(sprintf('Cannot Load User By Username "%s"', $username)); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* For now this functionality is disabled, unsure if actually needed |
80
|
|
|
*/ |
81
|
|
|
public function refreshUser(UserInterface $user) |
82
|
|
|
{ |
83
|
|
|
throw new RuntimeException(sprintf('Cannot Refresh User "%s"', $user->getUsername())); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Whether this provider supports the given user class |
88
|
|
|
* |
89
|
|
|
* @param string $class |
90
|
|
|
* |
91
|
|
|
* @return bool |
92
|
|
|
*/ |
93
|
|
|
public function supportsClass($class) |
94
|
|
|
{ |
95
|
|
|
return $class === 'Surfnet\StepupMiddlewareClientBundle\Identity\Dto\Identity'; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param string $identityId the UUID of the identity to find |
100
|
|
|
* @return null|Identity |
101
|
|
|
*/ |
102
|
|
|
public function findById($identityId) |
103
|
|
|
{ |
104
|
|
|
return $this->apiIdentityService->get($identityId); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param string $nameId |
109
|
|
|
* @return null|\Surfnet\StepupMiddlewareClientBundle\Identity\Dto\Identity |
110
|
|
|
* @throws \Surfnet\StepupRa\RaBundle\Exception\RuntimeException |
111
|
|
|
*/ |
112
|
|
|
public function findByNameId($nameId) |
113
|
|
|
{ |
114
|
|
|
$searchQuery = new IdentitySearchQuery(); |
115
|
|
|
$searchQuery->setNameId($nameId); |
116
|
|
|
|
117
|
|
|
try { |
118
|
|
|
$result = $this->apiIdentityService->search($searchQuery); |
119
|
|
|
} catch (Exception $e) { |
120
|
|
|
$message = sprintf('Exception when searching identity: "%s"', $e->getMessage()); |
121
|
|
|
$this->logger->critical($message); |
122
|
|
|
throw new RuntimeException($message, 0, $e); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$elements = $result->getElements(); |
126
|
|
|
if (count($elements) === 0) { |
127
|
|
|
return null; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
if (count($elements) === 1) { |
131
|
|
|
return reset($elements); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
throw new RuntimeException(sprintf( |
135
|
|
|
'Got an unexpected amount of identities, expected 0 or 1, got "%d"', |
136
|
|
|
count($elements) |
137
|
|
|
)); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param Identity $identity |
142
|
|
|
* @return \Surfnet\StepupMiddlewareClientBundle\Identity\Dto\RegistrationAuthorityCredentials|null |
143
|
|
|
*/ |
144
|
|
|
public function getRaCredentials(Identity $identity) |
145
|
|
|
{ |
146
|
|
|
try { |
147
|
|
|
$credentials = $this->apiIdentityService->getRegistrationAuthorityCredentials($identity); |
148
|
|
|
} catch (Exception $e) { |
149
|
|
|
$message = sprintf('Exception when retrieving RA credentials: "%s"', $e->getMessage()); |
150
|
|
|
$this->logger->critical($message); |
151
|
|
|
|
152
|
|
|
throw new RuntimeException($message, 0, $e); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return $credentials; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param SwitchLocaleCommand $command |
160
|
|
|
* @return bool |
161
|
|
|
*/ |
162
|
|
|
public function switchLocale(SwitchLocaleCommand $command) |
163
|
|
|
{ |
164
|
|
|
/** @var TokenInterface|null */ |
165
|
|
|
$token = $this->tokenStorage->getToken(); |
166
|
|
|
|
167
|
|
|
if (!$token) { |
168
|
|
|
throw new RuntimeException('Cannot switch locales when unauthenticated'); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** @var Identity $identity */ |
172
|
|
|
$identity = $token->getUser(); |
173
|
|
|
|
174
|
|
|
$expressLocalePreferenceCommand = new ExpressLocalePreferenceCommand(); |
175
|
|
|
$expressLocalePreferenceCommand->identityId = $command->identityId; |
176
|
|
|
$expressLocalePreferenceCommand->preferredLocale = $command->locale; |
177
|
|
|
|
178
|
|
|
$result = $this->commandService->execute($expressLocalePreferenceCommand); |
179
|
|
|
|
180
|
|
|
if ($result->isSuccessful()) { |
181
|
|
|
$identity->preferredLocale = $command->locale; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
return $result->isSuccessful(); |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|