1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) 2019 Arthur Schiwon <[email protected]> |
5
|
|
|
* |
6
|
|
|
* @author Arthur Schiwon <[email protected]> |
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
|
|
|
|
25
|
|
|
namespace OCA\WorkflowEngine\Service; |
26
|
|
|
|
27
|
|
|
use OCA\WorkflowEngine\AppInfo\Application; |
28
|
|
|
use OCA\WorkflowEngine\Entity\File; |
29
|
|
|
use OCA\WorkflowEngine\Helper\ScopeContext; |
30
|
|
|
use OCA\WorkflowEngine\Manager; |
31
|
|
|
use OCP\AppFramework\QueryException; |
32
|
|
|
use OCP\Files\Node; |
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\IRuleMatcher; |
43
|
|
|
|
44
|
|
|
class RuleMatcher implements IRuleMatcher { |
45
|
|
|
|
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
|
|
|
|
59
|
|
|
public function __construct(IUserSession $session, IServerContainer $container, IL10N $l, Manager $manager) { |
60
|
|
|
$this->session = $session; |
61
|
|
|
$this->manager = $manager; |
62
|
|
|
$this->container = $container; |
63
|
|
|
$this->l = $l; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function setFileInfo(IStorage $storage, string $path): void { |
67
|
|
|
$this->fileInfo['storage'] = $storage; |
68
|
|
|
$this->fileInfo['path'] = $path; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
|
72
|
|
|
public function setEntitySubject(IEntity $entity, $subject): void { |
73
|
|
|
$this->contexts[get_class($entity)] = [$entity, $subject]; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function getMatchingOperations(string $class, bool $returnFirstMatchingOperationOnly = true): array { |
77
|
|
|
$scopes[] = new ScopeContext(IManager::SCOPE_ADMIN); |
|
|
|
|
78
|
|
|
$user = $this->session->getUser(); |
79
|
|
|
if($user !== null) { |
80
|
|
|
$scopes[] = new ScopeContext(IManager::SCOPE_USER, $user->getUID()); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$operations = []; |
84
|
|
|
foreach ($scopes as $scope) { |
85
|
|
|
$operations = array_merge($operations, $this->manager->getOperations($class, $scope)); |
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$matches = []; |
89
|
|
|
foreach ($operations as $operation) { |
90
|
|
|
$checkIds = json_decode($operation['checks'], true); |
91
|
|
|
$checks = $this->manager->getChecks($checkIds); |
|
|
|
|
92
|
|
|
|
93
|
|
|
foreach ($checks as $check) { |
94
|
|
|
if (!$this->check($check)) { |
95
|
|
|
// Check did not match, continue with the next operation |
96
|
|
|
continue 2; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
if ($returnFirstMatchingOperationOnly) { |
101
|
|
|
return $operation; |
102
|
|
|
} |
103
|
|
|
$matches[] = $operation; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $matches; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param array $check |
111
|
|
|
* @return bool |
112
|
|
|
*/ |
113
|
|
|
public function check(array $check) { |
114
|
|
|
try { |
115
|
|
|
$checkInstance = $this->container->query($check['class']); |
116
|
|
|
} catch (QueryException $e) { |
117
|
|
|
// Check does not exist, assume it matches. |
118
|
|
|
return true; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
if ($checkInstance instanceof IFileCheck) { |
122
|
|
|
if (empty($this->fileInfo)) { |
123
|
|
|
throw new \RuntimeException('Must set file info before running the check'); |
124
|
|
|
} |
125
|
|
|
$checkInstance->setFileInfo($this->fileInfo['storage'], $this->fileInfo['path']); |
126
|
|
|
} elseif ($checkInstance instanceof IEntityCheck) { |
127
|
|
|
foreach($this->contexts as $entityInfo) { |
128
|
|
|
list($entity, $subject) = $entityInfo; |
129
|
|
|
$checkInstance->setEntitySubject($entity, $subject); |
130
|
|
|
} |
131
|
|
|
} else { |
132
|
|
|
// Check is invalid |
133
|
|
|
throw new \UnexpectedValueException($this->l->t('Check %s is invalid or does not exist', $check['class'])); |
134
|
|
|
} |
135
|
|
|
return $checkInstance->executeCheck($check['operator'], $check['value']); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|