Skip to content

Commit 297155b

Browse files
hamza221Chartman123
authored andcommitted
transfer ownership of a form
Signed-off-by: hamza221 <hamzamahjoubi221@gmail.com>
1 parent d2878a4 commit 297155b

File tree

9 files changed

+505
-198
lines changed

9 files changed

+505
-198
lines changed

appinfo/routes.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,14 @@
127127
'apiVersion' => 'v2.2'
128128
]
129129
],
130+
[
131+
'name' => 'api#transferOwner',
132+
'url' => '/api/{apiVersion}/form/transfer',
133+
'verb' => 'POST',
134+
'requirements' => [
135+
'apiVersion' => 'v2.2'
136+
]
137+
],
130138
[
131139
'name' => 'api#deleteForm',
132140
'url' => '/api/{apiVersion}/form/{id}',

docs/API.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,20 @@ Update a single or multiple properties of a form-object. Concerns **only** the F
221221
```
222222
"data": 3
223223
```
224+
### Transfer form ownership
225+
Transfer the ownership of a form to another user
226+
- Endpoint: `/api/v2.2/form/transfer`
227+
- Method: `POST`
228+
- Parameters:
229+
| Parameter | Type | Description |
230+
|-----------|---------|-------------|
231+
| _formId_ | Integer | ID of the form to tranfer |
232+
| _uid_ | Integer | ID of the new form owner |
233+
- Restrictions: The initiator must be the current form owner.
234+
- Response: **Status-Code OK**, as well as the id of the new owner.
235+
```
236+
"data": "user1"
237+
```
224238

225239
### Delete a form
226240
- Endpoint: `/api/v2.2/form/{id}`

lib/Controller/ApiController.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
use OCP\AppFramework\OCS\OCSException;
5252
use OCP\AppFramework\OCS\OCSForbiddenException;
5353
use OCP\AppFramework\OCSController;
54+
use OCP\Files\NotFoundException;
5455
use OCP\Files\NotPermittedException;
5556
use OCP\IL10N;
5657
use OCP\IRequest;
@@ -351,6 +352,50 @@ public function updateForm(int $id, array $keyValuePairs): DataResponse {
351352
return new DataResponse($form->getId());
352353
}
353354

355+
/**
356+
* @NoAdminRequired
357+
*
358+
* Transfer ownership of a form to another user
359+
*
360+
* @param int $formId id of the form to update
361+
* @param string $uid id of the new owner
362+
* @return DataResponse
363+
* @throws OCSBadRequestException
364+
* @throws OCSForbiddenException
365+
*/
366+
public function transferOwner(int $formId, string $uid): DataResponse {
367+
$this->logger->debug('Updating owner: formId: {formId}, userId: {uid}', [
368+
'formId' => $formId,
369+
'uid' => $uid
370+
]);
371+
372+
try {
373+
$form = $this->formMapper->findById($formId);
374+
} catch (IMapperException $e) {
375+
$this->logger->debug('Could not find form');
376+
throw new NotFoundException('Could not find form');
377+
}
378+
379+
$user = $this->userManager->get($uid);
380+
if($user == null) {
381+
$this->logger->debug('Could not find new form owner');
382+
throw new OCSBadRequestException('Could not find new form owner');
383+
}
384+
385+
if ($form->getOwnerId() !== $this->currentUser->getUID()) {
386+
$this->logger->debug('This form is not owned by the current user');
387+
throw new OCSForbiddenException('This form is not owned by the current user');
388+
}
389+
390+
// update form owner
391+
$form->setOwnerId($uid);
392+
393+
// Update changed Columns in Db.
394+
$this->formMapper->update($form);
395+
396+
return new DataResponse($form->getOwnerId());
397+
}
398+
354399
/**
355400
* @CORS
356401
* @NoAdminRequired

lib/Db/ShareMapper.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,28 @@ public function findPublicShareByHash(string $hash): Share {
103103

104104
return $this->findEntity($qb);
105105
}
106+
/**
107+
* Find Share by formId and user id
108+
* @param int $formId
109+
* @param string $uid
110+
* @return Share
111+
* @throws MultipleObjectsReturnedException if more than one result
112+
* @throws DoesNotExistException if not found
113+
*/
114+
public function findPublicShareByFormIdAndUid(int $formId, string $uid): Share {
115+
$qb = $this->db->getQueryBuilder();
106116

117+
$qb->select('*')
118+
->from($this->getTableName())
119+
->where(
120+
$qb->expr()->eq('form_id', $qb->createNamedParameter($formId, IQueryBuilder::PARAM_INT))
121+
)
122+
->andWhere(
123+
$qb->expr()->eq('share_with', $qb->createNamedParameter($uid, IQueryBuilder::PARAM_STR))
124+
);
125+
126+
return $this->findEntity($qb);
127+
}
107128
/**
108129
* Delete a share
109130
* @param int $id of the share.

src/Forms.vue

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,10 +230,12 @@ export default {
230230
231231
mounted() {
232232
subscribe('forms:last-updated:set', (id) => this.onLastUpdatedByEventBus(id))
233+
subscribe('forms:ownership-transfered', (id) => this.onDeleteForm(id))
233234
},
234235
235236
unmounted() {
236237
unsubscribe('forms:last-updated:set', (id) => this.onLastUpdatedByEventBus(id))
238+
unsubscribe('forms:ownership-transfered', (id) => this.onDeleteForm(id))
237239
},
238240
239241
methods: {

src/components/SidebarTabs/SettingsSidebarTab.vue

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@
8888
{{ t('forms', 'Message to show after a user submitted the form. Please note that the message will not be translated!') }}
8989
</div>
9090
</div>
91+
92+
<TransferOwnership :form="form" />
9193
</div>
9294
</template>
9395

@@ -96,6 +98,7 @@ import moment from '@nextcloud/moment'
9698
import NcCheckboxRadioSwitch from '@nextcloud/vue/dist/Components/NcCheckboxRadioSwitch.js'
9799
import NcDateTimePicker from '@nextcloud/vue/dist/Components/NcDateTimePicker.js'
98100
import ShareTypes from '../../mixins/ShareTypes.js'
101+
import TransferOwnership from './TransferOwnership.vue'
99102
100103
import { directive as ClickOutside } from 'v-click-outside'
101104
import { loadState } from '@nextcloud/initial-state'
@@ -104,6 +107,7 @@ export default {
104107
components: {
105108
NcCheckboxRadioSwitch,
106109
NcDateTimePicker,
110+
TransferOwnership,
107111
},
108112
109113
directives: {
@@ -286,6 +290,11 @@ export default {
286290
margin-inline-start: 40px;
287291
}
288292
293+
.sidebar-tabs__content {
294+
display: flex;
295+
flex-direction: column;
296+
gap: 8px;
297+
}
289298
.submission-message {
290299
&__description {
291300
color: var(--color-text-maxcontrast);

0 commit comments

Comments
 (0)