Passed
Push — master ( 68a02b...b29c3a )
by Christoph
15:41 queued 11s
created

GroupPlugin::search()   F

Complexity

Conditions 20
Paths 145

Size

Total Lines 83
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 20
eloc 51
nc 145
nop 4
dl 0
loc 83
rs 3.7916
c 1
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
29
namespace OC\Collaboration\Collaborators;
30
31
use OCP\Collaboration\Collaborators\ISearchPlugin;
32
use OCP\Collaboration\Collaborators\ISearchResult;
33
use OCP\Collaboration\Collaborators\SearchResultType;
34
use OCP\IConfig;
35
use OCP\IGroup;
36
use OCP\IGroupManager;
37
use OCP\IUserSession;
38
use OCP\Share\IShare;
39
40
class GroupPlugin implements ISearchPlugin {
41
	/** @var bool */
42
	protected $shareeEnumeration;
43
	/** @var bool */
44
	protected $shareWithGroupOnly;
45
	/** @var bool */
46
	protected $shareeEnumerationInGroupOnly;
47
	/** @var bool */
48
	protected $groupSharingDisabled;
49
50
	/** @var IGroupManager */
51
	private $groupManager;
52
	/** @var IConfig */
53
	private $config;
54
	/** @var IUserSession */
55
	private $userSession;
56
57
	public function __construct(IConfig $config, IGroupManager $groupManager, IUserSession $userSession) {
58
		$this->groupManager = $groupManager;
59
		$this->config = $config;
60
		$this->userSession = $userSession;
61
62
		$this->shareeEnumeration = $this->config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes') === 'yes';
63
		$this->shareWithGroupOnly = $this->config->getAppValue('core', 'shareapi_only_share_with_group_members', 'no') === 'yes';
64
		$this->shareeEnumerationInGroupOnly = $this->shareeEnumeration && $this->config->getAppValue('core', 'shareapi_restrict_user_enumeration_to_group', 'no') === 'yes';
65
		$this->groupSharingDisabled = $this->config->getAppValue('core', 'shareapi_allow_group_sharing', 'yes') === 'no';
66
	}
67
68
	public function search($search, $limit, $offset, ISearchResult $searchResult) {
69
		if ($this->groupSharingDisabled) {
70
			return false;
71
		}
72
73
		$hasMoreResults = false;
74
		$result = ['wide' => [], 'exact' => []];
75
76
		$groups = $this->groupManager->search($search, $limit, $offset);
77
		$groupIds = array_map(function (IGroup $group) {
78
			return $group->getGID();
79
		}, $groups);
80
81
		if (!$this->shareeEnumeration || count($groups) < $limit) {
82
			$hasMoreResults = true;
83
		}
84
85
		$userGroups = [];
86
		if (!empty($groups) && ($this->shareWithGroupOnly || $this->shareeEnumerationInGroupOnly)) {
87
			// Intersect all the groups that match with the groups this user is a member of
88
			$userGroups = $this->groupManager->getUserGroups($this->userSession->getUser());
89
			$userGroups = array_map(function (IGroup $group) {
90
				return $group->getGID();
91
			}, $userGroups);
92
			$groupIds = array_intersect($groupIds, $userGroups);
93
		}
94
95
		$lowerSearch = strtolower($search);
96
		foreach ($groups as $group) {
97
			if ($group->hideFromCollaboration()) {
98
				continue;
99
			}
100
101
			// FIXME: use a more efficient approach
102
			$gid = $group->getGID();
103
			if (!in_array($gid, $groupIds)) {
104
				continue;
105
			}
106
			if (strtolower($gid) === $lowerSearch || strtolower($group->getDisplayName()) === $lowerSearch) {
107
				$result['exact'][] = [
108
					'label' => $group->getDisplayName(),
109
					'value' => [
110
						'shareType' => IShare::TYPE_GROUP,
111
						'shareWith' => $gid,
112
					],
113
				];
114
			} else {
115
				if ($this->shareeEnumerationInGroupOnly && !in_array($group->getGID(), $userGroups, true)) {
116
					continue;
117
				}
118
				$result['wide'][] = [
119
					'label' => $group->getDisplayName(),
120
					'value' => [
121
						'shareType' => IShare::TYPE_GROUP,
122
						'shareWith' => $gid,
123
					],
124
				];
125
			}
126
		}
127
128
		if ($offset === 0 && empty($result['exact'])) {
129
			// On page one we try if the search result has a direct hit on the
130
			// user id and if so, we add that to the exact match list
131
			$group = $this->groupManager->get($search);
132
			if ($group instanceof IGroup && (!$this->shareWithGroupOnly || in_array($group->getGID(), $userGroups))) {
133
				$result['exact'][] = [
134
					'label' => $group->getDisplayName(),
135
					'value' => [
136
						'shareType' => IShare::TYPE_GROUP,
137
						'shareWith' => $group->getGID(),
138
					],
139
				];
140
			}
141
		}
142
143
		if (!$this->shareeEnumeration) {
144
			$result['wide'] = [];
145
		}
146
147
		$type = new SearchResultType('groups');
148
		$searchResult->addResultSet($type, $result['wide'], $result['exact']);
149
150
		return $hasMoreResults;
151
	}
152
}
153