UserWorkflowsController.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 OCA\WorkflowEngine\Helper\ScopeContext;
  9. use OCA\WorkflowEngine\Manager;
  10. use OCP\AppFramework\Http\Attribute\NoAdminRequired;
  11. use OCP\AppFramework\Http\Attribute\PasswordConfirmationRequired;
  12. use OCP\AppFramework\Http\DataResponse;
  13. use OCP\AppFramework\OCS\OCSBadRequestException;
  14. use OCP\AppFramework\OCS\OCSForbiddenException;
  15. use OCP\IRequest;
  16. use OCP\IUserSession;
  17. use OCP\WorkflowEngine\IManager;
  18. use Psr\Log\LoggerInterface;
  19. class UserWorkflowsController extends AWorkflowController {
  20. /** @var IUserSession */
  21. private $session;
  22. /** @var ScopeContext */
  23. private $scopeContext;
  24. public function __construct(
  25. $appName,
  26. IRequest $request,
  27. Manager $manager,
  28. IUserSession $session,
  29. LoggerInterface $logger
  30. ) {
  31. parent::__construct($appName, $request, $manager, $logger);
  32. $this->session = $session;
  33. }
  34. /**
  35. * Retrieve all configured workflow rules
  36. *
  37. * Example: curl -u joann -H "OCS-APIREQUEST: true" "http://my.nc.srvr/ocs/v2.php/apps/workflowengine/api/v1/workflows/user?format=json"
  38. *
  39. * @throws OCSForbiddenException
  40. */
  41. #[NoAdminRequired]
  42. public function index(): DataResponse {
  43. return parent::index();
  44. }
  45. /**
  46. * Example: curl -u joann -H "OCS-APIREQUEST: true" "http://my.nc.srvr/ocs/v2.php/apps/workflowengine/api/v1/workflows/user/OCA\\Workflow_DocToPdf\\Operation?format=json"
  47. * @throws OCSForbiddenException
  48. */
  49. #[NoAdminRequired]
  50. public function show(string $id): DataResponse {
  51. return parent::show($id);
  52. }
  53. /**
  54. * @throws OCSBadRequestException
  55. * @throws OCSForbiddenException
  56. */
  57. #[NoAdminRequired]
  58. #[PasswordConfirmationRequired]
  59. public function create(string $class, string $name, array $checks, string $operation, string $entity, array $events): DataResponse {
  60. return parent::create($class, $name, $checks, $operation, $entity, $events);
  61. }
  62. /**
  63. * @throws OCSBadRequestException
  64. * @throws OCSForbiddenException
  65. */
  66. #[NoAdminRequired]
  67. #[PasswordConfirmationRequired]
  68. public function update(int $id, string $name, array $checks, string $operation, string $entity, array $events): DataResponse {
  69. return parent::update($id, $name, $checks, $operation, $entity, $events);
  70. }
  71. /**
  72. * @throws OCSForbiddenException
  73. */
  74. #[NoAdminRequired]
  75. #[PasswordConfirmationRequired]
  76. public function destroy(int $id): DataResponse {
  77. return parent::destroy($id);
  78. }
  79. /**
  80. * @throws OCSForbiddenException
  81. */
  82. protected function getScopeContext(): ScopeContext {
  83. if ($this->scopeContext === null) {
  84. $user = $this->session->getUser();
  85. if (!$user || !$this->manager->isUserScopeEnabled()) {
  86. throw new OCSForbiddenException('User not logged in');
  87. }
  88. $this->scopeContext = new ScopeContext(IManager::SCOPE_USER, $user->getUID());
  89. }
  90. return $this->scopeContext;
  91. }
  92. }