Passed
Push — master ( 84a353...212138 )
by Blizzz
12:21 queued 10s
created

User_Proxy::activeBackends()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
rs 10
c 1
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Arthur Schiwon <[email protected]>
6
 * @author Christopher Schäpers <[email protected]>
7
 * @author Christoph Wurst <[email protected]>
8
 * @author Joas Schilling <[email protected]>
9
 * @author Lukas Reschke <[email protected]>
10
 * @author Morris Jobke <[email protected]>
11
 * @author Robin McCorkell <[email protected]>
12
 * @author Roger Szabo <[email protected]>
13
 * @author root <[email protected]>
14
 * @author Thomas Müller <[email protected]>
15
 * @author Vinicius Cubas Brand <[email protected]>
16
 *
17
 * @license AGPL-3.0
18
 *
19
 * This code is free software: you can redistribute it and/or modify
20
 * it under the terms of the GNU Affero General Public License, version 3,
21
 * as published by the Free Software Foundation.
22
 *
23
 * This program is distributed in the hope that it will be useful,
24
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26
 * GNU Affero General Public License for more details.
27
 *
28
 * You should have received a copy of the GNU Affero General Public License, version 3,
29
 * along with this program. If not, see <http://www.gnu.org/licenses/>
30
 *
31
 */
32
33
namespace OCA\User_LDAP;
34
35
use OCA\User_LDAP\User\User;
36
use OCP\IConfig;
37
use OCP\IUserSession;
38
use OCP\Notification\IManager as INotificationManager;
39
40
class User_Proxy extends Proxy implements \OCP\IUserBackend, \OCP\UserInterface, IUserLDAP {
41
	private $backends = [];
42
	/** @var User_LDAP */
43
	private $refBackend = null;
44
45
	/**
46
	 * Constructor
47
	 *
48
	 * @param array $serverConfigPrefixes array containing the config Prefixes
49
	 * @param ILDAPWrapper $ldap
50
	 * @param IConfig $ocConfig
51
	 * @param INotificationManager $notificationManager
52
	 * @param IUserSession $userSession
53
	 */
54
	public function __construct(
55
		array $serverConfigPrefixes,
56
		ILDAPWrapper $ldap,
57
		IConfig $ocConfig,
58
		INotificationManager $notificationManager,
59
		IUserSession $userSession,
60
		UserPluginManager $userPluginManager
61
	) {
62
		parent::__construct($ldap);
63
		foreach ($serverConfigPrefixes as $configPrefix) {
64
			$this->backends[$configPrefix] =
65
				new User_LDAP($this->getAccess($configPrefix), $ocConfig, $notificationManager, $userSession, $userPluginManager);
66
67
			if (is_null($this->refBackend)) {
68
				$this->refBackend = &$this->backends[$configPrefix];
69
			}
70
		}
71
	}
72
73
	/**
74
	 * Tries the backends one after the other until a positive result is returned from the specified method
75
	 *
76
	 * @param string $uid the uid connected to the request
77
	 * @param string $method the method of the user backend that shall be called
78
	 * @param array $parameters an array of parameters to be passed
79
	 * @return mixed the result of the method or false
80
	 */
81
	protected function walkBackends($uid, $method, $parameters) {
82
		$cacheKey = $this->getUserCacheKey($uid);
83
		foreach ($this->backends as $configPrefix => $backend) {
84
			$instance = $backend;
85
			if (!method_exists($instance, $method)
86
				&& method_exists($this->getAccess($configPrefix), $method)) {
87
				$instance = $this->getAccess($configPrefix);
88
			}
89
			if ($result = call_user_func_array([$instance, $method], $parameters)) {
90
				if (!$this->isSingleBackend()) {
91
					$this->writeToCache($cacheKey, $configPrefix);
92
				}
93
				return $result;
94
			}
95
		}
96
		return false;
97
	}
98
99
	/**
100
	 * Asks the backend connected to the server that supposely takes care of the uid from the request.
101
	 *
102
	 * @param string $uid the uid connected to the request
103
	 * @param string $method the method of the user backend that shall be called
104
	 * @param array $parameters an array of parameters to be passed
105
	 * @param mixed $passOnWhen the result matches this variable
106
	 * @return mixed the result of the method or false
107
	 */
108
	protected function callOnLastSeenOn($uid, $method, $parameters, $passOnWhen) {
109
		$cacheKey = $this->getUserCacheKey($uid);
110
		$prefix = $this->getFromCache($cacheKey);
111
		//in case the uid has been found in the past, try this stored connection first
112
		if (!is_null($prefix)) {
113
			if (isset($this->backends[$prefix])) {
114
				$instance = $this->backends[$prefix];
115
				if (!method_exists($instance, $method)
116
					&& method_exists($this->getAccess($prefix), $method)) {
117
					$instance = $this->getAccess($prefix);
118
				}
119
				$result = call_user_func_array([$instance, $method], $parameters);
120
				if ($result === $passOnWhen) {
121
					//not found here, reset cache to null if user vanished
122
					//because sometimes methods return false with a reason
123
					$userExists = call_user_func_array(
124
						[$this->backends[$prefix], 'userExistsOnLDAP'],
125
						[$uid]
126
					);
127
					if (!$userExists) {
128
						$this->writeToCache($cacheKey, null);
129
					}
130
				}
131
				return $result;
132
			}
133
		}
134
		return false;
135
	}
136
137
	protected function activeBackends(): int {
138
		return count($this->backends);
139
	}
140
141
	/**
142
	 * Check if backend implements actions
143
	 *
144
	 * @param int $actions bitwise-or'ed actions
145
	 * @return boolean
146
	 *
147
	 * Returns the supported actions as int to be
148
	 * compared with \OC\User\Backend::CREATE_USER etc.
149
	 */
150
	public function implementsActions($actions) {
151
		//it's the same across all our user backends obviously
152
		return $this->refBackend->implementsActions($actions);
153
	}
154
155
	/**
156
	 * Backend name to be shown in user management
157
	 *
158
	 * @return string the name of the backend to be shown
159
	 */
160
	public function getBackendName() {
161
		return $this->refBackend->getBackendName();
162
	}
163
164
	/**
165
	 * Get a list of all users
166
	 *
167
	 * @param string $search
168
	 * @param null|int $limit
169
	 * @param null|int $offset
170
	 * @return string[] an array of all uids
171
	 */
172
	public function getUsers($search = '', $limit = 10, $offset = 0) {
173
		//we do it just as the /OC_User implementation: do not play around with limit and offset but ask all backends
174
		$users = [];
175
		foreach ($this->backends as $backend) {
176
			$backendUsers = $backend->getUsers($search, $limit, $offset);
177
			if (is_array($backendUsers)) {
178
				$users = array_merge($users, $backendUsers);
179
			}
180
		}
181
		return $users;
182
	}
183
184
	/**
185
	 * check if a user exists
186
	 *
187
	 * @param string $uid the username
188
	 * @return boolean
189
	 */
190
	public function userExists($uid) {
191
		$existsOnLDAP = false;
192
		$existsLocally = $this->handleRequest($uid, 'userExists', [$uid]);
193
		if ($existsLocally) {
194
			$existsOnLDAP = $this->userExistsOnLDAP($uid);
195
		}
196
		if ($existsLocally && !$existsOnLDAP) {
197
			try {
198
				$user = $this->getLDAPAccess($uid)->userManager->get($uid);
199
				if ($user instanceof User) {
200
					$user->markUser();
201
				}
202
			} catch (\Exception $e) {
203
				// ignore
204
			}
205
		}
206
		return $existsLocally;
207
	}
208
209
	/**
210
	 * check if a user exists on LDAP
211
	 *
212
	 * @param string|\OCA\User_LDAP\User\User $user either the Nextcloud user
213
	 * name or an instance of that user
214
	 * @return boolean
215
	 */
216
	public function userExistsOnLDAP($user) {
217
		$id = ($user instanceof User) ? $user->getUsername() : $user;
218
		return $this->handleRequest($id, 'userExistsOnLDAP', [$user]);
219
	}
220
221
	/**
222
	 * Check if the password is correct
223
	 *
224
	 * @param string $uid The username
225
	 * @param string $password The password
226
	 * @return bool
227
	 *
228
	 * Check if the password is correct without logging in the user
229
	 */
230
	public function checkPassword($uid, $password) {
231
		return $this->handleRequest($uid, 'checkPassword', [$uid, $password]);
232
	}
233
234
	/**
235
	 * returns the username for the given login name, if available
236
	 *
237
	 * @param string $loginName
238
	 * @return string|false
239
	 */
240
	public function loginName2UserName($loginName) {
241
		$id = 'LOGINNAME,' . $loginName;
242
		return $this->handleRequest($id, 'loginName2UserName', [$loginName]);
243
	}
244
245
	/**
246
	 * returns the username for the given LDAP DN, if available
247
	 *
248
	 * @param string $dn
249
	 * @return string|false with the username
250
	 */
251
	public function dn2UserName($dn) {
252
		$id = 'DN,' . $dn;
253
		return $this->handleRequest($id, 'dn2UserName', [$dn]);
254
	}
255
256
	/**
257
	 * get the user's home directory
258
	 *
259
	 * @param string $uid the username
260
	 * @return boolean
261
	 */
262
	public function getHome($uid) {
263
		return $this->handleRequest($uid, 'getHome', [$uid]);
264
	}
265
266
	/**
267
	 * get display name of the user
268
	 *
269
	 * @param string $uid user ID of the user
270
	 * @return string display name
271
	 */
272
	public function getDisplayName($uid) {
273
		return $this->handleRequest($uid, 'getDisplayName', [$uid]);
274
	}
275
276
	/**
277
	 * set display name of the user
278
	 *
279
	 * @param string $uid user ID of the user
280
	 * @param string $displayName new display name
281
	 * @return string display name
282
	 */
283
	public function setDisplayName($uid, $displayName) {
284
		return $this->handleRequest($uid, 'setDisplayName', [$uid, $displayName]);
285
	}
286
287
	/**
288
	 * checks whether the user is allowed to change his avatar in Nextcloud
289
	 *
290
	 * @param string $uid the Nextcloud user name
291
	 * @return boolean either the user can or cannot
292
	 */
293
	public function canChangeAvatar($uid) {
294
		return $this->handleRequest($uid, 'canChangeAvatar', [$uid], true);
295
	}
296
297
	/**
298
	 * Get a list of all display names and user ids.
299
	 *
300
	 * @param string $search
301
	 * @param string|null $limit
302
	 * @param string|null $offset
303
	 * @return array an array of all displayNames (value) and the corresponding uids (key)
304
	 */
305
	public function getDisplayNames($search = '', $limit = null, $offset = null) {
306
		//we do it just as the /OC_User implementation: do not play around with limit and offset but ask all backends
307
		$users = [];
308
		foreach ($this->backends as $backend) {
309
			$backendUsers = $backend->getDisplayNames($search, $limit, $offset);
310
			if (is_array($backendUsers)) {
311
				$users = $users + $backendUsers;
312
			}
313
		}
314
		return $users;
315
	}
316
317
	/**
318
	 * delete a user
319
	 *
320
	 * @param string $uid The username of the user to delete
321
	 * @return bool
322
	 *
323
	 * Deletes a user
324
	 */
325
	public function deleteUser($uid) {
326
		return $this->handleRequest($uid, 'deleteUser', [$uid]);
327
	}
328
329
	/**
330
	 * Set password
331
	 *
332
	 * @param string $uid The username
333
	 * @param string $password The new password
334
	 * @return bool
335
	 *
336
	 */
337
	public function setPassword($uid, $password) {
338
		return $this->handleRequest($uid, 'setPassword', [$uid, $password]);
339
	}
340
341
	/**
342
	 * @return bool
343
	 */
344
	public function hasUserListings() {
345
		return $this->refBackend->hasUserListings();
346
	}
347
348
	/**
349
	 * Count the number of users
350
	 *
351
	 * @return int|bool
352
	 */
353
	public function countUsers() {
354
		$users = false;
355
		foreach ($this->backends as $backend) {
356
			$backendUsers = $backend->countUsers();
357
			if ($backendUsers !== false) {
358
				$users += $backendUsers;
359
			}
360
		}
361
		return $users;
362
	}
363
364
	/**
365
	 * Return access for LDAP interaction.
366
	 *
367
	 * @param string $uid
368
	 * @return Access instance of Access for LDAP interaction
369
	 */
370
	public function getLDAPAccess($uid) {
371
		return $this->handleRequest($uid, 'getLDAPAccess', [$uid]);
372
	}
373
374
	/**
375
	 * Return a new LDAP connection for the specified user.
376
	 * The connection needs to be closed manually.
377
	 *
378
	 * @param string $uid
379
	 * @return resource of the LDAP connection
380
	 */
381
	public function getNewLDAPConnection($uid) {
382
		return $this->handleRequest($uid, 'getNewLDAPConnection', [$uid]);
383
	}
384
385
	/**
386
	 * Creates a new user in LDAP
387
	 *
388
	 * @param $username
389
	 * @param $password
390
	 * @return bool
391
	 */
392
	public function createUser($username, $password) {
393
		return $this->handleRequest($username, 'createUser', [$username, $password]);
394
	}
395
}
396