|
51 | 51 | use OCP\AppFramework\OCS\OCSException; |
52 | 52 | use OCP\AppFramework\OCS\OCSForbiddenException; |
53 | 53 | use OCP\AppFramework\OCSController; |
| 54 | +use OCP\Files\NotFoundException; |
54 | 55 | use OCP\Files\NotPermittedException; |
55 | 56 | use OCP\IL10N; |
56 | 57 | use OCP\IRequest; |
@@ -351,6 +352,50 @@ public function updateForm(int $id, array $keyValuePairs): DataResponse { |
351 | 352 | return new DataResponse($form->getId()); |
352 | 353 | } |
353 | 354 |
|
| 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 | + |
354 | 399 | /** |
355 | 400 | * @CORS |
356 | 401 | * @NoAdminRequired |
|
0 commit comments