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\Traits\TStringTools; |
36
|
|
|
use Exception; |
37
|
|
|
use OCA\Circles\AppInfo\Application; |
38
|
|
|
use OCA\Circles\Db\CircleRequest; |
39
|
|
|
use OCA\Circles\Db\MemberRequest; |
40
|
|
|
use OCA\Circles\Exceptions\CircleNotFoundException; |
41
|
|
|
use OCA\Circles\Exceptions\FederatedEventException; |
42
|
|
|
use OCA\Circles\Exceptions\FederatedItemException; |
43
|
|
|
use OCA\Circles\Exceptions\FederatedUserException; |
44
|
|
|
use OCA\Circles\Exceptions\FederatedUserNotFoundException; |
45
|
|
|
use OCA\Circles\Exceptions\GroupNotFoundException; |
46
|
|
|
use OCA\Circles\Exceptions\InitiatorNotConfirmedException; |
47
|
|
|
use OCA\Circles\Exceptions\InvalidIdException; |
48
|
|
|
use OCA\Circles\Exceptions\MigrationTo22Exception; |
49
|
|
|
use OCA\Circles\Exceptions\OwnerNotFoundException; |
50
|
|
|
use OCA\Circles\Exceptions\RemoteInstanceException; |
51
|
|
|
use OCA\Circles\Exceptions\RemoteNotFoundException; |
52
|
|
|
use OCA\Circles\Exceptions\RemoteResourceNotFoundException; |
53
|
|
|
use OCA\Circles\Exceptions\RequestBuilderException; |
54
|
|
|
use OCA\Circles\Exceptions\SingleCircleNotFoundException; |
55
|
|
|
use OCA\Circles\Exceptions\UnknownRemoteException; |
56
|
|
|
use OCA\Circles\FederatedItems\SingleMemberAdd; |
57
|
|
|
use OCA\Circles\Model\Circle; |
58
|
|
|
use OCA\Circles\Model\Federated\FederatedEvent; |
59
|
|
|
use OCA\Circles\Model\FederatedUser; |
60
|
|
|
use OCA\Circles\Model\ManagedModel; |
61
|
|
|
use OCA\Circles\Model\Member; |
62
|
|
|
use OCP\IGroupManager; |
63
|
|
|
use OCP\IUserManager; |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Class SyncService |
68
|
|
|
* |
69
|
|
|
* @package OCA\Circles\Service |
70
|
|
|
*/ |
71
|
|
|
class SyncService { |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
use TStringTools; |
75
|
|
|
|
76
|
|
|
|
77
|
|
|
/** @var IUserManager */ |
78
|
|
|
private $userManager; |
79
|
|
|
|
80
|
|
|
/** @var IGroupManager */ |
81
|
|
|
private $groupManager; |
82
|
|
|
|
83
|
|
|
/** @var CircleRequest */ |
84
|
|
|
private $circleRequest; |
85
|
|
|
|
86
|
|
|
/** @var MemberRequest */ |
87
|
|
|
private $memberRequest; |
88
|
|
|
|
89
|
|
|
/** @var FederatedUserService */ |
90
|
|
|
private $federatedUserService; |
91
|
|
|
|
92
|
|
|
/** @var federatedEventService */ |
93
|
|
|
private $federatedEventService; |
94
|
|
|
|
95
|
|
|
/** @var MemberService */ |
96
|
|
|
private $memberService; |
97
|
|
|
|
98
|
|
|
/** @var MembershipService */ |
99
|
|
|
private $membershipService; |
100
|
|
|
|
101
|
|
|
/** @var ConfigService */ |
102
|
|
|
private $configService; |
103
|
|
|
|
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* SyncService constructor. |
107
|
|
|
* |
108
|
|
|
* @param IUserManager $userManager |
109
|
|
|
* @param IGroupManager $groupManager |
110
|
|
|
* @param CircleRequest $circleRequest |
111
|
|
|
* @param MemberRequest $memberRequest |
112
|
|
|
* @param FederatedUserService $federatedUserService |
113
|
|
|
* @param federatedEventService $federatedEventService |
114
|
|
|
* @param MemberService $memberService |
115
|
|
|
* @param MembershipService $membershipService |
116
|
|
|
* @param ConfigService $configService |
117
|
|
|
*/ |
118
|
|
View Code Duplication |
public function __construct( |
|
|
|
|
119
|
|
|
IUserManager $userManager, |
120
|
|
|
IGroupManager $groupManager, |
121
|
|
|
CircleRequest $circleRequest, |
122
|
|
|
MemberRequest $memberRequest, |
123
|
|
|
FederatedUserService $federatedUserService, |
124
|
|
|
federatedEventService $federatedEventService, |
125
|
|
|
MemberService $memberService, |
126
|
|
|
MembershipService $membershipService, |
127
|
|
|
ConfigService $configService |
128
|
|
|
) { |
129
|
|
|
$this->userManager = $userManager; |
130
|
|
|
$this->groupManager = $groupManager; |
131
|
|
|
$this->circleRequest = $circleRequest; |
132
|
|
|
$this->memberRequest = $memberRequest; |
133
|
|
|
$this->federatedUserService = $federatedUserService; |
134
|
|
|
$this->federatedEventService = $federatedEventService; |
135
|
|
|
$this->memberService = $memberService; |
136
|
|
|
$this->membershipService = $membershipService; |
137
|
|
|
$this->configService = $configService; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @return bool |
143
|
|
|
* @throws MigrationTo22Exception |
144
|
|
|
*/ |
145
|
|
|
public function migration(): bool { |
146
|
|
|
if ($this->configService->getAppValueBool(ConfigService::MIGRATION_22)) { |
147
|
|
|
return false; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
$this->migrationTo22(); |
151
|
|
|
$this->configService->setAppValue(ConfigService::MIGRATION_22, '1'); |
152
|
|
|
|
153
|
|
|
return true; |
154
|
|
|
|
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @return void |
159
|
|
|
* @throws MigrationTo22Exception |
160
|
|
|
*/ |
161
|
|
|
private function migrationTo22(): void { |
162
|
|
|
throw new MigrationTo22Exception('migration failed'); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @return void |
168
|
|
|
*/ |
169
|
|
|
public function syncAll(): void { |
170
|
|
|
$this->syncOcc(); |
171
|
|
|
$this->syncNextcloudUsers(); |
172
|
|
|
$this->syncGlobalScale(); |
173
|
|
|
$this->syncRemote(); |
174
|
|
|
$this->syncNextcloudGroups(); |
175
|
|
|
$this->syncContacts(); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @throws FederatedUserException |
181
|
|
|
* @throws InvalidIdException |
182
|
|
|
* @throws RequestBuilderException |
183
|
|
|
* @throws SingleCircleNotFoundException |
184
|
|
|
*/ |
185
|
|
|
public function syncOcc(): void { |
186
|
|
|
$this->federatedUserService->getAppInitiator('occ', Member::APP_OCC); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @return void |
192
|
|
|
*/ |
193
|
|
|
public function syncNextcloudUsers(): void { |
194
|
|
|
foreach ($this->userManager->search('') as $user) { |
195
|
|
|
try { |
196
|
|
|
$this->syncNextcloudUser($user->getUID()); |
197
|
|
|
} catch (Exception $e) { |
|
|
|
|
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @param string $userId |
204
|
|
|
* |
205
|
|
|
* @return FederatedUser |
206
|
|
|
* @throws FederatedUserException |
207
|
|
|
* @throws FederatedUserNotFoundException |
208
|
|
|
* @throws InvalidIdException |
209
|
|
|
* @throws SingleCircleNotFoundException |
210
|
|
|
* @throws RequestBuilderException |
211
|
|
|
*/ |
212
|
|
|
public function syncNextcloudUser(string $userId): FederatedUser { |
213
|
|
|
return $this->federatedUserService->getLocalFederatedUser($userId); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @return void |
219
|
|
|
*/ |
220
|
|
|
public function syncNextcloudGroups(): void { |
221
|
|
|
foreach ($this->groupManager->search('') as $group) { |
222
|
|
|
try { |
223
|
|
|
$this->syncNextcloudGroup($group->getGID()); |
224
|
|
|
} catch (Exception $e) { |
|
|
|
|
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* @param string $groupId |
231
|
|
|
* |
232
|
|
|
* @return Circle |
233
|
|
|
* @throws FederatedUserException |
234
|
|
|
* @throws FederatedUserNotFoundException |
235
|
|
|
* @throws GroupNotFoundException |
236
|
|
|
* @throws InvalidIdException |
237
|
|
|
* @throws SingleCircleNotFoundException |
238
|
|
|
* @throws FederatedEventException |
239
|
|
|
* @throws FederatedItemException |
240
|
|
|
* @throws InitiatorNotConfirmedException |
241
|
|
|
* @throws OwnerNotFoundException |
242
|
|
|
* @throws RemoteInstanceException |
243
|
|
|
* @throws RemoteNotFoundException |
244
|
|
|
* @throws RemoteResourceNotFoundException |
245
|
|
|
* @throws UnknownRemoteException |
246
|
|
|
* @throws RequestBuilderException |
247
|
|
|
*/ |
248
|
|
|
public function syncNextcloudGroup(string $groupId): Circle { |
249
|
|
|
$circle = $this->federatedUserService->getGroupCircle($groupId); |
250
|
|
|
$group = $this->groupManager->get($groupId); |
251
|
|
|
foreach ($group->getUsers() as $user) { |
252
|
|
|
$member = $this->generateGroupMember($circle, $user->getUID()); |
253
|
|
|
$event = new FederatedEvent(SingleMemberAdd::class); |
254
|
|
|
$event->setCircle($circle); |
255
|
|
|
$event->setMember($member); |
256
|
|
|
$this->federatedEventService->newEvent($event); |
257
|
|
|
|
258
|
|
|
// $this->memberRequest->insertOrUpdate($member); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
// $this->membershipService->onUpdate($circle->getSingleId()); |
262
|
|
|
|
263
|
|
|
return $circle; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* @param string $userId |
269
|
|
|
* |
270
|
|
|
* @throws FederatedUserException |
271
|
|
|
* @throws FederatedUserNotFoundException |
272
|
|
|
* @throws InvalidIdException |
273
|
|
|
* @throws RequestBuilderException |
274
|
|
|
* @throws SingleCircleNotFoundException |
275
|
|
|
*/ |
276
|
|
View Code Duplication |
public function userDeleted(string $userId): void { |
|
|
|
|
277
|
|
|
$federatedUser = $this->federatedUserService->getLocalFederatedUser($userId); |
278
|
|
|
|
279
|
|
|
// TODO: check existing circle generated by user ! |
280
|
|
|
$this->circleRequest->delete($federatedUser->getBasedOn()); |
281
|
|
|
$this->memberRequest->deleteAllFromCircle($federatedUser->getBasedOn()); |
282
|
|
|
$this->membershipService->onUpdate($federatedUser->getSingleId()); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* @param string $groupId |
288
|
|
|
* |
289
|
|
|
* @throws FederatedUserException |
290
|
|
|
* @throws InvalidIdException |
291
|
|
|
* @throws RequestBuilderException |
292
|
|
|
* @throws SingleCircleNotFoundException |
293
|
|
|
*/ |
294
|
|
|
public function groupDeleted(string $groupId): void { |
295
|
|
|
$circle = new Circle(); |
296
|
|
|
$circle->setName('group:' . $groupId) |
297
|
|
|
->setConfig(Circle::CFG_SYSTEM | Circle::CFG_NO_OWNER | Circle::CFG_HIDDEN) |
298
|
|
|
->setSource(Member::TYPE_GROUP); |
299
|
|
|
|
300
|
|
|
$owner = $this->federatedUserService->getAppInitiator(Application::APP_ID, Member::APP_CIRCLES); |
301
|
|
|
|
302
|
|
|
$member = new Member(); |
303
|
|
|
$member->importFromIFederatedUser($owner); |
304
|
|
|
$member->setLevel(Member::LEVEL_OWNER) |
305
|
|
|
->setStatus(Member::STATUS_MEMBER); |
306
|
|
|
$circle->setOwner($member); |
307
|
|
|
|
308
|
|
|
try { |
309
|
|
|
$circle = $this->circleRequest->searchCircle($circle); |
310
|
|
|
} catch (CircleNotFoundException $e) { |
311
|
|
|
return; |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
$this->circleRequest->delete($circle); |
315
|
|
|
$this->memberRequest->deleteAllFromCircle($circle); |
316
|
|
|
|
317
|
|
|
$this->membershipService->onUpdate($circle->getSingleId()); |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* @param Circle $circle |
323
|
|
|
* @param string $userId |
324
|
|
|
* |
325
|
|
|
* @return Member |
326
|
|
|
* @throws FederatedUserException |
327
|
|
|
* @throws FederatedUserNotFoundException |
328
|
|
|
* @throws InvalidIdException |
329
|
|
|
* @throws RequestBuilderException |
330
|
|
|
* @throws SingleCircleNotFoundException |
331
|
|
|
*/ |
332
|
|
|
private function generateGroupMember(Circle $circle, string $userId): Member { |
333
|
|
|
$federatedUser = $this->federatedUserService->getLocalFederatedUser($userId); |
334
|
|
|
|
335
|
|
|
$member = new Member(); |
336
|
|
|
$member->importFromIFederatedUser($federatedUser); |
337
|
|
|
$member->setId($this->uuid(ManagedModel::ID_LENGTH)); |
338
|
|
|
$member->setCircleId($circle->getSingleId()); |
339
|
|
|
$member->setLevel(Member::LEVEL_MEMBER); |
340
|
|
|
$member->setStatus(Member::STATUS_MEMBER); |
341
|
|
|
|
342
|
|
|
return $member; |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
|
346
|
|
|
/** |
347
|
|
|
* @param string $groupId |
348
|
|
|
* @param string $userId |
349
|
|
|
* |
350
|
|
|
* @return Member |
351
|
|
|
* @throws FederatedEventException |
352
|
|
|
* @throws FederatedItemException |
353
|
|
|
* @throws FederatedUserException |
354
|
|
|
* @throws FederatedUserNotFoundException |
355
|
|
|
* @throws GroupNotFoundException |
356
|
|
|
* @throws InitiatorNotConfirmedException |
357
|
|
|
* @throws InvalidIdException |
358
|
|
|
* @throws OwnerNotFoundException |
359
|
|
|
* @throws RemoteInstanceException |
360
|
|
|
* @throws RemoteNotFoundException |
361
|
|
|
* @throws RemoteResourceNotFoundException |
362
|
|
|
* @throws RequestBuilderException |
363
|
|
|
* @throws SingleCircleNotFoundException |
364
|
|
|
* @throws UnknownRemoteException |
365
|
|
|
*/ |
366
|
|
|
public function groupMemberAdded(string $groupId, string $userId): void { |
367
|
|
|
$circle = $this->federatedUserService->getGroupCircle($groupId); |
368
|
|
|
$member = $this->generateGroupMember($circle, $userId); |
369
|
|
|
|
370
|
|
|
$event = new FederatedEvent(SingleMemberAdd::class); |
371
|
|
|
$event->setCircle($circle); |
372
|
|
|
$event->setMember($member); |
373
|
|
|
$this->federatedEventService->newEvent($event); |
374
|
|
|
|
375
|
|
|
// $this->memberRequest->insertOrUpdate($member); |
376
|
|
|
|
377
|
|
|
// $this->membershipService->onUpdate($member->getSingleId()); |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
|
381
|
|
|
/** |
382
|
|
|
* @param string $groupId |
383
|
|
|
* @param string $userId |
384
|
|
|
* |
385
|
|
|
* @throws FederatedEventException |
386
|
|
|
* @throws FederatedItemException |
387
|
|
|
* @throws FederatedUserException |
388
|
|
|
* @throws FederatedUserNotFoundException |
389
|
|
|
* @throws GroupNotFoundException |
390
|
|
|
* @throws InitiatorNotConfirmedException |
391
|
|
|
* @throws InvalidIdException |
392
|
|
|
* @throws OwnerNotFoundException |
393
|
|
|
* @throws RemoteInstanceException |
394
|
|
|
* @throws RemoteNotFoundException |
395
|
|
|
* @throws RemoteResourceNotFoundException |
396
|
|
|
* @throws RequestBuilderException |
397
|
|
|
* @throws SingleCircleNotFoundException |
398
|
|
|
* @throws UnknownRemoteException |
399
|
|
|
*/ |
400
|
|
View Code Duplication |
public function groupMemberRemoved(string $groupId, string $userId): void { |
|
|
|
|
401
|
|
|
$circle = $this->federatedUserService->getGroupCircle($groupId); |
402
|
|
|
$federatedUser = $this->federatedUserService->getLocalFederatedUser($userId); |
403
|
|
|
|
404
|
|
|
$this->memberRequest->deleteFederatedUserFromCircle($federatedUser, $circle); |
405
|
|
|
$this->membershipService->onUpdate($federatedUser->getSingleId()); |
406
|
|
|
} |
407
|
|
|
|
408
|
|
|
|
409
|
|
|
/** |
410
|
|
|
* @return void |
411
|
|
|
*/ |
412
|
|
|
public function syncContacts(): void { |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
|
416
|
|
|
/** |
417
|
|
|
* @return void |
418
|
|
|
*/ |
419
|
|
|
public function syncGlobalScale(): void { |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
|
423
|
|
|
/** |
424
|
|
|
* @return void |
425
|
|
|
*/ |
426
|
|
|
public function syncRemote(): void { |
427
|
|
|
} |
428
|
|
|
|
429
|
|
|
|
430
|
|
|
/** |
431
|
|
|
* @param string $circleId |
432
|
|
|
* |
433
|
|
|
* @return void |
434
|
|
|
*/ |
435
|
|
|
public function syncRemoteCircle(string $circleId): void { |
|
|
|
|
436
|
|
|
} |
437
|
|
|
|
438
|
|
|
|
439
|
|
|
} |
440
|
|
|
|
441
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.