AWorkflowController.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\WorkflowEngine\Controller;
  8. use Doctrine\DBAL\Exception;
  9. use OCA\WorkflowEngine\Helper\ScopeContext;
  10. use OCA\WorkflowEngine\Manager;
  11. use OCP\AppFramework\Http\Attribute\PasswordConfirmationRequired;
  12. use OCP\AppFramework\Http\DataResponse;
  13. use OCP\AppFramework\OCS\OCSBadRequestException;
  14. use OCP\AppFramework\OCS\OCSException;
  15. use OCP\AppFramework\OCS\OCSForbiddenException;
  16. use OCP\AppFramework\OCSController;
  17. use OCP\IRequest;
  18. use Psr\Log\LoggerInterface;
  19. abstract class AWorkflowController extends OCSController {
  20. public function __construct(
  21. $appName,
  22. IRequest $request,
  23. protected Manager $manager,
  24. private LoggerInterface $logger,
  25. ) {
  26. parent::__construct($appName, $request);
  27. }
  28. /**
  29. * @throws OCSForbiddenException
  30. */
  31. abstract protected function getScopeContext(): ScopeContext;
  32. /**
  33. * Example: curl -u joann -H "OCS-APIREQUEST: true" "http://my.nc.srvr/ocs/v2.php/apps/workflowengine/api/v1/workflows/global?format=json"
  34. *
  35. * @throws OCSForbiddenException
  36. */
  37. public function index(): DataResponse {
  38. $operationsByClass = $this->manager->getAllOperations($this->getScopeContext());
  39. foreach ($operationsByClass as &$operations) {
  40. foreach ($operations as &$operation) {
  41. $operation = $this->manager->formatOperation($operation);
  42. }
  43. }
  44. return new DataResponse($operationsByClass);
  45. }
  46. /**
  47. * Example: curl -u joann -H "OCS-APIREQUEST: true" "http://my.nc.srvr/ocs/v2.php/apps/workflowengine/api/v1/workflows/global/OCA\\Workflow_DocToPdf\\Operation?format=json"
  48. *
  49. * @throws OCSForbiddenException
  50. */
  51. public function show(string $id): DataResponse {
  52. $context = $this->getScopeContext();
  53. // The ID corresponds to a class name
  54. $operations = $this->manager->getOperations($id, $context);
  55. foreach ($operations as &$operation) {
  56. $operation = $this->manager->formatOperation($operation);
  57. }
  58. return new DataResponse($operations);
  59. }
  60. /**
  61. * @throws OCSBadRequestException
  62. * @throws OCSForbiddenException
  63. * @throws OCSException
  64. */
  65. #[PasswordConfirmationRequired]
  66. public function create(
  67. string $class,
  68. string $name,
  69. array $checks,
  70. string $operation,
  71. string $entity,
  72. array $events,
  73. ): DataResponse {
  74. $context = $this->getScopeContext();
  75. try {
  76. $operation = $this->manager->addOperation($class, $name, $checks, $operation, $context, $entity, $events);
  77. $operation = $this->manager->formatOperation($operation);
  78. return new DataResponse($operation);
  79. } catch (\UnexpectedValueException $e) {
  80. throw new OCSBadRequestException($e->getMessage(), $e);
  81. } catch (\DomainException $e) {
  82. throw new OCSForbiddenException($e->getMessage(), $e);
  83. } catch (Exception $e) {
  84. $this->logger->error('Error when inserting flow', ['exception' => $e]);
  85. throw new OCSException('An internal error occurred', $e->getCode(), $e);
  86. }
  87. }
  88. /**
  89. * @throws OCSBadRequestException
  90. * @throws OCSForbiddenException
  91. * @throws OCSException
  92. */
  93. #[PasswordConfirmationRequired]
  94. public function update(
  95. int $id,
  96. string $name,
  97. array $checks,
  98. string $operation,
  99. string $entity,
  100. array $events,
  101. ): DataResponse {
  102. try {
  103. $context = $this->getScopeContext();
  104. $operation = $this->manager->updateOperation($id, $name, $checks, $operation, $context, $entity, $events);
  105. $operation = $this->manager->formatOperation($operation);
  106. return new DataResponse($operation);
  107. } catch (\UnexpectedValueException $e) {
  108. throw new OCSBadRequestException($e->getMessage(), $e);
  109. } catch (\DomainException $e) {
  110. throw new OCSForbiddenException($e->getMessage(), $e);
  111. } catch (Exception $e) {
  112. $this->logger->error('Error when updating flow with id ' . $id, ['exception' => $e]);
  113. throw new OCSException('An internal error occurred', $e->getCode(), $e);
  114. }
  115. }
  116. /**
  117. * @throws OCSBadRequestException
  118. * @throws OCSForbiddenException
  119. * @throws OCSException
  120. */
  121. #[PasswordConfirmationRequired]
  122. public function destroy(int $id): DataResponse {
  123. try {
  124. $deleted = $this->manager->deleteOperation($id, $this->getScopeContext());
  125. return new DataResponse($deleted);
  126. } catch (\UnexpectedValueException $e) {
  127. throw new OCSBadRequestException($e->getMessage(), $e);
  128. } catch (\DomainException $e) {
  129. throw new OCSForbiddenException($e->getMessage(), $e);
  130. } catch (Exception $e) {
  131. $this->logger->error('Error when deleting flow with id ' . $id, ['exception' => $e]);
  132. throw new OCSException('An internal error occurred', $e->getCode(), $e);
  133. }
  134. }
  135. }