|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | +namespace OCA\WorkflowEngine\Command; |
| 10 | + |
| 11 | +use OC\Core\Command\Base; |
| 12 | +use OC\User\NoUserException; |
| 13 | +use OCA\WorkflowEngine\Helper\ScopeContext; |
| 14 | +use OCA\WorkflowEngine\Manager; |
| 15 | +use OCP\IUserManager; |
| 16 | +use OCP\IUserSession; |
| 17 | +use OCP\WorkflowEngine\IManager; |
| 18 | +use Override; |
| 19 | +use Symfony\Component\Console\Input\InputArgument; |
| 20 | +use Symfony\Component\Console\Input\InputInterface; |
| 21 | +use Symfony\Component\Console\Output\OutputInterface; |
| 22 | + |
| 23 | +class Runtime extends Base { |
| 24 | + |
| 25 | + public function __construct( |
| 26 | + private Manager $manager, |
| 27 | + private IUserManager $userManager, |
| 28 | + private IUserSession $userSession, |
| 29 | + ) { |
| 30 | + parent::__construct(); |
| 31 | + } |
| 32 | + |
| 33 | + #[Override] |
| 34 | + protected function configure() { |
| 35 | + parent::configure(); |
| 36 | + $this |
| 37 | + ->setName('workflows:runtime:list') |
| 38 | + ->setDescription('Lists configured runtime workflows') |
| 39 | + // need to add an optional filtering by app |
| 40 | + ->addArgument( |
| 41 | + 'appId', |
| 42 | + InputArgument::OPTIONAL, |
| 43 | + 'Filter runtime workflows by appId', |
| 44 | + null |
| 45 | + ) |
| 46 | + ->addArgument( |
| 47 | + 'scope', |
| 48 | + InputArgument::OPTIONAL, |
| 49 | + 'Lists workflows for "admin", "user"', |
| 50 | + 'admin' |
| 51 | + ) |
| 52 | + ->addArgument( |
| 53 | + 'userId', |
| 54 | + InputArgument::OPTIONAL, |
| 55 | + 'User ID used for user scope and session', |
| 56 | + null |
| 57 | + ); |
| 58 | + } |
| 59 | + |
| 60 | + protected function mappedScope(string $scope): int { |
| 61 | + return match($scope) { |
| 62 | + 'admin' => IManager::SCOPE_ADMIN, |
| 63 | + 'user' => IManager::SCOPE_USER, |
| 64 | + default => -1, |
| 65 | + }; |
| 66 | + } |
| 67 | + |
| 68 | + #[Override] |
| 69 | + protected function execute(InputInterface $input, OutputInterface $output): int { |
| 70 | + $appId = $input->getArgument('appId'); |
| 71 | + $userId = $input->getArgument('userId'); |
| 72 | + |
| 73 | + if ($userId !== null) { |
| 74 | + $user = $this->userManager->get($userId); |
| 75 | + if ($user === null) { |
| 76 | + throw new NoUserException("user $userId not found"); |
| 77 | + } |
| 78 | + $this->userSession->setUser($user); |
| 79 | + $this->manager->reloadRuntimeOperations(); |
| 80 | + } |
| 81 | + |
| 82 | + $opsByClass = $this->manager->getAllRuntimeOperations( |
| 83 | + new ScopeContext( |
| 84 | + $this->mappedScope($input->getArgument('scope')), |
| 85 | + $input->getArgument('userId') |
| 86 | + ), |
| 87 | + $appId, |
| 88 | + ); |
| 89 | + |
| 90 | + foreach ($opsByClass as &$operations) { |
| 91 | + foreach ($operations as &$operation) { |
| 92 | + $checks = $operation->checks; |
| 93 | + $appId = $operation->appId; |
| 94 | + $operation = $operation->toArray(); |
| 95 | + $operation['checks'] = $this->manager->getRuntimeChecks($checks, $appId); |
| 96 | + } |
| 97 | + unset($operation); |
| 98 | + } |
| 99 | + unset($operations); |
| 100 | + |
| 101 | + $this->writeArrayInOutputFormat($input, $output, $opsByClass); |
| 102 | + |
| 103 | + return 0; |
| 104 | + } |
| 105 | +} |
0 commit comments