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

UserPlugin::search()   F

Complexity

Conditions 17
Paths 288

Size

Total Lines 87
Code Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 17
eloc 52
nc 288
nop 4
dl 0
loc 87
rs 3.6909
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
 *
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
27
use OCP\Collaboration\Collaborators\ISearchPlugin;
28
use OCP\Collaboration\Collaborators\ISearchResult;
29
use OCP\Collaboration\Collaborators\SearchResultType;
30
use OCP\IConfig;
31
use OCP\IGroupManager;
32
use OCP\IUser;
33
use OCP\IUserManager;
34
use OCP\IUserSession;
35
use OCP\Share;
36
37
class UserPlugin implements ISearchPlugin {
38
	/* @var bool */
39
	protected $shareWithGroupOnly;
40
	protected $shareeEnumeration;
41
42
	/** @var IConfig */
43
	private $config;
44
	/** @var IGroupManager */
45
	private $groupManager;
46
	/** @var IUserSession */
47
	private $userSession;
48
	/** @var IUserManager */
49
	private $userManager;
50
51 View Code Duplication
	public function __construct(IConfig $config, IUserManager $userManager, IGroupManager $groupManager, IUserSession $userSession) {
52
		$this->config = $config;
53
54
		$this->groupManager = $groupManager;
55
		$this->userSession = $userSession;
56
		$this->userManager = $userManager;
57
58
		$this->shareWithGroupOnly = $this->config->getAppValue('core', 'shareapi_only_share_with_group_members', 'no') === 'yes';
59
		$this->shareeEnumeration = $this->config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes') === 'yes';
60
	}
61
62
	public function search($search, $limit, $offset, ISearchResult $searchResult) {
63
		$result = ['wide' => [], 'exact' => []];
64
		$users = [];
65
		$hasMoreResults = false;
66
67
		$userGroups = [];
68
		if ($this->shareWithGroupOnly) {
69
			// Search in all the groups this user is part of
70
			$userGroups = $this->groupManager->getUserGroupIds($this->userSession->getUser());
0 ignored issues
show
Bug introduced by
It seems like $this->userSession->getUser() can be null; however, getUserGroupIds() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
71
			foreach ($userGroups as $userGroup) {
72
				$usersTmp = $this->groupManager->displayNamesInGroup($userGroup, $search, $limit, $offset);
73
				foreach ($usersTmp as $uid => $userDisplayName) {
74
					$users[$uid] = $userDisplayName;
75
				}
76
			}
77
		} else {
78
			// Search in all users
79
			$usersTmp = $this->userManager->searchDisplayName($search, $limit, $offset);
80
81
			foreach ($usersTmp as $user) {
82
				$users[$user->getUID()] = $user->getDisplayName();
83
			}
84
		}
85
86
		if (!$this->shareeEnumeration || sizeof($users) < $limit) {
87
			$hasMoreResults = true;
88
		}
89
90
		$foundUserById = false;
91
		$lowerSearch = strtolower($search);
92
		foreach ($users as $uid => $userDisplayName) {
93
			if (strtolower($uid) === $lowerSearch || strtolower($userDisplayName) === $lowerSearch) {
94
				if (strtolower($uid) === $lowerSearch) {
95
					$foundUserById = true;
96
				}
97
				$result['exact'][] = [
98
					'label' => $userDisplayName,
99
					'value' => [
100
						'shareType' => Share::SHARE_TYPE_USER,
101
						'shareWith' => $uid,
102
					],
103
				];
104
			} else {
105
				$result['wide'][] = [
106
					'label' => $userDisplayName,
107
					'value' => [
108
						'shareType' => Share::SHARE_TYPE_USER,
109
						'shareWith' => $uid,
110
					],
111
				];
112
			}
113
		}
114
115
		if ($offset === 0 && !$foundUserById) {
116
			// On page one we try if the search result has a direct hit on the
117
			// user id and if so, we add that to the exact match list
118
			$user = $this->userManager->get($search);
119
			if ($user instanceof IUser) {
120
				$addUser = true;
121
122
				if ($this->shareWithGroupOnly) {
123
					// Only add, if we have a common group
124
					$commonGroups = array_intersect($userGroups, $this->groupManager->getUserGroupIds($user));
125
					$addUser = !empty($commonGroups);
126
				}
127
128
				if ($addUser) {
129
					array_push($result['exact'], [
130
						'label' => $user->getDisplayName(),
131
						'value' => [
132
							'shareType' => Share::SHARE_TYPE_USER,
133
							'shareWith' => $user->getUID(),
134
						],
135
					]);
136
				}
137
			}
138
		}
139
140
		if (!$this->shareeEnumeration) {
141
			$result['wide'] = [];
142
		}
143
144
		$type = new SearchResultType('users');
145
		$searchResult->addResultSet($type, $result['wide'], $result['exact']);
146
147
		return $hasMoreResults;
148
	}
149
}
150