Completed
Pull Request — master (#625)
by Maxence
02:28
created

LocalController::memberConfirm()   A

Complexity

Conditions 2
Paths 7

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
cc 2
nc 7
nop 2
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\Controller;
33
34
35
use daita\MySmallPhpTools\Traits\Nextcloud\nc22\TNC22Deserialize;
36
use daita\MySmallPhpTools\Traits\Nextcloud\nc22\TNC22Logger;
37
use Exception;
38
use OCA\Circles\Exceptions\FederatedUserException;
39
use OCA\Circles\Exceptions\FederatedUserNotFoundException;
40
use OCA\Circles\Exceptions\InvalidIdException;
41
use OCA\Circles\Exceptions\RequestBuilderException;
42
use OCA\Circles\Exceptions\SingleCircleNotFoundException;
43
use OCA\Circles\Model\FederatedUser;
44
use OCA\Circles\Model\Member;
45
use OCA\Circles\Service\CircleService;
46
use OCA\Circles\Service\ConfigService;
47
use OCA\Circles\Service\FederatedUserService;
48
use OCA\Circles\Service\MemberService;
49
use OCA\Circles\Service\MembershipService;
50
use OCA\Circles\Service\SearchService;
51
use OCP\AppFramework\Http\DataResponse;
52
use OCP\AppFramework\OCS\OCSException;
53
use OCP\AppFramework\OCSController;
54
use OCP\IRequest;
55
use OCP\IUserSession;
56
57
58
/**
59
 * Class LocalController
60
 *
61
 * @package OCA\Circles\Controller
62
 */
63
class LocalController extends OcsController {
64
65
66
	use TNC22Deserialize;
67
	use TNC22Logger;
68
69
70
	/** @var IUserSession */
71
	private $userSession;
72
73
	/** @var FederatedUserService */
74
	private $federatedUserService;
75
76
	/** @var CircleService */
77
	private $circleService;
78
79
	/** @var MemberService */
80
	private $memberService;
81
82
	/** @var MembershipService */
83
	private $membershipService;
84
85
	/** @var SearchService */
86
	private $searchService;
87
88
	/** @var ConfigService */
89
	protected $configService;
90
91
92
	/**
93
	 * LocalController constructor.
94
	 *
95
	 * @param string $appName
96
	 * @param IRequest $request
97
	 * @param IUserSession $userSession
98
	 * @param FederatedUserService $federatedUserService
99
	 * @param CircleService $circleService
100
	 * @param MemberService $memberService
101
	 * @param MembershipService $membershipService
102
	 * @param SearchService $searchService
103
	 * @param ConfigService $configService
104
	 */
105
	public function __construct(
106
		string $appName,
107
		IRequest $request,
108
		IUserSession $userSession,
109
		FederatedUserService $federatedUserService,
110
		CircleService $circleService,
111
		MemberService $memberService,
112
		MembershipService $membershipService,
113
		SearchService $searchService,
114
		ConfigService $configService
115
	) {
116
		parent::__construct($appName, $request);
117
		$this->userSession = $userSession;
118
		$this->federatedUserService = $federatedUserService;
119
		$this->circleService = $circleService;
120
		$this->memberService = $memberService;
121
		$this->membershipService = $membershipService;
122
		$this->searchService = $searchService;
123
		$this->configService = $configService;
124
125
		$this->setup('app', 'circles');
126
	}
127
128
129
	/**
130
	 * @NoAdminRequired
131
	 *
132
	 * @param string $name
133
	 * @param bool $personal
134
	 * @param bool $local
135
	 *
136
	 * @return DataResponse
137
	 * @throws OCSException
138
	 */
139
	public function create(string $name, bool $personal = false, bool $local = false): DataResponse {
140
		try {
141
			$this->setCurrentFederatedUser();
142
			$circle = $this->circleService->create($name, null, $personal, $local);
143
144
			return new DataResponse($this->serializeArray($circle));
145
		} catch (Exception $e) {
146
			throw new OcsException($e->getMessage(), $e->getCode());
147
		}
148
	}
149
150
151
	/**
152
	 * @NoAdminRequired
153
	 *
154
	 * @param string $circleId
155
	 *
156
	 * @return DataResponse
157
	 * @throws OCSException
158
	 */
159 View Code Duplication
	public function destroy(string $circleId): DataResponse {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
160
		try {
161
			$this->setCurrentFederatedUser();
162
			$circle = $this->circleService->destroy($circleId);
163
164
			return new DataResponse($this->serializeArray($circle));
165
		} catch (Exception $e) {
166
			throw new OcsException($e->getMessage(), $e->getCode());
167
		}
168
	}
169
170
171
	/**
172
	 * @NoAdminRequired
173
	 *
174
	 * @param string $term
175
	 *
176
	 * @return DataResponse
177
	 * @throws OCSException
178
	 */
179 View Code Duplication
	public function search(string $term): DataResponse {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
180
		try {
181
			$this->setCurrentFederatedUser();
182
183
			return new DataResponse($this->serializeArray($this->searchService->search($term)));
184
		} catch (Exception $e) {
185
			throw new OcsException($e->getMessage(), $e->getCode());
186
		}
187
	}
188
189
190
	/**
191
	 * @NoAdminRequired
192
	 *
193
	 * @param string $circleId
194
	 *
195
	 * @return DataResponse
196
	 * @throws OCSException
197
	 */
198 View Code Duplication
	public function circleDetails(string $circleId): DataResponse {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
199
		try {
200
			$this->setCurrentFederatedUser();
201
202
			return new DataResponse($this->serialize($this->circleService->getCircle($circleId)));
203
		} catch (Exception $e) {
204
			throw new OcsException($e->getMessage(), $e->getCode());
205
		}
206
	}
207
208
209
	/**
210
	 * @NoAdminRequired
211
	 *
212
	 * @param string $circleId
213
	 * @param string $userId
214
	 * @param int $type
215
	 *
216
	 * @return DataResponse
217
	 * @throws OCSException
218
	 */
219
	public function memberAdd(string $circleId, string $userId, int $type): DataResponse {
220
		try {
221
			$this->setCurrentFederatedUser();
222
223
			// exception in Contact
224
			if ($type === Member::TYPE_CONTACT) {
225
				$currentUser = $this->federatedUserService->getCurrentUser();
226
				if (!$this->configService->isLocalInstance($currentUser->getInstance())) {
227
					throw new OCSException('works only from local instance', 404);
228
				}
229
230
				$userId = $currentUser->getUserId() . '/' . $userId;
231
			}
232
233
			$federatedUser = $this->federatedUserService->generateFederatedUser($userId, $type);
234
			$result = $this->memberService->addMember($circleId, $federatedUser);
235
236
			return new DataResponse($this->serializeArray($result));
237
		} catch (Exception $e) {
238
			throw new OCSException($e->getMessage(), $e->getCode());
239
		}
240
	}
241
242
243
	/**
244
	 * @NoAdminRequired
245
	 *
246
	 * @param string $circleId
247
	 * @param array $members
248
	 *
249
	 * @return DataResponse
250
	 * @throws OCSException
251
	 */
252
	public function membersAdd(string $circleId, array $members): DataResponse {
253
		try {
254
			$this->setCurrentFederatedUser();
255
256
			$federatedUsers = [];
257
			foreach ($members as $member) {
258
				// TODO: generate Multiple FederatedUsers using a single SQL request
259
				$federatedUsers[] = $this->federatedUserService->generateFederatedUser(
260
					$this->get('id', $member),
261
					$this->getInt('type', $member)
262
				);
263
			}
264
265
			$result = $this->memberService->addMembers($circleId, $federatedUsers);
266
267
			return new DataResponse($this->serializeArray($result));
268
		} catch (Exception $e) {
269
			throw new OCSException($e->getMessage(), $e->getCode());
270
		}
271
272
	}
273
274
275
	/**
276
	 * @NoAdminRequired
277
	 *
278
	 * @param string $circleId
279
	 *
280
	 * @return DataResponse
281
	 * @throws OCSException
282
	 */
283 View Code Duplication
	public function circleJoin(string $circleId): DataResponse {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
284
		try {
285
			$this->setCurrentFederatedUser();
286
			$result = $this->circleService->circleJoin($circleId);
287
288
			return new DataResponse($this->serializeArray($result));
289
		} catch (Exception $e) {
290
			throw new OCSException($e->getMessage(), $e->getCode());
291
		}
292
	}
293
294
295
	/**
296
	 * @NoAdminRequired
297
	 *
298
	 * @param string $circleId
299
	 *
300
	 * @return DataResponse
301
	 * @throws OCSException
302
	 */
303 View Code Duplication
	public function circleLeave(string $circleId): DataResponse {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
304
		try {
305
			$this->setCurrentFederatedUser();
306
			$result = $this->circleService->circleLeave($circleId);
307
308
			return new DataResponse($this->serializeArray($result));
309
		} catch (Exception $e) {
310
			throw new OCSException($e->getMessage(), $e->getCode());
311
		}
312
	}
313
314
315
	/**
316
	 * @NoAdminRequired
317
	 *
318
	 * @param string $circleId
319
	 * @param string $memberId
320
	 * @param string|int $level
321
	 *
322
	 * @return DataResponse
323
	 * @throws OCSException
324
	 */
325
	public function memberLevel(string $circleId, string $memberId, $level): DataResponse {
326
		try {
327
			$this->setCurrentFederatedUser();
328
			if (is_int($level)) {
329
				$level = Member::parseLevelInt($level);
330
			} else {
331
				$level = Member::parseLevelString($level);
332
			}
333
334
			$this->memberService->getMemberById($memberId, $circleId);
335
			$result = $this->memberService->memberLevel($memberId, $level);
336
337
			return new DataResponse($this->serializeArray($result));
338
		} catch (Exception $e) {
339
			throw new OcsException($e->getMessage(), $e->getCode());
340
		}
341
	}
342
343
344
	/**
345
	 * @NoAdminRequired
346
	 *
347
	 * @param string $circleId
348
	 * @param string $memberId
349
	 *
350
	 * @return DataResponse
351
	 * @throws OCSException
352
	 */
353
	public function memberConfirm(string $circleId, string $memberId): DataResponse {
354
		try {
355
			$this->setCurrentFederatedUser();
356
357
			$member = $this->memberService->getMemberById($memberId, $circleId);
358
			$federatedUser = new FederatedUser();
359
			$federatedUser->importFromIFederatedUser($member);
360
361
			$result = $this->memberService->addMember($circleId, $federatedUser);
362
363
			return new DataResponse($this->serializeArray($result));
364
		} catch (Exception $e) {
365
			throw new OCSException($e->getMessage(), $e->getCode());
366
		}
367
	}
368
369
370
	/**
371
	 * @NoAdminRequired
372
	 *
373
	 * @param string $circleId
374
	 * @param string $memberId
375
	 *
376
	 * @return DataResponse
377
	 * @throws OCSException
378
	 */
379 View Code Duplication
	public function memberRemove(string $circleId, string $memberId): DataResponse {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
380
		try {
381
			$this->setCurrentFederatedUser();
382
			$this->memberService->getMemberById($memberId, $circleId);
383
384
			$result = $this->memberService->removeMember($memberId);
385
386
			return new DataResponse($this->serializeArray($result));
387
		} catch (Exception $e) {
388
			throw new OCSException($e->getMessage(), $e->getCode());
389
		}
390
	}
391
392
393
	/**
394
	 * @NoAdminRequired
395
	 *
396
	 * @return DataResponse
397
	 * @throws OCSException
398
	 */
399
	public function circles(): DataResponse {
400
		try {
401
			$this->setCurrentFederatedUser();
402
403
			return new DataResponse($this->serializeArray($this->circleService->getCircles()));
404
		} catch (Exception $e) {
405
			throw new OCSException($e->getMessage(), $e->getCode());
406
		}
407
	}
408
409
410
	/**
411
	 * @NoAdminRequired
412
	 *
413
	 * @param string $circleId
414
	 *
415
	 * @return DataResponse
416
	 * @throws OCSException
417
	 */
418 View Code Duplication
	public function members(string $circleId): DataResponse {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
419
		try {
420
			$this->setCurrentFederatedUser();
421
422
			return new DataResponse($this->serializeArray($this->memberService->getMembers($circleId)));
423
		} catch (Exception $e) {
424
			throw new OCSException($e->getMessage(), $e->getCode());
425
		}
426
	}
427
428
429
	/**
430
	 * @NoAdminRequired
431
	 *
432
	 * @param string $circleId
433
	 * @param string $value
434
	 *
435
	 * @return DataResponse
436
	 * @throws OCSException
437
	 */
438
	public function editName(string $circleId, string $value): DataResponse {
439
		try {
440
			$this->setCurrentFederatedUser();
441
442
			$outcome = $this->circleService->updateName($circleId, $value);
443
444
			return new DataResponse($this->serializeArray($outcome));
445
		} catch (Exception $e) {
446
			throw new OCSException($e->getMessage(), $e->getCode());
447
		}
448
	}
449
450
451
	/**
452
	 * @NoAdminRequired
453
	 *
454
	 * @param string $circleId
455
	 * @param string $value
456
	 *
457
	 * @return DataResponse
458
	 * @throws OCSException
459
	 */
460 View Code Duplication
	public function editDescription(string $circleId, string $value): DataResponse {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
461
		try {
462
			$this->setCurrentFederatedUser();
463
464
			$outcome = $this->circleService->updateDescription($circleId, $value);
465
466
			return new DataResponse($this->serializeArray($outcome));
467
		} catch (Exception $e) {
468
			throw new OCSException($e->getMessage(), $e->getCode());
469
		}
470
	}
471
472
473
	/**
474
	 * @NoAdminRequired
475
	 *
476
	 * @param string $circleId
477
	 * @param array $value
478
	 *
479
	 * @return DataResponse
480
	 * @throws OCSException
481
	 */
482 View Code Duplication
	public function editSettings(string $circleId, array $value): DataResponse {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
483
		try {
484
			$this->setCurrentFederatedUser();
485
486
			$outcome = $this->circleService->updateSettings($circleId, $value);
487
488
			return new DataResponse($this->serializeArray($outcome));
489
		} catch (Exception $e) {
490
			throw new OCSException($e->getMessage(), $e->getCode());
491
		}
492
	}
493
494
495
	/**
496
	 * @NoAdminRequired
497
	 *
498
	 * @param string $circleId
499
	 * @param int $value
500
	 *
501
	 * @return DataResponse
502
	 * @throws OCSException
503
	 */
504 View Code Duplication
	public function editConfig(string $circleId, int $value): DataResponse {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
505
		try {
506
			$this->setCurrentFederatedUser();
507
508
			$outcome = $this->circleService->updateConfig($circleId, $value);
509
510
			return new DataResponse($this->serializeArray($outcome));
511
		} catch (Exception $e) {
512
			throw new OCSException($e->getMessage(), $e->getCode());
513
		}
514
	}
515
516
517
	/**
518
	 * @NoAdminRequired
519
	 *
520
	 * @param string $circleId
521
	 * @param string $singleId
522
	 *
523
	 * @return DataResponse
524
	 * @throws OCSException
525
	 */
526
	public function link(string $circleId, string $singleId): DataResponse {
527
		try {
528
			$this->setCurrentFederatedUser();
529
			$membership = $this->membershipService->getMembership($circleId, $singleId);
530
531
			return new DataResponse($this->serialize($membership));
532
		} catch (Exception $e) {
533
			throw new OCSException($e->getMessage(), $e->getCode());
534
		}
535
	}
536
537
538
	/**
539
	 * @throws FederatedUserNotFoundException
540
	 * @throws InvalidIdException
541
	 * @throws FederatedUserException
542
	 * @throws SingleCircleNotFoundException
543
	 * @throws RequestBuilderException
544
	 */
545
	private function setCurrentFederatedUser() {
546
		$user = $this->userSession->getUser();
547
		$this->federatedUserService->setLocalCurrentUser($user);
548
	}
549
550
}
551
552