-
Notifications
You must be signed in to change notification settings - Fork 338
Expand file tree
/
Copy pathBoardController.php
More file actions
294 lines (262 loc) · 9.82 KB
/
Copy pathBoardController.php
File metadata and controls
294 lines (262 loc) · 9.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
<?php
/**
* SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Deck\Controller;
use OCA\Deck\Db\Acl;
use OCA\Deck\Db\Board;
use OCA\Deck\NoPermissionException;
use OCA\Deck\Service\BoardService;
use OCA\Deck\Service\CsvImportService;
use OCA\Deck\Service\ExternalBoardService;
use OCA\Deck\Service\Importer\BoardImportService;
use OCA\Deck\Service\PermissionService;
use OCP\AppFramework\ApiController;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
use OCP\AppFramework\Http\DataResponse;
use OCP\IL10N;
use OCP\IRequest;
class BoardController extends ApiController {
public function __construct(
$appName,
IRequest $request,
private BoardService $boardService,
private ExternalBoardService $externalBoardService,
private PermissionService $permissionService,
private BoardImportService $boardImportService,
private CsvImportService $csvImportService,
private IL10N $l10n,
private $userId,
) {
parent::__construct($appName, $request);
}
#[NoAdminRequired]
public function index() {
return $this->boardService->findAll();
}
#[NoAdminRequired]
public function read(int $boardId): Board {
return $this->boardService->find($boardId);
}
#[NoAdminRequired]
public function create(string $title, string $color): Board {
return $this->boardService->create($title, $this->userId, $color);
}
#[NoAdminRequired]
public function update(int $id, string $title, string $color, bool $archived): Board {
return $this->boardService->update($id, $title, $color, $archived);
}
#[NoAdminRequired]
public function delete(int $boardId): Board {
return $this->boardService->delete($boardId);
}
#[NoAdminRequired]
public function deleteUndo(int $boardId): Board {
return $this->boardService->deleteUndo($boardId);
}
#[NoAdminRequired]
public function leave(int $boardId) {
return $this->boardService->leave($boardId);
}
#[NoAdminRequired]
public function getUserPermissions(int $boardId): array {
$permissions = $this->permissionService->getPermissions($boardId);
return [
'PERMISSION_READ' => $permissions[Acl::PERMISSION_READ],
'PERMISSION_EDIT' => $permissions[Acl::PERMISSION_EDIT],
'PERMISSION_MANAGE' => $permissions[Acl::PERMISSION_MANAGE],
'PERMISSION_SHARE' => $permissions[Acl::PERMISSION_SHARE]
];
}
/**
* @param $participant
*/
#[NoAdminRequired]
public function addAcl(int $boardId, int $type, $participant, bool $permissionEdit, bool $permissionShare, bool $permissionManage, ?string $remote = null): Acl {
return $this->boardService->addAcl($boardId, $type, $participant, $permissionEdit, $permissionShare, $permissionManage);
}
/**
* @NoAdminRequired
* @param $id
* @param $permissionEdit
* @param $permissionShare
* @param $permissionManage
* @return \OCP\AppFramework\Db\Entity
*/
public function updateAcl($id, $permissionEdit, $permissionShare, $permissionManage) {
return $this->boardService->updateAcl($id, $permissionEdit, $permissionShare, $permissionManage);
}
/**
* @NoAdminRequired
* @param $aclId
* @return \OCP\AppFramework\Db\Entity|null
*/
public function deleteAcl($aclId) {
return $this->boardService->deleteAcl($aclId);
}
/**
* @NoAdminRequired
*/
public function clone(int $boardId, bool $withCards = false, bool $withAssignments = false, bool $withLabels = false, bool $withDueDate = false, bool $moveCardsToLeftStack = false, bool $restoreArchivedCards = false): DataResponse {
return new DataResponse(
$this->boardService->clone($boardId, $this->userId, $withCards, $withAssignments, $withLabels, $withDueDate, $moveCardsToLeftStack, $restoreArchivedCards)
);
}
/**
* @NoAdminRequired
*/
public function transferOwner(int $boardId, string $newOwner): DataResponse {
if ($this->permissionService->userIsBoardOwner($boardId, $this->userId)) {
return new DataResponse($this->boardService->transferBoardOwnership($boardId, $newOwner), HTTP::STATUS_OK);
}
return new DataResponse([], HTTP::STATUS_UNAUTHORIZED);
}
/**
* @NoAdminRequired
* @param $boardId
* @return Board
* @throws \OCP\AppFramework\Db\DoesNotExistException
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
*/
public function export($boardId) {
return $this->boardService->export($boardId);
}
/**
* @NoAdminRequired
*/
public function import(): DataResponse {
if (!$this->permissionService->canCreate()) {
throw new NoPermissionException('Creating boards has been disabled for your account.');
}
$file = $this->request->getUploadedFile('file');
$error = null;
$phpFileUploadErrors = [
UPLOAD_ERR_OK => $this->l10n->t('The file was uploaded'),
UPLOAD_ERR_INI_SIZE => $this->l10n->t('The uploaded file exceeds the upload_max_filesize directive in php.ini'),
UPLOAD_ERR_FORM_SIZE => $this->l10n->t('The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form'),
UPLOAD_ERR_PARTIAL => $this->l10n->t('The file was only partially uploaded'),
UPLOAD_ERR_NO_FILE => $this->l10n->t('No file was uploaded'),
UPLOAD_ERR_NO_TMP_DIR => $this->l10n->t('Missing a temporary folder'),
UPLOAD_ERR_CANT_WRITE => $this->l10n->t('Could not write file to disk'),
UPLOAD_ERR_EXTENSION => $this->l10n->t('A PHP extension stopped the file upload'),
];
if (empty($file)) {
$error = $this->l10n->t('No file uploaded or file size exceeds maximum of %s', [\OCP\Util::humanFileSize(\OCP\Util::uploadLimit())]);
}
if (!empty($file) && array_key_exists('error', $file) && $file['error'] !== UPLOAD_ERR_OK) {
$error = $phpFileUploadErrors[$file['error']];
}
$isCsv = $this->isCsvFile($file);
if (!empty($file) && $file['error'] === UPLOAD_ERR_OK && !$isCsv && !in_array($file['type'], ['application/json', 'text/plain'], true)) {
$error = $this->l10n->t('Invalid file type. Only JSON and CSV files are allowed.');
}
if ($error !== null) {
return new DataResponse([
'status' => 'error',
'message' => $error,
], Http::STATUS_BAD_REQUEST);
}
try {
$fileContent = file_get_contents($file['tmp_name']);
if ($isCsv) {
$boardTitle = pathinfo($file['name'] ?? 'Imported Board', PATHINFO_FILENAME) ?: 'Imported Board';
$this->boardImportService->setSystem('DeckCsv');
$config = new \stdClass();
$config->owner = $this->userId;
$config->boardTitle = $boardTitle;
$this->boardImportService->setConfigInstance($config);
$data = new \stdClass();
$data->rawCsvContent = $fileContent;
$data->title = $boardTitle;
$this->boardImportService->setData($data);
} else {
$this->boardImportService->setSystem('DeckJson');
$config = new \stdClass();
$config->owner = $this->userId;
$this->boardImportService->setConfigInstance($config);
$this->boardImportService->setData(json_decode($fileContent));
}
$importErrors = [];
$this->boardImportService->registerErrorCollector(function (string $message) use (&$importErrors) {
$importErrors[] = $message;
});
$this->boardImportService->import();
$importedBoard = $this->boardImportService->getBoard();
$board = $this->boardService->find($importedBoard->getId());
return new DataResponse([
'board' => $board,
'import' => [
'errors' => $importErrors,
],
], Http::STATUS_OK);
} catch (\TypeError $e) {
return new DataResponse([
'status' => 'error',
'message' => $this->l10n->t('Invalid import data'),
], Http::STATUS_BAD_REQUEST);
} catch (\Exception $e) {
return new DataResponse([
'status' => 'error',
'message' => $this->l10n->t('Failed to import board'),
], Http::STATUS_BAD_REQUEST);
}
}
/**
* @NoAdminRequired
*/
public function importCsv(int $boardId): DataResponse {
$file = $this->request->getUploadedFile('file');
$error = null;
$phpFileUploadErrors = [
UPLOAD_ERR_OK => $this->l10n->t('The file was uploaded'),
UPLOAD_ERR_INI_SIZE => $this->l10n->t('The uploaded file exceeds the upload_max_filesize directive in php.ini'),
UPLOAD_ERR_FORM_SIZE => $this->l10n->t('The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form'),
UPLOAD_ERR_PARTIAL => $this->l10n->t('The file was only partially uploaded'),
UPLOAD_ERR_NO_FILE => $this->l10n->t('No file was uploaded'),
UPLOAD_ERR_NO_TMP_DIR => $this->l10n->t('Missing a temporary folder'),
UPLOAD_ERR_CANT_WRITE => $this->l10n->t('Could not write file to disk'),
UPLOAD_ERR_EXTENSION => $this->l10n->t('A PHP extension stopped the file upload'),
];
if (empty($file)) {
$error = $this->l10n->t('No file uploaded or file size exceeds maximum of %s', [\OCP\Util::humanFileSize(\OCP\Util::uploadLimit())]);
}
if (!empty($file) && array_key_exists('error', $file) && $file['error'] !== UPLOAD_ERR_OK) {
$error = $phpFileUploadErrors[$file['error']];
}
if (!empty($file) && $file['error'] === UPLOAD_ERR_OK && !$this->isCsvFile($file)) {
$error = $this->l10n->t('Invalid file type. Only CSV files are allowed.');
}
if ($error !== null) {
return new DataResponse([
'status' => 'error',
'message' => $error,
], Http::STATUS_BAD_REQUEST);
}
try {
$fileContent = file_get_contents($file['tmp_name']);
$importResult = $this->csvImportService->importToBoard($boardId, $fileContent, $this->userId);
$board = $this->boardService->find($boardId);
return new DataResponse([
'board' => $board,
'import' => $importResult,
], Http::STATUS_OK);
} catch (\Exception $e) {
return new DataResponse([
'status' => 'error',
'message' => $this->l10n->t('Failed to import cards from CSV'),
], Http::STATUS_BAD_REQUEST);
}
}
private function isCsvFile(?array $file): bool {
if (empty($file)) {
return false;
}
if (in_array($file['type'] ?? '', ['text/csv', 'application/csv'], true)) {
return true;
}
$name = $file['name'] ?? '';
return str_ends_with(strtolower($name), '.csv');
}
}