Completed
Pull Request — master (#386)
by Maxence
01:53
created

MembersController::removeMember()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 29

Duplication

Lines 29
Ratio 100 %

Importance

Changes 0
Metric Value
dl 29
loc 29
rs 9.456
c 0
b 0
f 0
cc 2
nc 3
nop 3
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\Model\SearchResult;
30
use OCA\Circles\Service\MiscService;
31
use OCP\AppFramework\Http\DataResponse;
32
33
class MembersController extends BaseController {
34
35
36
	/**
37
	 * @NoAdminRequired
38
	 * @NoSubAdminRequired
39
	 *
40
	 * @param string $uniqueId
41
	 * @param $ident
42
	 * @param $type
43
	 *
44
	 * @return DataResponse
45
	 */
46 View Code Duplication
	public function addMember($uniqueId, $ident, $type) {
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...
47
48
		try {
49
			$this->mustHaveFrontEndEnabled();
50
51
			$data = $this->membersService->addMember($uniqueId, $ident, (int)$type);
52
		} catch (\Exception $e) {
53
			return $this->fail(
54
				[
55
					'circle_id' => $uniqueId,
56
					'user_id'   => $ident,
57
					'user_type' => (int)$type,
58
					'display'   => MiscService::getDisplay($ident, (int)$type),
59
					'error'     => $e->getMessage()
60
				]
61
			);
62
		}
63
64
		return $this->success(
65
			[
66
				'circle_id' => $uniqueId,
67
				'user_id'   => $ident,
68
				'user_type' => (int)$type,
69
				'display'   => MiscService::getDisplay($ident, (int)$type),
70
				'members'   => $data
71
			]
72
		);
73
	}
74
75
76
	/**
77
	 * @NoAdminRequired
78
	 * @NoSubAdminRequired
79
	 *
80
	 * @param $memberId
81
	 *
82
	 * @return DataResponse
83
	 */
84 View Code Duplication
	public function addMemberById(string $memberId) {
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...
85
		try {
86
			$this->mustHaveFrontEndEnabled();
87
88
			$member = $this->membersService->getMemberById($memberId);
89
			$data = $this->membersService->addMember(
90
				$member->getCircleId(), $member->getUserId(), $member->getType()
91
			);
92
		} catch (\Exception $e) {
93
			return $this->fail(
94
				[
95
					'member_id' => $memberId,
96
					'error'     => $e->getMessage()
97
				]
98
			);
99
		}
100
101
		return $this->success(
102
			[
103
				'member_id' => $memberId,
104
				'members'   => $data
105
			]
106
		);
107
	}
108
109
110
	/**
111
	 * @NoAdminRequired
112
	 * @NoSubAdminRequired
113
	 *
114
	 * @param string $uniqueId
115
	 * @param string $member
116
	 * @param int $type
117
	 * @param int $level
118
	 *
119
	 * @return DataResponse
120
	 */
121
	public function levelMember($uniqueId, $member, $type, $level) {
122
123
		try {
124
			$this->mustHaveFrontEndEnabled();
125
126
			$data = $this->membersService->levelMember($uniqueId, $member, (int)$type, $level);
127
		} catch (\Exception $e) {
128
			return
129
				$this->fail(
130
					[
131
						'circle_id' => $uniqueId,
132
						'user_id'   => $member,
133
						'user_type' => (int)$type,
134
						'display'   => MiscService::getDisplay($member, (int)$type),
135
						'level'     => $level,
136
						'error'     => $e->getMessage()
137
					]
138
				);
139
		}
140
141
		return $this->success(
142
			[
143
				'circle_id' => $uniqueId,
144
				'user_id'   => $member,
145
				'user_type' => (int)$type,
146
				'display'   => MiscService::getDisplay($member, (int)$type),
147
				'level'     => $level,
148
				'members'   => $data,
149
			]
150
		);
151
	}
152
153
154
	/**
155
	 * @NoAdminRequired
156
	 * @NoSubAdminRequired
157
	 *
158
	 * @param string $uniqueId
159
	 * @param string $member
160
	 * @param int $type
161
	 *
162
	 * @return DataResponse
163
	 */
164 View Code Duplication
	public function removeMember($uniqueId, $member, $type) {
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...
165
166
		try {
167
			$this->mustHaveFrontEndEnabled();
168
169
			$data = $this->membersService->removeMember($uniqueId, $member, (int)$type);
170
		} catch (\Exception $e) {
171
			return
172
				$this->fail(
173
					[
174
						'circle_id' => $uniqueId,
175
						'user_id'   => $member,
176
						'user_type' => (int)$type,
177
						'display'   => MiscService::getDisplay($member, (int)$type),
178
						'error'     => $e->getMessage()
179
					]
180
				);
181
		}
182
183
		return $this->success(
184
			[
185
				'circle_id' => $uniqueId,
186
				'user_id'   => $member,
187
				'user_type' => (int)$type,
188
				'display'   => MiscService::getDisplay($member, (int)$type),
189
				'members'   => $data,
190
			]
191
		);
192
	}
193
194
195
	/**
196
	 * @NoAdminRequired
197
	 * @NoSubAdminRequired
198
	 *
199
	 * @param string $memberId
200
	 *
201
	 * @return DataResponse
202
	 */
203 View Code Duplication
	public function removeMemberById(string $memberId) {
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...
204
		try {
205
			$this->mustHaveFrontEndEnabled();
206
207
			$member = $this->membersService->getMemberById($memberId);
208
			$data = $this->membersService->removeMember(
209
				$member->getCircleId(), $member->getUserId(), $member->getType()
210
			);
211
		} catch (\Exception $e) {
212
			return
213
				$this->fail(
214
					[
215
						'member_id' => $memberId,
216
						'error'     => $e->getMessage()
217
					]
218
				);
219
		}
220
221
		return $this->success(
222
			[
223
				'member_id' => $memberId,
224
				'members'   => $data,
225
			]
226
		);
227
	}
228
229
230
	/**
231
	 * @NoAdminRequired
232
	 *
233
	 * @param string $search
234
	 *
235
	 * @return DataResponse
236
	 */
237
	public function searchGlobal($search) {
238
239
		try {
240
			$this->mustHaveFrontEndEnabled();
241
242
			$result = $this->searchService->searchGlobal($search);
243
		} catch (\Exception $e) {
244
			return
245
				$this->fail(
246
					[
247
						'search' => $search,
248
						'error'  => $e->getMessage()
249
					]
250
				);
251
		}
252
253
		if ($this->configService->getCoreValue('shareapi_allow_share_dialog_user_enumeration') === 'no') {
254
			$result = array_filter(
255
				$result,
256
				function($data, $k) use ($search) {
0 ignored issues
show
Unused Code introduced by
The parameter $k is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
257
					/** @var SearchResult $data */
258
					return $data->getIdent() === $search;
259
				}, ARRAY_FILTER_USE_BOTH
260
			);
261
		}
262
263
		return $this->success(['search' => $search, 'result' => $result]);
264
	}
265
266
}
267
268