|
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 Joas Schilling <[email protected]> |
|
8
|
|
|
* @author Lukas Reschke <[email protected]> |
|
9
|
|
|
* @author Morris Jobke <[email protected]> |
|
10
|
|
|
* @author Robin McCorkell <[email protected]> |
|
11
|
|
|
* @author Thomas Müller <[email protected]> |
|
12
|
|
|
* @author Roger Szabo <[email protected]> |
|
13
|
|
|
* |
|
14
|
|
|
* @license AGPL-3.0 |
|
15
|
|
|
* |
|
16
|
|
|
* This code is free software: you can redistribute it and/or modify |
|
17
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
|
18
|
|
|
* as published by the Free Software Foundation. |
|
19
|
|
|
* |
|
20
|
|
|
* This program is distributed in the hope that it will be useful, |
|
21
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
22
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
23
|
|
|
* GNU Affero General Public License for more details. |
|
24
|
|
|
* |
|
25
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
|
26
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
|
27
|
|
|
* |
|
28
|
|
|
*/ |
|
29
|
|
|
|
|
30
|
|
|
namespace OCA\User_LDAP; |
|
31
|
|
|
|
|
32
|
|
|
use OCA\User_LDAP\User\User; |
|
33
|
|
|
use OCP\IConfig; |
|
34
|
|
|
|
|
35
|
|
|
class User_Proxy extends Proxy implements \OCP\IUserBackend, \OCP\UserInterface, IUserLDAP { |
|
36
|
|
|
private $backends = array(); |
|
37
|
|
|
private $refBackend = null; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Constructor |
|
41
|
|
|
* @param array $serverConfigPrefixes array containing the config Prefixes |
|
42
|
|
|
*/ |
|
43
|
|
View Code Duplication |
public function __construct(array $serverConfigPrefixes, ILDAPWrapper $ldap, IConfig $ocConfig) { |
|
|
|
|
|
|
44
|
|
|
parent::__construct($ldap); |
|
45
|
|
|
foreach($serverConfigPrefixes as $configPrefix) { |
|
46
|
|
|
$this->backends[$configPrefix] = |
|
47
|
|
|
new User_LDAP($this->getAccess($configPrefix), $ocConfig); |
|
48
|
|
|
if(is_null($this->refBackend)) { |
|
49
|
|
|
$this->refBackend = &$this->backends[$configPrefix]; |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Tries the backends one after the other until a positive result is returned from the specified method |
|
56
|
|
|
* @param string $uid the uid connected to the request |
|
57
|
|
|
* @param string $method the method of the user backend that shall be called |
|
58
|
|
|
* @param array $parameters an array of parameters to be passed |
|
59
|
|
|
* @return mixed the result of the method or false |
|
60
|
|
|
*/ |
|
61
|
|
|
protected function walkBackends($uid, $method, $parameters) { |
|
62
|
|
|
$cacheKey = $this->getUserCacheKey($uid); |
|
63
|
|
|
foreach($this->backends as $configPrefix => $backend) { |
|
64
|
|
|
$instance = $backend; |
|
65
|
|
|
if(!method_exists($instance, $method) |
|
66
|
|
|
&& method_exists($this->getAccess($configPrefix), $method)) { |
|
67
|
|
|
$instance = $this->getAccess($configPrefix); |
|
68
|
|
|
} |
|
69
|
|
View Code Duplication |
if($result = call_user_func_array(array($instance, $method), $parameters)) { |
|
|
|
|
|
|
70
|
|
|
$this->writeToCache($cacheKey, $configPrefix); |
|
71
|
|
|
return $result; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
return false; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Asks the backend connected to the server that supposely takes care of the uid from the request. |
|
79
|
|
|
* @param string $uid the uid connected to the request |
|
80
|
|
|
* @param string $method the method of the user backend that shall be called |
|
81
|
|
|
* @param array $parameters an array of parameters to be passed |
|
82
|
|
|
* @param mixed $passOnWhen the result matches this variable |
|
83
|
|
|
* @return mixed the result of the method or false |
|
84
|
|
|
*/ |
|
85
|
|
|
protected function callOnLastSeenOn($uid, $method, $parameters, $passOnWhen) { |
|
86
|
|
|
$cacheKey = $this->getUserCacheKey($uid); |
|
87
|
|
|
$prefix = $this->getFromCache($cacheKey); |
|
88
|
|
|
//in case the uid has been found in the past, try this stored connection first |
|
89
|
|
|
if(!is_null($prefix)) { |
|
90
|
|
|
if(isset($this->backends[$prefix])) { |
|
91
|
|
|
$instance = $this->backends[$prefix]; |
|
92
|
|
|
if(!method_exists($instance, $method) |
|
93
|
|
|
&& method_exists($this->getAccess($prefix), $method)) { |
|
94
|
|
|
$instance = $this->getAccess($prefix); |
|
95
|
|
|
} |
|
96
|
|
|
$result = call_user_func_array(array($instance, $method), $parameters); |
|
97
|
|
View Code Duplication |
if($result === $passOnWhen) { |
|
|
|
|
|
|
98
|
|
|
//not found here, reset cache to null if user vanished |
|
99
|
|
|
//because sometimes methods return false with a reason |
|
100
|
|
|
$userExists = call_user_func_array( |
|
101
|
|
|
array($this->backends[$prefix], 'userExists'), |
|
102
|
|
|
array($uid) |
|
103
|
|
|
); |
|
104
|
|
|
if(!$userExists) { |
|
105
|
|
|
$this->writeToCache($cacheKey, null); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
return $result; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
return false; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Check if backend implements actions |
|
116
|
|
|
* @param int $actions bitwise-or'ed actions |
|
117
|
|
|
* @return boolean |
|
118
|
|
|
* |
|
119
|
|
|
* Returns the supported actions as int to be |
|
120
|
|
|
* compared with OC_USER_BACKEND_CREATE_USER etc. |
|
121
|
|
|
*/ |
|
122
|
|
|
public function implementsActions($actions) { |
|
123
|
|
|
//it's the same across all our user backends obviously |
|
124
|
|
|
return $this->refBackend->implementsActions($actions); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Backend name to be shown in user management |
|
129
|
|
|
* @return string the name of the backend to be shown |
|
130
|
|
|
*/ |
|
131
|
|
|
public function getBackendName() { |
|
132
|
|
|
return $this->refBackend->getBackendName(); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Get a list of all users |
|
137
|
|
|
* |
|
138
|
|
|
* @param string $search |
|
139
|
|
|
* @param null|int $limit |
|
140
|
|
|
* @param null|int $offset |
|
141
|
|
|
* @return string[] an array of all uids |
|
142
|
|
|
*/ |
|
143
|
|
View Code Duplication |
public function getUsers($search = '', $limit = 10, $offset = 0) { |
|
|
|
|
|
|
144
|
|
|
//we do it just as the /OC_User implementation: do not play around with limit and offset but ask all backends |
|
145
|
|
|
$users = array(); |
|
146
|
|
|
foreach($this->backends as $backend) { |
|
147
|
|
|
$backendUsers = $backend->getUsers($search, $limit, $offset); |
|
148
|
|
|
if (is_array($backendUsers)) { |
|
149
|
|
|
$users = array_merge($users, $backendUsers); |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
return $users; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* check if a user exists |
|
157
|
|
|
* @param string $uid the username |
|
158
|
|
|
* @return boolean |
|
159
|
|
|
*/ |
|
160
|
|
|
public function userExists($uid) { |
|
161
|
|
|
return $this->handleRequest($uid, 'userExists', array($uid)); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* check if a user exists on LDAP |
|
166
|
|
|
* @param string|\OCA\User_LDAP\User\User $user either the ownCloud user |
|
167
|
|
|
* name or an instance of that user |
|
168
|
|
|
* @return boolean |
|
169
|
|
|
*/ |
|
170
|
|
|
public function userExistsOnLDAP($user) { |
|
171
|
|
|
$id = ($user instanceof User) ? $user->getUsername() : $user; |
|
172
|
|
|
return $this->handleRequest($id, 'userExistsOnLDAP', array($user)); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Check if the password is correct |
|
177
|
|
|
* @param string $uid The username |
|
178
|
|
|
* @param string $password The password |
|
179
|
|
|
* @return bool |
|
180
|
|
|
* |
|
181
|
|
|
* Check if the password is correct without logging in the user |
|
182
|
|
|
*/ |
|
183
|
|
|
public function checkPassword($uid, $password) { |
|
184
|
|
|
return $this->handleRequest($uid, 'checkPassword', array($uid, $password)); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* returns the username for the given login name, if available |
|
189
|
|
|
* |
|
190
|
|
|
* @param string $loginName |
|
191
|
|
|
* @return string|false |
|
192
|
|
|
*/ |
|
193
|
|
|
public function loginName2UserName($loginName) { |
|
194
|
|
|
$id = 'LOGINNAME,' . $loginName; |
|
195
|
|
|
return $this->handleRequest($id, 'loginName2UserName', array($loginName)); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* returns the username for the given LDAP DN, if available |
|
200
|
|
|
* |
|
201
|
|
|
* @param string $dn |
|
202
|
|
|
* @return string|false with the username |
|
203
|
|
|
*/ |
|
204
|
|
|
public function dn2UserName($dn) { |
|
205
|
|
|
$id = 'DN,' . $dn; |
|
206
|
|
|
return $this->handleRequest($id, 'dn2UserName', array($dn)); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* get the user's home directory |
|
211
|
|
|
* @param string $uid the username |
|
212
|
|
|
* @return boolean |
|
213
|
|
|
*/ |
|
214
|
|
|
public function getHome($uid) { |
|
215
|
|
|
return $this->handleRequest($uid, 'getHome', array($uid)); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* get display name of the user |
|
220
|
|
|
* @param string $uid user ID of the user |
|
221
|
|
|
* @return string display name |
|
222
|
|
|
*/ |
|
223
|
|
|
public function getDisplayName($uid) { |
|
224
|
|
|
return $this->handleRequest($uid, 'getDisplayName', array($uid)); |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* checks whether the user is allowed to change his avatar in ownCloud |
|
229
|
|
|
* @param string $uid the ownCloud user name |
|
230
|
|
|
* @return boolean either the user can or cannot |
|
231
|
|
|
*/ |
|
232
|
|
|
public function canChangeAvatar($uid) { |
|
233
|
|
|
return $this->handleRequest($uid, 'canChangeAvatar', array($uid), true); |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* Get a list of all display names and user ids. |
|
238
|
|
|
* @param string $search |
|
239
|
|
|
* @param string|null $limit |
|
240
|
|
|
* @param string|null $offset |
|
241
|
|
|
* @return array an array of all displayNames (value) and the corresponding uids (key) |
|
242
|
|
|
*/ |
|
243
|
|
|
public function getDisplayNames($search = '', $limit = null, $offset = null) { |
|
244
|
|
|
//we do it just as the /OC_User implementation: do not play around with limit and offset but ask all backends |
|
245
|
|
|
$users = array(); |
|
246
|
|
|
foreach($this->backends as $backend) { |
|
247
|
|
|
$backendUsers = $backend->getDisplayNames($search, $limit, $offset); |
|
248
|
|
|
if (is_array($backendUsers)) { |
|
249
|
|
|
$users = $users + $backendUsers; |
|
250
|
|
|
} |
|
251
|
|
|
} |
|
252
|
|
|
return $users; |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
/** |
|
256
|
|
|
* delete a user |
|
257
|
|
|
* @param string $uid The username of the user to delete |
|
258
|
|
|
* @return bool |
|
259
|
|
|
* |
|
260
|
|
|
* Deletes a user |
|
261
|
|
|
*/ |
|
262
|
|
|
public function deleteUser($uid) { |
|
263
|
|
|
return $this->handleRequest($uid, 'deleteUser', array($uid)); |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
/** |
|
267
|
|
|
* Set password |
|
268
|
|
|
* @param string $uid The username |
|
269
|
|
|
* @param string $password The new password |
|
270
|
|
|
* @return bool |
|
271
|
|
|
* |
|
272
|
|
|
*/ |
|
273
|
|
|
public function setPassword($uid, $password) { |
|
274
|
|
|
return $this->handleRequest($uid, 'setPassword', array($uid, $password)); |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
/** |
|
278
|
|
|
* @return bool |
|
279
|
|
|
*/ |
|
280
|
|
|
public function hasUserListings() { |
|
281
|
|
|
return $this->refBackend->hasUserListings(); |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
/** |
|
285
|
|
|
* Count the number of users |
|
286
|
|
|
* @return int|bool |
|
287
|
|
|
*/ |
|
288
|
|
|
public function countUsers() { |
|
289
|
|
|
$users = false; |
|
290
|
|
|
foreach($this->backends as $backend) { |
|
291
|
|
|
$backendUsers = $backend->countUsers(); |
|
292
|
|
|
if ($backendUsers !== false) { |
|
293
|
|
|
$users += $backendUsers; |
|
294
|
|
|
} |
|
295
|
|
|
} |
|
296
|
|
|
return $users; |
|
297
|
|
|
} |
|
298
|
|
|
|
|
299
|
|
|
/** |
|
300
|
|
|
* Return access for LDAP interaction. |
|
301
|
|
|
* @param string $uid |
|
302
|
|
|
* @return Access instance of Access for LDAP interaction |
|
303
|
|
|
*/ |
|
304
|
|
|
public function getLDAPAccess($uid) { |
|
305
|
|
|
return $this->handleRequest($uid, 'getLDAPAccess', array($uid)); |
|
306
|
|
|
} |
|
307
|
|
|
|
|
308
|
|
|
/** |
|
309
|
|
|
* Return a new LDAP connection for the specified user. |
|
310
|
|
|
* The connection needs to be closed manually. |
|
311
|
|
|
* @param string $uid |
|
312
|
|
|
* @return resource of the LDAP connection |
|
313
|
|
|
*/ |
|
314
|
|
|
public function getNewLDAPConnection($uid) { |
|
315
|
|
|
return $this->handleRequest($uid, 'getNewLDAPConnection', array($uid)); |
|
316
|
|
|
} |
|
317
|
|
|
} |
|
318
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.