RuleMatcher.php 7.3 KB

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