From 0fe295f11eabf04f8f043067d0054b3c6ad49101 Mon Sep 17 00:00:00 2001 From: Roeland Jago Douma Date: Sun, 29 Mar 2020 11:59:19 +0200 Subject: [PATCH] Only show your own forms Signed-off-by: Roeland Jago Douma --- appinfo/routes.php | 2 +- lib/Controller/ApiController.php | 51 +++------- lib/Db/FormMapper.php | 33 ++++-- src/Forms.vue | 2 +- src/services/FormsService.js | 53 ---------- src/views/List.vue | 167 ------------------------------- 6 files changed, 40 insertions(+), 268 deletions(-) delete mode 100644 src/services/FormsService.js delete mode 100644 src/views/List.vue diff --git a/appinfo/routes.php b/appinfo/routes.php index fae44066..b87169c7 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -42,8 +42,8 @@ return [ ['name' => 'api#get_options', 'url' => '/get/options/{formId}', 'verb' => 'GET'], ['name' => 'api#get_shares', 'url' => '/get/shares/{formId}', 'verb' => 'GET'], ['name' => 'api#get_form', 'url' => '/get/form/{formId}', 'verb' => 'GET'], - ['name' => 'api#get_forms', 'url' => '/get/forms', 'verb' => 'GET'], + ['name' => 'api#getForms', 'url' => 'api/v1/forms', 'verb' => 'GET'], ['name' => 'api#newForm', 'url' => 'api/v1/form', 'verb' => 'POST'], ['name' => 'api#deleteForm', 'url' => 'api/v1/form/{id}', 'verb' => 'DELETE'], ['name' => 'api#updateQuestion', 'url' => 'api/v1/question/update/', 'verb' => 'POST'], diff --git a/lib/Controller/ApiController.php b/lib/Controller/ApiController.php index 60b6170b..315a7175 100644 --- a/lib/Controller/ApiController.php +++ b/lib/Controller/ApiController.php @@ -28,6 +28,7 @@ namespace OCA\Forms\Controller; +use OCA\Forms\AppInfo\Application; use OCP\AppFramework\Controller; use OCP\AppFramework\Http; use OCP\AppFramework\Http\DataResponse; @@ -53,8 +54,6 @@ use OCA\Forms\Db\QuestionMapper; use OCA\Forms\Db\Option; use OCA\Forms\Db\OptionMapper; -use OCP\Util; - class ApiController extends Controller { private $groupManager; @@ -71,21 +70,7 @@ class ApiController extends Controller { /** @var string */ private $userId; - /** - * PageController constructor. - * @param string $appName - * @param IGroupManager $groupManager - * @param IRequest $request - * @param IUserManager $userManager - * @param string $userId - * @param FormMapper $formMapper - * @param SubmissionMapper $submissionMapper - * @param AnswerMapper $answerMapper - * @param QuestionMapper $questionMapper - * @param OptionMapper $optionMapper - */ public function __construct( - $appName, IGroupManager $groupManager, IRequest $request, IUserManager $userManager, @@ -97,7 +82,7 @@ class ApiController extends Controller { OptionMapper $optionMapper, ILogger $logger ) { - parent::__construct($appName, $request); + parent::__construct(Application::APP_ID, $request); $this->userId = $userId; $this->groupManager = $groupManager; $this->userManager = $userManager; @@ -339,31 +324,23 @@ class ApiController extends Controller { } /** - * Get all forms * @NoAdminRequired - * @return DataResponse */ - public function getForms() { - if (!\OC::$server->getUserSession()->getUser() instanceof IUser) { - return new DataResponse(null, Http::STATUS_UNAUTHORIZED); + $forms = $this->formMapper->findAllByOwnerId($this->userId); + + $result = []; + foreach ($forms as $form) { + $result[] = [ + 'id' => $form->getId(), + 'form' => $form->read(), + 'mode' => 'edit', + 'shares' => $this->getShares($form->getId()), + 'questions' => $this->getQuestions($form->getId()) + ]; } - try { - $forms = $this->formMapper->findAll(); - } catch (DoesNotExistException $e) { - return new DataResponse($e, Http::STATUS_NOT_FOUND); - } - - $formsList = array(); - foreach ($forms as $formElement) { - $form = $this->getFullForm($formElement->id); - //if ($form['grantedAs'] !== 'none') { - $formsList[] = $form; - //} - } - - return new DataResponse($formsList, Http::STATUS_OK); + return new DataResponse($result); } /** diff --git a/lib/Db/FormMapper.php b/lib/Db/FormMapper.php index 0324952f..d1c96d4d 100644 --- a/lib/Db/FormMapper.php +++ b/lib/Db/FormMapper.php @@ -4,7 +4,7 @@ * * @author Vinzenz Rosenkranz * @author René Gieling -* + * * @license GNU AGPL version 3 or any later version * * This program is free software: you can redistribute it and/or modify @@ -32,6 +32,7 @@ class FormMapper extends QBMapper { /** * FormMapper constructor. + * * @param IDBConnection $db */ public function __construct(IDBConnection $db) { @@ -40,15 +41,15 @@ class FormMapper extends QBMapper { /** * @param Integer $id - * @throws \OCP\AppFramework\Db\DoesNotExistException if not found - * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException if more than one result * @return Form + * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException if more than one result + * @throws \OCP\AppFramework\Db\DoesNotExistException if not found */ public function find(int $id): Form { $qb = $this->db->getQueryBuilder(); $qb->select('*') - ->from($this->tableName) + ->from($this->getTableName()) ->where( $qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT)) ); @@ -58,15 +59,15 @@ class FormMapper extends QBMapper { /** * @param String $hash - * @throws \OCP\AppFramework\Db\DoesNotExistException if not found - * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException if more than one result * @return Form + * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException if more than one result + * @throws \OCP\AppFramework\Db\DoesNotExistException if not found */ public function findByHash(string $hash): Form { $qb = $this->db->getQueryBuilder(); $qb->select('*') - ->from($this->tableName) + ->from($this->getTableName()) ->where( $qb->expr()->eq('hash', $qb->createNamedParameter($hash, IQueryBuilder::PARAM_STR)) ); @@ -75,14 +76,28 @@ class FormMapper extends QBMapper { } /** - * @throws \OCP\AppFramework\Db\DoesNotExistException if not found * @return Form[] */ public function findAll(): array { $qb = $this->db->getQueryBuilder(); $qb->select('*') - ->from($this->tableName); + ->from($this->getTableName()); + + return $this->findEntities($qb); + } + + /** + * @return Form[] + */ + public function findAllByOwnerId(string $ownerId): array { + $qb = $this->db->getQueryBuilder(); + + $qb->select('*') + ->from($this->getTableName()) + ->where( + $qb->expr()->eq('owner_id', $qb->createNamedParameter($ownerId)) + ); return $this->findEntities($qb); } diff --git a/src/Forms.vue b/src/Forms.vue index 0048f8be..e77c0251 100644 --- a/src/Forms.vue +++ b/src/Forms.vue @@ -122,7 +122,7 @@ export default { async loadForms() { this.loading = true try { - const response = await axios.get(generateUrl('apps/forms/get/forms')) + const response = await axios.get(generateUrl('apps/forms/api/v1/forms')) this.forms = response.data } catch (error) { showError(t('forms', 'An error occurred while loading the forms list')) diff --git a/src/services/FormsService.js b/src/services/FormsService.js deleted file mode 100644 index 95a8bd46..00000000 --- a/src/services/FormsService.js +++ /dev/null @@ -1,53 +0,0 @@ -/** - * @copyright Copyright (c) 2019 John Molakvoæ - * - * @author John Molakvoæ - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -import axios from '@nextcloud/axios' - -/** - * Get the forms list - * - * @returns {Array} - */ -const getForms = async function() { - try { - const response = await axios.get(OC.generateUrl('apps/forms/get/forms')) - return response.data - } catch (error) { - console.error(error) - throw Error(t('forms', 'Unable to fetch the forms list')) - } -} - -/** - * Delete a form - * - * @param {int} id the form id to delete - */ -const deleteForm = async function(id) { - try { - axios.delete(OC.generateUrl('apps/forms/forms/{id}', { id })) - } catch (error) { - console.error(error) - throw Error(t('forms', 'Unable to delete the form')) - } -} - -export { deleteForm, getForms } diff --git a/src/views/List.vue b/src/views/List.vue deleted file mode 100644 index 63f3db78..00000000 --- a/src/views/List.vue +++ /dev/null @@ -1,167 +0,0 @@ - - - - - - -