Completed
Push — master ( 5412c2...2d62f9 )
by Blizzz
45:23 queued 29:26
created

GroupPlugin::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 3
dl 8
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Arthur Schiwon <[email protected]>
4
 *
5
 * @author Arthur Schiwon <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OC\Collaboration\Collaborators;
25
26
use OCP\Collaboration\Collaborators\ISearchPlugin;
27
use OCP\Collaboration\Collaborators\ISearchResult;
28
use OCP\Collaboration\Collaborators\SearchResultType;
29
use OCP\IConfig;
30
use OCP\IGroup;
31
use OCP\IGroupManager;
32
use OCP\IUserSession;
33
use OCP\Share;
34
35
class GroupPlugin implements ISearchPlugin {
36
	protected $shareeEnumeration;
37
	protected $shareWithGroupOnly;
38
39
	/** @var IGroupManager */
40
	private $groupManager;
41
	/** @var IConfig */
42
	private $config;
43
	/** @var IUserSession */
44
	private $userSession;
45
46 View Code Duplication
	public function __construct(IConfig $config, IGroupManager $groupManager, IUserSession $userSession) {
47
		$this->groupManager = $groupManager;
48
		$this->config = $config;
49
		$this->userSession = $userSession;
50
51
		$this->shareeEnumeration = $this->config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes') === 'yes';
52
		$this->shareWithGroupOnly = $this->config->getAppValue('core', 'shareapi_only_share_with_group_members', 'no') === 'yes';
53
	}
54
55
	public function search($search, $limit, $offset, ISearchResult $searchResult) {
56
		$hasMoreResults = false;
57
		$result = ['wide' => [], 'exact' => []];
58
59
		$groups = $this->groupManager->search($search, $limit, $offset);
60
		$groupIds = array_map(function (IGroup $group) { return $group->getGID(); }, $groups);
61
62
		if (!$this->shareeEnumeration || sizeof($groups) < $limit) {
63
			$hasMoreResults = true;
64
		}
65
66
		$userGroups =  [];
67
		if (!empty($groups) && $this->shareWithGroupOnly) {
68
			// Intersect all the groups that match with the groups this user is a member of
69
			$userGroups = $this->groupManager->getUserGroups($this->userSession->getUser());
70
			$userGroups = array_map(function (IGroup $group) { return $group->getGID(); }, $userGroups);
71
			$groupIds = array_intersect($groupIds, $userGroups);
72
		}
73
74
		$lowerSearch = strtolower($search);
75
		foreach ($groups as $group) {
76
			// FIXME: use a more efficient approach
77
			$gid = $group->getGID();
78
			if (!in_array($gid, $groupIds)) {
79
				continue;
80
			}
81
			if (strtolower($gid) === $lowerSearch || strtolower($group->getDisplayName()) === $lowerSearch) {
82
				$result['exact'][] = [
83
					'label' => $group->getDisplayName(),
84
					'value' => [
85
						'shareType' => Share::SHARE_TYPE_GROUP,
86
						'shareWith' => $gid,
87
					],
88
				];
89
			} else {
90
				$result['wide'][] = [
91
					'label' => $group->getDisplayName(),
92
					'value' => [
93
						'shareType' => Share::SHARE_TYPE_GROUP,
94
						'shareWith' => $gid,
95
					],
96
				];
97
			}
98
		}
99
100
		if ($offset === 0 && empty($result['exact'])) {
101
			// On page one we try if the search result has a direct hit on the
102
			// user id and if so, we add that to the exact match list
103
			$group = $this->groupManager->get($search);
104
			if ($group instanceof IGroup && (!$this->shareWithGroupOnly || in_array($group->getGID(), $userGroups))) {
105
				array_push($result['exact'], [
106
					'label' => $group->getDisplayName(),
107
					'value' => [
108
						'shareType' => Share::SHARE_TYPE_GROUP,
109
						'shareWith' => $group->getGID(),
110
					],
111
				]);
112
			}
113
		}
114
115
		if (!$this->shareeEnumeration) {
116
			$result['wide'] = [];
117
		}
118
119
		$type = new SearchResultType('groups');
120
		$searchResult->addResultSet($type, $result['wide'], $result['exact']);
121
122
		return $hasMoreResults;
123
	}
124
}
125