|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* CMS Pico - Create websites using Pico CMS for Nextcloud. |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright Copyright (c) 2017, Maxence Lange (<[email protected]>) |
|
6
|
|
|
* @copyright Copyright (c) 2019, Daniel Rudolf (<[email protected]>) |
|
7
|
|
|
* |
|
8
|
|
|
* @license GNU AGPL version 3 or any later version |
|
9
|
|
|
* |
|
10
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
11
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
12
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
13
|
|
|
* License, or (at your option) any later version. |
|
14
|
|
|
* |
|
15
|
|
|
* This program is distributed in the hope that it will be useful, |
|
16
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
17
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
18
|
|
|
* GNU Affero General Public License for more details. |
|
19
|
|
|
* |
|
20
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
21
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
22
|
|
|
*/ |
|
23
|
|
|
|
|
24
|
|
|
declare(strict_types=1); |
|
25
|
|
|
|
|
26
|
|
|
namespace OCA\CMSPico\Controller; |
|
27
|
|
|
|
|
28
|
|
|
use OCA\CMSPico\AppInfo\Application; |
|
29
|
|
|
use OCA\CMSPico\Exceptions\TemplateNotCompatibleException; |
|
30
|
|
|
use OCA\CMSPico\Exceptions\TemplateNotFoundException; |
|
31
|
|
|
use OCA\CMSPico\Exceptions\ThemeNotCompatibleException; |
|
32
|
|
|
use OCA\CMSPico\Exceptions\ThemeNotFoundException; |
|
33
|
|
|
use OCA\CMSPico\Exceptions\WebsiteAlreadyExistsException; |
|
34
|
|
|
use OCA\CMSPico\Exceptions\WebsiteForeignOwnerException; |
|
35
|
|
|
use OCA\CMSPico\Exceptions\WebsiteInvalidDataException; |
|
36
|
|
|
use OCA\CMSPico\Exceptions\WebsiteInvalidOwnerException; |
|
37
|
|
|
use OCA\CMSPico\Exceptions\WebsiteNotFoundException; |
|
38
|
|
|
use OCA\CMSPico\Model\Website; |
|
39
|
|
|
use OCA\CMSPico\Service\WebsitesService; |
|
40
|
|
|
use OCP\AppFramework\Controller; |
|
41
|
|
|
use OCP\AppFramework\Http\DataResponse; |
|
42
|
|
|
use OCP\IL10N; |
|
43
|
|
|
use OCP\ILogger; |
|
44
|
|
|
use OCP\IRequest; |
|
45
|
|
|
use OCP\IUserSession; |
|
46
|
|
|
use function OCA\CMSPico\t; |
|
47
|
|
|
|
|
48
|
|
|
class WebsitesController extends Controller |
|
49
|
|
|
{ |
|
50
|
|
|
use ControllerTrait; |
|
51
|
|
|
|
|
52
|
|
|
/** @var IUserSession */ |
|
53
|
|
|
private $userSession; |
|
54
|
|
|
|
|
55
|
|
|
/** @var IL10N */ |
|
56
|
|
|
private $l10n; |
|
57
|
|
|
|
|
58
|
|
|
/** @var WebsitesService */ |
|
59
|
|
|
private $websitesService; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* WebsitesController constructor. |
|
63
|
|
|
* |
|
64
|
|
|
* @param IRequest $request |
|
65
|
|
|
* @param IUserSession $userSession |
|
66
|
|
|
* @param IL10N $l10n |
|
67
|
|
|
* @param ILogger $logger |
|
68
|
|
|
* @param WebsitesService $websitesService |
|
69
|
|
|
*/ |
|
70
|
1 |
|
public function __construct( |
|
71
|
|
|
IRequest $request, |
|
72
|
|
|
IUserSession $userSession, |
|
73
|
|
|
IL10N $l10n, |
|
74
|
|
|
ILogger $logger, |
|
75
|
|
|
WebsitesService $websitesService |
|
76
|
|
|
) { |
|
77
|
1 |
|
parent::__construct(Application::APP_NAME, $request); |
|
78
|
|
|
|
|
79
|
1 |
|
$this->userSession = $userSession; |
|
80
|
1 |
|
$this->l10n = $l10n; |
|
81
|
1 |
|
$this->logger = $logger; |
|
82
|
1 |
|
$this->websitesService = $websitesService; |
|
83
|
1 |
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @NoAdminRequired |
|
87
|
|
|
* |
|
88
|
|
|
* @return DataResponse |
|
89
|
|
|
*/ |
|
90
|
6 |
|
public function getPersonalWebsites(): DataResponse |
|
91
|
|
|
{ |
|
92
|
|
|
try { |
|
93
|
6 |
|
$userId = $this->userSession->getUser()->getUID(); |
|
94
|
6 |
|
$data = [ 'websites' => $this->websitesService->getWebsitesFromUser($userId) ]; |
|
95
|
6 |
|
return new DataResponse($data); |
|
96
|
|
|
} catch (\Throwable $e) { |
|
97
|
|
|
return $this->createErrorResponse($e); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @NoAdminRequired |
|
103
|
|
|
* |
|
104
|
|
|
* @param array<string,string> $data |
|
105
|
|
|
* |
|
106
|
|
|
* @return DataResponse |
|
107
|
|
|
*/ |
|
108
|
3 |
|
public function createPersonalWebsite(array $data): DataResponse |
|
109
|
|
|
{ |
|
110
|
|
|
try { |
|
111
|
3 |
|
$userId = $this->userSession->getUser()->getUID(); |
|
112
|
|
|
|
|
113
|
3 |
|
$website = (new Website()) |
|
114
|
3 |
|
->setName($data['name'] ?? '') |
|
115
|
3 |
|
->setUserId($userId) |
|
116
|
3 |
|
->setSite($data['site'] ?? '') |
|
117
|
3 |
|
->setTheme($data['theme'] ?? '') |
|
118
|
3 |
|
->setPath($data['path'] ?? ''); |
|
119
|
|
|
|
|
120
|
3 |
|
$website->assertValidOwner(); |
|
121
|
|
|
|
|
122
|
3 |
|
$this->websitesService->createWebsite($website, $data['template'] ?? ''); |
|
123
|
|
|
|
|
124
|
2 |
|
return $this->getPersonalWebsites(); |
|
125
|
1 |
|
} catch (\Throwable $e) { |
|
126
|
1 |
|
$error = []; |
|
127
|
1 |
|
if ($e instanceof WebsiteAlreadyExistsException) { |
|
128
|
1 |
|
$error += [ 'errorField' => 'site', 'error' => $this->l10n->t('Website exists.') ]; |
|
129
|
|
|
} elseif ($e instanceof WebsiteInvalidOwnerException) { |
|
130
|
|
|
$error += [ 'errorField' => 'user', 'error' => $this->l10n->t('No permission.') ]; |
|
131
|
|
|
} elseif (($e instanceof WebsiteInvalidDataException) && $e->getField()) { |
|
132
|
|
|
$errorMessage = $e->getError() ?? t('The value given is invalid.'); |
|
133
|
|
|
$error += [ 'errorField' => $e->getField(), 'error' => $this->l10n->t($errorMessage) ]; |
|
134
|
|
|
} elseif ($e instanceof ThemeNotFoundException) { |
|
135
|
|
|
$error += [ 'errorField' => 'theme', 'error' => $this->l10n->t('Theme not found.') ]; |
|
136
|
|
|
} elseif ($e instanceof ThemeNotCompatibleException) { |
|
137
|
|
|
$error += [ 'errorField' => 'theme', 'error' => $this->l10n->t($e->getReason()) ]; |
|
138
|
|
|
} elseif ($e instanceof TemplateNotFoundException) { |
|
139
|
|
|
$error += [ 'errorField' => 'template', 'error' => $this->l10n->t('Template not found.') ]; |
|
140
|
|
|
} elseif ($e instanceof TemplateNotCompatibleException) { |
|
141
|
|
|
$error += [ 'errorField' => 'template', 'error' => $this->l10n->t($e->getReason()) ]; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
1 |
|
return $this->createErrorResponse($e, $error); |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* @NoAdminRequired |
|
150
|
|
|
* |
|
151
|
|
|
* @param int $siteId |
|
152
|
|
|
* @param mixed[] $data |
|
153
|
|
|
* |
|
154
|
|
|
* @return DataResponse |
|
155
|
|
|
*/ |
|
156
|
1 |
|
public function updatePersonalWebsite(int $siteId, array $data): DataResponse |
|
157
|
|
|
{ |
|
158
|
|
|
try { |
|
159
|
1 |
|
$website = $this->websitesService->getWebsiteFromId($siteId); |
|
160
|
|
|
|
|
161
|
1 |
|
$userId = $this->userSession->getUser()->getUID(); |
|
162
|
1 |
|
$website->assertOwnedBy($userId); |
|
163
|
|
|
|
|
164
|
1 |
|
foreach ($data as $key => $value) { |
|
165
|
1 |
|
switch ($key) { |
|
166
|
1 |
|
case 'name': |
|
167
|
|
|
$website->setName($value); |
|
168
|
|
|
break; |
|
169
|
|
|
|
|
170
|
1 |
|
case 'type': |
|
171
|
1 |
|
$website->setType((int) $value); |
|
172
|
1 |
|
break; |
|
173
|
|
|
|
|
174
|
|
|
case 'theme': |
|
175
|
|
|
$website->setTheme($value); |
|
176
|
|
|
break; |
|
177
|
|
|
|
|
178
|
|
|
case 'options': |
|
179
|
|
|
foreach ($value as $optionKey => $optionValue) { |
|
180
|
|
|
switch ($optionKey) { |
|
181
|
|
|
case 'group_access': |
|
182
|
|
|
$groupAccess = $optionValue ? explode('|', $optionValue) : []; |
|
183
|
|
|
$website->setGroupAccess($groupAccess); |
|
184
|
|
|
break; |
|
185
|
|
|
|
|
186
|
|
|
default: |
|
187
|
|
|
throw new WebsiteInvalidDataException( |
|
188
|
|
|
$website->getSite(), |
|
189
|
|
|
'options.' . $optionKey |
|
190
|
|
|
); |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
break; |
|
194
|
|
|
|
|
195
|
|
|
default: |
|
196
|
|
|
throw new WebsiteInvalidDataException($website->getSite(), $key); |
|
197
|
|
|
} |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
1 |
|
$this->websitesService->updateWebsite($website); |
|
201
|
|
|
|
|
202
|
1 |
|
return $this->getPersonalWebsites(); |
|
203
|
|
|
} catch (\Throwable $e) { |
|
204
|
|
|
$error = []; |
|
205
|
|
|
if (($e instanceof WebsiteNotFoundException) || ($e instanceof WebsiteForeignOwnerException)) { |
|
206
|
|
|
$error += [ 'errorField' => 'identifier', 'error' => $this->l10n->t('Website not found.') ]; |
|
207
|
|
|
} elseif (($e instanceof WebsiteInvalidDataException) && $e->getField()) { |
|
208
|
|
|
$errorMessage = $e->getError() ?? t('The value given is invalid.'); |
|
209
|
|
|
$error += [ 'errorField' => $e->getField(), 'error' => $this->l10n->t($errorMessage) ]; |
|
210
|
|
|
} elseif ($e instanceof ThemeNotFoundException) { |
|
211
|
|
|
$error += [ 'errorField' => 'theme', 'error' => $this->l10n->t('Theme not found.') ]; |
|
212
|
|
|
} elseif ($e instanceof ThemeNotCompatibleException) { |
|
213
|
|
|
$error += [ 'errorField' => 'theme', 'error' => $this->l10n->t($e->getReason()) ]; |
|
214
|
|
|
} elseif ($e instanceof TemplateNotFoundException) { |
|
215
|
|
|
$error += [ 'errorField' => 'template', 'error' => $this->l10n->t('Template not found.') ]; |
|
216
|
|
|
} elseif ($e instanceof TemplateNotCompatibleException) { |
|
217
|
|
|
$error += [ 'errorField' => 'template', 'error' => $this->l10n->t($e->getReason()) ]; |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
return $this->createErrorResponse($e, $error); |
|
221
|
|
|
} |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* @NoAdminRequired |
|
226
|
|
|
* |
|
227
|
|
|
* @param int $siteId |
|
228
|
|
|
* |
|
229
|
|
|
* @return DataResponse |
|
230
|
|
|
*/ |
|
231
|
1 |
|
public function removePersonalWebsite(int $siteId): DataResponse |
|
232
|
|
|
{ |
|
233
|
|
|
try { |
|
234
|
1 |
|
$website = $this->websitesService->getWebsiteFromId($siteId); |
|
235
|
|
|
|
|
236
|
1 |
|
$userId = $this->userSession->getUser()->getUID(); |
|
237
|
1 |
|
$website->assertOwnedBy($userId); |
|
238
|
|
|
|
|
239
|
1 |
|
$this->websitesService->deleteWebsite($website); |
|
240
|
|
|
|
|
241
|
1 |
|
return $this->getPersonalWebsites(); |
|
242
|
|
|
} catch (WebsiteNotFoundException | WebsiteForeignOwnerException $e) { |
|
243
|
|
|
return $this->createErrorResponse($e, [ 'error' => $this->l10n->t('Website not found.') ]); |
|
244
|
|
|
} catch (\Throwable $e) { |
|
245
|
|
|
return $this->createErrorResponse($e); |
|
246
|
|
|
} |
|
247
|
|
|
} |
|
248
|
|
|
} |
|
249
|
|
|
|