Completed
Pull Request — master (#617)
by Maxence
03:02
created

LocalController::editName()   A

Complexity

Conditions 2
Paths 4

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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