Completed
Push — stable10 ( d896d4...713e20 )
by Joas
26:40 queued 16:05
created

UserGroupMembership::executeCheck()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 7
c 1
b 0
f 1
nc 2
nop 2
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\IL10N;
28
use OCP\IUser;
29
use OCP\IUserSession;
30
use OCP\WorkflowEngine\ICheck;
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 IStorage $storage
62
	 * @param string $path
63
	 */
64
	public function setFileInfo(IStorage $storage, $path) {
65
		// A different path doesn't change group memberships, so nothing to do here.
66
	}
67
68
	/**
69
	 * @param string $operator
70
	 * @param string $value
71
	 * @return bool
72
	 */
73
	public function executeCheck($operator, $value) {
74
		$user = $this->userSession->getUser();
75
76
		if ($user instanceof IUser) {
77
			$groupIds = $this->getUserGroups($user);
78
			return ($operator === 'is') === in_array($value, $groupIds);
79
		} else {
80
			return $operator !== 'is';
81
		}
82
	}
83
84
85
	/**
86
	 * @param string $operator
87
	 * @param string $value
88
	 * @throws \UnexpectedValueException
89
	 */
90
	public function validateCheck($operator, $value) {
91 View Code Duplication
		if (!in_array($operator, ['is', '!is'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
92
			throw new \UnexpectedValueException($this->l->t('The given operator is invalid'), 1);
93
		}
94
95
		if (!$this->groupManager->groupExists($value)) {
96
			throw new \UnexpectedValueException($this->l->t('The given group does not exist'), 2);
97
		}
98
	}
99
100
	/**
101
	 * @param IUser $user
102
	 * @return string[]
103
	 */
104
	protected function getUserGroups(IUser $user) {
105
		$uid = $user->getUID();
106
107
		if ($this->cachedUser !== $uid) {
108
			$this->cachedUser = $uid;
109
			$this->cachedGroupMemberships = $this->groupManager->getUserGroupIds($user);
110
		}
111
112
		return $this->cachedGroupMemberships;
113
	}
114
}
115