1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Copyright (c) 2016 Morris Jobke <[email protected]> |
4
|
|
|
* |
5
|
|
|
* @license GNU AGPL version 3 or any later version |
6
|
|
|
* |
7
|
|
|
* This program is free software: you can redistribute it and/or modify |
8
|
|
|
* it under the terms of the GNU Affero General Public License as |
9
|
|
|
* published by the Free Software Foundation, either version 3 of the |
10
|
|
|
* License, or (at your option) any later version. |
11
|
|
|
* |
12
|
|
|
* This program is distributed in the hope that it will be useful, |
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15
|
|
|
* GNU Affero General Public License for more details. |
16
|
|
|
* |
17
|
|
|
* You should have received a copy of the GNU Affero General Public License |
18
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
19
|
|
|
* |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
namespace OCA\WorkflowEngine; |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
use OCP\AppFramework\QueryException; |
26
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder; |
27
|
|
|
use OCP\Files\Storage\IStorage; |
28
|
|
|
use OCP\IDBConnection; |
29
|
|
|
use OCP\IL10N; |
30
|
|
|
use OCP\IServerContainer; |
31
|
|
|
use OCP\WorkflowEngine\ICheck; |
32
|
|
|
use OCP\WorkflowEngine\IManager; |
33
|
|
|
|
34
|
|
|
class Manager implements IManager { |
35
|
|
|
|
36
|
|
|
/** @var IStorage */ |
37
|
|
|
protected $storage; |
38
|
|
|
|
39
|
|
|
/** @var string */ |
40
|
|
|
protected $path; |
41
|
|
|
|
42
|
|
|
/** @var array[] */ |
43
|
|
|
protected $operations = []; |
44
|
|
|
|
45
|
|
|
/** @var array[] */ |
46
|
|
|
protected $checks = []; |
47
|
|
|
|
48
|
|
|
/** @var IDBConnection */ |
49
|
|
|
protected $connection; |
50
|
|
|
|
51
|
|
|
/** @var IServerContainer|\OC\Server */ |
52
|
|
|
protected $container; |
53
|
|
|
|
54
|
|
|
/** @var IL10N */ |
55
|
|
|
protected $l; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param IDBConnection $connection |
59
|
|
|
* @param IServerContainer $container |
60
|
|
|
* @param IL10N $l |
61
|
|
|
*/ |
62
|
|
|
public function __construct(IDBConnection $connection, IServerContainer $container, IL10N $l) { |
63
|
|
|
$this->connection = $connection; |
64
|
|
|
$this->container = $container; |
65
|
|
|
$this->l = $l; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @inheritdoc |
70
|
|
|
*/ |
71
|
|
|
public function setFileInfo(IStorage $storage, $path) { |
72
|
|
|
$this->storage = $storage; |
73
|
|
|
$this->path = $path; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @inheritdoc |
78
|
|
|
*/ |
79
|
|
|
public function getMatchingOperations($class, $returnFirstMatchingOperationOnly = true) { |
80
|
|
|
$operations = $this->getOperations($class); |
81
|
|
|
|
82
|
|
|
$matches = []; |
83
|
|
|
foreach ($operations as $operation) { |
84
|
|
|
$checkIds = json_decode($operation['checks'], true); |
85
|
|
|
$checks = $this->getChecks($checkIds); |
86
|
|
|
|
87
|
|
|
foreach ($checks as $check) { |
88
|
|
|
if (!$this->check($check)) { |
89
|
|
|
// Check did not match, continue with the next operation |
90
|
|
|
continue 2; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
if ($returnFirstMatchingOperationOnly) { |
95
|
|
|
return $operation; |
96
|
|
|
} |
97
|
|
|
$matches[] = $operation; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $matches; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param array $check |
105
|
|
|
* @return bool |
106
|
|
|
*/ |
107
|
|
|
protected function check(array $check) { |
108
|
|
|
try { |
109
|
|
|
$checkInstance = $this->container->query($check['class']); |
|
|
|
|
110
|
|
|
} catch (QueryException $e) { |
111
|
|
|
// Check does not exist, assume it matches. |
112
|
|
|
return true; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
if ($checkInstance instanceof ICheck) { |
116
|
|
|
$checkInstance->setFileInfo($this->storage, $this->path); |
117
|
|
|
return $checkInstance->executeCheck($check['operator'], $check['value']); |
118
|
|
|
} else { |
119
|
|
|
// Check is invalid |
120
|
|
|
throw new \UnexpectedValueException($this->l->t('Check %s is invalid or does not exist', $check['class'])); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param string $class |
126
|
|
|
* @return array[] |
127
|
|
|
*/ |
128
|
|
|
public function getOperations($class) { |
129
|
|
|
if (isset($this->operations[$class])) { |
130
|
|
|
return $this->operations[$class]; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
$query = $this->connection->getQueryBuilder(); |
134
|
|
|
|
135
|
|
|
$query->select('*') |
136
|
|
|
->from('flow_operations') |
137
|
|
|
->where($query->expr()->eq('class', $query->createNamedParameter($class))); |
138
|
|
|
$result = $query->execute(); |
139
|
|
|
|
140
|
|
|
$this->operations[$class] = []; |
141
|
|
|
while ($row = $result->fetch()) { |
142
|
|
|
$this->operations[$class][] = $row; |
143
|
|
|
} |
144
|
|
|
$result->closeCursor(); |
145
|
|
|
|
146
|
|
|
return $this->operations[$class]; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param int $id |
151
|
|
|
* @return array |
152
|
|
|
* @throws \UnexpectedValueException |
153
|
|
|
*/ |
154
|
|
View Code Duplication |
protected function getOperation($id) { |
|
|
|
|
155
|
|
|
$query = $this->connection->getQueryBuilder(); |
156
|
|
|
$query->select('*') |
157
|
|
|
->from('flow_operations') |
158
|
|
|
->where($query->expr()->eq('id', $query->createNamedParameter($id))); |
159
|
|
|
$result = $query->execute(); |
160
|
|
|
$row = $result->fetch(); |
161
|
|
|
$result->closeCursor(); |
162
|
|
|
|
163
|
|
|
if ($row) { |
164
|
|
|
return $row; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
throw new \UnexpectedValueException($this->l->t('Operation #%s does not exist', $id)); |
|
|
|
|
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @param string $class |
172
|
|
|
* @param string $name |
173
|
|
|
* @param array[] $checks |
174
|
|
|
* @param string $operation |
175
|
|
|
* @return array The added operation |
176
|
|
|
* @throws \UnexpectedValueException |
177
|
|
|
*/ |
178
|
|
|
public function addOperation($class, $name, array $checks, $operation) { |
179
|
|
|
$checkIds = []; |
180
|
|
View Code Duplication |
foreach ($checks as $check) { |
|
|
|
|
181
|
|
|
$checkIds[] = $this->addCheck($check['class'], $check['operator'], $check['value']); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
$query = $this->connection->getQueryBuilder(); |
185
|
|
|
$query->insert('flow_operations') |
186
|
|
|
->values([ |
187
|
|
|
'class' => $query->createNamedParameter($class), |
188
|
|
|
'name' => $query->createNamedParameter($name), |
189
|
|
|
'checks' => $query->createNamedParameter(json_encode(array_unique($checkIds))), |
190
|
|
|
'operation' => $query->createNamedParameter($operation), |
191
|
|
|
]); |
192
|
|
|
$query->execute(); |
193
|
|
|
|
194
|
|
|
$id = $query->getLastInsertId(); |
195
|
|
|
return $this->getOperation($id); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @param int $id |
200
|
|
|
* @param string $name |
201
|
|
|
* @param array[] $checks |
202
|
|
|
* @param string $operation |
203
|
|
|
* @return array The updated operation |
204
|
|
|
* @throws \UnexpectedValueException |
205
|
|
|
*/ |
206
|
|
|
public function updateOperation($id, $name, array $checks, $operation) { |
207
|
|
|
$checkIds = []; |
208
|
|
View Code Duplication |
foreach ($checks as $check) { |
|
|
|
|
209
|
|
|
$checkIds[] = $this->addCheck($check['class'], $check['operator'], $check['value']); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
$query = $this->connection->getQueryBuilder(); |
213
|
|
|
$query->update('flow_operations') |
214
|
|
|
->set('name', $query->createNamedParameter($name)) |
215
|
|
|
->set('checks', $query->createNamedParameter(json_encode(array_unique($checkIds)))) |
216
|
|
|
->set('operation', $query->createNamedParameter($operation)) |
217
|
|
|
->where($query->expr()->eq('id', $query->createNamedParameter($id))); |
218
|
|
|
$query->execute(); |
219
|
|
|
|
220
|
|
|
return $this->getOperation($id); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* @param int $id |
225
|
|
|
* @return bool |
226
|
|
|
* @throws \UnexpectedValueException |
227
|
|
|
*/ |
228
|
|
|
public function deleteOperation($id) { |
229
|
|
|
$query = $this->connection->getQueryBuilder(); |
230
|
|
|
$query->delete('flow_operations') |
231
|
|
|
->where($query->expr()->eq('id', $query->createNamedParameter($id))); |
232
|
|
|
return (bool) $query->execute(); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* @param int[] $checkIds |
237
|
|
|
* @return array[] |
238
|
|
|
*/ |
239
|
|
|
public function getChecks(array $checkIds) { |
240
|
|
|
$checkIds = array_map('intval', $checkIds); |
241
|
|
|
|
242
|
|
|
$checks = []; |
243
|
|
|
foreach ($checkIds as $i => $checkId) { |
244
|
|
|
if (isset($this->checks[$checkId])) { |
245
|
|
|
$checks[$checkId] = $this->checks[$checkId]; |
246
|
|
|
unset($checkIds[$i]); |
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
if (empty($checkIds)) { |
251
|
|
|
return $checks; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
$query = $this->connection->getQueryBuilder(); |
255
|
|
|
$query->select('*') |
256
|
|
|
->from('flow_checks') |
257
|
|
|
->where($query->expr()->in('id', $query->createNamedParameter($checkIds, IQueryBuilder::PARAM_INT_ARRAY))); |
258
|
|
|
$result = $query->execute(); |
259
|
|
|
|
260
|
|
|
$checks = []; |
261
|
|
|
while ($row = $result->fetch()) { |
262
|
|
|
$this->checks[(int) $row['id']] = $row; |
263
|
|
|
$checks[(int) $row['id']] = $row; |
264
|
|
|
} |
265
|
|
|
$result->closeCursor(); |
266
|
|
|
|
267
|
|
|
$checkIds = array_diff($checkIds, array_keys($checks)); |
268
|
|
|
|
269
|
|
|
if (!empty($checkIds)) { |
270
|
|
|
$missingCheck = array_pop($checkIds); |
271
|
|
|
throw new \UnexpectedValueException($this->l->t('Check #%s does not exist', $missingCheck)); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
return $checks; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* @param string $class |
279
|
|
|
* @param string $operator |
280
|
|
|
* @param string $value |
281
|
|
|
* @return int Check unique ID |
282
|
|
|
* @throws \UnexpectedValueException |
283
|
|
|
*/ |
284
|
|
|
protected function addCheck($class, $operator, $value) { |
285
|
|
|
/** @var ICheck $check */ |
286
|
|
|
$check = $this->container->query($class); |
|
|
|
|
287
|
|
|
$check->validateCheck($operator, $value); |
288
|
|
|
|
289
|
|
|
$hash = md5($class . '::' . $operator . '::' . $value); |
290
|
|
|
|
291
|
|
|
$query = $this->connection->getQueryBuilder(); |
292
|
|
|
$query->select('id') |
293
|
|
|
->from('flow_checks') |
294
|
|
|
->where($query->expr()->eq('hash', $query->createNamedParameter($hash))); |
295
|
|
|
$result = $query->execute(); |
296
|
|
|
|
297
|
|
|
if ($row = $result->fetch()) { |
298
|
|
|
$result->closeCursor(); |
299
|
|
|
return (int) $row['id']; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
$query = $this->connection->getQueryBuilder(); |
303
|
|
|
$query->insert('flow_checks') |
304
|
|
|
->values([ |
305
|
|
|
'class' => $query->createNamedParameter($class), |
306
|
|
|
'operator' => $query->createNamedParameter($operator), |
307
|
|
|
'value' => $query->createNamedParameter($value), |
308
|
|
|
'hash' => $query->createNamedParameter($hash), |
309
|
|
|
]); |
310
|
|
|
$query->execute(); |
311
|
|
|
|
312
|
|
|
return $query->getLastInsertId(); |
313
|
|
|
} |
314
|
|
|
} |
315
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: