Completed
Pull Request — master (#641)
by Maxence
02:35
created

AdminController::circleJoin()   A

Complexity

Conditions 2
Paths 4

Size

Total Lines 10

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
dl 10
loc 10
rs 9.9332
c 0
b 0
f 0
cc 2
nc 4
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 ArtificialOwl\MySmallPhpTools\Traits\Nextcloud\nc22\TNC22Deserialize;
36
use ArtificialOwl\MySmallPhpTools\Traits\Nextcloud\nc22\TNC22Logger;
37
use Exception;
38
use OCA\Circles\Exceptions\ContactAddressBookNotFoundException;
39
use OCA\Circles\Exceptions\ContactFormatException;
40
use OCA\Circles\Exceptions\ContactNotFoundException;
41
use OCA\Circles\Exceptions\FederatedUserException;
42
use OCA\Circles\Exceptions\FederatedUserNotFoundException;
43
use OCA\Circles\Exceptions\InvalidIdException;
44
use OCA\Circles\Exceptions\RequestBuilderException;
45
use OCA\Circles\Exceptions\SingleCircleNotFoundException;
46
use OCA\Circles\Model\FederatedUser;
47
use OCA\Circles\Model\Member;
48
use OCA\Circles\Service\CircleService;
49
use OCA\Circles\Service\ConfigService;
50
use OCA\Circles\Service\FederatedUserService;
51
use OCA\Circles\Service\MemberService;
52
use OCA\Circles\Service\MembershipService;
53
use OCA\Circles\Service\SearchService;
54
use OCP\AppFramework\Http\DataResponse;
55
use OCP\AppFramework\OCS\OCSException;
56
use OCP\AppFramework\OCSController;
57
use OCP\IRequest;
58
use OCP\IUserSession;
59
60
61
/**
62
 * Class AdminController
63
 *
64
 * @package OCA\Circles\Controller
65
 */
66
class AdminController extends OcsController {
67
68
69
	use TNC22Deserialize;
70
	use TNC22Logger;
71
72
73
	/** @var IUserSession */
74
	private $userSession;
75
76
	/** @var FederatedUserService */
77
	private $federatedUserService;
78
79
	/** @var CircleService */
80
	private $circleService;
81
82
	/** @var MemberService */
83
	private $memberService;
84
85
	/** @var MembershipService */
86
	private $membershipService;
87
88
	/** @var SearchService */
89
	private $searchService;
90
91
	/** @var ConfigService */
92
	protected $configService;
93
94
95
	/**
96
	 * LocalController constructor.
97
	 *
98
	 * @param string $appName
99
	 * @param IRequest $request
100
	 * @param IUserSession $userSession
101
	 * @param FederatedUserService $federatedUserService
102
	 * @param CircleService $circleService
103
	 * @param MemberService $memberService
104
	 * @param MembershipService $membershipService
105
	 * @param SearchService $searchService
106
	 * @param ConfigService $configService
107
	 */
108 View Code Duplication
	public function __construct(
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...
109
		string $appName,
110
		IRequest $request,
111
		IUserSession $userSession,
112
		FederatedUserService $federatedUserService,
113
		CircleService $circleService,
114
		MemberService $memberService,
115
		MembershipService $membershipService,
116
		SearchService $searchService,
117
		ConfigService $configService
118
	) {
119
		parent::__construct($appName, $request);
120
		$this->userSession = $userSession;
121
		$this->federatedUserService = $federatedUserService;
122
		$this->circleService = $circleService;
123
		$this->memberService = $memberService;
124
		$this->membershipService = $membershipService;
125
		$this->searchService = $searchService;
126
		$this->configService = $configService;
127
128
		$this->setup('app', 'circles');
129
	}
130
131
132
	/**
133
	 * @param string $emulated
134
	 * @param string $name
135
	 * @param bool $personal
136
	 * @param bool $local
137
	 *
138
	 * @return DataResponse
139
	 * @throws OCSException
140
	 */
141 View Code Duplication
	public function create(
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...
142
		string $emulated,
143
		string $name,
144
		bool $personal = false,
145
		bool $local = false
146
	): DataResponse {
147
		try {
148
			$this->setLocalFederatedUser($emulated);
149
			$circle = $this->circleService->create($name, null, $personal, $local);
150
151
			return new DataResponse($this->serializeArray($circle));
152
		} catch (Exception $e) {
153
			throw new OcsException($e->getMessage(), $e->getCode());
154
		}
155
	}
156
157
158
	/**
159
	 * @param string $emulated
160
	 * @param string $circleId
161
	 *
162
	 * @return DataResponse
163
	 * @throws OCSException
164
	 */
165 View Code Duplication
	public function destroy(string $emulated, 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...
166
		try {
167
			$this->setLocalFederatedUser($emulated);
168
			$circle = $this->circleService->destroy($circleId);
169
170
			return new DataResponse($this->serializeArray($circle));
171
		} catch (Exception $e) {
172
			throw new OcsException($e->getMessage(), $e->getCode());
173
		}
174
	}
175
176
177
	/**
178
	 * @param string $emulated
179
	 * @param string $circleId
180
	 * @param string $userId
181
	 * @param int $type
182
	 *
183
	 * @return DataResponse
184
	 * @throws OCSException
185
	 */
186 View Code Duplication
	public function memberAdd(string $emulated, string $circleId, string $userId, int $type): 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...
187
		try {
188
			$this->setLocalFederatedUser($emulated);
189
190
			// exception in Contact
191
			if ($type === Member::TYPE_CONTACT) {
192
				$currentUser = $this->federatedUserService->getCurrentUser();
193
				if (!$this->configService->isLocalInstance($currentUser->getInstance())) {
194
					throw new OCSException('works only from local instance', 404);
195
				}
196
197
				$userId = $currentUser->getUserId() . '/' . $userId;
198
			}
199
200
			$federatedUser = $this->federatedUserService->generateFederatedUser($userId, $type);
201
			$result = $this->memberService->addMember($circleId, $federatedUser);
202
203
			return new DataResponse($this->serializeArray($result));
204
		} catch (Exception $e) {
205
			throw new OCSException($e->getMessage(), $e->getCode());
206
		}
207
	}
208
209
210
	/**
211
	 * @param string $emulated
212
	 * @param string $circleId
213
	 * @param string $memberId
214
	 * @param string|int $level
215
	 *
216
	 * @return DataResponse
217
	 * @throws OCSException
218
	 */
219 View Code Duplication
	public function memberLevel(string $emulated, string $circleId, string $memberId, $level): 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...
220
		try {
221
			$this->setLocalFederatedUser($emulated);
222
			if (is_int($level)) {
223
				$level = Member::parseLevelInt($level);
224
			} else {
225
				$level = Member::parseLevelString($level);
226
			}
227
228
			$this->memberService->getMemberById($memberId, $circleId);
229
			$result = $this->memberService->memberLevel($memberId, $level);
230
231
			return new DataResponse($this->serializeArray($result));
232
		} catch (Exception $e) {
233
			throw new OcsException($e->getMessage(), $e->getCode());
234
		}
235
	}
