|
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 Exception; |
|
30
|
|
|
use OCA\Circles\Exceptions\CircleNameFirstCharException; |
|
31
|
|
|
use OCA\Circles\Exceptions\CircleNameTooShortException; |
|
32
|
|
|
use OCA\Circles\Exceptions\CircleTypeDisabledException; |
|
33
|
|
|
use OCA\Circles\Exceptions\FederatedCircleNotAllowedException; |
|
34
|
|
|
use OCP\AppFramework\Http\DataResponse; |
|
35
|
|
|
|
|
36
|
|
|
class CirclesController extends BaseController { |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @NoAdminRequired |
|
40
|
|
|
* @NoSubAdminRequired |
|
41
|
|
|
* |
|
42
|
|
|
* @param $type |
|
43
|
|
|
* @param string $name |
|
44
|
|
|
* |
|
45
|
|
|
* @return DataResponse |
|
46
|
|
|
*/ |
|
47
|
|
|
public function create($type, $name) { |
|
48
|
|
|
|
|
49
|
|
|
try { |
|
50
|
|
|
$this->verifyCreationName($name); |
|
51
|
|
|
$data = $this->circlesService->createCircle($type, $name); |
|
52
|
|
|
|
|
53
|
|
|
return $this->success(['name' => $name, 'circle' => $data, 'type' => $type]); |
|
54
|
|
|
} catch (Exception $e) { |
|
55
|
|
|
return $this->fail(['type' => $type, 'name' => $name, 'error' => $e->getMessage()]); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* verifyCreationName(); |
|
63
|
|
|
* |
|
64
|
|
|
* Verify the name at the creation of a circle: |
|
65
|
|
|
* Name must contain at least 3 chars. |
|
66
|
|
|
* First char must be alpha-numeric. |
|
67
|
|
|
* |
|
68
|
|
|
* @param $name |
|
69
|
|
|
* |
|
70
|
|
|
* @throws CircleNameFirstCharException |
|
71
|
|
|
* @throws CircleNameTooShortException |
|
72
|
|
|
*/ |
|
73
|
|
|
private function verifyCreationName($name) { |
|
74
|
|
|
if (strlen($name) < 3) { |
|
75
|
|
|
throw new CircleNameTooShortException( |
|
76
|
|
|
$this->l10n->t('The name of your circle must contain at least 3 characters') |
|
77
|
|
|
); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$chars = 'abcdefghijklmnopqrstuvwxyz0123456789'; |
|
81
|
|
|
if (strpos($chars, strtolower(substr($name, 0, 1))) === false) { |
|
82
|
|
|
throw new CircleNameFirstCharException( |
|
83
|
|
|
$this->l10n->t( |
|
84
|
|
|
"The name of your circle must start with an alpha-numerical character" |
|
85
|
|
|
) |
|
86
|
|
|
); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @NoAdminRequired |
|
93
|
|
|
* @NoSubAdminRequired |
|
94
|
|
|
* |
|
95
|
|
|
* @param $type |
|
96
|
|
|
* @param string $name |
|
97
|
|
|
* @param int $level |
|
98
|
|
|
* |
|
99
|
|
|
* @return DataResponse |
|
100
|
|
|
*/ |
|
101
|
|
|
public function listing($type, $name = '', $level = 0) { |
|
102
|
|
|
|
|
103
|
|
|
try { |
|
104
|
|
|
$data = $this->circlesService->listCircles($type, $name, $level); |
|
105
|
|
|
|
|
106
|
|
|
return $this->success(['type' => $type, 'data' => $data]); |
|
107
|
|
|
} catch (CircleTypeDisabledException $e) { |
|
108
|
|
|
|
|
109
|
|
|
return $this->fail(['type' => $type, 'error' => $e->getMessage()]); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @NoAdminRequired |
|
116
|
|
|
* @NoSubAdminRequired |
|
117
|
|
|
* |
|
118
|
|
|
* @param string $uniqueId |
|
119
|
|
|
* |
|
120
|
|
|
* @return DataResponse |
|
121
|
|
|
*/ |
|
122
|
|
View Code Duplication |
public function details($uniqueId) { |
|
|
|
|
|
|
123
|
|
|
try { |
|
124
|
|
|
$circle = $this->circlesService->detailsCircle($uniqueId); |
|
125
|
|
|
|
|
126
|
|
|
return $this->success(['circle_id' => $uniqueId, 'details' => $circle]); |
|
127
|
|
|
} catch (\Exception $e) { |
|
128
|
|
|
|
|
129
|
|
|
return $this->fail(['circle_id' => $uniqueId, 'error' => $e->getMessage()]); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @NoAdminRequired |
|
137
|
|
|
* @NoSubAdminRequired |
|
138
|
|
|
* |
|
139
|
|
|
* @param string $uniqueId |
|
140
|
|
|
* @param array $settings |
|
141
|
|
|
* |
|
142
|
|
|
* @return DataResponse |
|
143
|
|
|
*/ |
|
144
|
|
View Code Duplication |
public function settings($uniqueId, $settings) { |
|
|
|
|
|
|
145
|
|
|
try { |
|
146
|
|
|
$this->verifyCreationName($settings['circle_name']); |
|
147
|
|
|
$circle = $this->circlesService->settingsCircle($uniqueId, $settings); |
|
148
|
|
|
|
|
149
|
|
|
return $this->success(['circle_id' => $uniqueId, 'details' => $circle]); |
|
150
|
|
|
} catch (\Exception $e) { |
|
151
|
|
|
|
|
152
|
|
|
return $this->fail(['circle_id' => $uniqueId, 'error' => $e->getMessage()]); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* @NoAdminRequired |
|
160
|
|
|
* @NoSubAdminRequired |
|
161
|
|
|
* |
|
162
|
|
|
* @param string $uniqueId |
|
163
|
|
|
* |
|
164
|
|
|
* @return DataResponse |
|
165
|
|
|
*/ |
|
166
|
|
View Code Duplication |
public function join($uniqueId) { |
|
|
|
|
|
|
167
|
|
|
try { |
|
168
|
|
|
$data = $this->circlesService->joinCircle($uniqueId); |
|
169
|
|
|
|
|
170
|
|
|
return $this->success(['circle_id' => $uniqueId, 'member' => $data]); |
|
171
|
|
|
} catch (\Exception $e) { |
|
172
|
|
|
|
|
173
|
|
|
return $this->fail(['circle_id' => $uniqueId, 'error' => $e->getMessage()]); |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* @NoAdminRequired |
|
180
|
|
|
* @NoSubAdminRequired |
|
181
|
|
|
* |
|
182
|
|
|
* @param string $uniqueId |
|
183
|
|
|
* |
|
184
|
|
|
* @return DataResponse |
|
185
|
|
|
*/ |
|
186
|
|
View Code Duplication |
public function leave($uniqueId) { |
|
|
|
|
|
|
187
|
|
|
try { |
|
188
|
|
|
$data = $this->circlesService->leaveCircle($uniqueId); |
|
189
|
|
|
|
|
190
|
|
|
return $this->success(['circle_id' => $uniqueId, 'member' => $data]); |
|
191
|
|
|
} catch (\Exception $e) { |
|
192
|
|
|
|
|
193
|
|
|
return $this->fail(['circle_id' => $uniqueId, 'error' => $e->getMessage()]); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* @NoAdminRequired |
|
201
|
|
|
* @NoSubAdminRequired |
|
202
|
|
|
* |
|
203
|
|
|
* @param string $uniqueId |
|
204
|
|
|
* |
|
205
|
|
|
* @return DataResponse |
|
206
|
|
|
*/ |
|
207
|
|
|
public function destroy($uniqueId) { |
|
208
|
|
|
try { |
|
209
|
|
|
$this->circlesService->removeCircle($uniqueId); |
|
210
|
|
|
|
|
211
|
|
|
return $this->success(['circle_id' => $uniqueId]); |
|
212
|
|
|
} catch (\Exception $e) { |
|
213
|
|
|
return $this->fail(['circle_id' => $uniqueId, 'error' => $e->getMessage()]); |
|
214
|
|
|
} |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* link() |
|
220
|
|
|
* |
|
221
|
|
|
* Called from the UI to create a initiate the process of linking 2 [remote] circles. |
|
222
|
|
|
* $remote format: <circle_name>@<remote_host> |
|
223
|
|
|
* |
|
224
|
|
|
* @NoAdminRequired |
|
225
|
|
|
* @NoSubAdminRequired |
|
226
|
|
|
* |
|
227
|
|
|
* @param string $uniqueId |
|
228
|
|
|
* @param string $remote |
|
229
|
|
|
* |
|
230
|
|
|
* @return DataResponse |
|
231
|
|
|
*/ |
|
232
|
|
|
public function link($uniqueId, $remote) { |
|
233
|
|
|
try { |
|
234
|
|
|
$link = $this->federatedService->linkCircle($uniqueId, $remote); |
|
235
|
|
|
$links = $this->circlesService->detailsCircle($uniqueId) |
|
236
|
|
|
->getLinks(); |
|
237
|
|
|
|
|
238
|
|
|
return $this->success( |
|
239
|
|
|
['circle_id' => $uniqueId, 'remote' => $remote, 'link' => $link, 'links' => $links] |
|
240
|
|
|
); |
|
241
|
|
|
} catch (\Exception $e) { |
|
242
|
|
|
return $this->fail( |
|
243
|
|
|
['circle_id' => $uniqueId, 'remote' => $remote, 'error' => $e->getMessage()] |
|
244
|
|
|
); |
|
245
|
|
|
} |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
|
|
249
|
|
|
/** |
|
250
|
|
|
* linkStatus(); |
|
251
|
|
|
* |
|
252
|
|
|
* Modify a link status. Used to confirm/dismiss a request or putting down a link. |
|
253
|
|
|
* The function will modify local status and broadcast the status to the remote. |
|
254
|
|
|
* |
|
255
|
|
|
* Note: should be moved to a LinkController |
|
256
|
|
|
* |
|
257
|
|
|
* @NoAdminRequired |
|
258
|
|
|
* @NoSubAdminRequired |
|
259
|
|
|
* |
|
260
|
|
|
* @param int $linkId |
|
261
|
|
|
* @param int $status |
|
262
|
|
|
* |
|
263
|
|
|
* @return DataResponse |
|
264
|
|
|
* @throws FederatedCircleNotAllowedException |
|
265
|
|
|
*/ |
|
266
|
|
|
public function linkStatus($linkId, $status) { |
|
267
|
|
|
|
|
268
|
|
|
if (!$this->configService->isFederatedCirclesAllowed()) { |
|
269
|
|
|
throw new FederatedCircleNotAllowedException( |
|
270
|
|
|
$this->l10n->t("Federated circles are not allowed on this Nextcloud") |
|
271
|
|
|
); |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
try { |
|
275
|
|
|
$links = $this->federatedService->linkStatus($linkId, $status); |
|
276
|
|
|
|
|
277
|
|
|
return $this->success(['link_id' => $linkId, 'links' => $links]); |
|
278
|
|
|
} catch (\Exception $e) { |
|
279
|
|
|
return $this->fail( |
|
280
|
|
|
['link_id' => $linkId, 'error' => $e->getMessage()] |
|
281
|
|
|
); |
|
282
|
|
|
} |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
|
|
|
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
|
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.