Skip to content

Commit

Permalink
transfer ownership of a form
Browse files Browse the repository at this point in the history
Signed-off-by: hamza221 <[email protected]>
  • Loading branch information
hamza221 authored and Chartman123 committed Dec 12, 2023
1 parent d2878a4 commit 297155b
Show file tree
Hide file tree
Showing 9 changed files with 505 additions and 198 deletions.
8 changes: 8 additions & 0 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,14 @@
'apiVersion' => 'v2.2'
]
],
[
'name' => 'api#transferOwner',
'url' => '/api/{apiVersion}/form/transfer',
'verb' => 'POST',
'requirements' => [
'apiVersion' => 'v2.2'
]
],
[
'name' => 'api#deleteForm',
'url' => '/api/{apiVersion}/form/{id}',
Expand Down
14 changes: 14 additions & 0 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,20 @@ Update a single or multiple properties of a form-object. Concerns **only** the F
```
"data": 3
```
### Transfer form ownership
Transfer the ownership of a form to another user
- Endpoint: `/api/v2.2/form/transfer`
- Method: `POST`
- Parameters:
| Parameter | Type | Description |
|-----------|---------|-------------|
| _formId_ | Integer | ID of the form to tranfer |
| _uid_ | Integer | ID of the new form owner |
- Restrictions: The initiator must be the current form owner.
- Response: **Status-Code OK**, as well as the id of the new owner.
```
"data": "user1"
```

### Delete a form
- Endpoint: `/api/v2.2/form/{id}`
Expand Down
45 changes: 45 additions & 0 deletions lib/Controller/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
use OCP\AppFramework\OCS\OCSException;
use OCP\AppFramework\OCS\OCSForbiddenException;
use OCP\AppFramework\OCSController;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\IL10N;
use OCP\IRequest;
Expand Down Expand Up @@ -351,6 +352,50 @@ public function updateForm(int $id, array $keyValuePairs): DataResponse {
return new DataResponse($form->getId());
}

/**
* @NoAdminRequired
*
* Transfer ownership of a form to another user
*
* @param int $formId id of the form to update
* @param string $uid id of the new owner
* @return DataResponse
* @throws OCSBadRequestException
* @throws OCSForbiddenException
*/
public function transferOwner(int $formId, string $uid): DataResponse {
$this->logger->debug('Updating owner: formId: {formId}, userId: {uid}', [
'formId' => $formId,
'uid' => $uid
]);

Check warning on line 370 in lib/Controller/ApiController.php

View check run for this annotation

Codecov / codecov/patch

lib/Controller/ApiController.php#L366-L370

Added lines #L366 - L370 were not covered by tests

try {
$form = $this->formMapper->findById($formId);
} catch (IMapperException $e) {
$this->logger->debug('Could not find form');
throw new NotFoundException('Could not find form');

Check warning on line 376 in lib/Controller/ApiController.php

View check run for this annotation

Codecov / codecov/patch

lib/Controller/ApiController.php#L373-L376

Added lines #L373 - L376 were not covered by tests
}

$user = $this->userManager->get($uid);
if($user == null) {
$this->logger->debug('Could not find new form owner');
throw new OCSBadRequestException('Could not find new form owner');

Check warning on line 382 in lib/Controller/ApiController.php

View check run for this annotation

Codecov / codecov/patch

lib/Controller/ApiController.php#L379-L382

Added lines #L379 - L382 were not covered by tests
}

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

Check warning on line 387 in lib/Controller/ApiController.php

View check run for this annotation

Codecov / codecov/patch

lib/Controller/ApiController.php#L385-L387

Added lines #L385 - L387 were not covered by tests
}

// update form owner
$form->setOwnerId($uid);

Check warning on line 391 in lib/Controller/ApiController.php

View check run for this annotation

Codecov / codecov/patch

lib/Controller/ApiController.php#L391

Added line #L391 was not covered by tests

// Update changed Columns in Db.
$this->formMapper->update($form);

Check warning on line 394 in lib/Controller/ApiController.php

View check run for this annotation

Codecov / codecov/patch

lib/Controller/ApiController.php#L394

Added line #L394 was not covered by tests

return new DataResponse($form->getOwnerId());

Check warning on line 396 in lib/Controller/ApiController.php

View check run for this annotation

Codecov / codecov/patch

lib/Controller/ApiController.php#L396

Added line #L396 was not covered by tests
}

