Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the possibility to collaborate on forms #1417

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,14 @@
'apiVersion' => 'v2'
]
],
[
'name' => 'api#getCollaborationForms',
'url' => '/api/{apiVersion}/collaboration_forms',
'verb' => 'GET',
'requirements' => [
'apiVersion' => 'v2'
]
],

// Questions
[
Expand Down Expand Up @@ -209,6 +217,14 @@
'apiVersion' => 'v2'
]
],
[
'name' => 'shareApi#toggleEditor',
'url' => '/api/{apiVersion}/share/toggleEditor',
'verb' => 'POST',
'requirements' => [
'apiVersion' => 'v2'
]
],

// Submissions
[
Expand Down
34 changes: 28 additions & 6 deletions lib/Controller/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,28 @@ public function getSharedForms(): DataResponse {
return new DataResponse($result);
}

/**
* @NoAdminRequired
*
* Read List of forms shared with current user
* Return only with necessary information and editing enabled for Listing.
* @return DataResponse
*/
public function getCollaborationForms(): DataResponse {
$forms = $this->formMapper->findAll();

$result = [];
foreach ($forms as $form) {
// Check if the form should be shown on sidebar
if (!$this->formsService->isSharedCollaborationFormShown($form->getId())) {
continue;
}
$result[] = $this->formsService->getPartialFormArray($form->getId());
}

return new DataResponse($result);
}

/**
* @NoAdminRequired
*
Expand Down Expand Up @@ -363,7 +385,7 @@ public function updateForm(int $id, array $keyValuePairs): DataResponse {
throw new OCSBadRequestException();
}

if ($form->getOwnerId() !== $this->currentUser->getUID()) {
if (!$this->formsService->isAllowedToEdit($form->getId())) {
$this->logger->debug('This form is not owned by the current user');
throw new OCSForbiddenException();
}
Expand Down Expand Up @@ -460,7 +482,7 @@ public function newQuestion(int $formId, string $type, string $text = ''): DataR
throw new OCSBadRequestException();
}

if ($form->getOwnerId() !== $this->currentUser->getUID()) {
if (!$this->formsService->isAllowedToEdit($form->getId())) {
$this->logger->debug('This form is not owned by the current user');
throw new OCSForbiddenException();
}
Expand Down Expand Up @@ -610,7 +632,7 @@ public function updateQuestion(int $id, array $keyValuePairs): DataResponse {
throw new OCSBadRequestException('Could not find form or question');
}

if ($form->getOwnerId() !== $this->currentUser->getUID()) {
if (!$this->formsService->isAllowedToEdit($form->getId())) {
$this->logger->debug('This form is not owned by the current user');
throw new OCSForbiddenException();
}
Expand Down Expand Up @@ -716,7 +738,7 @@ public function newOption(int $questionId, string $text): DataResponse {
throw new OCSBadRequestException('Could not find form or question');
}

if ($form->getOwnerId() !== $this->currentUser->getUID()) {
if (!$this->formsService->isAllowedToEdit($form->getId())) {
$this->logger->debug('This form is not owned by the current user');
throw new OCSForbiddenException();
}
Expand Down Expand Up @@ -757,7 +779,7 @@ public function updateOption(int $id, array $keyValuePairs): DataResponse {
throw new OCSBadRequestException('Could not find option, question or form');
}

if ($form->getOwnerId() !== $this->currentUser->getUID()) {
if (!$this->formsService->isAllowedToEdit($form->getId())) {
$this->logger->debug('This form is not owned by the current user');
throw new OCSForbiddenException();
}
Expand Down Expand Up @@ -836,7 +858,7 @@ public function getSubmissions(string $hash): DataResponse {
throw new OCSBadRequestException();
}

if ($form->getOwnerId() !== $this->currentUser->getUID()) {
if (!$this->formsService->isAllowedToEdit($form->getId())) {
$this->logger->debug('This form is not owned by the current user');
throw new OCSForbiddenException();
}
Expand Down
54 changes: 53 additions & 1 deletion lib/Controller/ShareApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public function newShare(int $formId, int $shareType, string $shareWith = ''): D
}

// Check for permission to share form
if ($form->getOwnerId() !== $this->currentUser->getUID()) {
if (!$this->formsService->isAllowedToEdit($formId)) {
$this->logger->debug('This form is not owned by the current user');
throw new OCSForbiddenException();
}
Expand Down Expand Up @@ -244,4 +244,56 @@ public function deleteShare(int $id): DataResponse {

return new DataResponse($id);
}

/**
* @NoAdminRequired
*
* toggle editor role in shares
*
* @param int $id of the share to update
* @param bool $isEditor state of the editor role
* @param bool $uid id of the shared with user
* @return DataResponse
* @throws OCSBadRequestException
* @throws OCSForbiddenException
*/
public function toggleEditor(int $formId, bool $isEditor, string $uid): DataResponse {
$this->logger->debug('updating editor role in share: {id} to {isEditor} for user: {uid}', [
'id' => $id,
'isEditor' => $isEditor,
'uid' => $uid
]);
$shareId = $this->formsService->getShareByFromIdAndUserid($formId, $uid);
if ($shareId < 0) {
$shareData = $this->newShare($formId, IShare::TYPE_USER, $uid);
if ($isEditor) {
$share = Share::fromParams($shareData);
$share->setIsEditor($isEditor);
$this->shareMapper->update($share);
}
return new DataResponse($share->getId());
} else {
try {
$share = $this->shareMapper->findById($shareId);
$form = $this->formMapper->findById($formId);
} catch (IMapperException $e) {
$this->logger->debug('Could not find share', ['exception' => $e]);
throw new OCSBadRequestException('Could not find share');
}
}

if ($form->getOwnerId() !== $this->currentUser->getUID()) {
$this->logger->debug('This form is not owned by the current user');
throw new OCSForbiddenException();
}

if ($share->getIsEditor() !== $isEditor) {
$share->setIsEditor($isEditor);
$this->shareMapper->update($share);
return new DataResponse($share->getId());
}
$this->logger->debug('Share is already in the required state.');

return new DataResponse($share->getId());
}
}
6 changes: 6 additions & 0 deletions lib/Db/Share.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
* @method void setShareType(integer $value)
* @method string getShareWith()
* @method void setShareWith(string $value)
* @method string getIsEditor()
* @method void setIsEditor(bool $value)
*/
class Share extends Entity {
/** @var int */
Expand All @@ -43,6 +45,8 @@ class Share extends Entity {
protected $shareType;
/** @var string */
protected $shareWith;
/** @var bool */
protected $isEditor;

/**
* Option constructor.
Expand All @@ -51,6 +55,7 @@ public function __construct() {
$this->addType('formId', 'integer');
$this->addType('shareType', 'integer');
$this->addType('shareWith', 'string');
$this->addType('isEditor', 'bool');
}

public function read(): array {
Expand All @@ -59,6 +64,7 @@ public function read(): array {
'formId' => $this->getFormId(),
'shareType' => $this->getShareType(),
'shareWith' => $this->getShareWith(),
'isEditor' => $this->getIsEditor(),
];
}
}
59 changes: 59 additions & 0 deletions lib/Migration/Version3000Date20221127191108.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2022 Hamza Mahjoubi <hamzamahjoubi221@gmail.com>
*
* @author Hamza Mahjoubi <hamzamahjoubi221@gmail.com>
*
* @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 <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\Forms\Migration;

use Closure;
use OCP\DB\ISchemaWrapper;
use OCP\DB\Types;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;

class Version3000Date20221127191108 extends SimpleMigrationStep {

/**
* @param IOutput $output
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
* @param array $options
* @return null|ISchemaWrapper
*/
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
/** @var ISchemaWrapper $schema */
$schema = $schemaClosure();
$table = $schema->getTable('forms_v2_shares');

if (!$table->hasColumn('is_editor')) {
$table->addColumn('is_editor', Types::BOOLEAN, [
'notnull' => false,
'default' => 0,
]);

return $schema;
}

return null;
}
}
Loading