|
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\Check; |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
use OCP\IGroupManager; |
|
26
|
|
|
use OCP\IL10N; |
|
27
|
|
|
use OCP\IUser; |
|
28
|
|
|
use OCP\IUserSession; |
|
29
|
|
|
use OCP\WorkflowEngine\ICheck; |
|
30
|
|
|
use OCP\WorkflowEngine\IManager; |
|
31
|
|
|
|
|
32
|
|
|
class UserGroupMembership implements ICheck { |
|
33
|
|
|
|
|
34
|
|
|
/** @var string */ |
|
35
|
|
|
protected $cachedUser; |
|
36
|
|
|
|
|
37
|
|
|
/** @var string[] */ |
|
38
|
|
|
protected $cachedGroupMemberships; |
|
39
|
|
|
|
|
40
|
|
|
/** @var IUserSession */ |
|
41
|
|
|
protected $userSession; |
|
42
|
|
|
|
|
43
|
|
|
/** @var IGroupManager */ |
|
44
|
|
|
protected $groupManager; |
|
45
|
|
|
|
|
46
|
|
|
/** @var IL10N */ |
|
47
|
|
|
protected $l; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param IUserSession $userSession |
|
51
|
|
|
* @param IGroupManager $groupManager |
|
52
|
|
|
* @param IL10N $l |
|
53
|
|
|
*/ |
|
54
|
|
|
public function __construct(IUserSession $userSession, IGroupManager $groupManager, IL10N $l) { |
|
55
|
|
|
$this->userSession = $userSession; |
|
56
|
|
|
$this->groupManager = $groupManager; |
|
57
|
|
|
$this->l = $l; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param string $operator |
|
62
|
|
|
* @param string $value |
|
63
|
|
|
* @return bool |
|
64
|
|
|
*/ |
|
65
|
|
|
public function executeCheck($operator, $value) { |
|
66
|
|
|
$user = $this->userSession->getUser(); |
|
67
|
|
|
|
|
68
|
|
|
if ($user instanceof IUser) { |
|
69
|
|
|
$groupIds = $this->getUserGroups($user); |
|
70
|
|
|
return ($operator === 'is') === in_array($value, $groupIds); |
|
71
|
|
|
} else { |
|
72
|
|
|
return $operator !== 'is'; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param string $operator |
|
79
|
|
|
* @param string $value |
|
80
|
|
|
* @throws \UnexpectedValueException |
|
81
|
|
|
*/ |
|
82
|
|
|
public function validateCheck($operator, $value) { |
|
83
|
|
|
if (!in_array($operator, ['is', '!is'])) { |
|
84
|
|
|
throw new \UnexpectedValueException($this->l->t('The given operator is invalid'), 1); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
if (!$this->groupManager->groupExists($value)) { |
|
88
|
|
|
throw new \UnexpectedValueException($this->l->t('The given group does not exist'), 2); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @param IUser $user |
|
94
|
|
|
* @return string[] |
|
95
|
|
|
*/ |
|
96
|
|
|
protected function getUserGroups(IUser $user) { |
|
97
|
|
|
$uid = $user->getUID(); |
|
98
|
|
|
|
|
99
|
|
|
if ($this->cachedUser !== $uid) { |
|
100
|
|
|
$this->cachedUser = $uid; |
|
101
|
|
|
$this->cachedGroupMemberships = $this->groupManager->getUserGroupIds($user); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
return $this->cachedGroupMemberships; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
public function supportedEntities(): array { |
|
108
|
|
|
// universal by default |
|
109
|
|
|
return []; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public function isAvailableForScope(int $scope): bool { |
|
113
|
|
|
// admin only by default |
|
114
|
|
|
return $scope === IManager::SCOPE_ADMIN; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|