/**
* @CORS
* @NoAdminRequired
Expand Down
21 changes: 21 additions & 0 deletions lib/Db/ShareMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,28 @@ public function findPublicShareByHash(string $hash): Share {

return $this->findEntity($qb);
}
/**
* Find Share by formId and user id
* @param int $formId
* @param string $uid
* @return Share
* @throws MultipleObjectsReturnedException if more than one result
* @throws DoesNotExistException if not found
*/
public function findPublicShareByFormIdAndUid(int $formId, string $uid): Share {
$qb = $this->db->getQueryBuilder();

Check warning on line 115 in lib/Db/ShareMapper.php

View check run for this annotation

Codecov / codecov/patch

lib/Db/ShareMapper.php#L114-L115

Added lines #L114 - L115 were not covered by tests

$qb->select('*')
->from($this->getTableName())
->where(
$qb->expr()->eq('form_id', $qb->createNamedParameter($formId, IQueryBuilder::PARAM_INT))
)
->andWhere(
$qb->expr()->eq('share_with', $qb->createNamedParameter($uid, IQueryBuilder::PARAM_STR))
);

Check warning on line 124 in lib/Db/ShareMapper.php

View check run for this annotation

Codecov / codecov/patch

lib/Db/ShareMapper.php#L117-L124

Added lines #L117 - L124 were not covered by tests

return $this->findEntity($qb);

Check warning on line 126 in lib/Db/ShareMapper.php

View check run for this annotation

Codecov / codecov/patch

lib/Db/ShareMapper.php#L126

Added line #L126 was not covered by tests
}
/**
* Delete a share
* @param int $id of the share.
Expand Down
2 changes: 2 additions & 0 deletions src/Forms.vue
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,12 @@ export default {
mounted() {
subscribe('forms:last-updated:set', (id) => this.onLastUpdatedByEventBus(id))
subscribe('forms:ownership-transfered', (id) => this.onDeleteForm(id))
},
unmounted() {
unsubscribe('forms:last-updated:set', (id) => this.onLastUpdatedByEventBus(id))
unsubscribe('forms:ownership-transfered', (id) => this.onDeleteForm(id))
},
methods: {
Expand Down
9 changes: 9 additions & 0 deletions src/components/SidebarTabs/SettingsSidebarTab.vue
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@
{{ t('forms', 'Message to show after a user submitted the form. Please note that the message will not be translated!') }}
</div>
</div>

<TransferOwnership :form="form" />
</div>
</template>

Expand All @@ -96,6 +98,7 @@ import moment from '@nextcloud/moment'
import NcCheckboxRadioSwitch from '@nextcloud/vue/dist/Components/NcCheckboxRadioSwitch.js'
import NcDateTimePicker from '@nextcloud/vue/dist/Components/NcDateTimePicker.js'
import ShareTypes from '../../mixins/ShareTypes.js'
import TransferOwnership from './TransferOwnership.vue'
import { directive as ClickOutside } from 'v-click-outside'
import { loadState } from '@nextcloud/initial-state'
Expand All @@ -104,6 +107,7 @@ export default {
components: {
NcCheckboxRadioSwitch,
NcDateTimePicker,
TransferOwnership,
},
directives: {
Expand Down Expand Up @@ -286,6 +290,11 @@ export default {
margin-inline-start: 40px;
}
.sidebar-tabs__content {
display: flex;
flex-direction: column;
gap: 8px;
}
.submission-message {
&__description {
color: var(--color-text-maxcontrast);
Expand Down
Loading

0 comments on commit 297155b

Please sign in to comment.