You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RuleMatcher.php 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\WorkflowEngine\Service;
  25. use OCA\WorkflowEngine\Helper\LogContext;
  26. use OCA\WorkflowEngine\Helper\ScopeContext;
  27. use OCA\WorkflowEngine\Manager;
  28. use OCP\AppFramework\QueryException;
  29. use OCP\Files\Storage\IStorage;
  30. use OCP\IL10N;
  31. use OCP\IServerContainer;
  32. use OCP\IUserSession;
  33. use OCP\WorkflowEngine\ICheck;
  34. use OCP\WorkflowEngine\IEntity;
  35. use OCP\WorkflowEngine\IEntityCheck;
  36. use OCP\WorkflowEngine\IFileCheck;
  37. use OCP\WorkflowEngine\IManager;
  38. use OCP\WorkflowEngine\IOperation;
  39. use OCP\WorkflowEngine\IRuleMatcher;
  40. use RuntimeException;
  41. class RuleMatcher implements IRuleMatcher {
  42. /** @var IUserSession */
  43. protected $session;
  44. /** @var IManager */
  45. protected $manager;
  46. /** @var array */
  47. protected $contexts;
  48. /** @var IServerContainer */
  49. protected $container;
  50. /** @var array */
  51. protected $fileInfo = [];
  52. /** @var IL10N */
  53. protected $l;
  54. /** @var IOperation */
  55. protected $operation;
  56. /** @var IEntity */
  57. protected $entity;
  58. /** @var Logger */
  59. protected $logger;
  60. /** @var string */
  61. protected $eventName;
  62. public function __construct(
  63. IUserSession $session,
  64. IServerContainer $container,
  65. IL10N $l,
  66. Manager $manager,
  67. Logger $logger
  68. ) {
  69. $this->session = $session;
  70. $this->manager = $manager;
  71. $this->container = $container;
  72. $this->l = $l;
  73. $this->logger = $logger;
  74. }
  75. public function setFileInfo(IStorage $storage, string $path, bool $isDir = false): void {
  76. $this->fileInfo['storage'] = $storage;
  77. $this->fileInfo['path'] = $path;
  78. $this->fileInfo['isDir'] = $isDir;
  79. }
  80. public function setEntitySubject(IEntity $entity, $subject): void {
  81. $this->contexts[get_class($entity)] = [$entity, $subject];
  82. }
  83. public function setOperation(IOperation $operation): void {
  84. if ($this->operation !== null) {
  85. throw new RuntimeException('This method must not be called more than once');
  86. }
  87. $this->operation = $operation;
  88. }
  89. public function setEntity(IEntity $entity): void {
  90. if ($this->entity !== null) {
  91. throw new RuntimeException('This method must not be called more than once');
  92. }
  93. $this->entity = $entity;
  94. }
  95. public function setEventName(string $eventName): void {
  96. if ($this->eventName !== null) {
  97. throw new RuntimeException('This method must not be called more than once');
  98. }
  99. $this->eventName = $eventName;
  100. }
  101. public function getEntity(): IEntity {
  102. if ($this->entity === null) {
  103. throw new \LogicException('Entity was not set yet');
  104. }
  105. return $this->entity;
  106. }
  107. public function getFlows(bool $returnFirstMatchingOperationOnly = true): array {
  108. if (!$this->operation) {
  109. throw new RuntimeException('Operation is not set');
  110. }
  111. return $this->getMatchingOperations(get_class($this->operation), $returnFirstMatchingOperationOnly);
  112. }
  113. public function getMatchingOperations(string $class, bool $returnFirstMatchingOperationOnly = true): array {
  114. $scopes[] = new ScopeContext(IManager::SCOPE_ADMIN);
  115. $user = $this->session->getUser();
  116. if ($user !== null && $this->manager->isUserScopeEnabled()) {
  117. $scopes[] = new ScopeContext(IManager::SCOPE_USER, $user->getUID());
  118. }
  119. $ctx = new LogContext();
  120. $ctx
  121. ->setScopes($scopes)
  122. ->setEntity($this->entity)
  123. ->setOperation($this->operation);
  124. $this->logger->logFlowRequests($ctx);
  125. $operations = [];
  126. foreach ($scopes as $scope) {
  127. $operations = array_merge($operations, $this->manager->getOperations($class, $scope));
  128. }
  129. if ($this->entity instanceof IEntity) {
  130. /** @var ScopeContext[] $additionalScopes */
  131. $additionalScopes = $this->manager->getAllConfiguredScopesForOperation($class);
  132. foreach ($additionalScopes as $hash => $scopeCandidate) {
  133. if ($scopeCandidate->getScope() !== IManager::SCOPE_USER || in_array($scopeCandidate, $scopes)) {
  134. continue;
  135. }
  136. if ($this->entity->isLegitimatedForUserId($scopeCandidate->getScopeId())) {
  137. $ctx = new LogContext();
  138. $ctx
  139. ->setScopes([$scopeCandidate])
  140. ->setEntity($this->entity)
  141. ->setOperation($this->operation);
  142. $this->logger->logScopeExpansion($ctx);
  143. $operations = array_merge($operations, $this->manager->getOperations($class, $scopeCandidate));
  144. }
  145. }
  146. }
  147. $matches = [];
  148. foreach ($operations as $operation) {
  149. $configuredEvents = json_decode($operation['events'], true);
  150. if ($this->eventName !== null && !in_array($this->eventName, $configuredEvents)) {
  151. continue;
  152. }
  153. $checkIds = json_decode($operation['checks'], true);
  154. $checks = $this->manager->getChecks($checkIds);
  155. foreach ($checks as $check) {
  156. if (!$this->check($check)) {
  157. // Check did not match, continue with the next operation
  158. continue 2;
  159. }
  160. }
  161. $ctx = new LogContext();
  162. $ctx
  163. ->setEntity($this->entity)
  164. ->setOperation($this->operation)
  165. ->setConfiguration($operation);
  166. $this->logger->logPassedCheck($ctx);
  167. if ($returnFirstMatchingOperationOnly) {
  168. $ctx = new LogContext();
  169. $ctx
  170. ->setEntity($this->entity)
  171. ->setOperation($this->operation)
  172. ->setConfiguration($operation);
  173. $this->logger->logRunSingle($ctx);
  174. return $operation;
  175. }
  176. $matches[] = $operation;
  177. }
  178. $ctx = new LogContext();
  179. $ctx
  180. ->setEntity($this->entity)
  181. ->setOperation($this->operation);
  182. if (!empty($matches)) {
  183. $ctx->setConfiguration($matches);
  184. $this->logger->logRunAll($ctx);
  185. } else {
  186. $this->logger->logRunNone($ctx);
  187. }
  188. return $matches;
  189. }
  190. /**
  191. * @param array $check
  192. * @return bool
  193. */
  194. public function check(array $check) {
  195. try {
  196. $checkInstance = $this->container->query($check['class']);
  197. } catch (QueryException $e) {
  198. // Check does not exist, assume it matches.
  199. return true;
  200. }
  201. if ($checkInstance instanceof IFileCheck) {
  202. if (empty($this->fileInfo)) {
  203. throw new RuntimeException('Must set file info before running the check');
  204. }
  205. $checkInstance->setFileInfo($this->fileInfo['storage'], $this->fileInfo['path'], $this->fileInfo['isDir']);
  206. } elseif ($checkInstance instanceof IEntityCheck) {
  207. foreach ($this->contexts as $entityInfo) {
  208. [$entity, $subject] = $entityInfo;
  209. $checkInstance->setEntitySubject($entity, $subject);
  210. }
  211. } elseif (!$checkInstance instanceof ICheck) {
  212. // Check is invalid
  213. throw new \UnexpectedValueException($this->l->t('Check %s is invalid or does not exist', $check['class']));
  214. }
  215. return $checkInstance->executeCheck($check['operator'], $check['value']);
  216. }
  217. }