From 7aefa91870355c558c1bd649a8ebcf540d07ee99 Mon Sep 17 00:00:00 2001 From: Philipp Hempel Date: Fri, 28 Jul 2023 13:27:53 +0200 Subject: [PATCH] Updated API Signed-off-by: Philipp Hempel --- appinfo/routes.php | 33 ++-- lib/Api/V1Api.php | 5 +- lib/Controller/Api1Controller.php | 218 ++++++++++----------- lib/Controller/ColumnController.php | 11 +- lib/Controller/Errors.php | 7 - lib/Controller/ImportController.php | 14 +- lib/Controller/RowController.php | 32 +-- lib/Controller/ShareController.php | 8 +- lib/Controller/TableController.php | 17 +- lib/Controller/TableTemplateController.php | 8 +- lib/Controller/ViewController.php | 8 +- lib/Service/ColumnService.php | 22 +-- lib/Service/ImportService.php | 13 +- lib/Service/PermissionsService.php | 11 +- lib/Service/RowService.php | 53 ++--- lib/Service/ShareService.php | 4 +- lib/Service/TableService.php | 39 ---- lib/Service/TableTemplateService.php | 2 +- lib/Service/ViewService.php | 5 + src/modules/main/modals/CreateColumn.vue | 1 - src/modules/main/modals/CreateRow.vue | 2 +- src/modules/navigation/modals/Import.vue | 2 +- src/modules/sidebar/partials/ShareList.vue | 2 +- src/store/data.js | 4 +- 24 files changed, 229 insertions(+), 292 deletions(-) diff --git a/appinfo/routes.php b/appinfo/routes.php index 1ba86b0a0..dba66b010 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -15,47 +15,43 @@ // -> tables ['name' => 'api1#index', 'url' => '/api/1/tables', 'verb' => 'GET'], ['name' => 'api1#createTable', 'url' => '/api/1/tables', 'verb' => 'POST'], - ['name' => 'api1#updateTable', 'url' => '/api/1/tables/{tableId}', 'verb' => 'PUT'], ['name' => 'api1#getTable', 'url' => '/api/1/tables/{tableId}', 'verb' => 'GET'], ['name' => 'api1#deleteTable', 'url' => '/api/1/tables/{tableId}', 'verb' => 'DELETE'], // -> views ['name' => 'api1#indexViews', 'url' => '/api/1/tables/{tableId}/views', 'verb' => 'GET'], ['name' => 'api1#createView', 'url' => '/api/1/tables/{tableId}/views', 'verb' => 'POST'], ['name' => 'api1#getView', 'url' => '/api/1/views/{viewId}', 'verb' => 'GET'], - ['name' => 'api1#deleteView', 'url' => '/api/1/views/{viewId}', 'verb' => 'DELETE'], ['name' => 'api1#updateView', 'url' => '/api/1/views/{viewId}', 'verb' => 'PUT'], + ['name' => 'api1#deleteView', 'url' => '/api/1/views/{viewId}', 'verb' => 'DELETE'], // -> share ['name' => 'api1#getShare', 'url' => '/api/1/shares/{shareId}', 'verb' => 'GET'], + ['name' => 'api1#indexViewShares', 'url' => '/api/1/views/{viewId}/shares', 'verb' => 'GET'], + ['name' => 'api1#indexTableManageShares', 'url' => '/api/1/tables/{tableId}/shares', 'verb' => 'GET'], + ['name' => 'api1#createShare', 'url' => '/api/1/shares', 'verb' => 'POST'], ['name' => 'api1#deleteShare', 'url' => '/api/1/shares/{shareId}', 'verb' => 'DELETE'], ['name' => 'api1#updateSharePermissions', 'url' => '/api/1/shares/{shareId}', 'verb' => 'PUT'], - // -> share -> table - ['name' => 'api1#indexTableShares', 'url' => '/api/1/tables/{tableId}/shares', 'verb' => 'GET'], - ['name' => 'api1#createTableShare', 'url' => '/api/1/tables/{tableId}/shares', 'verb' => 'POST'], // -> columns ['name' => 'api1#indexTableColumns', 'url' => '/api/1/tables/{tableId}/columns', 'verb' => 'GET'], - ['name' => 'api1#createTableColumn', 'url' => '/api/1/tables/{tableId}/columns', 'verb' => 'POST'], ['name' => 'api1#indexViewColumns', 'url' => '/api/1/views/{viewId}/columns', 'verb' => 'GET'], - ['name' => 'api1#getColumn', 'url' => '/api/1/columns/{columnId}', 'verb' => 'GET'], - ['name' => 'api1#deleteColumn', 'url' => '/api/1/columns/{columnId}', 'verb' => 'DELETE'], + ['name' => 'api1#createColumn', 'url' => '/api/1/views/{viewId}/columns', 'verb' => 'POST'], ['name' => 'api1#updateColumn', 'url' => '/api/1/columns/{columnId}', 'verb' => 'PUT'], - // -> rows -> table + ['name' => 'api1#getColumn', 'url' => '/api/1/columns/{columnId}', 'verb' => 'GET'], + ['name' => 'api1#deleteColumn', 'url' => '/api/1/columns/{columnId}', 'verb' => 'DELETE'], + // -> rows ['name' => 'api1#indexTableRowsSimple', 'url' => '/api/1/tables/{tableId}/rows/simple', 'verb' => 'GET'], - ['name' => 'api1#createRow', 'url' => '/api/1/tables/{tableId}/rows', 'verb' => 'POST'], - // -> rows -> view ['name' => 'api1#indexViewRows', 'url' => '/api/1/views/{viewId}/rows', 'verb' => 'GET'], - // -> row + ['name' => 'api1#createRow', 'url' => '/api/1/views/{viewId}/rows', 'verb' => 'POST'], ['name' => 'api1#getRow', 'url' => '/api/1/rows/{rowId}', 'verb' => 'GET'], - ['name' => 'api1#updateRow', 'url' => '/api/1/rows/{rowId}', 'verb' => 'PUT'], - ['name' => 'api1#deleteRow', 'url' => '/api/1/rows/{rowId}', 'verb' => 'DELETE'], + ['name' => 'api1#updateRow', 'url' => '/api/1/views/{viewId}/rows/{rowId}', 'verb' => 'PUT'], + ['name' => 'api1#deleteRow', 'url' => '/api/1/views/{viewId}/rows/{rowId}', 'verb' => 'DELETE'], // -> import - ['name' => 'api1#createImport', 'url' => '/api/1/import/table/{tableId}', 'verb' => 'POST'], + ['name' => 'api1#createImport', 'url' => '/api/1/import/views/{viewId}', 'verb' => 'POST'], // table ['name' => 'table#index', 'url' => '/table', 'verb' => 'GET'], ['name' => 'table#show', 'url' => '/table/{id}', 'verb' => 'GET'], ['name' => 'table#create', 'url' => '/table', 'verb' => 'POST'], - ['name' => 'table#update', 'url' => '/table/{id}', 'verb' => 'PUT'], ['name' => 'table#destroy', 'url' => '/table/{id}', 'verb' => 'DELETE'], // view @@ -77,8 +73,7 @@ // rows ['name' => 'row#show', 'url' => '/row/{id}', 'verb' => 'GET'], ['name' => 'row#indexView', 'url' => '/row/view/{viewId}', 'verb' => 'GET'], - ['name' => 'row#create', 'url' => '/row/column/{columnId}', 'verb' => 'POST'], - ['name' => 'row#createComplete', 'url' => '/row', 'verb' => 'POST'], + ['name' => 'row#create', 'url' => '/row', 'verb' => 'POST'], ['name' => 'row#update', 'url' => '/row/{id}/column/{columnId}', 'verb' => 'PUT'], ['name' => 'row#updateSet', 'url' => '/row/{id}', 'verb' => 'PUT'], ['name' => 'row#destroy', 'url' => '/view/{viewId}/row/{id}', 'verb' => 'DELETE'], @@ -92,6 +87,6 @@ ['name' => 'share#destroy', 'url' => '/share/{id}', 'verb' => 'DELETE'], // import - ['name' => 'import#import', 'url' => '/import/table/{tableId}', 'verb' => 'POST'], + ['name' => 'import#import', 'url' => '/import/view/{viewId}', 'verb' => 'POST'], ] ]; diff --git a/lib/Api/V1Api.php b/lib/Api/V1Api.php index 52b5f560b..2cea74b30 100644 --- a/lib/Api/V1Api.php +++ b/lib/Api/V1Api.php @@ -25,10 +25,11 @@ public function __construct(ColumnService $columnService, RowService $rowService * @throws InternalError * @throws PermissionError */ - public function getData(int $viewId, ?int $limit, ?int $offset): array { + public function getData(int $viewId, ?int $limit, ?int $offset, string $userId): + array { $columns = $this->columnService->findAllByView($viewId); - $rows = $this->rowService->findAllByView($viewId, $limit, $offset); + $rows = $this->rowService->findAllByView($viewId, $userId, $limit, $offset); $data = []; diff --git a/lib/Controller/Api1Controller.php b/lib/Controller/Api1Controller.php index ec9e636b1..921798f60 100644 --- a/lib/Controller/Api1Controller.php +++ b/lib/Controller/Api1Controller.php @@ -19,6 +19,7 @@ use OCP\AppFramework\Http\DataResponse; use OCP\DB\Exception; use OCP\IRequest; +use Psr\Log\LoggerInterface; class Api1Controller extends ApiController { private TableService $tableService; @@ -33,6 +34,8 @@ class Api1Controller extends ApiController { private string $userId; + protected LoggerInterface $logger; + use Errors; @@ -46,6 +49,7 @@ public function __construct( ViewService $viewService, ViewMapper $viewMapper, V1Api $v1Api, + LoggerInterface $logger, string $userId ) { parent::__construct(Application::APP_ID, $request); @@ -58,8 +62,11 @@ public function __construct( $this->viewMapper = $viewMapper; $this->userId = $userId; $this->v1Api = $v1Api; + $this->logger = $logger; } + // Tables + /** * @NoAdminRequired * @CORS @@ -71,17 +78,6 @@ public function index(): DataResponse { }); } - /** - * @NoAdminRequired - * @CORS - * @NoCSRFRequired - */ - public function createImport(int $tableId, string $path, bool $createMissingColumns = true): DataResponse { - return $this->handleError(function () use ($tableId, $path, $createMissingColumns) { - return $this->importService->import($tableId, $path, $createMissingColumns); - }); - } - /** * @NoAdminRequired * @CORS @@ -93,17 +89,6 @@ public function createTable(string $title, ?string $emoji, string $template = 'c }); } - /** - * @NoAdminRequired - * @CORS - * @NoCSRFRequired - */ - public function updateTable(int $tableId, ?string $title, ?string $emoji): DataResponse { - return $this->handleError(function () use ($tableId, $title, $emoji) { - return $this->tableService->update($tableId, $title, $emoji); - }); - } - /** * @NoAdminRequired * @CORS @@ -126,15 +111,24 @@ public function deleteTable(int $tableId): DataResponse { }); } + // Views + /** * @NoAdminRequired * @CORS * @NoCSRFRequired */ - public function getShare(int $shareId): DataResponse { - return $this->handleError(function () use ($shareId) { - return $this->shareService->find($shareId); - }); + public function indexViews(int $tableId, string $keyword = null, int $limit = 100, int $offset = 0): DataResponse { + if ($keyword) { + return $this->handleError(function () use ($keyword, $limit, $offset) { + return $this->viewService->search($keyword, $limit, $offset); + }); + } else { + return $this->handleError(function () use ($tableId) { + return $this->viewService->findAll($this->tableService->find($tableId)); + }); + } + } /** @@ -142,9 +136,9 @@ public function getShare(int $shareId): DataResponse { * @CORS * @NoCSRFRequired */ - public function indexTableShares(int $tableId): DataResponse { - return $this->handleError(function () use ($tableId) { - return $this->shareService->findAll('table', $tableId); + public function createView(int $tableId, string $title, ?string $emoji): DataResponse { + return $this->handleError(function () use ($tableId, $title, $emoji) { + return $this->viewService->create($title, $emoji, $this->tableService->find($tableId)); }); } @@ -153,9 +147,9 @@ public function indexTableShares(int $tableId): DataResponse { * @CORS * @NoCSRFRequired */ - public function createTableShare(int $tableId, string $receiver, string $receiverType, bool $permissionRead, bool $permissionCreate, bool $permissionUpdate, bool $permissionDelete, bool $permissionManage): DataResponse { - return $this->handleError(function () use ($tableId, $receiver, $receiverType, $permissionRead, $permissionCreate, $permissionUpdate, $permissionDelete, $permissionManage) { - return $this->shareService->create($tableId, 'table', $receiver, $receiverType, $permissionRead, $permissionCreate, $permissionUpdate, $permissionDelete, $permissionManage); + public function getView(int $viewId): DataResponse { + return $this->handleError(function () use ($viewId) { + return $this->viewService->find($viewId); }); } @@ -164,9 +158,9 @@ public function createTableShare(int $tableId, string $receiver, string $receive * @CORS * @NoCSRFRequired */ - public function deleteShare(int $shareId): DataResponse { - return $this->handleError(function () use ($shareId) { - return $this->shareService->delete($shareId); + public function updateView(int $viewId, array $data): DataResponse { + return $this->handleError(function () use ($viewId, $data) { + return $this->viewService->update($viewId, $data); }); } @@ -175,28 +169,23 @@ public function deleteShare(int $shareId): DataResponse { * @CORS * @NoCSRFRequired */ - public function updateSharePermissions(int $shareId, string $permissionType, bool $permissionValue): DataResponse { - return $this->handleError(function () use ($shareId, $permissionType, $permissionValue) { - return $this->shareService->updatePermission($shareId, $permissionType, $permissionValue); + public function deleteView(int $viewId): DataResponse { + return $this->handleError(function () use ($viewId) { + return $this->viewService->delete($viewId); }); } + // Shares + /** * @NoAdminRequired * @CORS * @NoCSRFRequired */ - public function indexViews(int $tableId, string $keyword = null, int $limit = 100, int $offset = 0): DataResponse { - if ($keyword) { - return $this->handleError(function () use ($keyword, $limit, $offset) { - return $this->viewService->search($keyword, $limit, $offset); - }); - } else { - return $this->handleError(function () use ($tableId) { - return $this->viewService->findAll($this->tableService->find($tableId)); - }); - } - + public function getShare(int $shareId): DataResponse { + return $this->handleError(function () use ($shareId) { + return $this->shareService->find($shareId); + }); } /** @@ -204,24 +193,21 @@ public function indexViews(int $tableId, string $keyword = null, int $limit = 10 * @CORS * @NoCSRFRequired */ - public function createView(int $tableId, string $title, ?string $emoji): DataResponse { - return $this->handleError(function () use ($tableId, $title, $emoji) { - return $this->viewService->create($title, $emoji, $this->tableService->find($tableId)); + public function indexViewShares(int $viewId): DataResponse { + return $this->handleError(function () use ($viewId) { + return $this->shareService->findAll('view', $viewId); }); } /** - * @param int $id - * @return Table - * @throws Exception - * @throws InternalError - * @throws NotFoundError - * @throws PermissionError + * @NoAdminRequired + * @CORS + * @NoCSRFRequired */ - private function getTableFromViewId(int $id): Table { - $view = $this->viewMapper->find($id); - return $this->tableService->find($view->getTableId()); - + public function indexTableManageShares(int $tableId): DataResponse { + return $this->handleError(function () use ($tableId) { + return $this->shareService->findAll('table', $tableId); + }); } /** @@ -229,9 +215,9 @@ private function getTableFromViewId(int $id): Table { * @CORS * @NoCSRFRequired */ - public function getView(int $viewId): DataResponse { - return $this->handleError(function () use ($viewId) { - return $this->viewService->find($viewId, $this->getTableFromViewId($viewId)); + public function createShare(int $nodeId, string $nodeType, string $receiver, string $receiverType, bool $permissionRead = false, bool $permissionCreate = false, bool $permissionUpdate = false, bool $permissionDelete = false, bool $permissionManage = false): DataResponse { + return $this->handleError(function () use ($nodeId, $nodeType, $receiver, $receiverType, $permissionRead, $permissionCreate, $permissionUpdate, $permissionDelete, $permissionManage) { + return $this->shareService->create($nodeId, $nodeType, $receiver, $receiverType, $permissionRead, $permissionCreate, $permissionUpdate, $permissionDelete, $permissionManage); }); } @@ -240,9 +226,9 @@ public function getView(int $viewId): DataResponse { * @CORS * @NoCSRFRequired */ - public function updateView(int $viewId, array $data): DataResponse { - return $this->handleError(function () use ($viewId, $data) { - return $this->viewService->update($viewId, $data, $this->getTableFromViewId($viewId)); + public function deleteShare(int $shareId): DataResponse { + return $this->handleError(function () use ($shareId) { + return $this->shareService->delete($shareId); }); } @@ -251,12 +237,14 @@ public function updateView(int $viewId, array $data): DataResponse { * @CORS * @NoCSRFRequired */ - public function deleteView(int $viewId): DataResponse { - return $this->handleError(function () use ($viewId) { - return $this->viewService->delete($viewId, $this->getTableFromViewId($viewId)); + public function updateSharePermissions(int $shareId, string $permissionType, bool $permissionValue): DataResponse { + return $this->handleError(function () use ($shareId, $permissionType, $permissionValue) { + return $this->shareService->updatePermission($shareId, $permissionType, $permissionValue); }); } + // Columns + /** * @NoAdminRequired * @CORS @@ -284,8 +272,7 @@ public function indexViewColumns(int $viewId): DataResponse { * @CORS * @NoCSRFRequired */ - public function createTableColumn( - int $tableId, + public function createColumn( int $viewId, string $title, string $type, @@ -308,10 +295,10 @@ public function createTableColumn( ?string $selectionOptions = '', ?string $selectionDefault = '', - ?string $datetimeDefault = '' + ?string $datetimeDefault = '', + ?array $selectedViewIds = [] ): DataResponse { return $this->handleError(function () use ( - $tableId, $viewId, $type, $subtype, @@ -334,11 +321,11 @@ public function createTableColumn( $selectionOptions, $selectionDefault, - $datetimeDefault + $datetimeDefault, + $selectedViewIds ) { return $this->columnService->create( $this->userId, - $tableId, $viewId, $type, $subtype, @@ -361,7 +348,8 @@ public function createTableColumn( $selectionOptions, $selectionDefault, - $datetimeDefault + $datetimeDefault, + $selectedViewIds ); }); } @@ -470,15 +458,6 @@ public function deleteColumn(int $columnId): DataResponse { }); } - - - - - - - - - /** * @NoAdminRequired * @CORS @@ -486,7 +465,7 @@ public function deleteColumn(int $columnId): DataResponse { */ public function indexTableRowsSimple(int $tableId, ?int $limit, ?int $offset): DataResponse { return $this->handleError(function () use ($tableId, $limit, $offset) { - return $this->v1Api->getData($tableId, $limit, $offset); + return $this->v1Api->getData($tableId, $limit, $offset, $this->userId,); }); } @@ -501,6 +480,24 @@ public function indexViewRows(int $viewId, ?int $limit, ?int $offset): DataRespo }); } + /** + * @NoAdminRequired + * @CORS + * @NoCSRFRequired + */ + public function createRow(int $viewId, array $data): DataResponse { + $dataNew = []; + foreach ($data as $key => $value) { + $dataNew[] = [ + 'columnId' => (int) $key, + 'value' => $value + ]; + } + + return $this->handleError(function () use ($dataNew, $viewId) { + return $this->rowService->create($viewId, $dataNew); + }); + } /** * @NoAdminRequired @@ -518,18 +515,16 @@ public function getRow(int $rowId): DataResponse { * @CORS * @NoCSRFRequired */ - public function createRow(int $tableId, int $viewId, string $data): DataResponse { + public function updateRow(int $rowId, int $viewId, array $data): DataResponse { $dataNew = []; - $array = json_decode($data, true); - foreach ($array as $key => $value) { + foreach ($data as $key => $value) { $dataNew[] = [ 'columnId' => (int) $key, 'value' => $value ]; } - - return $this->handleError(function () use ($dataNew, $viewId, $tableId) { - return $this->rowService->createComplete($viewId, $tableId, $dataNew); + return $this->handleError(function () use ($rowId, $viewId, $dataNew) { + return $this->rowService->updateSet($rowId, $viewId, $dataNew, $this->userId); }); } @@ -538,29 +533,34 @@ public function createRow(int $tableId, int $viewId, string $data): DataResponse * @CORS * @NoCSRFRequired */ - public function updateRow(int $rowId, int $viewId, string $data): DataResponse { - $dataNew = []; - $array = json_decode($data, true); - foreach ($array as $key => $value) { - $dataNew[] = [ - 'columnId' => (int) $key, - 'value' => $value - ]; - } - - return $this->handleError(function () use ($rowId, $viewId, $dataNew) { - return $this->rowService->updateSet($rowId, $viewId, $dataNew); + public function deleteRow(int $rowId, int $viewId): DataResponse { + return $this->handleError(function () use ($rowId, $viewId) { + return $this->rowService->delete($rowId, $viewId, $this->userId); + }); + } + + /** + * @NoAdminRequired + * @CORS + * @NoCSRFRequired + */ + public function createImport(int $viewId, string $path, bool $createMissingColumns = true): DataResponse { + return $this->handleError(function () use ($viewId, $path, $createMissingColumns) { + return $this->importService->import($viewId, $path, $createMissingColumns); }); } + // Api Function for backward compatibility + /** * @NoAdminRequired * @CORS * @NoCSRFRequired */ - public function deleteRow(int $rowId): DataResponse { - return $this->handleError(function () use ($rowId) { - return $this->rowService->delete($rowId); + public function updateTable(int $tableId, ?string $title, ?string $emoji): DataResponse { + return $this->handleError(function () use ($tableId, $title, $emoji) { + return $this->tableService->update($tableId, $title, $emoji); }); } + } diff --git a/lib/Controller/ColumnController.php b/lib/Controller/ColumnController.php index c68bd34c0..eb6326211 100644 --- a/lib/Controller/ColumnController.php +++ b/lib/Controller/ColumnController.php @@ -7,18 +7,24 @@ use OCP\AppFramework\Controller; use OCP\AppFramework\Http\DataResponse; use OCP\IRequest; +use Psr\Log\LoggerInterface; class ColumnController extends Controller { private ColumnService $service; private string $userId; + + protected LoggerInterface $logger; use Errors; - public function __construct(IRequest $request, + public function __construct( + IRequest $request, + LoggerInterface $logger, ColumnService $service, string $userId) { parent::__construct(Application::APP_ID, $request); + $this->logger = $logger; $this->service = $service; $this->userId = $userId; } @@ -54,7 +60,6 @@ public function show(int $id): DataResponse { * @NoAdminRequired */ public function create( - int $tableId, int $viewId, string $type, ?string $subtype, @@ -81,7 +86,6 @@ public function create( ?array $selectedViewIds ): DataResponse { return $this->handleError(function () use ( - $tableId, $viewId, $type, $subtype, @@ -108,7 +112,6 @@ public function create( $selectedViewIds) { return $this->service->create( $this->userId, - $tableId, $viewId, $type, $subtype, diff --git a/lib/Controller/Errors.php b/lib/Controller/Errors.php index 6b593bcc0..631ee9cbe 100644 --- a/lib/Controller/Errors.php +++ b/lib/Controller/Errors.php @@ -8,16 +8,9 @@ use OCA\Tables\Errors\PermissionError; use OCP\AppFramework\Http; use OCP\AppFramework\Http\DataResponse; -use Psr\Log\LoggerInterface; trait Errors { - private LoggerInterface $logger; - - public function __construct(LoggerInterface $logger) { - $this->logger = $logger; - } - protected function handleError(Closure $callback): DataResponse { try { return new DataResponse($callback()); diff --git a/lib/Controller/ImportController.php b/lib/Controller/ImportController.php index 9bae4e37b..85a8d1bf7 100644 --- a/lib/Controller/ImportController.php +++ b/lib/Controller/ImportController.php @@ -7,19 +7,25 @@ use OCP\AppFramework\Controller; use OCP\AppFramework\Http\DataResponse; use OCP\IRequest; +use Psr\Log\LoggerInterface; class ImportController extends Controller { private ImportService $service; private string $userId; + protected LoggerInterface $logger; + use Errors; - public function __construct(IRequest $request, + public function __construct( + IRequest $request, + LoggerInterface $logger, ImportService $service, string $userId) { parent::__construct(Application::APP_ID, $request); + $this->logger = $logger; $this->service = $service; $this->userId = $userId; } @@ -28,9 +34,9 @@ public function __construct(IRequest $request, /** * @NoAdminRequired */ - public function import(int $tableId, int $viewId, String $path, bool $createMissingColumns = true): DataResponse { - return $this->handleError(function () use ($tableId, $viewId, $path, $createMissingColumns) { - return $this->service->import($tableId, $viewId, $path, $createMissingColumns); + public function import(int $viewId, String $path, bool $createMissingColumns = true): DataResponse { + return $this->handleError(function () use ($viewId, $path, $createMissingColumns) { + return $this->service->import($viewId, $path, $createMissingColumns); }); } } diff --git a/lib/Controller/RowController.php b/lib/Controller/RowController.php index cea43abec..ee1b08606 100644 --- a/lib/Controller/RowController.php +++ b/lib/Controller/RowController.php @@ -7,6 +7,7 @@ use OCP\AppFramework\Controller; use OCP\AppFramework\Http\DataResponse; use OCP\IRequest; +use Psr\Log\LoggerInterface; class RowController extends Controller { /** @var RowService */ @@ -15,12 +16,17 @@ class RowController extends Controller { /** @var string */ private string $userId; + protected LoggerInterface $logger; + use Errors; - public function __construct(IRequest $request, + public function __construct( + IRequest $request, + LoggerInterface $logger, RowService $service, string $userId) { parent::__construct(Application::APP_ID, $request); + $this->logger = $logger; $this->service = $service; $this->userId = $userId; } @@ -47,32 +53,12 @@ public function show(int $id): DataResponse { * @NoAdminRequired */ public function create( - int $tableId, - int $viewId, - int $columnId, - string $data - ): DataResponse { - return $this->handleError(function () use ($tableId, $viewId, $columnId, $data) { - return $this->service->create( - $tableId, - $viewId, - $columnId, - $data); - }); - } - - /** - * @NoAdminRequired - */ - public function createComplete( - int $tableId, int $viewId, array $data ): DataResponse { - return $this->handleError(function () use ($tableId, $viewId, $data) { - return $this->service->createComplete( + return $this->handleError(function () use ($viewId, $data) { + return $this->service->create( $viewId, - $tableId, $data); }); } diff --git a/lib/Controller/ShareController.php b/lib/Controller/ShareController.php index 667137d5a..ed16c68f0 100644 --- a/lib/Controller/ShareController.php +++ b/lib/Controller/ShareController.php @@ -7,6 +7,7 @@ use OCP\AppFramework\Controller; use OCP\AppFramework\Http\DataResponse; use OCP\IRequest; +use Psr\Log\LoggerInterface; class ShareController extends Controller { /** @var ShareService */ @@ -15,13 +16,18 @@ class ShareController extends Controller { /** @var string */ private $userId; + protected LoggerInterface $logger; + use Errors; - public function __construct(IRequest $request, + public function __construct( + IRequest $request, + LoggerInterface $logger, ShareService $service, string $userId) { parent::__construct(Application::APP_ID, $request); + $this->logger = $logger; $this->service = $service; $this->userId = $userId; } diff --git a/lib/Controller/TableController.php b/lib/Controller/TableController.php index 4a0e6d78b..7d03ff4cc 100644 --- a/lib/Controller/TableController.php +++ b/lib/Controller/TableController.php @@ -7,19 +7,25 @@ use OCP\AppFramework\Controller; use OCP\AppFramework\Http\DataResponse; use OCP\IRequest; +use Psr\Log\LoggerInterface; class TableController extends Controller { private TableService $service; private string $userId; + protected LoggerInterface $logger; + use Errors; - public function __construct(IRequest $request, + public function __construct( + IRequest $request, + LoggerInterface $logger, TableService $service, string $userId) { parent::__construct(Application::APP_ID, $request); + $this->logger = $logger; $this->service = $service; $this->userId = $userId; } @@ -52,15 +58,6 @@ public function create(string $title, string $template, string $emoji): DataResp }); } - /** - * @NoAdminRequired - */ - public function update(int $id, string $title = null, string $emoji = null): DataResponse { - return $this->handleError(function () use ($id, $title, $emoji) { - return $this->service->update($id, $title, $emoji, $this->userId); - }); - } - /** * @NoAdminRequired */ diff --git a/lib/Controller/TableTemplateController.php b/lib/Controller/TableTemplateController.php index 941b9a107..56bcfe785 100644 --- a/lib/Controller/TableTemplateController.php +++ b/lib/Controller/TableTemplateController.php @@ -7,16 +7,22 @@ use OCP\AppFramework\Controller; use OCP\AppFramework\Http\DataResponse; use OCP\IRequest; +use Psr\Log\LoggerInterface; class TableTemplateController extends Controller { /** @var TableTemplateService */ private $service; + protected LoggerInterface $logger; + use Errors; - public function __construct(IRequest $request, + public function __construct( + IRequest $request, + LoggerInterface $logger, TableTemplateService $service) { parent::__construct(Application::APP_ID, $request); + $this->logger = $logger; $this->service = $service; } diff --git a/lib/Controller/ViewController.php b/lib/Controller/ViewController.php index 7b503e6b4..c60d7f62e 100644 --- a/lib/Controller/ViewController.php +++ b/lib/Controller/ViewController.php @@ -13,6 +13,7 @@ use OCP\AppFramework\Controller; use OCP\AppFramework\Http\DataResponse; use OCP\IRequest; +use Psr\Log\LoggerInterface; class ViewController extends Controller { private ViewService $service; @@ -23,15 +24,20 @@ class ViewController extends Controller { private string $userId; + protected LoggerInterface $logger; + use Errors; - public function __construct(IRequest $request, + 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; diff --git a/lib/Service/ColumnService.php b/lib/Service/ColumnService.php index 5e43ce4a8..eba1ee0ea 100644 --- a/lib/Service/ColumnService.php +++ b/lib/Service/ColumnService.php @@ -129,7 +129,7 @@ public function find(int $id, string $userId = null): Column { * @noinspection DuplicatedCode * * @param string|null $userId - * @param int $tableId + * @param int $viewId * @param string $type * @param string|null $subtype * @param string $title @@ -156,7 +156,6 @@ public function find(int $id, string $userId = null): Column { */ public function create( ?string $userId, - int $tableId, int $viewId, string $type, ?string $subtype, @@ -183,15 +182,16 @@ public function create( ?array $selectedViewIds ):Column { // security - $table = $this->tableMapper->find($tableId); + $view = $this->viewService->find($viewId); + $table = $this->tableMapper->find($view->getTableId()); if (!$this->permissionsService->canCreateColumns($table)) { - throw new PermissionError('create column at the table id = '.$tableId.' is not allowed.'); + throw new PermissionError('create column at the table id = '.$table->getId().' is not allowed.'); } $time = new DateTime(); $item = new Column(); $item->setTitle($title); - $item->setTableId($tableId); + $item->setTableId($table->getId()); $item->setType($type); $item->setSubtype($subtype !== null ? $subtype: ''); $item->setMandatory($mandatory); @@ -216,17 +216,15 @@ public function create( try { $entity = $this->mapper->insert($item); // Add columns to view(s) - $baseView = $this->viewService->findBaseView($table, true); - $this->viewService->update($baseView->getId(), ['columns' => json_encode(array_merge($baseView->getColumnsArray(), [$entity->getId()]))], $userId, true); - if($viewId != $baseView->getId()) { - $view = $this->viewService->find($viewId); - $this->viewService->update($viewId, ['columns' => json_encode(array_merge($view->getColumnsArray(), [$entity->getId()]))], $userId, true); + $this->viewService->update($view->getId(), ['columns' => json_encode(array_merge($view->getColumnsArray(), [$entity->getId()]))], $userId, true); + if (!$view->getIsBaseView()){ + $baseView = $this->viewService->findBaseView($table, true); + $this->viewService->update($baseView->getId(), ['columns' => json_encode(array_merge($baseView->getColumnsArray(), [$entity->getId()]))], $userId, true); } foreach ($selectedViewIds as $viewId) { $view = $this->viewService->find($viewId); $this->viewService->update($viewId, ['columns' => json_encode(array_merge($view->getColumnsArray(), [$entity->getId()]))], $userId, true); } - return $entity; } catch (\OCP\DB\Exception $e) { $this->logger->error($e->getMessage()); @@ -425,7 +423,7 @@ public function findOrCreateColumnsByTitleForTableAsArray(int $tableId, int $vie // if column was not found if($result[$i] === '' && $createUnknownColumns) { $description = $this->l->t('This column was automatically created by the import service.'); - $result[$i] = $this->create($userId, $tableId, $viewId, 'text', 'line', $title, false, $description, null, null, null, null, null, null, null, null, null, null, null, null, null, []); + $result[$i] = $this->create($userId, $viewId, 'text', 'line', $title, false, $description, null, null, null, null, null, null, null, null, null, null, null, null, null, []); $countCreatedColumns++; } } diff --git a/lib/Service/ImportService.php b/lib/Service/ImportService.php index a14bc9b47..75756deb5 100644 --- a/lib/Service/ImportService.php +++ b/lib/Service/ImportService.php @@ -27,6 +27,7 @@ class ImportService extends SuperService { private IRootFolder $rootFolder; private ColumnService $columnService; private RowService $rowService; + private ViewService $viewService; private IUserManager $userManager; private int $tableId = -1; @@ -38,11 +39,12 @@ class ImportService extends SuperService { private int $countErrors = 0; public function __construct(PermissionsService $permissionsService, LoggerInterface $logger, ?string $userId, - IRootFolder $rootFolder, ColumnService $columnService, RowService $rowService, IUserManager $userManager) { + IRootFolder $rootFolder, ColumnService $columnService, RowService $rowService, ViewService $viewService, IUserManager $userManager) { parent::__construct($logger, $userId, $permissionsService); $this->rootFolder = $rootFolder; $this->columnService = $columnService; $this->rowService = $rowService; + $this->viewService = $viewService; $this->userManager = $userManager; } @@ -54,8 +56,9 @@ public function __construct(PermissionsService $permissionsService, LoggerInterf * @throws InternalError * @throws NotFoundError */ - public function import(int $tableId, int $viewId, string $path, bool $createMissingColumns = true): array { - if (!$this->permissionsService->canCreateRowsByViewId($viewId)) { + public function import(int $viewId, string $path, bool $createMissingColumns = true): array { + $view = $this->viewService->find($viewId); + if (!$this->permissionsService->canCreateRows($view)) { throw new PermissionError('create row at the view id = '.$viewId.' is not allowed.'); } if ($this->userManager->get($this->userId) === null) { @@ -64,7 +67,7 @@ public function import(int $tableId, int $viewId, string $path, bool $createMiss throw new InternalError($error); } - $this->tableId = $tableId; + $this->tableId = $view->getTableId(); $this->viewId = $viewId; $this->createUnknownColumns = $createMissingColumns; @@ -162,7 +165,7 @@ private function createRow(Row $row): void { ]; } try { - $this->rowService->createComplete($this->viewId, $this->tableId, $data); + $this->rowService->create($this->viewId, $data); $this->countInsertedRows++; } catch (PermissionError $e) { $this->logger->error('Could not create row while importing, no permission.', ['exception' => $e]); diff --git a/lib/Service/PermissionsService.php b/lib/Service/PermissionsService.php index daf5fb280..72d430f3b 100644 --- a/lib/Service/PermissionsService.php +++ b/lib/Service/PermissionsService.php @@ -162,8 +162,8 @@ public function canReadRowsByElement($element, string $nodeType, ?string $userId * @param string|null $userId * @return bool */ - public function canCreateRowsByViewId(int $viewId, ?string $userId = null): bool { - return $this->checkPermissionById($viewId, 'view', 'create', $userId); + public function canCreateRows($view, ?string $userId = null): bool { + return $this->checkPermission($view, 'view', 'create', $userId); } /** @@ -191,7 +191,7 @@ public function canUpdateRowsByTableId(int $tableId, ?string $userId = null): bo * @return bool */ public function canDeleteRowsByViewId(int $viewId, ?string $userId = null): bool { - return $this->checkPermissionById($tableId, 'table', 'manage', $userId); + return $this->checkPermissionById($viewId, 'view', 'delete', $userId); } /** @@ -199,8 +199,9 @@ public function canDeleteRowsByViewId(int $viewId, ?string $userId = null): bool * @param string|null $userId * @return bool */ - public function canDeleteRowsByTableId(int $viewId, ?string $userId = null): bool { - return $this->checkPermissionById($viewId, 'view', 'delete', $userId); + public function canDeleteRowsByTableId(int $tableId, ?string $userId = null): bool { + return $this->checkPermissionById($tableId, 'table', 'manage', $userId); + } diff --git a/lib/Service/RowService.php b/lib/Service/RowService.php index 6b1426dc3..ab5ab1b5c 100644 --- a/lib/Service/RowService.php +++ b/lib/Service/RowService.php @@ -77,8 +77,7 @@ public function find(int $id): Row { /** * @param int $tableId - * @param int $columnId - * @param string $data + * @param array $data * @return Row * @throws PermissionError * @throws \OCP\DB\Exception @@ -86,55 +85,28 @@ public function find(int $id): Row { * @noinspection DuplicatedCode */ public function create( - int $tableId, int $viewId, - int $columnId, - string $data + array $data ):Row { + + $view = $this->viewMapper->find($viewId); // security - if (!$this->permissionsService->canCreateRowsByViewId($viewId)) { + if (!$this->permissionsService->canCreateRows($view)) { throw new PermissionError('create row at the view id = '.$viewId.' is not allowed.'); } - $time = new DateTime(); - $item = new Row(); - $d = []; - $d[] = [ - "columnId" => $columnId, - "value" => $data - ]; - $item->setDataArray($d); - $item->setTableId($tableId); - $item->setCreatedBy($this->userId); - $item->setCreatedAt($time->format('Y-m-d H:i:s')); - $item->setLastEditBy($this->userId); - $item->setLastEditAt($time->format('Y-m-d H:i:s')); - return $this->mapper->insert($item); - } + $viewColumns = $view->getColumnsArray(); - /** - * @param int $tableId - * @param array $data - * @return Row - * @throws PermissionError - * @throws \OCP\DB\Exception - * @noinspection PhpUndefinedMethodInspection - * @noinspection DuplicatedCode - */ - public function createComplete( - int $viewId, - int $tableId, - array $data - ):Row { - // security - if (!$this->permissionsService->canCreateRowsByViewId($viewId)) { - throw new PermissionError('create row at the view id = '.$viewId.' is not allowed.'); - } + foreach ($data as $entry) { + if (!in_array($entry['columnId'], $viewColumns)) { + throw new InternalError('Column with id '.$entry['columnId'].' is not part of view with id '.$view->getId()); + } + } $time = new DateTime(); $item = new Row(); $item->setDataArray($data); - $item->setTableId($tableId); + $item->setTableId($view->getTableId()); $item->setCreatedBy($this->userId); $item->setCreatedAt($time->format('Y-m-d H:i:s')); $item->setLastEditBy($this->userId); @@ -233,7 +205,6 @@ public function updateSet( foreach ($data as $dataObject) { $d = $this->replaceOrAddData($d, $dataObject); } - $item->setDataArray($d); $item->setLastEditBy($this->userId); $item->setLastEditAt($time->format('Y-m-d H:i:s')); diff --git a/lib/Service/ShareService.php b/lib/Service/ShareService.php index b9fda7de4..a2e214b3a 100644 --- a/lib/Service/ShareService.php +++ b/lib/Service/ShareService.php @@ -48,12 +48,12 @@ public function __construct(PermissionsService $permissionsService, LoggerInterf * * @psalm-param 'table' $nodeType */ - public function findAll(string $nodeType, int $tableId, ?string $userId = null): array { + public function findAll(string $nodeType, int $nodeId, ?string $userId = null): array { $userId = $this->permissionsService->preCheckUserId($userId); try { /** @var string $userId */ - $shares = $this->mapper->findAllSharesForNode($nodeType, $tableId, $userId); + $shares = $this->mapper->findAllSharesForNode($nodeType, $nodeId, $userId); return $this->addReceiverDisplayNames($shares); } catch (\OCP\DB\Exception $e) { $this->logger->error($e->getMessage()); diff --git a/lib/Service/TableService.php b/lib/Service/TableService.php index 0201b80a3..3660e32ae 100644 --- a/lib/Service/TableService.php +++ b/lib/Service/TableService.php @@ -250,45 +250,6 @@ public function create(string $title, string $template, ?string $emoji, ?string return $table; } - /** - * @noinspection PhpUndefinedMethodInspection - * - * @param int $id $userId - * @param string|null $title - * @param string|null $emoji - * @param string|null $userId - * @return Table - * @throws InternalError - */ - public function update(int $id, ?string $title, ?string $emoji, ?string $userId = null): Table { - $userId = $this->permissionsService->preCheckUserId($userId); - - try { - $table = $this->mapper->find($id); - - // security - if (!$this->permissionsService->canManageTable($table, $userId)) { - throw new PermissionError('PermissionError: can not update table with id '.$id); - } - - $time = new DateTime(); - if ($title !== null) { - $table->setTitle($title); - } - if ($emoji !== null) { - $table->setEmoji($emoji); - } - $table->setLastEditBy($userId); - $table->setLastEditAt($time->format('Y-m-d H:i:s')); - $table = $this->mapper->update($table); - $this->enhanceTable($table, $userId); - return $table; - } catch (Exception $e) { - $this->logger->error($e->getMessage(), ['exception' => $e]); - throw new InternalError($e->getMessage()); - } - } - /** * @throws InternalError */ diff --git a/lib/Service/TableTemplateService.php b/lib/Service/TableTemplateService.php index 3c0e28bf0..087bc61aa 100644 --- a/lib/Service/TableTemplateService.php +++ b/lib/Service/TableTemplateService.php @@ -850,7 +850,7 @@ private function createRow(Table $table, int $viewId, array $values): void { ]; } try { - $this->rowService->createComplete($viewId, $table->getId(), $data); + $this->rowService->create($viewId, $data); } catch (PermissionError $e) { $this->logger->warning('Cannot create row, permission denied: '.$e->getMessage()); } catch (Exception $e) { diff --git a/lib/Service/ViewService.php b/lib/Service/ViewService.php index 0cce57d1c..6496b3ef2 100644 --- a/lib/Service/ViewService.php +++ b/lib/Service/ViewService.php @@ -228,7 +228,12 @@ public function update(int $id, array $data, ?string $userId = null, bool $skipT throw new PermissionError('PermissionError: can not update view with id '.$id); } + $updatebleColumns = array('title', 'emoji', 'description', 'columns', 'sort', 'filter'); + foreach ($data as $key => $value) { + if (!in_array($key, $updatebleColumns)) { + throw new InternalError('Column '.$key.' can not be updated.'); + } $setterMethod = 'set'.ucfirst($key); $view->$setterMethod($value); } diff --git a/src/modules/main/modals/CreateColumn.vue b/src/modules/main/modals/CreateColumn.vue index 0fdf73691..438d67514 100644 --- a/src/modules/main/modals/CreateColumn.vue +++ b/src/modules/main/modals/CreateColumn.vue @@ -250,7 +250,6 @@ export default { description: this.column.description, selectedViewIds: this.column.selectedViews.map(view => view.id), mandatory: this.column.mandatory, - tableId: this.activeView.tableId, viewId: this.activeView.id, } if (this.combinedType === ColumnTypes.TextLine || this.combinedType === ColumnTypes.TextLong) { diff --git a/src/modules/main/modals/CreateRow.vue b/src/modules/main/modals/CreateRow.vue index 2f968b5d8..e56080509 100644 --- a/src/modules/main/modals/CreateRow.vue +++ b/src/modules/main/modals/CreateRow.vue @@ -115,7 +115,7 @@ export default { value, }) } - await this.$store.dispatch('insertNewRow', { tableId: this.activeView.tableId, viewId: this.activeView.id, data }) + await this.$store.dispatch('insertNewRow', { viewId: this.activeView.id, data }) } catch (e) { console.error(e) showError(t('tables', 'Could not create new row')) diff --git a/src/modules/navigation/modals/Import.vue b/src/modules/navigation/modals/Import.vue index 4b53275e0..18e2cb197 100644 --- a/src/modules/navigation/modals/Import.vue +++ b/src/modules/navigation/modals/Import.vue @@ -203,7 +203,7 @@ export default { async import() { this.loading = true try { - const res = await axios.post(generateUrl('/apps/tables/import/table/' + this.view.tableId), { viewId: this.view.id, path: this.path, createMissingColumns: this.getCreateMissingColumns }) + const res = await axios.post(generateUrl('/apps/tables/import/view/' + this.view.id), { path: this.path, createMissingColumns: this.getCreateMissingColumns }) if (res.status === 200) { this.result = res.data this.loading = false diff --git a/src/modules/sidebar/partials/ShareList.vue b/src/modules/sidebar/partials/ShareList.vue index ca387101f..87cc027cc 100644 --- a/src/modules/sidebar/partials/ShareList.vue +++ b/src/modules/sidebar/partials/ShareList.vue @@ -83,7 +83,7 @@ - {{ t('tables', 'Demote to table manager') }} + {{ t('tables', 'Demote table manager') }}