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