236
237
238
	/**
239
	 * @param string $emulated
240
	 *
241
	 * @return DataResponse
242
	 * @throws OCSException
243
	 */
244 View Code Duplication
	public function circles(string $emulated): 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...
245
		try {
246
			$this->setLocalFederatedUser($emulated);
247
248
			return new DataResponse($this->serializeArray($this->circleService->getCircles()));
249
		} catch (Exception $e) {
250
			throw new OCSException($e->getMessage(), $e->getCode());
251
		}
252
	}
253
254
255
	/**
256
	 * @param string $emulated
257
	 * @param string $circleId
258
	 *
259
	 * @return DataResponse
260
	 * @throws OCSException
261
	 */
262 View Code Duplication
	public function circleDetails(string $emulated, 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...
263
		try {
264
			$this->setLocalFederatedUser($emulated);
265
266
			return new DataResponse($this->serialize($this->circleService->getCircle($circleId)));
267
		} catch (Exception $e) {
268
			throw new OcsException($e->getMessage(), $e->getCode());
269
		}
270
	}
271
272
273
	/**
274
	 * @param string $emulated
275
	 * @param string $circleId
276
	 *
277
	 * @return DataResponse
278
	 * @throws OCSException
279
	 */
280 View Code Duplication
	public function circleJoin(string $emulated, 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...
281
		try {
282
			$this->setLocalFederatedUser($emulated);
283
			$result = $this->circleService->circleJoin($circleId);
284
285
			return new DataResponse($this->serializeArray($result));
286
		} catch (Exception $e) {
287
			throw new OCSException($e->getMessage(), $e->getCode());
288
		}
289
	}
290
291
292
	/**
293
	 * @param string $emulated
294
	 * @param string $circleId
295
	 *
296
	 * @return DataResponse
297
	 * @throws OCSException
298
	 */
299 View Code Duplication
	public function circleLeave(string $emulated, 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...
300
		try {
301
			$this->setLocalFederatedUser($emulated);
302
			$result = $this->circleService->circleLeave($circleId);
303
304
			return new DataResponse($this->serializeArray($result));
305
		} catch (Exception $e) {
306
			throw new OCSException($e->getMessage(), $e->getCode());
307
		}
308
	}
309
310
311
	/**
312
	 * @param string $emulated
313
	 * @param string $circleId
314
	 * @param string $memberId
315
	 *
316
	 * @return DataResponse
317
	 * @throws OCSException
318
	 */
319 View Code Duplication
	public function memberConfirm(string $emulated, 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...
320
		try {
321
			$this->setLocalFederatedUser($emulated);
322
323
			$member = $this->memberService->getMemberById($memberId, $circleId);
324
			$federatedUser = new FederatedUser();
325
			$federatedUser->importFromIFederatedUser($member);
326
327
			$result = $this->memberService->addMember($circleId, $federatedUser);
328
329
			return new DataResponse($this->serializeArray($result));
330
		} catch (Exception $e) {
331
			throw new OCSException($e->getMessage(), $e->getCode());
332
		}
333
	}
