1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Circles - Bring cloud-users closer together. |
8
|
|
|
* |
9
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
10
|
|
|
* later. See the COPYING file. |
11
|
|
|
* |
12
|
|
|
* @author Maxence Lange <[email protected]> |
13
|
|
|
* @copyright 2021 |
14
|
|
|
* @license GNU AGPL version 3 or any later version |
15
|
|
|
* |
16
|
|
|
* This program is free software: you can redistribute it and/or modify |
17
|
|
|
* it under the terms of the GNU Affero General Public License as |
18
|
|
|
* published by the Free Software Foundation, either version 3 of the |
19
|
|
|
* License, or (at your option) any later version. |
20
|
|
|
* |
21
|
|
|
* This program is distributed in the hope that it will be useful, |
22
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
23
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
24
|
|
|
* GNU Affero General Public License for more details. |
25
|
|
|
* |
26
|
|
|
* You should have received a copy of the GNU Affero General Public License |
27
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
28
|
|
|
* |
29
|
|
|
*/ |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
namespace OCA\Circles\Service; |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
use daita\MySmallPhpTools\Exceptions\InvalidItemException; |
36
|
|
|
use daita\MySmallPhpTools\Exceptions\RequestNetworkException; |
37
|
|
|
use daita\MySmallPhpTools\Exceptions\SignatoryException; |
38
|
|
|
use daita\MySmallPhpTools\Traits\Nextcloud\nc21\TNC21Logger; |
39
|
|
|
use daita\MySmallPhpTools\Traits\TArrayTools; |
40
|
|
|
use daita\MySmallPhpTools\Traits\TStringTools; |
41
|
|
|
use Exception; |
42
|
|
|
use OCA\Circles\Db\CircleRequest; |
43
|
|
|
use OCA\Circles\Db\MemberRequest; |
44
|
|
|
use OCA\Circles\Db\MembershipRequest; |
45
|
|
|
use OCA\Circles\Exceptions\CircleNotFoundException; |
46
|
|
|
use OCA\Circles\Exceptions\FederatedUserException; |
47
|
|
|
use OCA\Circles\Exceptions\FederatedUserNotFoundException; |
48
|
|
|
use OCA\Circles\Exceptions\InitiatorNotFoundException; |
49
|
|
|
use OCA\Circles\Exceptions\InvalidIdException; |
50
|
|
|
use OCA\Circles\Exceptions\MemberNotFoundException; |
51
|
|
|
use OCA\Circles\Exceptions\OwnerNotFoundException; |
52
|
|
|
use OCA\Circles\Exceptions\RemoteInstanceException; |
53
|
|
|
use OCA\Circles\Exceptions\RemoteNotFoundException; |
54
|
|
|
use OCA\Circles\Exceptions\RemoteResourceNotFoundException; |
55
|
|
|
use OCA\Circles\Exceptions\UnknownRemoteException; |
56
|
|
|
use OCA\Circles\Exceptions\UserTypeNotFoundException; |
57
|
|
|
use OCA\Circles\IFederatedUser; |
58
|
|
|
use OCA\Circles\Model\Circle; |
59
|
|
|
use OCA\Circles\Model\Federated\RemoteInstance; |
60
|
|
|
use OCA\Circles\Model\FederatedUser; |
61
|
|
|
use OCA\Circles\Model\ManagedModel; |
62
|
|
|
use OCA\Circles\Model\Member; |
63
|
|
|
use OCP\IUser; |
64
|
|
|
use OCP\IUserManager; |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Class FederatedUserService |
69
|
|
|
* |
70
|
|
|
* @package OCA\Circles\Service |
71
|
|
|
*/ |
72
|
|
|
class FederatedUserService { |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
use TArrayTools; |
76
|
|
|
use TStringTools; |
77
|
|
|
use TNC21Logger; |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
/** @var IUserManager */ |
81
|
|
|
private $userManager; |
82
|
|
|
|
83
|
|
|
/** @var MembershipRequest */ |
84
|
|
|
private $membershipRequest; |
85
|
|
|
|
86
|
|
|
/** @var CircleRequest */ |
87
|
|
|
private $circleRequest; |
88
|
|
|
|
89
|
|
|
/** @var MemberRequest */ |
90
|
|
|
private $memberRequest; |
91
|
|
|
|
92
|
|
|
/** @var RemoteService */ |
93
|
|
|
private $remoteService; |
94
|
|
|
|
95
|
|
|
/** @var ConfigService */ |
96
|
|
|
private $configService; |
97
|
|
|
|
98
|
|
|
|
99
|
|
|
/** @var FederatedUser */ |
100
|
|
|
private $currentUser = null; |
101
|
|
|
|
102
|
|
|
/** @var RemoteInstance */ |
103
|
|
|
private $remoteInstance = null; |
104
|
|
|
|
105
|
|
|
/** @var bool */ |
106
|
|
|
private $bypass = false; |
107
|
|
|
|
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* FederatedUserService constructor. |
111
|
|
|
* |
112
|
|
|
* @param IUserManager $userManager |
113
|
|
|
* @param MembershipRequest $membershipRequest |
114
|
|
|
* @param CircleRequest $circleRequest |
115
|
|
|
* @param MemberRequest $memberRequest |
116
|
|
|
* @param RemoteService $remoteService |
117
|
|
|
* @param ConfigService $configService |
118
|
|
|
*/ |
119
|
|
|
public function __construct( |
120
|
|
|
IUserManager $userManager, MembershipRequest $membershipRequest, CircleRequest $circleRequest, |
121
|
|
|
MemberRequest $memberRequest, RemoteService $remoteService, ConfigService $configService |
122
|
|
|
) { |
123
|
|
|
$this->userManager = $userManager; |
124
|
|
|
$this->membershipRequest = $membershipRequest; |
125
|
|
|
$this->circleRequest = $circleRequest; |
126
|
|
|
$this->memberRequest = $memberRequest; |
127
|
|
|
$this->remoteService = $remoteService; |
128
|
|
|
$this->configService = $configService; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param IUser|null $user |
134
|
|
|
* |
135
|
|
|
* @throws CircleNotFoundException |
136
|
|
|
* @throws FederatedUserNotFoundException |
137
|
|
|
* @throws InvalidIdException |
138
|
|
|
*/ |
139
|
|
|
public function setLocalCurrentUser(?IUser $user) { |
140
|
|
|
if ($user === null) { |
141
|
|
|
return; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
$this->currentUser = $this->getLocalFederatedUser($user->getUID()); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* set a CurrentUser, based on a IFederatedUser. |
150
|
|
|
* CurrentUser is mainly used to manage rights when requesting the database. |
151
|
|
|
* |
152
|
|
|
* @param IFederatedUser $federatedUser |
153
|
|
|
* |
154
|
|
|
* @throws FederatedUserException |
155
|
|
|
*/ |
156
|
|
|
public function setCurrentUser(IFederatedUser $federatedUser): void { |
157
|
|
|
if (!($federatedUser instanceof FederatedUser)) { |
158
|
|
|
$tmp = new FederatedUser(); |
159
|
|
|
$tmp->importFromIFederatedUser($federatedUser); |
160
|
|
|
$federatedUser = $tmp; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
$this->confirmFederatedUser($federatedUser); |
164
|
|
|
|
165
|
|
|
$this->currentUser = $federatedUser; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @return FederatedUser|null |
170
|
|
|
*/ |
171
|
|
|
public function getCurrentUser(): ?FederatedUser { |
172
|
|
|
return $this->currentUser; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @return bool |
177
|
|
|
*/ |
178
|
|
|
public function hasCurrentUser(): bool { |
179
|
|
|
return !is_null($this->currentUser); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @throws InitiatorNotFoundException |
184
|
|
|
*/ |
185
|
|
|
public function mustHaveCurrentUser(): void { |
186
|
|
|
if ($this->bypass) { |
187
|
|
|
return; |
188
|
|
|
} |
189
|
|
|
if (!$this->hasCurrentUser() && !$this->hasRemoteInstance()) { |
190
|
|
|
throw new InitiatorNotFoundException(); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @param bool $bypass |
196
|
|
|
*/ |
197
|
|
|
public function bypassCurrentUserCondition(bool $bypass): void { |
198
|
|
|
$this->bypass = $bypass; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* set a RemoteInstance, mostly from a remote request (RemoteController) |
204
|
|
|
* Used to limit rights in some request in the local database. |
205
|
|
|
* |
206
|
|
|
* @param RemoteInstance $remoteInstance |
207
|
|
|
*/ |
208
|
|
|
public function setRemoteInstance(RemoteInstance $remoteInstance): void { |
209
|
|
|
$this->remoteInstance = $remoteInstance; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @return RemoteInstance|null |
214
|
|
|
*/ |
215
|
|
|
public function getRemoteInstance(): ?RemoteInstance { |
216
|
|
|
return $this->remoteInstance; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* @return bool |
221
|
|
|
*/ |
222
|
|
|
public function hasRemoteInstance(): bool { |
223
|
|
|
return !is_null($this->remoteInstance); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Get the full FederatedUser for a local user. |
229
|
|
|
* Will generate the SingleId if none exist |
230
|
|
|
* |
231
|
|
|
* @param string $userId |
232
|
|
|
* |
233
|
|
|
* @return FederatedUser |
234
|
|
|
* @throws CircleNotFoundException |
235
|
|
|
* @throws FederatedUserNotFoundException |
236
|
|
|
* @throws InvalidIdException |
237
|
|
|
*/ |
238
|
|
|
public function getLocalFederatedUser(string $userId): FederatedUser { |
239
|
|
|
$user = $this->userManager->get($userId); |
240
|
|
|
if ($user === null) { |
241
|
|
|
throw new FederatedUserNotFoundException('user ' . $userId . ' not found'); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
$federatedUser = new FederatedUser(); |
245
|
|
|
$federatedUser->set($user->getUID()); |
246
|
|
|
$this->fillSingleCircleId($federatedUser); |
247
|
|
|
|
248
|
|
|
return $federatedUser; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* some ./occ commands allows to add an Initiator, or force the PoV from the local circles' owner |
254
|
|
|
* |
255
|
|
|
* TODO: manage non-user type ? |
256
|
|
|
* |
257
|
|
|
* @param string $userId |
258
|
|
|
* @param string $circleId |
259
|
|
|
* @param bool $bypass |
260
|
|
|
* |
261
|
|
|
* @throws CircleNotFoundException |
262
|
|
|
* @throws FederatedUserException |
263
|
|
|
* @throws FederatedUserNotFoundException |
264
|
|
|
* @throws InvalidIdException |
265
|
|
|
* @throws InvalidItemException |
266
|
|
|
* @throws OwnerNotFoundException |
267
|
|
|
* @throws RemoteInstanceException |
268
|
|
|
* @throws RemoteNotFoundException |
269
|
|
|
* @throws RemoteResourceNotFoundException |
270
|
|
|
* @throws RequestNetworkException |
271
|
|
|
* @throws SignatoryException |
272
|
|
|
* @throws UnknownRemoteException |
273
|
|
|
* @throws UserTypeNotFoundException |
274
|
|
|
* @throws MemberNotFoundException |
275
|
|
|
*/ |
276
|
|
|
public function commandLineInitiator(string $userId, string $circleId = '', bool $bypass = false): void { |
277
|
|
|
if ($userId !== '') { |
278
|
|
|
$this->setCurrentUser($this->getFederatedUser($userId)); |
279
|
|
|
|
280
|
|
|
return; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
if ($circleId !== '') { |
284
|
|
|
$localCircle = $this->circleRequest->getCircle($circleId); |
285
|
|
|
if ($this->configService->isLocalInstance($localCircle->getInstance())) { |
286
|
|
|
// TODO: manage NO_OWNER circles |
287
|
|
|
$this->setCurrentUser($localCircle->getOwner()); |
288
|
|
|
|
289
|
|
|
return; |
290
|
|
|
} |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
if (!$bypass) { |
294
|
|
|
throw new CircleNotFoundException( |
295
|
|
|
'This Circle is not managed from this instance, please use --initiator' |
296
|
|
|
); |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
$this->bypassCurrentUserCondition($bypass); |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* Works like getFederatedUser, but returns a member. |
305
|
|
|
* Allow to specify a level: handle@instance,level |
306
|
|
|
* |
307
|
|
|
* Used for filters when searching for Circles |
308
|
|
|
* TODO: Used outside of ./occ circles:manage:list ? |
309
|
|
|
* |
310
|
|
|
* @param string $userId |
311
|
|
|
* @param int $level |
312
|
|
|
* |
313
|
|
|
* @return Member |
314
|
|
|
* @throws CircleNotFoundException |
315
|
|
|
* @throws FederatedUserException |
316
|
|
|
* @throws FederatedUserNotFoundException |
317
|
|
|
* @throws InvalidItemException |
318
|
|
|
* @throws MemberNotFoundException |
319
|
|
|
* @throws OwnerNotFoundException |
320
|
|
|
* @throws RemoteInstanceException |
321
|
|
|
* @throws RemoteNotFoundException |
322
|
|
|
* @throws RemoteResourceNotFoundException |
323
|
|
|
* @throws RequestNetworkException |
324
|
|
|
* @throws SignatoryException |
325
|
|
|
* @throws UnknownRemoteException |
326
|
|
|
* @throws UserTypeNotFoundException |
327
|
|
|
*/ |
328
|
|
|
public function getFederatedMember(string $userId, int $level = Member::LEVEL_MEMBER): Member { |
329
|
|
|
$userId = trim($userId, ','); |
330
|
|
|
if (strpos($userId, ',') !== false) { |
331
|
|
|
list($userId, $level) = explode(',', $userId); |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
$federatedUser = $this->getFederatedUser($userId); |
335
|
|
|
$member = new Member(); |
336
|
|
|
$member->importFromIFederatedUser($federatedUser); |
337
|
|
|
$member->setLevel((int)$level); |
338
|
|
|
|
339
|
|
|
return $member; |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
|
343
|
|
|
/** |
344
|
|
|
* Generate a FederatedUser based on local data. |
345
|
|
|
* WARNING: There is no confirmation that the returned FederatedUser exists or is valid at this point. |
346
|
|
|
* Use getFederatedUser() instead if a valid and confirmed FederatedUser is needed. |
347
|
|
|
* |
348
|
|
|
* if $federatedId is a known SingleId, will returns data from the local database. |
349
|
|
|
* if $federatedId is a local username, will returns data from the local database. |
350
|
|
|
* Otherwise, the FederatedUser will not contains a SingleId. |
351
|
|
|
* |
352
|
|
|
* @param string $userId |
353
|
|
|
* @param int $type |
354
|
|
|
* |
355
|
|
|
* @return FederatedUser |
356
|
|
|
*/ |
357
|
|
|
public function generateFederatedUser(string $userId, int $type = 0): FederatedUser { |
358
|
|
|
$userId = trim($userId, '@'); |
359
|
|
|
if (strpos($userId, '@') === false) { |
360
|
|
|
$instance = ''; |
361
|
|
|
} else { |
362
|
|
|
list($userId, $instance) = $this->extractIdAndInstance($userId); |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
try { |
366
|
|
|
if (($type === 0 || $type === Member::TYPE_USER) |
367
|
|
|
&& ($instance === '' || $this->configService->isLocalInstance($instance))) { |
368
|
|
|
return $this->getLocalFederatedUser($userId); |
369
|
|
|
} |
370
|
|
|
} catch (CircleNotFoundException | FederatedUserNotFoundException | InvalidIdException $e) { |
|
|
|
|
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
try { |
374
|
|
|
if ($type === 0 || $type === Member::TYPE_SINGLE) { |
375
|
|
|
$federatedUser = $this->memberRequest->getFederatedUserBySingleId($userId); |
376
|
|
|
if ($instance === '' || $federatedUser->getInstance() === $instance) { |
377
|
|
|
return $federatedUser; |
378
|
|
|
} |
379
|
|
|
} |
380
|
|
|
} catch (MemberNotFoundException $e) { |
|
|
|
|
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
try { |
384
|
|
|
if ($type === 0 || $type === Member::TYPE_CIRCLE) { |
385
|
|
|
$federatedUser = $this->circleRequest->getFederatedUserByCircleId($userId); |
386
|
|
|
if ($instance === '' || $federatedUser->getInstance() === $instance) { |
387
|
|
|
return $federatedUser; |
388
|
|
|
} |
389
|
|
|
} |
390
|
|
|
} catch (CircleNotFoundException | OwnerNotFoundException $e) { |
|
|
|
|
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
// TODO: search for other possible entries, like mail address |
394
|
|
|
|
395
|
|
|
if ($type === 0) { |
396
|
|
|
$type = Member::TYPE_USER; |
397
|
|
|
} |
398
|
|
|
|
399
|
|
|
$federatedUser = new FederatedUser(); |
400
|
|
|
$federatedUser->set($userId, $instance, $type); |
401
|
|
|
|
402
|
|
|
return $federatedUser; |
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
|
406
|
|
|
/** |
407
|
|
|
* get a valid FederatedUser, based on the federatedId (userId@instance) its the type. |
408
|
|
|
* If instance is local, get the local valid FederatedUser |
409
|
|
|
* If instance is not local, get the remote valid FederatedUser |
410
|
|
|
* |
411
|
|
|
* @param string $federatedId |
412
|
|
|
* @param int $userType |
413
|
|
|
* |
414
|
|
|
* @return FederatedUser |
415
|
|
|
* @throws CircleNotFoundException |
416
|
|
|
* @throws FederatedUserException |
417
|
|
|
* @throws FederatedUserNotFoundException |
418
|
|
|
* @throws InvalidItemException |
419
|
|
|
* @throws MemberNotFoundException |
420
|
|
|
* @throws OwnerNotFoundException |
421
|
|
|
* @throws RemoteInstanceException |
422
|
|
|
* @throws RemoteNotFoundException |
423
|
|
|
* @throws RemoteResourceNotFoundException |
424
|
|
|
* @throws RequestNetworkException |
425
|
|
|
* @throws SignatoryException |
426
|
|
|
* @throws UnknownRemoteException |
427
|
|
|
* @throws UserTypeNotFoundException |
428
|
|
|
*/ |
429
|
|
|
public function getFederatedUser(string $federatedId, int $userType = Member::TYPE_USER): FederatedUser { |
430
|
|
|
list($singleId, $instance) = $this->extractIdAndInstance($federatedId); |
431
|
|
|
|
432
|
|
|
switch ($userType) { |
433
|
|
|
case Member::TYPE_USER: |
434
|
|
|
try { |
435
|
|
|
return $this->getFederatedUser_User($singleId, $instance); |
436
|
|
|
} catch (RemoteNotFoundException $e) { |
437
|
|
|
throw new RemoteNotFoundException('Instance %s not found', ['instance' => $instance]); |
438
|
|
|
} catch (Exception $e) { |
439
|
|
|
} |
440
|
|
|
|
441
|
|
|
return $this->getFederatedUser_SingleId($singleId, $instance); |
442
|
|
|
case Member::TYPE_CIRCLE: |
443
|
|
|
return $this->getFederatedUser_Circle($singleId, $instance); |
444
|
|
|
case Member::TYPE_SINGLE: |
445
|
|
|
return $this->getFederatedUser_SingleId($singleId, $instance); |
446
|
|
|
} |
447
|
|
|
|
448
|
|
|
throw new UserTypeNotFoundException(); |
449
|
|
|
} |
450
|
|
|
|
451
|
|
|
|
452
|
|
|
/** |
453
|
|
|
* @param string $singleId |
454
|
|
|
* @param string $instance |
455
|
|
|
* |
456
|
|
|
* @return FederatedUser |
457
|
|
|
* @throws FederatedUserException |
458
|
|
|
* @throws FederatedUserNotFoundException |
459
|
|
|
* @throws InvalidItemException |
460
|
|
|
* @throws MemberNotFoundException |
461
|
|
|
* @throws RemoteInstanceException |
462
|
|
|
* @throws RemoteNotFoundException |
463
|
|
|
* @throws RemoteResourceNotFoundException |
464
|
|
|
* @throws RequestNetworkException |
465
|
|
|
* @throws SignatoryException |
466
|
|
|
* @throws UnknownRemoteException |
467
|
|
|
*/ |
468
|
|
|
private function getFederatedUser_SingleId(string $singleId, string $instance): FederatedUser { |
469
|
|
|
if (strlen($singleId) !== ManagedModel::ID_LENGTH) { |
470
|
|
|
throw new MemberNotFoundException(); |
471
|
|
|
} |
472
|
|
|
|
473
|
|
|
if ($this->configService->isLocalInstance($instance)) { |
474
|
|
|
return $this->memberRequest->getFederatedUserBySingleId($singleId); |
475
|
|
|
} else { |
476
|
|
|
$federatedUser = |
477
|
|
|
$this->remoteService->getFederatedUserFromInstance($singleId, $instance, Member::TYPE_SINGLE); |
478
|
|
|
$this->confirmLocalSingleId($federatedUser); |
479
|
|
|
|
480
|
|
|
return $federatedUser; |
481
|
|
|
} |
482
|
|
|
} |
483
|
|
|
|
484
|
|
|
|
485
|
|
|
/** |
486
|
|
|
* @param string $userId |
487
|
|
|
* @param string $instance |
488
|
|
|
* |
489
|
|
|
* @return FederatedUser |
490
|
|
|
* @throws CircleNotFoundException |
491
|
|
|
* @throws FederatedUserException |
492
|
|
|
* @throws FederatedUserNotFoundException |
493
|
|
|
* @throws InvalidIdException |
494
|
|
|
* @throws InvalidItemException |
495
|
|
|
* @throws RemoteInstanceException |
496
|
|
|
* @throws RemoteNotFoundException |
497
|
|
|
* @throws RemoteResourceNotFoundException |
498
|
|
|
* @throws RequestNetworkException |
499
|
|
|
* @throws SignatoryException |
500
|
|
|
* @throws UnknownRemoteException |
501
|
|
|
*/ |
502
|
|
|
private function getFederatedUser_User(string $userId, string $instance): FederatedUser { |
503
|
|
|
if ($this->configService->isLocalInstance($instance)) { |
504
|
|
|
return $this->getLocalFederatedUser($userId); |
505
|
|
|
} else { |
506
|
|
|
$federatedUser = |
507
|
|
|
$this->remoteService->getFederatedUserFromInstance($userId, $instance, Member::TYPE_USER); |
508
|
|
|
$this->confirmLocalSingleId($federatedUser); |
509
|
|
|
|
510
|
|
|
return $federatedUser; |
511
|
|
|
} |
512
|
|
|
} |
513
|
|
|
|
514
|
|
|
|
515
|
|
|
/** |
516
|
|
|
* // TODO: do we need to have this working on remote instance ? |
517
|
|
|
* |
518
|
|
|
* @param string $circleId |
519
|
|
|
* @param string $instance |
520
|
|
|
* |
521
|
|
|
* @return FederatedUser |
522
|
|
|
* @throws CircleNotFoundException |
523
|
|
|
* @throws FederatedUserException |
524
|
|
|
* @throws FederatedUserNotFoundException |
525
|
|
|
* @throws InvalidItemException |
526
|
|
|
* @throws OwnerNotFoundException |
527
|
|
|
* @throws RemoteInstanceException |
528
|
|
|
* @throws RemoteNotFoundException |
529
|
|
|
* @throws RemoteResourceNotFoundException |
530
|
|
|
* @throws RequestNetworkException |
531
|
|
|
* @throws SignatoryException |
532
|
|
|
* @throws UnknownRemoteException |
533
|
|
|
*/ |
534
|
|
|
private function getFederatedUser_Circle(string $circleId, string $instance): FederatedUser { |
535
|
|
|
// TODO: check remote instance for existing circle |
536
|
|
|
if ($this->configService->isLocalInstance($instance)) { |
537
|
|
|
$circle = $this->circleRequest->getCircle($circleId); |
538
|
|
|
$federatedUser = new FederatedUser(); |
539
|
|
|
$federatedUser->set($circleId, $circle->getInstance(), Member::TYPE_CIRCLE); |
540
|
|
|
$federatedUser->setSingleId($circleId); |
541
|
|
|
} else { |
542
|
|
|
$federatedUser = |
543
|
|
|
$this->remoteService->getFederatedUserFromInstance($circleId, $instance, Member::TYPE_CIRCLE); |
544
|
|
|
$this->confirmLocalSingleId($federatedUser); |
545
|
|
|
} |
546
|
|
|
|
547
|
|
|
return $federatedUser; |
548
|
|
|
} |
549
|
|
|
|
550
|
|
|
|
551
|
|
|
/** |
552
|
|
|
* extract userID and instance from a federatedId |
553
|
|
|
* |
554
|
|
|
* @param string $federatedId |
555
|
|
|
* |
556
|
|
|
* @return array |
557
|
|
|
*/ |
558
|
|
|
private function extractIdAndInstance(string $federatedId): array { |
559
|
|
|
$federatedId = trim($federatedId, '@'); |
560
|
|
|
if (strpos($federatedId, '@') === false) { |
561
|
|
|
$userId = $federatedId; |
562
|
|
|
$instance = $this->configService->getFrontalInstance(); |
563
|
|
|
} else { |
564
|
|
|
list($userId, $instance) = explode('@', $federatedId); |
565
|
|
|
} |
566
|
|
|
|
567
|
|
|
return [$userId, $instance]; |
568
|
|
|
} |
569
|
|
|
|
570
|
|
|
|
571
|
|
|
/** |
572
|
|
|
* @param FederatedUser $federatedUser |
573
|
|
|
* |
574
|
|
|
* @throws CircleNotFoundException |
575
|
|
|
* @throws InvalidIdException |
576
|
|
|
*/ |
577
|
|
|
private function fillSingleCircleId(FederatedUser $federatedUser): void { |
578
|
|
|
if ($federatedUser->getSingleId() !== '') { |
579
|
|
|
return; |
580
|
|
|
} |
581
|
|
|
|
582
|
|
|
$circle = $this->getSingleCircle($federatedUser); |
583
|
|
|
$federatedUser->setSingleId($circle->getId()); |
584
|
|
|
} |
585
|
|
|
|
586
|
|
|
|
587
|
|
|
/** |
588
|
|
|
* get the Single Circle from a local user |
589
|
|
|
* |
590
|
|
|
* @param FederatedUser $federatedUser |
591
|
|
|
* |
592
|
|
|
* @return Circle |
593
|
|
|
* @throws CircleNotFoundException |
594
|
|
|
* @throws InvalidIdException |
595
|
|
|
* @throws FederatedUserException |
596
|
|
|
*/ |
597
|
|
|
private function getSingleCircle(FederatedUser $federatedUser): Circle { |
598
|
|
|
if (!$this->configService->isLocalInstance($federatedUser->getInstance())) { |
599
|
|
|
throw new FederatedUserException('FederatedUser must be local'); |
600
|
|
|
} |
601
|
|
|
|
602
|
|
|
try { |
603
|
|
|
return $this->circleRequest->getInitiatorCircle($federatedUser); |
604
|
|
|
} catch (CircleNotFoundException $e) { |
605
|
|
|
$circle = new Circle(); |
606
|
|
|
$id = $this->token(ManagedModel::ID_LENGTH); |
607
|
|
|
|
608
|
|
|
$circle->setName('single:' . $federatedUser->getUserId() . ':' . $id) |
609
|
|
|
->setId($id) |
610
|
|
|
->setConfig(Circle::CFG_SINGLE); |
611
|
|
|
$this->circleRequest->save($circle); |
612
|
|
|
|
613
|
|
|
$owner = new Member(); |
614
|
|
|
$owner->importFromIFederatedUser($federatedUser); |
615
|
|
|
$owner->setLevel(Member::LEVEL_OWNER) |
616
|
|
|
->setCircleId($id) |
617
|
|
|
->setSingleId($id) |
618
|
|
|
->setId($id) |
619
|
|
|
->setCachedName($owner->getUserId()) |
620
|
|
|
->setStatus('Member'); |
621
|
|
|
$this->memberRequest->save($owner); |
622
|
|
|
} |
623
|
|
|
|
624
|
|
|
return $this->circleRequest->getInitiatorCircle($federatedUser); |
625
|
|
|
} |
626
|
|
|
|
627
|
|
|
|
628
|
|
|
/** |
629
|
|
|
* Confirm that all field of a FederatedUser are filled. |
630
|
|
|
* |
631
|
|
|
* @param FederatedUser $federatedUser |
632
|
|
|
* |
633
|
|
|
* @throws FederatedUserException |
634
|
|
|
*/ |
635
|
|
|
private function confirmFederatedUser(FederatedUser $federatedUser): void { |
636
|
|
|
if ($federatedUser->getUserId() === '' |
637
|
|
|
|| $federatedUser->getSingleId() === '' |
638
|
|
|
|| $federatedUser->getUserType() === '' |
|
|
|
|
639
|
|
|
|| $federatedUser->getInstance() === '') { |
640
|
|
|
$this->debug('FederatedUser is not empty', ['federatedUser' => $federatedUser]); |
641
|
|
|
throw new FederatedUserException('FederatedUser is not complete'); |
642
|
|
|
} |
643
|
|
|
} |
644
|
|
|
|
645
|
|
|
/** |
646
|
|
|
* Confirm that the singleId of a FederatedUser is unique and not used to any other member of the |
647
|
|
|
* database. |
648
|
|
|
* |
649
|
|
|
* @param FederatedUser $federatedUser |
650
|
|
|
* |
651
|
|
|
* @throws FederatedUserException |
652
|
|
|
*/ |
653
|
|
|
public function confirmLocalSingleId(FederatedUser $federatedUser): void { |
654
|
|
|
$members = $this->memberRequest->getMembersBySingleId($federatedUser->getSingleId()); |
655
|
|
|
|
656
|
|
|
foreach ($members as $member) { |
657
|
|
|
if (!$federatedUser->compareWith($member)) { |
658
|
|
|
$this->debug( |
659
|
|
|
'uniqueness of SingleId could not be confirmed', |
660
|
|
|
['federatedUser' => $federatedUser, 'localMember' => $member] |
661
|
|
|
); |
662
|
|
|
throw new FederatedUserException('uniqueness of SingleId could not be confirmed'); |
663
|
|
|
} |
664
|
|
|
} |
665
|
|
|
} |
666
|
|
|
|
667
|
|
|
|
668
|
|
|
// /** |
669
|
|
|
// * @param FederatedUser $federatedUser |
670
|
|
|
// * |
671
|
|
|
// * @return Membership[] |
672
|
|
|
// */ |
673
|
|
|
// public function generateMemberships(FederatedUser $federatedUser): array { |
674
|
|
|
// $circles = $this->circleRequest->getCircles(null, $federatedUser); |
675
|
|
|
// $memberships = []; |
676
|
|
|
// foreach ($circles as $circle) { |
677
|
|
|
// $initiator = $circle->getInitiator(); |
678
|
|
|
// if (!$initiator->isMember()) { |
679
|
|
|
// continue; |
680
|
|
|
// } |
681
|
|
|
// |
682
|
|
|
// $memberships[] = new Membership( |
683
|
|
|
// $initiator->getId(), $circle->getId(), $federatedUser->getSingleId(), $initiator->getLevel() |
684
|
|
|
// ); |
685
|
|
|
// |
686
|
|
|
//// $newUser = new CurrentUser($circle->getId(), Member::TYPE_CIRCLE, ''); |
687
|
|
|
//// $circles = $this->circleRequest->getCircles(null, $currentUser); |
688
|
|
|
// } |
689
|
|
|
// |
690
|
|
|
// return $memberships; |
691
|
|
|
// } |
692
|
|
|
// |
693
|
|
|
// |
694
|
|
|
// /** |
695
|
|
|
// * @param FederatedUser|null $federatedUser |
696
|
|
|
// */ |
697
|
|
|
// public function updateMemberships(?FederatedUser $federatedUser = null) { |
698
|
|
|
// if (is_null($federatedUser)) { |
699
|
|
|
// $federatedUser = $this->getCurrentUser(); |
700
|
|
|
// } else { |
701
|
|
|
// $federatedUser->setMemberships($this->membershipRequest->getMemberships($federatedUser)); |
702
|
|
|
// } |
703
|
|
|
// |
704
|
|
|
// if (is_null($federatedUser)) { |
705
|
|
|
// return; |
706
|
|
|
// } |
707
|
|
|
// |
708
|
|
|
// $last = $this->generateMemberships($federatedUser); |
709
|
|
|
// |
710
|
|
|
// echo 'known: ' . json_encode($federatedUser->getMemberships()) . "\n"; |
711
|
|
|
// echo 'last: ' . json_encode($last) . "\n"; |
712
|
|
|
// |
713
|
|
|
//// |
714
|
|
|
//// $circles = $this->circleRequest->getCircles(null, $viewer); |
715
|
|
|
//// foreach ($circles as $circle) { |
716
|
|
|
//// $viewer = $circle->getViewer(); |
717
|
|
|
//// if (!$viewer->isMember()) { |
718
|
|
|
//// continue; |
719
|
|
|
//// } |
720
|
|
|
//// |
721
|
|
|
//// echo 'new member: ' . json_encode($viewer) . "\n"; |
722
|
|
|
////// $this->federatedUserService->updateMembership($circle); |
723
|
|
|
//// } |
724
|
|
|
// |
725
|
|
|
// |
726
|
|
|
// } |
727
|
|
|
|
728
|
|
|
|
729
|
|
|
} |
730
|
|
|
|
731
|
|
|
|