Passed
Push — master ( 7c30b6...8261d7 )
by Morris
13:25
created
lib/private/Collaboration/Collaborators/UserPlugin.php 1 patch
Indentation   +203 added lines, -203 removed lines patch added patch discarded remove patch
@@ -41,207 +41,207 @@
 block discarded – undo
41 41
 use OCP\UserStatus\IManager as IUserStatusManager;
42 42
 
43 43
 class UserPlugin implements ISearchPlugin {
44
-	/* @var bool */
45
-	protected $shareWithGroupOnly;
46
-	protected $shareeEnumeration;
47
-	protected $shareeEnumerationInGroupOnly;
48
-
49
-	/** @var IConfig */
50
-	private $config;
51
-	/** @var IGroupManager */
52
-	private $groupManager;
53
-	/** @var IUserSession */
54
-	private $userSession;
55
-	/** @var IUserManager */
56
-	private $userManager;
57
-	/** @var IUserStatusManager */
58
-	private $userStatusManager;
59
-
60
-	/**
61
-	 * UserPlugin constructor.
62
-	 *
63
-	 * @param IConfig $config
64
-	 * @param IUserManager $userManager
65
-	 * @param IGroupManager $groupManager
66
-	 * @param IUserSession $userSession
67
-	 * @param IUserStatusManager $userStatusManager
68
-	 */
69
-	public function __construct(IConfig $config,
70
-								IUserManager $userManager,
71
-								IGroupManager $groupManager,
72
-								IUserSession $userSession,
73
-								IUserStatusManager $userStatusManager) {
74
-		$this->config = $config;
75
-
76
-		$this->groupManager = $groupManager;
77
-		$this->userSession = $userSession;
78
-		$this->userManager = $userManager;
79
-		$this->userStatusManager = $userStatusManager;
80
-
81
-		$this->shareWithGroupOnly = $this->config->getAppValue('core', 'shareapi_only_share_with_group_members', 'no') === 'yes';
82
-		$this->shareeEnumeration = $this->config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes') === 'yes';
83
-		$this->shareeEnumerationInGroupOnly = $this->shareeEnumeration && $this->config->getAppValue('core', 'shareapi_restrict_user_enumeration_to_group', 'no') === 'yes';
84
-	}
85
-
86
-	public function search($search, $limit, $offset, ISearchResult $searchResult) {
87
-		$result = ['wide' => [], 'exact' => []];
88
-		$users = [];
89
-		$hasMoreResults = false;
90
-
91
-		$currentUserGroups = $this->groupManager->getUserGroupIds($this->userSession->getUser());
92
-		if ($this->shareWithGroupOnly) {
93
-			// Search in all the groups this user is part of
94
-			foreach ($currentUserGroups as $userGroupId) {
95
-				$usersInGroup = $this->groupManager->displayNamesInGroup($userGroupId, $search, $limit, $offset);
96
-				foreach ($usersInGroup as $userId => $displayName) {
97
-					$userId = (string) $userId;
98
-					$user = $this->userManager->get($userId);
99
-					if (!$user->isEnabled()) {
100
-						// Ignore disabled users
101
-						continue;
102
-					}
103
-					$users[$userId] = $user;
104
-				}
105
-				if (count($usersInGroup) >= $limit) {
106
-					$hasMoreResults = true;
107
-				}
108
-			}
109
-		} else {
110
-			// Search in all users
111
-			$usersTmp = $this->userManager->searchDisplayName($search, $limit, $offset);
112
-			foreach ($usersTmp as $user) {
113
-				if ($user->isEnabled()) { // Don't keep deactivated users
114
-					$users[$user->getUID()] = $user;
115
-				}
116
-			}
117
-		}
118
-
119
-		$this->takeOutCurrentUser($users);
120
-
121
-		if (!$this->shareeEnumeration || count($users) < $limit) {
122
-			$hasMoreResults = true;
123
-		}
124
-
125
-		$foundUserById = false;
126
-		$lowerSearch = strtolower($search);
127
-		$userStatuses = $this->userStatusManager->getUserStatuses(array_keys($users));
128
-		foreach ($users as $uid => $user) {
129
-			$userDisplayName = $user->getDisplayName();
130
-			$userEmail = $user->getEMailAddress();
131
-			$uid = (string) $uid;
132
-
133
-			$status = [];
134
-			if (array_key_exists($uid, $userStatuses)) {
135
-				$userStatus = $userStatuses[$uid];
136
-				$status = [
137
-					'status' => $userStatus->getStatus(),
138
-					'message' => $userStatus->getMessage(),
139
-					'icon' => $userStatus->getIcon(),
140
-					'clearAt' => $userStatus->getClearAt()
141
-						? (int)$userStatus->getClearAt()->format('U')
142
-						: null,
143
-				];
144
-			}
145
-
146
-
147
-			if (
148
-				strtolower($uid) === $lowerSearch ||
149
-				strtolower($userDisplayName) === $lowerSearch ||
150
-				strtolower($userEmail) === $lowerSearch
151
-			) {
152
-				if (strtolower($uid) === $lowerSearch) {
153
-					$foundUserById = true;
154
-				}
155
-				$result['exact'][] = [
156
-					'label' => $userDisplayName,
157
-					'value' => [
158
-						'shareType' => IShare::TYPE_USER,
159
-						'shareWith' => $uid,
160
-					],
161
-					'status' => $status,
162
-				];
163
-			} else {
164
-				$addToWideResults = false;
165
-				if ($this->shareeEnumeration && !$this->shareeEnumerationInGroupOnly) {
166
-					$addToWideResults = true;
167
-				}
168
-
169
-				if ($this->shareeEnumerationInGroupOnly) {
170
-					$commonGroups = array_intersect($currentUserGroups, $this->groupManager->getUserGroupIds($user));
171
-					if (!empty($commonGroups)) {
172
-						$addToWideResults = true;
173
-					}
174
-				}
175
-
176
-				if ($addToWideResults) {
177
-					$result['wide'][] = [
178
-						'label' => $userDisplayName,
179
-						'value' => [
180
-							'shareType' => IShare::TYPE_USER,
181
-							'shareWith' => $uid,
182
-						],
183
-						'status' => $status,
184
-					];
185
-				}
186
-			}
187
-		}
188
-
189
-		if ($offset === 0 && !$foundUserById) {
190
-			// On page one we try if the search result has a direct hit on the
191
-			// user id and if so, we add that to the exact match list
192
-			$user = $this->userManager->get($search);
193
-			if ($user instanceof IUser) {
194
-				$addUser = true;
195
-
196
-				if ($this->shareWithGroupOnly) {
197
-					// Only add, if we have a common group
198
-					$commonGroups = array_intersect($currentUserGroups, $this->groupManager->getUserGroupIds($user));
199
-					$addUser = !empty($commonGroups);
200
-				}
201
-
202
-				if ($addUser) {
203
-					$status = [];
204
-					if (array_key_exists($user->getUID(), $userStatuses)) {
205
-						$userStatus = $userStatuses[$user->getUID()];
206
-						$status = [
207
-							'status' => $userStatus->getStatus(),
208
-							'message' => $userStatus->getMessage(),
209
-							'icon' => $userStatus->getIcon(),
210
-							'clearAt' => $userStatus->getClearAt()
211
-								? (int)$userStatus->getClearAt()->format('U')
212
-								: null,
213
-						];
214
-					}
215
-
216
-					$result['exact'][] = [
217
-						'label' => $user->getDisplayName(),
218
-						'value' => [
219
-							'shareType' => IShare::TYPE_USER,
220
-							'shareWith' => $user->getUID(),
221
-						],
222
-						'status' => $status,
223
-					];
224
-				}
225
-			}
226
-		}
227
-
228
-
229
-
230
-		$type = new SearchResultType('users');
231
-		$searchResult->addResultSet($type, $result['wide'], $result['exact']);
232
-		if (count($result['exact'])) {
233
-			$searchResult->markExactIdMatch($type);
234
-		}
235
-
236
-		return $hasMoreResults;
237
-	}
238
-
239
-	public function takeOutCurrentUser(array &$users) {
240
-		$currentUser = $this->userSession->getUser();
241
-		if (!is_null($currentUser)) {
242
-			if (isset($users[$currentUser->getUID()])) {
243
-				unset($users[$currentUser->getUID()]);
244
-			}
245
-		}
246
-	}
44
+    /* @var bool */
45
+    protected $shareWithGroupOnly;
46
+    protected $shareeEnumeration;
47
+    protected $shareeEnumerationInGroupOnly;
48
+
49
+    /** @var IConfig */
50
+    private $config;
51
+    /** @var IGroupManager */
52
+    private $groupManager;
53
+    /** @var IUserSession */
54
+    private $userSession;
55
+    /** @var IUserManager */
56
+    private $userManager;
57
+    /** @var IUserStatusManager */
58
+    private $userStatusManager;
59
+
60
+    /**
61
+     * UserPlugin constructor.
62
+     *
63
+     * @param IConfig $config
64
+     * @param IUserManager $userManager
65
+     * @param IGroupManager $groupManager
66
+     * @param IUserSession $userSession
67
+     * @param IUserStatusManager $userStatusManager
68
+     */
69
+    public function __construct(IConfig $config,
70
+                                IUserManager $userManager,
71
+                                IGroupManager $groupManager,
72
+                                IUserSession $userSession,
73
+                                IUserStatusManager $userStatusManager) {
74
+        $this->config = $config;
75
+
76
+        $this->groupManager = $groupManager;
77
+        $this->userSession = $userSession;
78
+        $this->userManager = $userManager;
79
+        $this->userStatusManager = $userStatusManager;
80
+
81
+        $this->shareWithGroupOnly = $this->config->getAppValue('core', 'shareapi_only_share_with_group_members', 'no') === 'yes';
82
+        $this->shareeEnumeration = $this->config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes') === 'yes';
83
+        $this->shareeEnumerationInGroupOnly = $this->shareeEnumeration && $this->config->getAppValue('core', 'shareapi_restrict_user_enumeration_to_group', 'no') === 'yes';
84
+    }
85
+
86
+    public function search($search, $limit, $offset, ISearchResult $searchResult) {
87
+        $result = ['wide' => [], 'exact' => []];
88
+        $users = [];
89
+        $hasMoreResults = false;
90
+
91
+        $currentUserGroups = $this->groupManager->getUserGroupIds($this->userSession->getUser());
92
+        if ($this->shareWithGroupOnly) {
93
+            // Search in all the groups this user is part of
94
+            foreach ($currentUserGroups as $userGroupId) {
95
+                $usersInGroup = $this->groupManager->displayNamesInGroup($userGroupId, $search, $limit, $offset);
96
+                foreach ($usersInGroup as $userId => $displayName) {
97
+                    $userId = (string) $userId;
98
+                    $user = $this->userManager->get($userId);
99
+                    if (!$user->isEnabled()) {
100
+                        // Ignore disabled users
101
+                        continue;
102
+                    }
103
+                    $users[$userId] = $user;
104
+                }
105
+                if (count($usersInGroup) >= $limit) {
106
+                    $hasMoreResults = true;
107
+                }
108
+            }
109
+        } else {
110
+            // Search in all users
111
+            $usersTmp = $this->userManager->searchDisplayName($search, $limit, $offset);
112
+            foreach ($usersTmp as $user) {
113
+                if ($user->isEnabled()) { // Don't keep deactivated users
114
+                    $users[$user->getUID()] = $user;
115
+                }
116
+            }
117
+        }
118
+
119
+        $this->takeOutCurrentUser($users);
120
+
121
+        if (!$this->shareeEnumeration || count($users) < $limit) {
122
+            $hasMoreResults = true;
123
+        }
124
+
125
+        $foundUserById = false;
126
+        $lowerSearch = strtolower($search);
127
+        $userStatuses = $this->userStatusManager->getUserStatuses(array_keys($users));
128
+        foreach ($users as $uid => $user) {
129
+            $userDisplayName = $user->getDisplayName();
130
+            $userEmail = $user->getEMailAddress();
131
+            $uid = (string) $uid;
132
+
133
+            $status = [];
134
+            if (array_key_exists($uid, $userStatuses)) {
135
+                $userStatus = $userStatuses[$uid];
136
+                $status = [
137
+                    'status' => $userStatus->getStatus(),
138
+                    'message' => $userStatus->getMessage(),
139
+                    'icon' => $userStatus->getIcon(),
140
+                    'clearAt' => $userStatus->getClearAt()
141
+                        ? (int)$userStatus->getClearAt()->format('U')
142
+                        : null,
143
+                ];
144
+            }
145
+
146
+
147
+            if (
148
+                strtolower($uid) === $lowerSearch ||
149
+                strtolower($userDisplayName) === $lowerSearch ||
150
+                strtolower($userEmail) === $lowerSearch
151
+            ) {
152
+                if (strtolower($uid) === $lowerSearch) {
153
+                    $foundUserById = true;
154
+                }
155
+                $result['exact'][] = [
156
+                    'label' => $userDisplayName,
157
+                    'value' => [
158
+                        'shareType' => IShare::TYPE_USER,
159
+                        'shareWith' => $uid,
160
+                    ],
161
+                    'status' => $status,
162
+                ];
163
+            } else {
164
+                $addToWideResults = false;
165
+                if ($this->shareeEnumeration && !$this->shareeEnumerationInGroupOnly) {
166
+                    $addToWideResults = true;
167
+                }
168
+
169
+                if ($this->shareeEnumerationInGroupOnly) {
170
+                    $commonGroups = array_intersect($currentUserGroups, $this->groupManager->getUserGroupIds($user));
171
+                    if (!empty($commonGroups)) {
172
+                        $addToWideResults = true;
173
+                    }
174
+                }
175
+
176
+                if ($addToWideResults) {
177
+                    $result['wide'][] = [
178
+                        'label' => $userDisplayName,
179
+                        'value' => [
180
+                            'shareType' => IShare::TYPE_USER,
181
+                            'shareWith' => $uid,
182
+                        ],
183
+                        'status' => $status,
184
+                    ];
185
+                }
186
+            }
187
+        }
188
+
189
+        if ($offset === 0 && !$foundUserById) {
190
+            // On page one we try if the search result has a direct hit on the
191
+            // user id and if so, we add that to the exact match list
192
+            $user = $this->userManager->get($search);
193
+            if ($user instanceof IUser) {
194
+                $addUser = true;
195
+
196
+                if ($this->shareWithGroupOnly) {
197
+                    // Only add, if we have a common group
198
+                    $commonGroups = array_intersect($currentUserGroups, $this->groupManager->getUserGroupIds($user));
199
+                    $addUser = !empty($commonGroups);
200
+                }
201
+
202
+                if ($addUser) {
203
+                    $status = [];
204
+                    if (array_key_exists($user->getUID(), $userStatuses)) {
205
+                        $userStatus = $userStatuses[$user->getUID()];
206
+                        $status = [
207
+                            'status' => $userStatus->getStatus(),
208
+                            'message' => $userStatus->getMessage(),
209
+                            'icon' => $userStatus->getIcon(),
210
+                            'clearAt' => $userStatus->getClearAt()
211
+                                ? (int)$userStatus->getClearAt()->format('U')
212
+                                : null,
213
+                        ];
214
+                    }
215
+
216
+                    $result['exact'][] = [
217
+                        'label' => $user->getDisplayName(),
218
+                        'value' => [
219
+                            'shareType' => IShare::TYPE_USER,
220
+                            'shareWith' => $user->getUID(),
221
+                        ],
222
+                        'status' => $status,
223
+                    ];
224
+                }
225
+            }
226
+        }
227
+
228
+
229
+
230
+        $type = new SearchResultType('users');
231
+        $searchResult->addResultSet($type, $result['wide'], $result['exact']);
232
+        if (count($result['exact'])) {
233
+            $searchResult->markExactIdMatch($type);
234
+        }
235
+
236
+        return $hasMoreResults;
237
+    }
238
+
239
+    public function takeOutCurrentUser(array &$users) {
240
+        $currentUser = $this->userSession->getUser();
241
+        if (!is_null($currentUser)) {
242
+            if (isset($users[$currentUser->getUID()])) {
243
+                unset($users[$currentUser->getUID()]);
244
+            }
245
+        }
246
+    }
247 247
 }
Please login to merge, or discard this patch.