GroupPlugin::search()   F
last analyzed

Complexity

Conditions 21
Paths 145

Size

Total Lines 83
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 21
eloc 51
nc 145
nop 4
dl 0
loc 83
rs 3.7916
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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