AWorkflowController.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2019 Arthur Schiwon <blizzz@arthur-schiwon.de>
  5. *
  6. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  7. * @author blizzz <blizzz@arthur-schiwon.de>
  8. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OCA\WorkflowEngine\Controller;
  27. use Doctrine\DBAL\Exception;
  28. use OCA\WorkflowEngine\Helper\ScopeContext;
  29. use OCA\WorkflowEngine\Manager;
  30. use OCP\AppFramework\Http\Attribute\PasswordConfirmationRequired;
  31. use OCP\AppFramework\Http\DataResponse;
  32. use OCP\AppFramework\OCS\OCSBadRequestException;
  33. use OCP\AppFramework\OCS\OCSException;
  34. use OCP\AppFramework\OCS\OCSForbiddenException;
  35. use OCP\AppFramework\OCSController;
  36. use OCP\IRequest;
  37. use Psr\Log\LoggerInterface;
  38. abstract class AWorkflowController extends OCSController {
  39. /** @var Manager */
  40. protected $manager;
  41. /** @var LoggerInterface */
  42. private $logger;
  43. public function __construct(
  44. $appName,
  45. IRequest $request,
  46. Manager $manager,
  47. LoggerInterface $logger
  48. ) {
  49. parent::__construct($appName, $request);
  50. $this->manager = $manager;
  51. $this->logger = $logger;
  52. }
  53. /**
  54. * @throws OCSForbiddenException
  55. */
  56. abstract protected function getScopeContext(): ScopeContext;
  57. /**
  58. * Example: curl -u joann -H "OCS-APIREQUEST: true" "http://my.nc.srvr/ocs/v2.php/apps/workflowengine/api/v1/workflows/global?format=json"
  59. *
  60. * @throws OCSForbiddenException
  61. */
  62. public function index(): DataResponse {
  63. $operationsByClass = $this->manager->getAllOperations($this->getScopeContext());
  64. foreach ($operationsByClass as &$operations) {
  65. foreach ($operations as &$operation) {
  66. $operation = $this->manager->formatOperation($operation);
  67. }
  68. }
  69. return new DataResponse($operationsByClass);
  70. }
  71. /**
  72. * 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"
  73. *
  74. * @throws OCSForbiddenException
  75. */
  76. public function show(string $id): DataResponse {
  77. $context = $this->getScopeContext();
  78. // The ID corresponds to a class name
  79. $operations = $this->manager->getOperations($id, $context);
  80. foreach ($operations as &$operation) {
  81. $operation = $this->manager->formatOperation($operation);
  82. }
  83. return new DataResponse($operations);
  84. }
  85. /**
  86. * @throws OCSBadRequestException
  87. * @throws OCSForbiddenException
  88. * @throws OCSException
  89. */
  90. #[PasswordConfirmationRequired]
  91. public function create(
  92. string $class,
  93. string $name,
  94. array $checks,
  95. string $operation,
  96. string $entity,
  97. array $events
  98. ): DataResponse {
  99. $context = $this->getScopeContext();
  100. try {
  101. $operation = $this->manager->addOperation($class, $name, $checks, $operation, $context, $entity, $events);
  102. $operation = $this->manager->formatOperation($operation);
  103. return new DataResponse($operation);
  104. } catch (\UnexpectedValueException $e) {
  105. throw new OCSBadRequestException($e->getMessage(), $e);
  106. } catch (\DomainException $e) {
  107. throw new OCSForbiddenException($e->getMessage(), $e);
  108. } catch (Exception $e) {
  109. $this->logger->error('Error when inserting flow', ['exception' => $e]);
  110. throw new OCSException('An internal error occurred', $e->getCode(), $e);
  111. }
  112. }
  113. /**
  114. * @throws OCSBadRequestException
  115. * @throws OCSForbiddenException
  116. * @throws OCSException
  117. */
  118. #[PasswordConfirmationRequired]
  119. public function update(
  120. int $id,
  121. string $name,
  122. array $checks,
  123. string $operation,
  124. string $entity,
  125. array $events
  126. ): DataResponse {
  127. try {
  128. $context = $this->getScopeContext();
  129. $operation = $this->manager->updateOperation($id, $name, $checks, $operation, $context, $entity, $events);
  130. $operation = $this->manager->formatOperation($operation);
  131. return new DataResponse($operation);
  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 updating flow with id ' . $id, ['exception' => $e]);
  138. throw new OCSException('An internal error occurred', $e->getCode(), $e);
  139. }
  140. }
  141. /**
  142. * @throws OCSBadRequestException
  143. * @throws OCSForbiddenException
  144. * @throws OCSException
  145. */
  146. #[PasswordConfirmationRequired]
  147. public function destroy(int $id): DataResponse {
  148. try {
  149. $deleted = $this->manager->deleteOperation($id, $this->getScopeContext());
  150. return new DataResponse($deleted);
  151. } catch (\UnexpectedValueException $e) {
  152. throw new OCSBadRequestException($e->getMessage(), $e);
  153. } catch (\DomainException $e) {
  154. throw new OCSForbiddenException($e->getMessage(), $e);
  155. } catch (Exception $e) {
  156. $this->logger->error('Error when deleting flow with id ' . $id, ['exception' => $e]);
  157. throw new OCSException('An internal error occurred', $e->getCode(), $e);
  158. }
  159. }
  160. }