Completed
Pull Request — master (#7914)
by Blizzz
24:07 queued 05:40
created

UserPlugin::takeOutCurrentUser()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 1
dl 0
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
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
		$this->takeOutCurrentUser($users);
87
88
		if (!$this->shareeEnumeration || sizeof($users) < $limit) {
89
			$hasMoreResults = true;
90
		}
91
92
		$foundUserById = false;
93
		$lowerSearch = strtolower($search);
94
		foreach ($users as $uid => $userDisplayName) {
95
			if (strtolower($uid) === $lowerSearch || strtolower($userDisplayName) === $lowerSearch) {
96
				if (strtolower($uid) === $lowerSearch) {
97
					$foundUserById = true;
98
				}
99
				$result['exact'][] = [
100
					'label' => $userDisplayName,
101
					'value' => [
102
						'shareType' => Share::SHARE_TYPE_USER,
103
						'shareWith' => $uid,
104
					],
105
				];
106
			} else {
107
				$result['wide'][] = [
108
					'label' => $userDisplayName,
109
					'value' => [
110
						'shareType' => Share::SHARE_TYPE_USER,
111
						'shareWith' => $uid,
112
					],
113
				];
114
			}
115
		}
116
117
		if ($offset === 0 && !$foundUserById) {
118
			// On page one we try if the search result has a direct hit on the
119
			// user id and if so, we add that to the exact match list
120
			$user = $this->userManager->get($search);
121
			if ($user instanceof IUser) {
122
				$addUser = true;
123
124
				if ($this->shareWithGroupOnly) {
125
					// Only add, if we have a common group
126
					$commonGroups = array_intersect($userGroups, $this->groupManager->getUserGroupIds($user));
127
					$addUser = !empty($commonGroups);
128
				}
129
130
				if ($addUser) {
131
					array_push($result['exact'], [
132
						'label' => $user->getDisplayName(),
133
						'value' => [
134
							'shareType' => Share::SHARE_TYPE_USER,
135
							'shareWith' => $user->getUID(),
136
						],
137
					]);
138
				}
139
			}
140
		}
141
142
		if (!$this->shareeEnumeration) {
143
			$result['wide'] = [];
144
		}
145
146
		$type = new SearchResultType('users');
147
		$searchResult->addResultSet($type, $result['wide'], $result['exact']);
148
149
		return $hasMoreResults;
150
	}
151
152
	public function takeOutCurrentUser(array &$users) {
153
		$currentUser = $this->userSession->getUser();
154
		if(!is_null($currentUser)) {
155
			if (isset($users[$currentUser->getUID()])) {
156
				unset($users[$currentUser->getUID()]);
157
			}
158
		}
159
	}
160
}
161