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\WebsiteExistsException; |
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
|
|
|
|
47
|
|
|
class WebsitesController extends Controller |
48
|
|
|
{ |
49
|
|
|
use ControllerTrait; |
50
|
|
|
|
51
|
|
|
/** @var IUserSession */ |
52
|
|
|
private $userSession; |
53
|
|
|
|
54
|
|
|
/** @var IL10N */ |
55
|
|
|
private $l10n; |
56
|
|
|
|
57
|
|
|
/** @var WebsitesService */ |
58
|
|
|
private $websitesService; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* WebsitesController constructor. |
62
|
|
|
* |
63
|
|
|
* @param IRequest $request |
64
|
|
|
* @param IUserSession $userSession |
65
|
|
|
* @param IL10N $l10n |
66
|
|
|
* @param ILogger $logger |
67
|
|
|
* @param WebsitesService $websitesService |
68
|
|
|
*/ |
69
|
1 |
|
public function __construct( |
70
|
|
|
IRequest $request, |
71
|
|
|
IUserSession $userSession, |
72
|
|
|
IL10N $l10n, |
73
|
|
|
ILogger $logger, |
74
|
|
|
WebsitesService $websitesService |
75
|
|
|
) { |
76
|
1 |
|
parent::__construct(Application::APP_NAME, $request); |
77
|
|
|
|
78
|
1 |
|
$this->userSession = $userSession; |
79
|
1 |
|
$this->l10n = $l10n; |
80
|
1 |
|
$this->logger = $logger; |
81
|
1 |
|
$this->websitesService = $websitesService; |
82
|
1 |
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @NoAdminRequired |
86
|
|
|
* |
87
|
|
|
* @return DataResponse |
88
|
|
|
*/ |
89
|
6 |
|
public function getPersonalWebsites(): DataResponse |
90
|
|
|
{ |
91
|
|
|
try { |
92
|
6 |
|
$userId = $this->userSession->getUser()->getUID(); |
93
|
6 |
|
$data = [ 'websites' => $this->websitesService->getWebsitesFromUser($userId) ]; |
94
|
6 |
|
return new DataResponse($data); |
95
|
|
|
} catch (\Throwable $e) { |
96
|
|
|
return $this->createErrorResponse($e); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @NoAdminRequired |
102
|
|
|
* |
103
|
|
|
* @param array<string,string> $data |
104
|
|
|
* |
105
|
|
|
* @return DataResponse |
106
|
|
|
*/ |
107
|
3 |
|
public function createPersonalWebsite(array $data): DataResponse |
108
|
|
|
{ |
109
|
|
|
try { |
110
|
3 |
|
$userId = $this->userSession->getUser()->getUID(); |
111
|
|
|
|
112
|
3 |
|
$website = (new Website()) |
113
|
3 |
|
->setName($data['name'] ?? '') |
114
|
3 |
|
->setUserId($userId) |
115
|
3 |
|
->setSite($data['site'] ?? '') |
116
|
3 |
|
->setTheme($data['theme'] ?? '') |
117
|
3 |
|
->setPath($data['path'] ?? ''); |
118
|
|
|
|
119
|
3 |
|
$website->assertValidOwner(); |
120
|
|
|
|
121
|
3 |
|
$this->websitesService->createWebsite($website, $data['template'] ?? ''); |
122
|
|
|
|
123
|
2 |
|
return $this->getPersonalWebsites(); |
124
|
1 |
|
} catch (\Throwable $e) { |
125
|
1 |
|
$error = []; |
126
|
1 |
|
if ($e instanceof WebsiteExistsException) { |
127
|
1 |
|
$error += [ 'errorField' => 'site', 'error' => $this->l10n->t('Website exists.') ]; |
128
|
|
|
} elseif ($e instanceof WebsiteInvalidOwnerException) { |
129
|
|
|
$error += [ 'errorField' => 'user', 'error' => $this->l10n->t('No permission.') ]; |
130
|
|
|
} elseif (($e instanceof WebsiteInvalidDataException) && $e->getField()) { |
131
|
|
|
$error += [ 'errorField' => $e->getField(), 'error' => $e->getMessage() ]; |
132
|
|
|
} elseif ($e instanceof ThemeNotFoundException) { |
133
|
|
|
$error += [ 'errorField' => 'theme', 'error' => $this->l10n->t('Theme not found.') ]; |
134
|
|
|
} elseif ($e instanceof ThemeNotCompatibleException) { |
135
|
|
|
$error += [ 'errorField' => 'theme', 'error' => $this->l10n->t($e->getReason()) ]; |
136
|
|
|
} elseif ($e instanceof TemplateNotFoundException) { |
137
|
|
|
$error += [ 'errorField' => 'template', 'error' => $this->l10n->t('Template not found.') ]; |
138
|
|
|
} elseif ($e instanceof TemplateNotCompatibleException) { |
139
|
|
|
$error += [ 'errorField' => 'template', 'error' => $this->l10n->t($e->getReason()) ]; |
140
|
|
|
} |
141
|
|
|
|
142
|
1 |
|
return $this->createErrorResponse($e, $error); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @NoAdminRequired |
148
|
|
|
* |
149
|
|
|
* @param int $siteId |
150
|
|
|
* @param mixed[] $data |
151
|
|
|
* |
152
|
|
|
* @return DataResponse |
153
|
|
|
*/ |
154
|
1 |
|
public function updatePersonalWebsite(int $siteId, array $data): DataResponse |
155
|
|
|
{ |
156
|
|
|
try { |
157
|
1 |
|
$website = $this->websitesService->getWebsiteFromId($siteId); |
158
|
|
|
|
159
|
1 |
|
$userId = $this->userSession->getUser()->getUID(); |
160
|
1 |
|
$website->assertOwnedBy($userId); |
161
|
|
|
|
162
|
1 |
|
foreach ($data as $key => $value) { |
163
|
1 |
|
switch ($key) { |
164
|
1 |
|
case 'name': |
165
|
|
|
$website->setName($value); |
166
|
|
|
break; |
167
|
|
|
|
168
|
1 |
|
case 'type': |
169
|
1 |
|
$website->setType((int) $value); |
170
|
1 |
|
break; |
171
|
|
|
|
172
|
|
|
case 'theme': |
173
|
|
|
$website->setTheme($value); |
174
|
|
|
break; |
175
|
|
|
|
176
|
|
|
case 'options': |
177
|
|
|
foreach ($value as $optionKey => $optionValue) { |
178
|
|
|
switch ($optionKey) { |
179
|
|
|
case 'group_access': |
180
|
|
|
$groupAccess = $optionValue ? explode('|', $optionValue) : []; |
181
|
|
|
$website->setGroupAccess($groupAccess); |
182
|
|
|
break; |
183
|
|
|
|
184
|
|
|
default: |
185
|
|
|
throw new WebsiteInvalidDataException(); |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
break; |
189
|
|
|
|
190
|
|
|
default: |
191
|
|
|
throw new WebsiteInvalidDataException(); |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
195
|
1 |
|
$this->websitesService->updateWebsite($website); |
196
|
|
|
|
197
|
1 |
|
return $this->getPersonalWebsites(); |
198
|
|
|
} catch (\Throwable $e) { |
199
|
|
|
$error = []; |
200
|
|
|
if (($e instanceof WebsiteNotFoundException) || ($e instanceof WebsiteForeignOwnerException)) { |
201
|
|
|
$error += [ 'errorField' => 'identifier', 'error' => $this->l10n->t('Website not found.') ]; |
202
|
|
|
} elseif ($e instanceof WebsiteInvalidDataException) { |
203
|
|
|
$error += [ 'errorField' => $e->getField(), 'error' => $e->getMessage() ]; |
204
|
|
|
} elseif ($e instanceof ThemeNotFoundException) { |
205
|
|
|
$error += [ 'errorField' => 'theme', 'error' => $this->l10n->t('Theme not found.') ]; |
206
|
|
|
} elseif ($e instanceof ThemeNotCompatibleException) { |
207
|
|
|
$error += [ 'errorField' => 'theme', 'error' => $this->l10n->t($e->getReason()) ]; |
208
|
|
|
} elseif ($e instanceof TemplateNotFoundException) { |
209
|
|
|
$error += [ 'errorField' => 'template', 'error' => $this->l10n->t('Template not found.') ]; |
210
|
|
|
} elseif ($e instanceof TemplateNotCompatibleException) { |
211
|
|
|
$error += [ 'errorField' => 'template', 'error' => $this->l10n->t($e->getReason()) ]; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
return $this->createErrorResponse($e, $error); |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @NoAdminRequired |
220
|
|
|
* |
221
|
|
|
* @param int $siteId |
222
|
|
|
* |
223
|
|
|
* @return DataResponse |
224
|
|
|
*/ |
225
|
1 |
|
public function removePersonalWebsite(int $siteId): DataResponse |
226
|
|
|
{ |
227
|
|
|
try { |
228
|
1 |
|
$website = $this->websitesService->getWebsiteFromId($siteId); |
229
|
|
|
|
230
|
1 |
|
$userId = $this->userSession->getUser()->getUID(); |
231
|
1 |
|
$website->assertOwnedBy($userId); |
232
|
|
|
|
233
|
1 |
|
$this->websitesService->deleteWebsite($website); |
234
|
|
|
|
235
|
1 |
|
return $this->getPersonalWebsites(); |
236
|
|
|
} catch (WebsiteNotFoundException | WebsiteForeignOwnerException $e) { |
237
|
|
|
return $this->createErrorResponse($e, [ 'error' => $this->l10n->t('Website not found.') ]); |
238
|
|
|
} catch (\Throwable $e) { |
239
|
|
|
return $this->createErrorResponse($e); |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
|