|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright 2015 SURFnet B.V. |
|
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 OpenConext\ProfileBundle\Service; |
|
20
|
|
|
|
|
21
|
|
|
use OpenConext\Profile\Entity\User; |
|
22
|
|
|
use OpenConext\Profile\Api\AuthenticatedUserProvider as AuthenticatedUserProviderInterface; |
|
23
|
|
|
use OpenConext\Profile\Repository\UserRepository; |
|
24
|
|
|
use OpenConext\Profile\Value\EntityId; |
|
25
|
|
|
use OpenConext\Profile\Value\Locale; |
|
26
|
|
|
use OpenConext\ProfileBundle\Profile\Command\ChangeLocaleCommand; |
|
27
|
|
|
use OpenConext\UserLifecycleApiClientBundle\Http\JsonApiClient as UserLifecycleApiClient; |
|
28
|
|
|
|
|
29
|
|
|
final class UserService |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* @var SupportContactEmailService |
|
33
|
|
|
*/ |
|
34
|
|
|
private $supportContactEmailService; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var UserRepository |
|
38
|
|
|
*/ |
|
39
|
|
|
private $userRepository; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var AuthenticatedUserProviderInterface |
|
43
|
|
|
*/ |
|
44
|
|
|
private $authenticatedUserProvider; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var LocaleService |
|
48
|
|
|
*/ |
|
49
|
|
|
private $localeService; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @var EntityId |
|
53
|
|
|
*/ |
|
54
|
|
|
private $engineBlockEntityId; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @var UserLifecycleApiClient |
|
58
|
|
|
*/ |
|
59
|
|
|
private $userLifecycleApiClient; |
|
60
|
|
|
|
|
61
|
|
|
public function __construct( |
|
62
|
|
|
SupportContactEmailService $supportContactEmailService, |
|
63
|
|
|
UserRepository $userRepository, |
|
64
|
|
|
AuthenticatedUserProviderInterface $authenticatedUserProvider, |
|
65
|
|
|
LocaleService $localeService, |
|
66
|
|
|
EntityId $engineBlockEntityId |
|
67
|
|
|
) { |
|
68
|
|
|
$this->supportContactEmailService = $supportContactEmailService; |
|
69
|
|
|
$this->userRepository = $userRepository; |
|
70
|
|
|
$this->authenticatedUserProvider = $authenticatedUserProvider; |
|
71
|
|
|
$this->localeService = $localeService; |
|
72
|
|
|
$this->engineBlockEntityId = $engineBlockEntityId; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* The user lifecycle client is optional. |
|
77
|
|
|
* |
|
78
|
|
|
* @param UserLifecycleApiClient $client |
|
|
|
|
|
|
79
|
|
|
*/ |
|
80
|
|
|
public function setUserLifecycleApiClient(UserLifecycleApiClient $client = null) |
|
81
|
|
|
{ |
|
82
|
|
|
$this->userLifecycleApiClient = $client; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @return User |
|
87
|
|
|
*/ |
|
88
|
|
|
public function getUser() |
|
89
|
|
|
{ |
|
90
|
|
|
$user = $this->userRepository->findUser(); |
|
91
|
|
|
|
|
92
|
|
|
if ($user) { |
|
93
|
|
|
return $user; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
$user = new User($this->authenticatedUserProvider->getCurrentUser(), $this->localeService->getLocale()); |
|
97
|
|
|
$user = $this->enrichUserWithSupportContactEmail($user); |
|
98
|
|
|
|
|
99
|
|
|
$this->userRepository->save($user); |
|
100
|
|
|
|
|
101
|
|
|
return $user; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @param ChangeLocaleCommand $changeLocaleCommand |
|
106
|
|
|
*/ |
|
107
|
|
|
public function changeLocale(ChangeLocaleCommand $changeLocaleCommand) |
|
108
|
|
|
{ |
|
109
|
|
|
$user = $this->getUser(); |
|
110
|
|
|
$user->switchLocaleTo(new Locale($changeLocaleCommand->newLocale)); |
|
111
|
|
|
|
|
112
|
|
|
$this->localeService->saveLocaleOf($user); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @return array |
|
117
|
|
|
*/ |
|
118
|
|
|
public function getUserLifecycleData() |
|
119
|
|
|
{ |
|
120
|
|
|
if (!$this->userLifecycleApiIsEnabled()) { |
|
121
|
|
|
return; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
$user = $this->getUser(); |
|
125
|
|
|
|
|
126
|
|
|
return $this->userLifecycleApiClient->read( |
|
127
|
|
|
sprintf('/api/deprovision/%s', $user->getId()) |
|
128
|
|
|
); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @return bool |
|
133
|
|
|
*/ |
|
134
|
|
|
public function userLifecycleApiIsEnabled() |
|
135
|
|
|
{ |
|
136
|
|
|
return $this->userLifecycleApiClient !== null; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @param User $user |
|
141
|
|
|
* @return User |
|
142
|
|
|
*/ |
|
143
|
|
|
private function enrichUserWithSupportContactEmail(User $user) |
|
144
|
|
|
{ |
|
145
|
|
|
$entityIds = $this->authenticatedUserProvider->getCurrentUser()->getAuthenticatingAuthorities(); |
|
146
|
|
|
$authenticatingIdpEntityId = $this->getNearestAuthenticatingAuthorityEntityId($entityIds); |
|
147
|
|
|
|
|
148
|
|
|
if ($authenticatingIdpEntityId === null) { |
|
149
|
|
|
return $user; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
$supportContactEmail = $this->supportContactEmailService->findSupportContactEmailForIdp( |
|
153
|
|
|
$authenticatingIdpEntityId |
|
154
|
|
|
); |
|
155
|
|
|
|
|
156
|
|
|
if ($supportContactEmail === null) { |
|
157
|
|
|
return $user; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
return $user->withSupportContactEmail($supportContactEmail); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @param EntityId[] $entityIds |
|
165
|
|
|
* @return null|EntityId |
|
166
|
|
|
*/ |
|
167
|
|
|
private function getNearestAuthenticatingAuthorityEntityId(array $entityIds) |
|
168
|
|
|
{ |
|
169
|
|
|
$lastEntityId = array_pop($entityIds); |
|
170
|
|
|
|
|
171
|
|
|
if ($lastEntityId === null) { |
|
172
|
|
|
return null; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
if (!$this->engineBlockEntityId->equals($lastEntityId)) { |
|
176
|
|
|
return $lastEntityId; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
return array_pop($entityIds); |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
|
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.