-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathViewController.php
More file actions
126 lines (107 loc) · 3.92 KB
/
ViewController.php
File metadata and controls
126 lines (107 loc) · 3.92 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
<?php
/**
* SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Tables\Controller;
use OCA\Tables\AppInfo\Application;
use OCA\Tables\Db\Table;
use OCA\Tables\Db\ViewMapper;
use OCA\Tables\Errors\InternalError;
use OCA\Tables\Errors\NotFoundError;
use OCA\Tables\Errors\PermissionError;
use OCA\Tables\Middleware\Attribute\RequirePermission;
use OCA\Tables\Model\ViewUpdateInput;
use OCA\Tables\Service\TableService;
use OCA\Tables\Service\ViewService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
use OCP\AppFramework\Http\DataResponse;
use OCP\IRequest;
use Psr\Log\LoggerInterface;
class ViewController extends Controller {
private ViewService $service;
private ViewMapper $mapper;
private TableService $tableService;
private string $userId;
protected LoggerInterface $logger;
use Errors;
public function __construct(
IRequest $request,
LoggerInterface $logger,
ViewService $service,
ViewMapper $mapper,
TableService $tableService,
string $userId) {
parent::__construct(Application::APP_ID, $request);
$this->logger = $logger;
$this->service = $service;
$this->mapper = $mapper;
$this->tableService = $tableService;
$this->userId = $userId;
}
#[NoAdminRequired]
#[RequirePermission(permission: Application::PERMISSION_READ, type: Application::NODE_TYPE_TABLE, idParam: 'tableId')]
public function index(int $tableId): DataResponse {
return $this->handleError(function () use ($tableId) {
return $this->service->findAll($this->getTable($tableId), $this->userId);
});
}
#[NoAdminRequired]
public function indexSharedWithMe(): DataResponse {
return $this->handleError(function () {
return $this->service->findSharedViewsWithMe($this->userId);
});
}
#[NoAdminRequired]
#[RequirePermission(permission: Application::PERMISSION_READ, type: Application::NODE_TYPE_VIEW, idParam: 'id')]
public function show(int $id): DataResponse {
return $this->handleError(function () use ($id) {
return $this->service->find($id);
});
}
#[NoAdminRequired]
#[RequirePermission(permission: Application::PERMISSION_MANAGE, type: Application::NODE_TYPE_TABLE, idParam: 'tableId')]
public function create(int $tableId, string $title, ?string $emoji, ?string $layout = null): DataResponse {
return $this->handleError(function () use ($tableId, $title, $emoji, $layout) {
return $this->service->create($title, $emoji, $this->getTable($tableId, true), null, $layout);
});
}
#[NoAdminRequired]
#[RequirePermission(permission: Application::PERMISSION_MANAGE, type: Application::NODE_TYPE_VIEW, idParam: 'id')]
public function update(int $id, array $data): DataResponse {
return $this->handleError(function () use ($id, $data) {
$inputData = ViewUpdateInput::fromInputArray($data);
return $this->service->update($id, $inputData, $this->userId);
});
}
#[NoAdminRequired]
#[RequirePermission(permission: Application::PERMISSION_MANAGE, type: Application::NODE_TYPE_VIEW, idParam: 'id')]
public function destroy(int $id): DataResponse {
return $this->handleError(function () use ($id) {
return $this->service->delete($id);
});
}
/**
* @param int $tableId
* @param bool $skipTableEnhancement
* @return Table
* @throws InternalError
* @throws NotFoundError
* @throws PermissionError
*/
private function getTable(int $tableId, bool $skipTableEnhancement = false): Table {
try {
return $this->tableService->find($tableId, $skipTableEnhancement);
} catch (InternalError $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
throw new InternalError($e->getMessage());
} catch (NotFoundError $e) {
$this->logger->warning($e->getMessage(), ['exception' => $e]);
throw new NotFoundError($e->getMessage());
} catch (PermissionError $e) {
$this->logger->warning($e->getMessage(), ['exception' => $e]);
throw new PermissionError($e->getMessage());
}
}
}