1<?php
2/**
3 * @copyright Copyright (c) 2016 Morris Jobke <hey@morrisjobke.de>
4 *
5 * @author Joas Schilling <coding@schilljs.com>
6 * @author Julius Härtl <jus@bitgrid.net>
7 * @author Morris Jobke <hey@morrisjobke.de>
8 *
9 * @license GNU AGPL version 3 or any later version
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Affero General Public License as
13 * published by the Free Software Foundation, either version 3 of the
14 * License, or (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Affero General Public License for more details.
20 *
21 * You should have received a copy of the GNU Affero General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 *
24 */
25namespace OCA\WorkflowEngine\Check;
26
27use OCP\IGroupManager;
28use OCP\IL10N;
29use OCP\IUser;
30use OCP\IUserSession;
31use OCP\WorkflowEngine\ICheck;
32use OCP\WorkflowEngine\IManager;
33
34class UserGroupMembership implements ICheck {
35
36	/** @var string */
37	protected $cachedUser;
38
39	/** @var string[] */
40	protected $cachedGroupMemberships;
41
42	/** @var IUserSession */
43	protected $userSession;
44
45	/** @var IGroupManager */
46	protected $groupManager;
47
48	/** @var IL10N */
49	protected $l;
50
51	/**
52	 * @param IUserSession $userSession
53	 * @param IGroupManager $groupManager
54	 * @param IL10N $l
55	 */
56	public function __construct(IUserSession $userSession, IGroupManager $groupManager, IL10N $l) {
57		$this->userSession = $userSession;
58		$this->groupManager = $groupManager;
59		$this->l = $l;
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($this->l->t('The given operator is invalid'), 1);
87		}
88
89		if (!$this->groupManager->groupExists($value)) {
90			throw new \UnexpectedValueException($this->l->t('The given 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	public function supportedEntities(): array {
110		// universal by default
111		return [];
112	}
113
114	public function isAvailableForScope(int $scope): bool {
115		// admin only by default
116		return $scope === IManager::SCOPE_ADMIN;
117	}
118}
119