Completed
Push — master ( 96b2c0...c8832b )
by Maxence
03:52
created

GroupsController::add()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 15
nc 3
nop 2
1
<?php
2
/**
3
 * Circles - Bring cloud-users closer together.
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Maxence Lange <[email protected]>
9
 * @copyright 2017
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
 *
25
 */
26
27
namespace OCA\Circles\Controller;
28
29
use OCA\Circles\Exceptions\LinkedGroupNotAllowedException;
30
use OCP\AppFramework\Http\DataResponse;
31
32
class GroupsController extends BaseController {
33
34
35
	/**
36
	 * @NoAdminRequired
37
	 * @NoSubAdminRequired
38
	 *
39
	 * @param string $uniqueId
40
	 * @param string $name
41
	 *
42
	 * @return DataResponse
43
	 * @throws LinkedGroupNotAllowedException
44
	 */
45
	public function add($uniqueId, $name) {
46
		if (!$this->configService->isLinkedGroupsAllowed()) {
47
			throw new LinkedGroupNotAllowedException(
48
				$this->l10n->t("Linked Groups are not allowed on this Nextcloud")
49
			);
50
		}
51
52
		try {
53
			$data = $this->groupsService->linkGroup($uniqueId, $name);
54
		} catch (\Exception $e) {
55
			return $this->fail(
56
				[
57
					'circle_id' => $uniqueId,
58
					'name'      => $name,
59
					'error'     => $e->getMessage()
60
				]
61
			);
62
		}
63
64
		return $this->success(
65
			[
66
				'circle_id' => $uniqueId,
67
				'name'      => $name,
68
				'groups'    => $data
69
			]
70
		);
71
	}
72
73
74
	/**
75
	 * @NoAdminRequired
76
	 * @NoSubAdminRequired
77
	 *
78
	 * @param string $uniqueId
79
	 * @param string $group
80
	 * @param int $level
81
	 *
82
	 * @return DataResponse
83
	 */
84
	public function level($uniqueId, $group, $level) {
85
86
		try {
87
			$data = $this->groupsService->levelGroup($uniqueId, $group, $level);
88
		} catch (\Exception $e) {
89
			return
90
				$this->fail(
91
					[
92
						'circle_id' => $uniqueId,
93
						'name'      => $group,
94
						'level'     => $level,
95
						'error'     => $e->getMessage()
96
					]
97
				);
98
		}
99
100
		return $this->success(
101
			[
102
				'circle_id' => $uniqueId,
103
				'name'      => $group,
104
				'level'     => $level,
105
				'groups'    => $data,
106
			]
107
		);
108
	}
109
110
111
	/**
112
	 * @NoAdminRequired
113
	 * @NoSubAdminRequired
114
	 *
115
	 * @param string $uniqueId
116
	 * @param string $group
117
	 *
118
	 * @return DataResponse
119
	 * @throws LinkedGroupNotAllowedException
120
	 */
121
	public function remove($uniqueId, $group) {
122
		if (!$this->configService->isLinkedGroupsAllowed()) {
123
			throw new LinkedGroupNotAllowedException(
124
				$this->l10n->t('Linked Groups are not allowed on this Nextcloud')
125
			);
126
		}
127
128
		$args = ['circle_id' => $uniqueId, 'name' => $group];
129
		try {
130
			$data = $this->groupsService->unlinkGroup($uniqueId, $group);
131
		} catch (\Exception $e) {
132
			return $this->fail(array_merge($args, ['error' => $e->getMessage()]));
133
		}
134
135
		return $this->success(array_merge($args, ['groups' => $data]));
136
	}
137
138
139
}
140
141