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