@@ -34,101 +34,101 @@ |
||
34 | 34 | |
35 | 35 | class Application extends \OCP\AppFramework\App { |
36 | 36 | |
37 | - const APP_ID = 'workflowengine'; |
|
38 | - |
|
39 | - /** @var EventDispatcherInterface */ |
|
40 | - protected $dispatcher; |
|
41 | - /** @var Manager */ |
|
42 | - protected $manager; |
|
43 | - |
|
44 | - public function __construct() { |
|
45 | - parent::__construct(self::APP_ID); |
|
46 | - |
|
47 | - $this->getContainer()->registerAlias('RequestTimeController', RequestTime::class); |
|
48 | - |
|
49 | - $this->dispatcher = $this->getContainer()->getServer()->getEventDispatcher(); |
|
50 | - $this->manager = $this->getContainer()->query(Manager::class); |
|
51 | - } |
|
52 | - |
|
53 | - /** |
|
54 | - * Register all hooks and listeners |
|
55 | - */ |
|
56 | - public function registerHooksAndListeners() { |
|
57 | - $this->dispatcher->addListener( |
|
58 | - 'OCP\WorkflowEngine::loadAdditionalSettingScripts', |
|
59 | - function () { |
|
60 | - if (!function_exists('style')) { |
|
61 | - // This is hacky, but we need to load the template class |
|
62 | - class_exists(Template::class, true); |
|
63 | - } |
|
64 | - |
|
65 | - style(self::APP_ID, [ |
|
66 | - 'admin', |
|
67 | - ]); |
|
68 | - |
|
69 | - script('core', [ |
|
70 | - 'files/fileinfo', |
|
71 | - 'files/client', |
|
72 | - 'systemtags/systemtags', |
|
73 | - 'systemtags/systemtagmodel', |
|
74 | - 'systemtags/systemtagscollection', |
|
75 | - ]); |
|
76 | - |
|
77 | - script(self::APP_ID, [ |
|
78 | - 'workflowengine', |
|
79 | - ]); |
|
80 | - }, |
|
81 | - -100 |
|
82 | - ); |
|
83 | - } |
|
84 | - |
|
85 | - public function registerRuleListeners() { |
|
86 | - $configuredEvents = $this->manager->getAllConfiguredEvents(); |
|
87 | - |
|
88 | - foreach ($configuredEvents as $operationClass => $events) { |
|
89 | - foreach ($events as $entityClass => $eventNames) { |
|
90 | - array_map(function (string $eventName) use ($operationClass, $entityClass) { |
|
91 | - $this->dispatcher->addListener( |
|
92 | - $eventName, |
|
93 | - function ($event) use ($eventName, $operationClass, $entityClass) { |
|
94 | - $ruleMatcher = $this->manager->getRuleMatcher(); |
|
95 | - try { |
|
96 | - /** @var IEntity $entity */ |
|
97 | - $entity = $this->getContainer()->query($entityClass); |
|
98 | - /** @var IOperation $operation */ |
|
99 | - $operation = $this->getContainer()->query($operationClass); |
|
100 | - |
|
101 | - $ruleMatcher->setEntity($entity); |
|
102 | - $ruleMatcher->setOperation($operation); |
|
103 | - |
|
104 | - if ($event instanceof Event) { |
|
105 | - $entity->prepareRuleMatcher($ruleMatcher, $eventName, $event); |
|
106 | - $operation->onEvent($eventName, $event, $ruleMatcher); |
|
107 | - } else if ($entity instanceof IEntityCompat && $operation instanceof IOperationCompat) { |
|
108 | - // TODO: Remove this block (and the compat classes) in the first major release in 2023 |
|
109 | - $entity->prepareRuleMatcherCompat($ruleMatcher, $eventName, $event); |
|
110 | - $operation->onEventCompat($eventName, $event, $ruleMatcher); |
|
111 | - } else { |
|
112 | - $logger = $this->getContainer()->getServer()->getLogger(); |
|
113 | - $logger->debug( |
|
114 | - 'Cannot handle event {name} of {event} against entity {entity} and operation {operation}', |
|
115 | - [ |
|
116 | - 'app' => self::APP_ID, |
|
117 | - 'name' => $eventName, |
|
118 | - 'event' => get_class($event), |
|
119 | - 'entity' => $entityClass, |
|
120 | - 'operation' => $operationClass, |
|
121 | - ] |
|
122 | - ); |
|
123 | - } |
|
124 | - |
|
125 | - } catch (QueryException $e) { |
|
126 | - // Ignore query exceptions since they might occur when an entity/operation were setup before by an app that is disabled now |
|
127 | - } |
|
128 | - } |
|
129 | - ); |
|
130 | - }, $eventNames ?? []); |
|
131 | - } |
|
132 | - } |
|
133 | - } |
|
37 | + const APP_ID = 'workflowengine'; |
|
38 | + |
|
39 | + /** @var EventDispatcherInterface */ |
|
40 | + protected $dispatcher; |
|
41 | + /** @var Manager */ |
|
42 | + protected $manager; |
|
43 | + |
|
44 | + public function __construct() { |
|
45 | + parent::__construct(self::APP_ID); |
|
46 | + |
|
47 | + $this->getContainer()->registerAlias('RequestTimeController', RequestTime::class); |
|
48 | + |
|
49 | + $this->dispatcher = $this->getContainer()->getServer()->getEventDispatcher(); |
|
50 | + $this->manager = $this->getContainer()->query(Manager::class); |
|
51 | + } |
|
52 | + |
|
53 | + /** |
|
54 | + * Register all hooks and listeners |
|
55 | + */ |
|
56 | + public function registerHooksAndListeners() { |
|
57 | + $this->dispatcher->addListener( |
|
58 | + 'OCP\WorkflowEngine::loadAdditionalSettingScripts', |
|
59 | + function () { |
|
60 | + if (!function_exists('style')) { |
|
61 | + // This is hacky, but we need to load the template class |
|
62 | + class_exists(Template::class, true); |
|
63 | + } |
|
64 | + |
|
65 | + style(self::APP_ID, [ |
|
66 | + 'admin', |
|
67 | + ]); |
|
68 | + |
|
69 | + script('core', [ |
|
70 | + 'files/fileinfo', |
|
71 | + 'files/client', |
|
72 | + 'systemtags/systemtags', |
|
73 | + 'systemtags/systemtagmodel', |
|
74 | + 'systemtags/systemtagscollection', |
|
75 | + ]); |
|
76 | + |
|
77 | + script(self::APP_ID, [ |
|
78 | + 'workflowengine', |
|
79 | + ]); |
|
80 | + }, |
|
81 | + -100 |
|
82 | + ); |
|
83 | + } |
|
84 | + |
|
85 | + public function registerRuleListeners() { |
|
86 | + $configuredEvents = $this->manager->getAllConfiguredEvents(); |
|
87 | + |
|
88 | + foreach ($configuredEvents as $operationClass => $events) { |
|
89 | + foreach ($events as $entityClass => $eventNames) { |
|
90 | + array_map(function (string $eventName) use ($operationClass, $entityClass) { |
|
91 | + $this->dispatcher->addListener( |
|
92 | + $eventName, |
|
93 | + function ($event) use ($eventName, $operationClass, $entityClass) { |
|
94 | + $ruleMatcher = $this->manager->getRuleMatcher(); |
|
95 | + try { |
|
96 | + /** @var IEntity $entity */ |
|
97 | + $entity = $this->getContainer()->query($entityClass); |
|
98 | + /** @var IOperation $operation */ |
|
99 | + $operation = $this->getContainer()->query($operationClass); |
|
100 | + |
|
101 | + $ruleMatcher->setEntity($entity); |
|
102 | + $ruleMatcher->setOperation($operation); |
|
103 | + |
|
104 | + if ($event instanceof Event) { |
|
105 | + $entity->prepareRuleMatcher($ruleMatcher, $eventName, $event); |
|
106 | + $operation->onEvent($eventName, $event, $ruleMatcher); |
|
107 | + } else if ($entity instanceof IEntityCompat && $operation instanceof IOperationCompat) { |
|
108 | + // TODO: Remove this block (and the compat classes) in the first major release in 2023 |
|
109 | + $entity->prepareRuleMatcherCompat($ruleMatcher, $eventName, $event); |
|
110 | + $operation->onEventCompat($eventName, $event, $ruleMatcher); |
|
111 | + } else { |
|
112 | + $logger = $this->getContainer()->getServer()->getLogger(); |
|
113 | + $logger->debug( |
|
114 | + 'Cannot handle event {name} of {event} against entity {entity} and operation {operation}', |
|
115 | + [ |
|
116 | + 'app' => self::APP_ID, |
|
117 | + 'name' => $eventName, |
|
118 | + 'event' => get_class($event), |
|
119 | + 'entity' => $entityClass, |
|
120 | + 'operation' => $operationClass, |
|
121 | + ] |
|
122 | + ); |
|
123 | + } |
|
124 | + |
|
125 | + } catch (QueryException $e) { |
|
126 | + // Ignore query exceptions since they might occur when an entity/operation were setup before by an app that is disabled now |
|
127 | + } |
|
128 | + } |
|
129 | + ); |
|
130 | + }, $eventNames ?? []); |
|
131 | + } |
|
132 | + } |
|
133 | + } |
|
134 | 134 | } |
@@ -42,143 +42,143 @@ |
||
42 | 42 | |
43 | 43 | class RuleMatcher implements IRuleMatcher { |
44 | 44 | |
45 | - /** @var IUserSession */ |
|
46 | - protected $session; |
|
47 | - /** @var IManager */ |
|
48 | - protected $manager; |
|
49 | - /** @var array */ |
|
50 | - protected $contexts; |
|
51 | - /** @var IServerContainer */ |
|
52 | - protected $container; |
|
53 | - /** @var array */ |
|
54 | - protected $fileInfo = []; |
|
55 | - /** @var IL10N */ |
|
56 | - protected $l; |
|
57 | - /** @var IOperation */ |
|
58 | - protected $operation; |
|
59 | - /** @var IEntity */ |
|
60 | - protected $entity; |
|
61 | - |
|
62 | - public function __construct( |
|
63 | - IUserSession $session, |
|
64 | - IServerContainer $container, |
|
65 | - IL10N $l, |
|
66 | - Manager $manager |
|
67 | - ) { |
|
68 | - $this->session = $session; |
|
69 | - $this->manager = $manager; |
|
70 | - $this->container = $container; |
|
71 | - $this->l = $l; |
|
72 | - } |
|
73 | - |
|
74 | - public function setFileInfo(IStorage $storage, string $path, bool $isDir = false): void { |
|
75 | - $this->fileInfo['storage'] = $storage; |
|
76 | - $this->fileInfo['path'] = $path; |
|
77 | - $this->fileInfo['isDir'] = $isDir; |
|
78 | - } |
|
79 | - |
|
80 | - public function setEntitySubject(IEntity $entity, $subject): void { |
|
81 | - $this->contexts[get_class($entity)] = [$entity, $subject]; |
|
82 | - } |
|
83 | - |
|
84 | - public function setOperation(IOperation $operation): void { |
|
85 | - if($this->operation !== null) { |
|
86 | - throw new RuntimeException('This method must not be called more than once'); |
|
87 | - } |
|
88 | - $this->operation = $operation; |
|
89 | - } |
|
90 | - |
|
91 | - public function setEntity(IEntity $entity): void { |
|
92 | - if($this->entity !== null) { |
|
93 | - throw new RuntimeException('This method must not be called more than once'); |
|
94 | - } |
|
95 | - $this->entity = $entity; |
|
96 | - } |
|
97 | - |
|
98 | - public function getEntity(): IEntity { |
|
99 | - if($this->entity === null) { |
|
100 | - throw new \LogicException('Entity was not set yet'); |
|
101 | - } |
|
102 | - return $this->entity; |
|
103 | - } |
|
104 | - |
|
105 | - public function getFlows(bool $returnFirstMatchingOperationOnly = true): array { |
|
106 | - if(!$this->operation) { |
|
107 | - throw new RuntimeException('Operation is not set'); |
|
108 | - } |
|
109 | - return $this->getMatchingOperations(get_class($this->operation), $returnFirstMatchingOperationOnly); |
|
110 | - } |
|
111 | - |
|
112 | - public function getMatchingOperations(string $class, bool $returnFirstMatchingOperationOnly = true): array { |
|
113 | - $scopes[] = new ScopeContext(IManager::SCOPE_ADMIN); |
|
114 | - $user = $this->session->getUser(); |
|
115 | - if($user !== null) { |
|
116 | - $scopes[] = new ScopeContext(IManager::SCOPE_USER, $user->getUID()); |
|
117 | - } |
|
118 | - |
|
119 | - $operations = []; |
|
120 | - foreach ($scopes as $scope) { |
|
121 | - $operations = array_merge($operations, $this->manager->getOperations($class, $scope)); |
|
122 | - } |
|
123 | - |
|
124 | - $additionalScopes = $this->manager->getAllConfiguredScopesForOperation($class); |
|
125 | - foreach ($additionalScopes as $hash => $scopeCandidate) { |
|
126 | - /** @var ScopeContext $scopeCandidate */ |
|
127 | - if ($scopeCandidate->getScope() !== IManager::SCOPE_USER || in_array($scopeCandidate, $scopes)) { |
|
128 | - continue; |
|
129 | - } |
|
130 | - if ($this->entity->isLegitimatedForUserId($scopeCandidate->getScopeId())) { |
|
131 | - $operations = array_merge($operations, $this->manager->getOperations($class, $scopeCandidate)); |
|
132 | - } |
|
133 | - } |
|
134 | - |
|
135 | - $matches = []; |
|
136 | - foreach ($operations as $operation) { |
|
137 | - $checkIds = json_decode($operation['checks'], true); |
|
138 | - $checks = $this->manager->getChecks($checkIds); |
|
139 | - |
|
140 | - foreach ($checks as $check) { |
|
141 | - if (!$this->check($check)) { |
|
142 | - // Check did not match, continue with the next operation |
|
143 | - continue 2; |
|
144 | - } |
|
145 | - } |
|
146 | - |
|
147 | - if ($returnFirstMatchingOperationOnly) { |
|
148 | - return $operation; |
|
149 | - } |
|
150 | - $matches[] = $operation; |
|
151 | - } |
|
152 | - |
|
153 | - return $matches; |
|
154 | - } |
|
155 | - |
|
156 | - /** |
|
157 | - * @param array $check |
|
158 | - * @return bool |
|
159 | - */ |
|
160 | - public function check(array $check) { |
|
161 | - try { |
|
162 | - $checkInstance = $this->container->query($check['class']); |
|
163 | - } catch (QueryException $e) { |
|
164 | - // Check does not exist, assume it matches. |
|
165 | - return true; |
|
166 | - } |
|
167 | - |
|
168 | - if ($checkInstance instanceof IFileCheck) { |
|
169 | - if (empty($this->fileInfo)) { |
|
170 | - throw new RuntimeException('Must set file info before running the check'); |
|
171 | - } |
|
172 | - $checkInstance->setFileInfo($this->fileInfo['storage'], $this->fileInfo['path'], $this->fileInfo['isDir']); |
|
173 | - } elseif ($checkInstance instanceof IEntityCheck) { |
|
174 | - foreach($this->contexts as $entityInfo) { |
|
175 | - list($entity, $subject) = $entityInfo; |
|
176 | - $checkInstance->setEntitySubject($entity, $subject); |
|
177 | - } |
|
178 | - } else if(!$checkInstance instanceof ICheck) { |
|
179 | - // Check is invalid |
|
180 | - throw new \UnexpectedValueException($this->l->t('Check %s is invalid or does not exist', $check['class'])); |
|
181 | - } |
|
182 | - return $checkInstance->executeCheck($check['operator'], $check['value']); |
|
183 | - } |
|
45 | + /** @var IUserSession */ |
|
46 | + protected $session; |
|
47 | + /** @var IManager */ |
|
48 | + protected $manager; |
|
49 | + /** @var array */ |
|
50 | + protected $contexts; |
|
51 | + /** @var IServerContainer */ |
|
52 | + protected $container; |
|
53 | + /** @var array */ |
|
54 | + protected $fileInfo = []; |
|
55 | + /** @var IL10N */ |
|
56 | + protected $l; |
|
57 | + /** @var IOperation */ |
|
58 | + protected $operation; |
|
59 | + /** @var IEntity */ |
|
60 | + protected $entity; |
|
61 | + |
|
62 | + public function __construct( |
|
63 | + IUserSession $session, |
|
64 | + IServerContainer $container, |
|
65 | + IL10N $l, |
|
66 | + Manager $manager |
|
67 | + ) { |
|
68 | + $this->session = $session; |
|
69 | + $this->manager = $manager; |
|
70 | + $this->container = $container; |
|
71 | + $this->l = $l; |
|
72 | + } |
|
73 | + |
|
74 | + public function setFileInfo(IStorage $storage, string $path, bool $isDir = false): void { |
|
75 | + $this->fileInfo['storage'] = $storage; |
|
76 | + $this->fileInfo['path'] = $path; |
|
77 | + $this->fileInfo['isDir'] = $isDir; |
|
78 | + } |
|
79 | + |
|
80 | + public function setEntitySubject(IEntity $entity, $subject): void { |
|
81 | + $this->contexts[get_class($entity)] = [$entity, $subject]; |
|
82 | + } |
|
83 | + |
|
84 | + public function setOperation(IOperation $operation): void { |
|
85 | + if($this->operation !== null) { |
|
86 | + throw new RuntimeException('This method must not be called more than once'); |
|
87 | + } |
|
88 | + $this->operation = $operation; |
|
89 | + } |
|
90 | + |
|
91 | + public function setEntity(IEntity $entity): void { |
|
92 | + if($this->entity !== null) { |
|
93 | + throw new RuntimeException('This method must not be called more than once'); |
|
94 | + } |
|
95 | + $this->entity = $entity; |
|
96 | + } |
|
97 | + |
|
98 | + public function getEntity(): IEntity { |
|
99 | + if($this->entity === null) { |
|
100 | + throw new \LogicException('Entity was not set yet'); |
|
101 | + } |
|
102 | + return $this->entity; |
|
103 | + } |
|
104 | + |
|
105 | + public function getFlows(bool $returnFirstMatchingOperationOnly = true): array { |
|
106 | + if(!$this->operation) { |
|
107 | + throw new RuntimeException('Operation is not set'); |
|
108 | + } |
|
109 | + return $this->getMatchingOperations(get_class($this->operation), $returnFirstMatchingOperationOnly); |
|
110 | + } |
|
111 | + |
|
112 | + public function getMatchingOperations(string $class, bool $returnFirstMatchingOperationOnly = true): array { |
|
113 | + $scopes[] = new ScopeContext(IManager::SCOPE_ADMIN); |
|
114 | + $user = $this->session->getUser(); |
|
115 | + if($user !== null) { |
|
116 | + $scopes[] = new ScopeContext(IManager::SCOPE_USER, $user->getUID()); |
|
117 | + } |
|
118 | + |
|
119 | + $operations = []; |
|
120 | + foreach ($scopes as $scope) { |
|
121 | + $operations = array_merge($operations, $this->manager->getOperations($class, $scope)); |
|
122 | + } |
|
123 | + |
|
124 | + $additionalScopes = $this->manager->getAllConfiguredScopesForOperation($class); |
|
125 | + foreach ($additionalScopes as $hash => $scopeCandidate) { |
|
126 | + /** @var ScopeContext $scopeCandidate */ |
|
127 | + if ($scopeCandidate->getScope() !== IManager::SCOPE_USER || in_array($scopeCandidate, $scopes)) { |
|
128 | + continue; |
|
129 | + } |
|
130 | + if ($this->entity->isLegitimatedForUserId($scopeCandidate->getScopeId())) { |
|
131 | + $operations = array_merge($operations, $this->manager->getOperations($class, $scopeCandidate)); |
|
132 | + } |
|
133 | + } |
|
134 | + |
|
135 | + $matches = []; |
|
136 | + foreach ($operations as $operation) { |
|
137 | + $checkIds = json_decode($operation['checks'], true); |
|
138 | + $checks = $this->manager->getChecks($checkIds); |
|
139 | + |
|
140 | + foreach ($checks as $check) { |
|
141 | + if (!$this->check($check)) { |
|
142 | + // Check did not match, continue with the next operation |
|
143 | + continue 2; |
|
144 | + } |
|
145 | + } |
|
146 | + |
|
147 | + if ($returnFirstMatchingOperationOnly) { |
|
148 | + return $operation; |
|
149 | + } |
|
150 | + $matches[] = $operation; |
|
151 | + } |
|
152 | + |
|
153 | + return $matches; |
|
154 | + } |
|
155 | + |
|
156 | + /** |
|
157 | + * @param array $check |
|
158 | + * @return bool |
|
159 | + */ |
|
160 | + public function check(array $check) { |
|
161 | + try { |
|
162 | + $checkInstance = $this->container->query($check['class']); |
|
163 | + } catch (QueryException $e) { |
|
164 | + // Check does not exist, assume it matches. |
|
165 | + return true; |
|
166 | + } |
|
167 | + |
|
168 | + if ($checkInstance instanceof IFileCheck) { |
|
169 | + if (empty($this->fileInfo)) { |
|
170 | + throw new RuntimeException('Must set file info before running the check'); |
|
171 | + } |
|
172 | + $checkInstance->setFileInfo($this->fileInfo['storage'], $this->fileInfo['path'], $this->fileInfo['isDir']); |
|
173 | + } elseif ($checkInstance instanceof IEntityCheck) { |
|
174 | + foreach($this->contexts as $entityInfo) { |
|
175 | + list($entity, $subject) = $entityInfo; |
|
176 | + $checkInstance->setEntitySubject($entity, $subject); |
|
177 | + } |
|
178 | + } else if(!$checkInstance instanceof ICheck) { |
|
179 | + // Check is invalid |
|
180 | + throw new \UnexpectedValueException($this->l->t('Check %s is invalid or does not exist', $check['class'])); |
|
181 | + } |
|
182 | + return $checkInstance->executeCheck($check['operator'], $check['value']); |
|
183 | + } |
|
184 | 184 | } |