AWorkflowController.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. /** @var Manager */
  21. protected $manager;
  22. /** @var LoggerInterface */
  23. private $logger;
  24. public function __construct(
  25. $appName,
  26. IRequest $request,
  27. Manager $manager,
  28. LoggerInterface $logger
  29. ) {
  30. parent::__construct($appName, $request);
  31. $this->manager = $manager;
  32. $this->logger = $logger;
  33. }
  34. /**
  35. * @throws OCSForbiddenException
  36. */
  37. abstract protected function getScopeContext(): ScopeContext;
  38. /**
  39. * Example: curl -u joann -H "OCS-APIREQUEST: true" "http://my.nc.srvr/ocs/v2.php/apps/workflowengine/api/v1/workflows/global?format=json"
  40. *
  41. * @throws OCSForbiddenException
  42. */
  43. public function index(): DataResponse {
  44. $operationsByClass = $this->manager->getAllOperations($this->getScopeContext());
  45. foreach ($operationsByClass as &$operations) {
  46. foreach ($operations as &$operation) {
  47. $operation = $this->manager->formatOperation($operation);
  48. }
  49. }
  50. return new DataResponse($operationsByClass);
  51. }
  52. /**
  53. * 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"
  54. *
  55. * @throws OCSForbiddenException
  56. */
  57. public function show(string $id): DataResponse {
  58. $context = $this->getScopeContext();
  59. // The ID corresponds to a class name
  60. $operations = $this->manager->getOperations($id, $context);
  61. foreach ($operations as &$operation) {
  62. $operation = $this->manager->formatOperation($operation);
  63. }
  64. return new DataResponse($operations);
  65. }
  66. /**
  67. * @throws OCSBadRequestException
  68. * @throws OCSForbiddenException
  69. * @throws OCSException
  70. */
  71. #[PasswordConfirmationRequired]
  72. public function create(
  73. string $class,
  74. string $name,
  75. array $checks,
  76. string $operation,
  77. string $entity,
  78. array $events
  79. ): DataResponse {
  80. $context = $this->getScopeContext();
  81. try {
  82. $operation = $this->manager->addOperation($class, $name, $checks, $operation, $context, $entity, $events);
  83. $operation = $this->manager->formatOperation($operation);
  84. return new DataResponse($operation);
  85. } catch (\UnexpectedValueException $e) {
  86. throw new OCSBadRequestException($e->getMessage(), $e);
  87. } catch (\DomainException $e) {
  88. throw new OCSForbiddenException($e->getMessage(), $e);
  89. } catch (Exception $e) {
  90. $this->logger->error('Error when inserting flow', ['exception' => $e]);
  91. throw new OCSException('An internal error occurred', $e->getCode(), $e);
  92. }
  93. }
  94. /**
  95. * @throws OCSBadRequestException
  96. * @throws OCSForbiddenException
  97. * @throws OCSException
  98. */
  99. #[PasswordConfirmationRequired]
  100. public function update(
  101. int $id,
  102. string $name,
  103. array $checks,
  104. string $operation,
  105. string $entity,
  106. array $events
  107. ): DataResponse {
  108. try {
  109. $context = $this->getScopeContext();
  110. $operation = $this->manager->updateOperation($id, $name, $checks, $operation, $context, $entity, $events);
  111. $operation = $this->manager->formatOperation($operation);
  112. return new DataResponse($operation);
  113. } catch (\UnexpectedValueException $e) {
  114. throw new OCSBadRequestException($e->getMessage(), $e);
  115. } catch (\DomainException $e) {
  116. throw new OCSForbiddenException($e->getMessage(), $e);
  117. } catch (Exception $e) {
  118. $this->logger->error('Error when updating flow with id ' . $id, ['exception' => $e]);
  119. throw new OCSException('An internal error occurred', $e->getCode(), $e);
  120. }
  121. }
  122. /**
  123. * @throws OCSBadRequestException
  124. * @throws OCSForbiddenException
  125. * @throws OCSException
  126. */
  127. #[PasswordConfirmationRequired]
  128. public function destroy(int $id): DataResponse {
  129. try {
  130. $deleted = $this->manager->deleteOperation($id, $this->getScopeContext());
  131. return new DataResponse($deleted);
  132. } catch (\UnexpectedValueException $e) {
  133. throw new OCSBadRequestException($e->getMessage(), $e);
  134. } catch (\DomainException $e) {
  135. throw new OCSForbiddenException($e->getMessage(), $e);
  136. } catch (Exception $e) {
  137. $this->logger->error('Error when deleting flow with id ' . $id, ['exception' => $e]);
  138. throw new OCSException('An internal error occurred', $e->getCode(), $e);
  139. }
  140. }
  141. }