Completed
Push — master ( 72ae75...318d68 )
by Joas
36:13 queued 23:35
created

UserGroupMembership::getUserGroups()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 6
c 1
b 0
f 1
nc 2
nop 1
dl 0
loc 10
rs 9.4285
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\Files\Storage\IStorage;
26
use OCP\IGroupManager;
27
use OCP\IUser;
28
use OCP\IUserSession;
29
use OCP\WorkflowEngine\ICheck;
30
31
class UserGroupMembership implements ICheck {
32
33
	/** @var string */
34
	protected $cachedUser;
35
36
	/** @var string[] */
37
	protected $cachedGroupMemberships;
38
39
	/** @var IUserSession */
40
	protected $userSession;
41
42
	/** @var IGroupManager */
43
	protected $groupManager;
44
45
	/**
46
	 * @param IUserSession $userSession
47
	 * @param IGroupManager $groupManager
48
	 */
49
	public function __construct(IUserSession $userSession, IGroupManager $groupManager) {
50
		$this->userSession = $userSession;
51
		$this->groupManager = $groupManager;
52
	}
53
54
	/**
55
	 * @param IStorage $storage
56
	 * @param string $path
57
	 */
58
	public function setFileInfo(IStorage $storage, $path) {
59
		// A different path doesn't change group memberships, so nothing to do here.
60
	}
61
62
	/**
63
	 * @param string $operator
64
	 * @param string $value
65
	 * @return bool
66
	 */
67
	public function executeCheck($operator, $value) {
68
		$user = $this->userSession->getUser();
69
70
		if ($user instanceof IUser) {
71
			$groupIds = $this->getUserGroups($user);
72
			return ($operator === 'is') === in_array($value, $groupIds);
73
		} else {
74
			return $operator !== 'is';
75
		}
76
	}
77
78
79
	/**
80
	 * @param string $operator
81
	 * @param string $value
82
	 * @throws \UnexpectedValueException
83
	 */
84
	public function validateCheck($operator, $value) {
85
		if (!in_array($operator, ['is', '!is'])) {
86
			throw new \UnexpectedValueException('Invalid operator', 1);
87
		}
88
89
		if (!$this->groupManager->groupExists($value)) {
90
			throw new \UnexpectedValueException('Group does not exist', 2);
91
		}
92
	}
93
94
	/**
95
	 * @param IUser $user
96
	 * @return string[]
97
	 */
98
	protected function getUserGroups(IUser $user) {
99
		$uid = $user->getUID();
100
101
		if ($this->cachedUser !== $uid) {
102
			$this->cachedUser = $uid;
103
			$this->cachedGroupMemberships = $this->groupManager->getUserGroupIds($user);
104
		}
105
106
		return $this->cachedGroupMemberships;
107
	}
108
}
109