AWorkflowController.php 5.4 KB

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