334
335
336
	/**
337
	 * @param string $emulated
338
	 * @param string $circleId
339
	 * @param string $memberId
340
	 *
341
	 * @return DataResponse
342
	 * @throws OCSException
343
	 */
344 View Code Duplication
	public function memberRemove(string $emulated, 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...
345
		try {
346
			$this->setLocalFederatedUser($emulated);
347
			$this->memberService->getMemberById($memberId, $circleId);
348
349
			$result = $this->memberService->removeMember($memberId);
350
351
			return new DataResponse($this->serializeArray($result));
352
		} catch (Exception $e) {
353
			throw new OCSException($e->getMessage(), $e->getCode());
354
		}
355
	}
356
357
358
	/**
359
	 * @param string $emulated
360
	 * @param string $circleId
361
	 *
362
	 * @return DataResponse
363
	 * @throws OCSException
364
	 */
365 View Code Duplication
	public function members(string $emulated, 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...
366
		try {
367
			$this->setLocalFederatedUser($emulated);
368
369
			return new DataResponse($this->serializeArray($this->memberService->getMembers($circleId)));
370
		} catch (Exception $e) {
371
			throw new OCSException($e->getMessage(), $e->getCode());
372
		}
373
	}
374
375
376
	/**
377
	 * @param string $emulated
378
	 * @param string $circleId
379
	 * @param string $value
380
	 *
381
	 * @return DataResponse
382
	 * @throws OCSException
383
	 */
384 View Code Duplication
	public function editName(string $emulated, 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...
385
		try {
386
			$this->setLocalFederatedUser($emulated);
387
388
			$outcome = $this->circleService->updateName($circleId, $value);
389
390
			return new DataResponse($this->serializeArray($outcome));
391
		} catch (Exception $e) {
392
			throw new OCSException($e->getMessage(), $e->getCode());
393
		}
394
	}
395
396
397
	/**
398
	 * @param string $emulated
399
	 * @param string $circleId
400
	 * @param string $value
401
	 *
402
	 * @return DataResponse
403
	 * @throws OCSException
404
	 */
405 View Code Duplication
	public function editDescription(string $emulated, 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...
406
		try {
407
			$this->setLocalFederatedUser($emulated);
408
409
			$outcome = $this->circleService->updateDescription($circleId, $value);
410
411
			return new DataResponse($this->serializeArray($outcome));
412
		} catch (Exception $e) {
413
			throw new OCSException($e->getMessage(), $e->getCode());
414
		}
415
	}
416
417
418
	/**
419
	 * @param string $emulated
420
	 * @param string $circleId
421
	 * @param array $value
422
	 *
423
	 * @return DataResponse
424
	 * @throws OCSException
425
	 */
426 View Code Duplication
	public function editSettings(string $emulated, 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...
427
		try {
428
			$this->setLocalFederatedUser($emulated);
429
430
			$outcome = $this->circleService->updateSettings($circleId, $value);
431
432
			return new DataResponse($this->serializeArray($outcome));
433
		} catch (Exception $e) {
434
			throw new OCSException($e->getMessage(), $e->getCode());
435
		}
436
	}
437
438
439
	/**
440
	 * @param string $emulated
441
	 * @param string $circleId
442
	 * @param int $value
443
	 *
444
	 * @return DataResponse
445
	 * @throws OCSException
446
	 */
447 View Code Duplication
	public function editConfig(string $emulated, 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...
448
		try {
449
			$this->setLocalFederatedUser($emulated);
450
451
			$outcome = $this->circleService->updateConfig($circleId, $value);
452
453
			return new DataResponse($this->serializeArray($outcome));
454
		} catch (Exception $e) {
455
			throw new OCSException($e->getMessage(), $e->getCode());
456
		}
457
	}
458
459
460
	/**
461
	 * @param string $emulated
462
	 * @param string $circleId
463
	 * @param string $singleId
464
	 *
465
	 * @return DataResponse
466
	 * @throws OCSException
467
	 */
468 View Code Duplication
	public function link(string $emulated, string $circleId, string $singleId): 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...
469
		try {
470
			$this->setLocalFederatedUser($emulated);
471
			$membership = $this->membershipService->getMembership($circleId, $singleId);
472
473
			return new DataResponse($this->serialize($membership));
474
		} catch (Exception $e) {
475
			throw new OCSException($e->getMessage(), $e->getCode());
476
		}
477
	}
478
479
480
	/**
481
	 * @param string $emulated
482
	 *
483
	 * @throws FederatedUserException
484
	 * @throws FederatedUserNotFoundException
485
	 * @throws InvalidIdException
486
	 * @throws RequestBuilderException
487
	 * @throws SingleCircleNotFoundException
488
	 * @throws ContactAddressBookNotFoundException
489
	 * @throws ContactFormatException
490
	 * @throws ContactNotFoundException
491
	 */
492
	private function setLocalFederatedUser(string $emulated): void {
493
		$user = $this->userSession->getUser();
494
		$this->federatedUserService->setCurrentPatron($user->getUID());
495
		$this->federatedUserService->setLocalCurrentUserId($emulated);
496
	}
497
498
}
499
500