@@ -94,7 +94,7 @@ discard block |
||
94 | 94 | case 'database': |
95 | 95 | case 'mysql': |
96 | 96 | case 'sqlite': |
97 | - \OCP\Util::writeLog('core', 'Adding user backend ' . $backend . '.', \OCP\Util::DEBUG); |
|
97 | + \OCP\Util::writeLog('core', 'Adding user backend '.$backend.'.', \OCP\Util::DEBUG); |
|
98 | 98 | self::$_usedBackends[$backend] = new \OC\User\Database(); |
99 | 99 | \OC::$server->getUserManager()->registerBackend(self::$_usedBackends[$backend]); |
100 | 100 | break; |
@@ -103,8 +103,8 @@ discard block |
||
103 | 103 | \OC::$server->getUserManager()->registerBackend(self::$_usedBackends[$backend]); |
104 | 104 | break; |
105 | 105 | default: |
106 | - \OCP\Util::writeLog('core', 'Adding default user backend ' . $backend . '.', \OCP\Util::DEBUG); |
|
107 | - $className = 'OC_USER_' . strtoupper($backend); |
|
106 | + \OCP\Util::writeLog('core', 'Adding default user backend '.$backend.'.', \OCP\Util::DEBUG); |
|
107 | + $className = 'OC_USER_'.strtoupper($backend); |
|
108 | 108 | self::$_usedBackends[$backend] = new $className(); |
109 | 109 | \OC::$server->getUserManager()->registerBackend(self::$_usedBackends[$backend]); |
110 | 110 | break; |
@@ -140,10 +140,10 @@ discard block |
||
140 | 140 | self::useBackend($backend); |
141 | 141 | self::$_setupedBackends[] = $i; |
142 | 142 | } else { |
143 | - \OCP\Util::writeLog('core', 'User backend ' . $class . ' already initialized.', \OCP\Util::DEBUG); |
|
143 | + \OCP\Util::writeLog('core', 'User backend '.$class.' already initialized.', \OCP\Util::DEBUG); |
|
144 | 144 | } |
145 | 145 | } else { |
146 | - \OCP\Util::writeLog('core', 'User backend ' . $class . ' not found.', \OCP\Util::ERROR); |
|
146 | + \OCP\Util::writeLog('core', 'User backend '.$class.' not found.', \OCP\Util::ERROR); |
|
147 | 147 | } |
148 | 148 | } |
149 | 149 | } |
@@ -471,7 +471,7 @@ discard block |
||
471 | 471 | if ($user) { |
472 | 472 | return $user->getHome(); |
473 | 473 | } else { |
474 | - return \OC::$server->getSystemConfig()->getValue('datadirectory', OC::$SERVERROOT . '/data') . '/' . $uid; |
|
474 | + return \OC::$server->getSystemConfig()->getValue('datadirectory', OC::$SERVERROOT.'/data').'/'.$uid; |
|
475 | 475 | } |
476 | 476 | } |
477 | 477 |
@@ -57,436 +57,436 @@ |
||
57 | 57 | */ |
58 | 58 | class OC_User { |
59 | 59 | |
60 | - /** |
|
61 | - * @return \OC\User\Session |
|
62 | - */ |
|
63 | - public static function getUserSession() { |
|
64 | - return OC::$server->getUserSession(); |
|
65 | - } |
|
66 | - |
|
67 | - private static $_usedBackends = array(); |
|
68 | - |
|
69 | - private static $_setupedBackends = array(); |
|
70 | - |
|
71 | - // bool, stores if a user want to access a resource anonymously, e.g if they open a public link |
|
72 | - private static $incognitoMode = false; |
|
73 | - |
|
74 | - /** |
|
75 | - * Adds the backend to the list of used backends |
|
76 | - * |
|
77 | - * @param string|\OCP\UserInterface $backend default: database The backend to use for user management |
|
78 | - * @return bool |
|
79 | - * |
|
80 | - * Set the User Authentication Module |
|
81 | - */ |
|
82 | - public static function useBackend($backend = 'database') { |
|
83 | - if ($backend instanceof \OCP\UserInterface) { |
|
84 | - self::$_usedBackends[get_class($backend)] = $backend; |
|
85 | - \OC::$server->getUserManager()->registerBackend($backend); |
|
86 | - } else { |
|
87 | - // You'll never know what happens |
|
88 | - if (null === $backend OR !is_string($backend)) { |
|
89 | - $backend = 'database'; |
|
90 | - } |
|
91 | - |
|
92 | - // Load backend |
|
93 | - switch ($backend) { |
|
94 | - case 'database': |
|
95 | - case 'mysql': |
|
96 | - case 'sqlite': |
|
97 | - \OCP\Util::writeLog('core', 'Adding user backend ' . $backend . '.', \OCP\Util::DEBUG); |
|
98 | - self::$_usedBackends[$backend] = new \OC\User\Database(); |
|
99 | - \OC::$server->getUserManager()->registerBackend(self::$_usedBackends[$backend]); |
|
100 | - break; |
|
101 | - case 'dummy': |
|
102 | - self::$_usedBackends[$backend] = new \Test\Util\User\Dummy(); |
|
103 | - \OC::$server->getUserManager()->registerBackend(self::$_usedBackends[$backend]); |
|
104 | - break; |
|
105 | - default: |
|
106 | - \OCP\Util::writeLog('core', 'Adding default user backend ' . $backend . '.', \OCP\Util::DEBUG); |
|
107 | - $className = 'OC_USER_' . strtoupper($backend); |
|
108 | - self::$_usedBackends[$backend] = new $className(); |
|
109 | - \OC::$server->getUserManager()->registerBackend(self::$_usedBackends[$backend]); |
|
110 | - break; |
|
111 | - } |
|
112 | - } |
|
113 | - return true; |
|
114 | - } |
|
115 | - |
|
116 | - /** |
|
117 | - * remove all used backends |
|
118 | - */ |
|
119 | - public static function clearBackends() { |
|
120 | - self::$_usedBackends = array(); |
|
121 | - \OC::$server->getUserManager()->clearBackends(); |
|
122 | - } |
|
123 | - |
|
124 | - /** |
|
125 | - * setup the configured backends in config.php |
|
126 | - */ |
|
127 | - public static function setupBackends() { |
|
128 | - OC_App::loadApps(['prelogin']); |
|
129 | - $backends = \OC::$server->getSystemConfig()->getValue('user_backends', []); |
|
130 | - if (isset($backends['default']) && !$backends['default']) { |
|
131 | - // clear default backends |
|
132 | - self::clearBackends(); |
|
133 | - } |
|
134 | - foreach ($backends as $i => $config) { |
|
135 | - if (!is_array($config)) { |
|
136 | - continue; |
|
137 | - } |
|
138 | - $class = $config['class']; |
|
139 | - $arguments = $config['arguments']; |
|
140 | - if (class_exists($class)) { |
|
141 | - if (array_search($i, self::$_setupedBackends) === false) { |
|
142 | - // make a reflection object |
|
143 | - $reflectionObj = new ReflectionClass($class); |
|
144 | - |
|
145 | - // use Reflection to create a new instance, using the $args |
|
146 | - $backend = $reflectionObj->newInstanceArgs($arguments); |
|
147 | - self::useBackend($backend); |
|
148 | - self::$_setupedBackends[] = $i; |
|
149 | - } else { |
|
150 | - \OCP\Util::writeLog('core', 'User backend ' . $class . ' already initialized.', \OCP\Util::DEBUG); |
|
151 | - } |
|
152 | - } else { |
|
153 | - \OCP\Util::writeLog('core', 'User backend ' . $class . ' not found.', \OCP\Util::ERROR); |
|
154 | - } |
|
155 | - } |
|
156 | - } |
|
157 | - |
|
158 | - /** |
|
159 | - * Try to login a user, assuming authentication |
|
160 | - * has already happened (e.g. via Single Sign On). |
|
161 | - * |
|
162 | - * Log in a user and regenerate a new session. |
|
163 | - * |
|
164 | - * @param \OCP\Authentication\IApacheBackend $backend |
|
165 | - * @return bool |
|
166 | - */ |
|
167 | - public static function loginWithApache(\OCP\Authentication\IApacheBackend $backend) { |
|
168 | - |
|
169 | - $uid = $backend->getCurrentUserId(); |
|
170 | - $run = true; |
|
171 | - OC_Hook::emit("OC_User", "pre_login", array("run" => &$run, "uid" => $uid)); |
|
172 | - |
|
173 | - if ($uid) { |
|
174 | - if (self::getUser() !== $uid) { |
|
175 | - self::setUserId($uid); |
|
176 | - $userSession = self::getUserSession(); |
|
177 | - $userSession->setLoginName($uid); |
|
178 | - $request = OC::$server->getRequest(); |
|
179 | - $userSession->createSessionToken($request, $uid, $uid); |
|
180 | - // setup the filesystem |
|
181 | - OC_Util::setupFS($uid); |
|
182 | - // first call the post_login hooks, the login-process needs to be |
|
183 | - // completed before we can safely create the users folder. |
|
184 | - // For example encryption needs to initialize the users keys first |
|
185 | - // before we can create the user folder with the skeleton files |
|
186 | - OC_Hook::emit("OC_User", "post_login", array("uid" => $uid, 'password' => '')); |
|
187 | - //trigger creation of user home and /files folder |
|
188 | - \OC::$server->getUserFolder($uid); |
|
189 | - } |
|
190 | - return true; |
|
191 | - } |
|
192 | - return false; |
|
193 | - } |
|
194 | - |
|
195 | - /** |
|
196 | - * Verify with Apache whether user is authenticated. |
|
197 | - * |
|
198 | - * @return boolean|null |
|
199 | - * true: authenticated |
|
200 | - * false: not authenticated |
|
201 | - * null: not handled / no backend available |
|
202 | - */ |
|
203 | - public static function handleApacheAuth() { |
|
204 | - $backend = self::findFirstActiveUsedBackend(); |
|
205 | - if ($backend) { |
|
206 | - OC_App::loadApps(); |
|
207 | - |
|
208 | - //setup extra user backends |
|
209 | - self::setupBackends(); |
|
210 | - self::getUserSession()->unsetMagicInCookie(); |
|
211 | - |
|
212 | - return self::loginWithApache($backend); |
|
213 | - } |
|
214 | - |
|
215 | - return null; |
|
216 | - } |
|
217 | - |
|
218 | - |
|
219 | - /** |
|
220 | - * Sets user id for session and triggers emit |
|
221 | - * |
|
222 | - * @param string $uid |
|
223 | - */ |
|
224 | - public static function setUserId($uid) { |
|
225 | - $userSession = \OC::$server->getUserSession(); |
|
226 | - $userManager = \OC::$server->getUserManager(); |
|
227 | - if ($user = $userManager->get($uid)) { |
|
228 | - $userSession->setUser($user); |
|
229 | - } else { |
|
230 | - \OC::$server->getSession()->set('user_id', $uid); |
|
231 | - } |
|
232 | - } |
|
233 | - |
|
234 | - /** |
|
235 | - * Sets user display name for session |
|
236 | - * |
|
237 | - * @param string $uid |
|
238 | - * @param string $displayName |
|
239 | - * @return bool Whether the display name could get set |
|
240 | - */ |
|
241 | - public static function setDisplayName($uid, $displayName = null) { |
|
242 | - if (is_null($displayName)) { |
|
243 | - $displayName = $uid; |
|
244 | - } |
|
245 | - $user = \OC::$server->getUserManager()->get($uid); |
|
246 | - if ($user) { |
|
247 | - return $user->setDisplayName($displayName); |
|
248 | - } else { |
|
249 | - return false; |
|
250 | - } |
|
251 | - } |
|
252 | - |
|
253 | - /** |
|
254 | - * Check if the user is logged in, considers also the HTTP basic credentials |
|
255 | - * |
|
256 | - * @deprecated use \OC::$server->getUserSession()->isLoggedIn() |
|
257 | - * @return bool |
|
258 | - */ |
|
259 | - public static function isLoggedIn() { |
|
260 | - return \OC::$server->getUserSession()->isLoggedIn(); |
|
261 | - } |
|
262 | - |
|
263 | - /** |
|
264 | - * set incognito mode, e.g. if a user wants to open a public link |
|
265 | - * |
|
266 | - * @param bool $status |
|
267 | - */ |
|
268 | - public static function setIncognitoMode($status) { |
|
269 | - self::$incognitoMode = $status; |
|
270 | - } |
|
271 | - |
|
272 | - /** |
|
273 | - * get incognito mode status |
|
274 | - * |
|
275 | - * @return bool |
|
276 | - */ |
|
277 | - public static function isIncognitoMode() { |
|
278 | - return self::$incognitoMode; |
|
279 | - } |
|
280 | - |
|
281 | - /** |
|
282 | - * Supplies an attribute to the logout hyperlink. The default behaviour |
|
283 | - * is to return an href with '?logout=true' appended. However, it can |
|
284 | - * supply any attribute(s) which are valid for <a>. |
|
285 | - * |
|
286 | - * @return string with one or more HTML attributes. |
|
287 | - */ |
|
288 | - public static function getLogoutAttribute() { |
|
289 | - $backend = self::findFirstActiveUsedBackend(); |
|
290 | - if ($backend) { |
|
291 | - return $backend->getLogoutAttribute(); |
|
292 | - } |
|
293 | - |
|
294 | - $logoutUrl = \OC::$server->getURLGenerator()->linkToRouteAbsolute( |
|
295 | - 'core.login.logout', |
|
296 | - [ |
|
297 | - 'requesttoken' => \OCP\Util::callRegister(), |
|
298 | - ] |
|
299 | - ); |
|
300 | - |
|
301 | - return 'href="'.$logoutUrl.'"'; |
|
302 | - } |
|
303 | - |
|
304 | - /** |
|
305 | - * Check if the user is an admin user |
|
306 | - * |
|
307 | - * @param string $uid uid of the admin |
|
308 | - * @return bool |
|
309 | - */ |
|
310 | - public static function isAdminUser($uid) { |
|
311 | - $group = \OC::$server->getGroupManager()->get('admin'); |
|
312 | - $user = \OC::$server->getUserManager()->get($uid); |
|
313 | - if ($group && $user && $group->inGroup($user) && self::$incognitoMode === false) { |
|
314 | - return true; |
|
315 | - } |
|
316 | - return false; |
|
317 | - } |
|
318 | - |
|
319 | - |
|
320 | - /** |
|
321 | - * get the user id of the user currently logged in. |
|
322 | - * |
|
323 | - * @return string|bool uid or false |
|
324 | - */ |
|
325 | - public static function getUser() { |
|
326 | - $uid = \OC::$server->getSession() ? \OC::$server->getSession()->get('user_id') : null; |
|
327 | - if (!is_null($uid) && self::$incognitoMode === false) { |
|
328 | - return $uid; |
|
329 | - } else { |
|
330 | - return false; |
|
331 | - } |
|
332 | - } |
|
333 | - |
|
334 | - /** |
|
335 | - * get the display name of the user currently logged in. |
|
336 | - * |
|
337 | - * @param string $uid |
|
338 | - * @return string uid or false |
|
339 | - */ |
|
340 | - public static function getDisplayName($uid = null) { |
|
341 | - if ($uid) { |
|
342 | - $user = \OC::$server->getUserManager()->get($uid); |
|
343 | - if ($user) { |
|
344 | - return $user->getDisplayName(); |
|
345 | - } else { |
|
346 | - return $uid; |
|
347 | - } |
|
348 | - } else { |
|
349 | - $user = self::getUserSession()->getUser(); |
|
350 | - if ($user) { |
|
351 | - return $user->getDisplayName(); |
|
352 | - } else { |
|
353 | - return false; |
|
354 | - } |
|
355 | - } |
|
356 | - } |
|
357 | - |
|
358 | - /** |
|
359 | - * Set password |
|
360 | - * |
|
361 | - * @param string $uid The username |
|
362 | - * @param string $password The new password |
|
363 | - * @param string $recoveryPassword for the encryption app to reset encryption keys |
|
364 | - * @return bool |
|
365 | - * |
|
366 | - * Change the password of a user |
|
367 | - */ |
|
368 | - public static function setPassword($uid, $password, $recoveryPassword = null) { |
|
369 | - $user = \OC::$server->getUserManager()->get($uid); |
|
370 | - if ($user) { |
|
371 | - return $user->setPassword($password, $recoveryPassword); |
|
372 | - } else { |
|
373 | - return false; |
|
374 | - } |
|
375 | - } |
|
376 | - |
|
377 | - /** |
|
378 | - * Check if the password is correct |
|
379 | - * |
|
380 | - * @param string $uid The username |
|
381 | - * @param string $password The password |
|
382 | - * @return string|false user id a string on success, false otherwise |
|
383 | - * |
|
384 | - * Check if the password is correct without logging in the user |
|
385 | - * returns the user id or false |
|
386 | - */ |
|
387 | - public static function checkPassword($uid, $password) { |
|
388 | - $manager = \OC::$server->getUserManager(); |
|
389 | - $username = $manager->checkPassword($uid, $password); |
|
390 | - if ($username !== false) { |
|
391 | - return $username->getUID(); |
|
392 | - } |
|
393 | - return false; |
|
394 | - } |
|
395 | - |
|
396 | - /** |
|
397 | - * @param string $uid The username |
|
398 | - * @return string |
|
399 | - * |
|
400 | - * returns the path to the users home directory |
|
401 | - * @deprecated Use \OC::$server->getUserManager->getHome() |
|
402 | - */ |
|
403 | - public static function getHome($uid) { |
|
404 | - $user = \OC::$server->getUserManager()->get($uid); |
|
405 | - if ($user) { |
|
406 | - return $user->getHome(); |
|
407 | - } else { |
|
408 | - return \OC::$server->getSystemConfig()->getValue('datadirectory', OC::$SERVERROOT . '/data') . '/' . $uid; |
|
409 | - } |
|
410 | - } |
|
411 | - |
|
412 | - /** |
|
413 | - * Get a list of all users |
|
414 | - * |
|
415 | - * @return array an array of all uids |
|
416 | - * |
|
417 | - * Get a list of all users. |
|
418 | - * @param string $search |
|
419 | - * @param integer $limit |
|
420 | - * @param integer $offset |
|
421 | - */ |
|
422 | - public static function getUsers($search = '', $limit = null, $offset = null) { |
|
423 | - $users = \OC::$server->getUserManager()->search($search, $limit, $offset); |
|
424 | - $uids = array(); |
|
425 | - foreach ($users as $user) { |
|
426 | - $uids[] = $user->getUID(); |
|
427 | - } |
|
428 | - return $uids; |
|
429 | - } |
|
430 | - |
|
431 | - /** |
|
432 | - * Get a list of all users display name |
|
433 | - * |
|
434 | - * @param string $search |
|
435 | - * @param int $limit |
|
436 | - * @param int $offset |
|
437 | - * @return array associative array with all display names (value) and corresponding uids (key) |
|
438 | - * |
|
439 | - * Get a list of all display names and user ids. |
|
440 | - * @deprecated Use \OC::$server->getUserManager->searchDisplayName($search, $limit, $offset) instead. |
|
441 | - */ |
|
442 | - public static function getDisplayNames($search = '', $limit = null, $offset = null) { |
|
443 | - $displayNames = array(); |
|
444 | - $users = \OC::$server->getUserManager()->searchDisplayName($search, $limit, $offset); |
|
445 | - foreach ($users as $user) { |
|
446 | - $displayNames[$user->getUID()] = $user->getDisplayName(); |
|
447 | - } |
|
448 | - return $displayNames; |
|
449 | - } |
|
450 | - |
|
451 | - /** |
|
452 | - * check if a user exists |
|
453 | - * |
|
454 | - * @param string $uid the username |
|
455 | - * @return boolean |
|
456 | - */ |
|
457 | - public static function userExists($uid) { |
|
458 | - return \OC::$server->getUserManager()->userExists($uid); |
|
459 | - } |
|
460 | - |
|
461 | - /** |
|
462 | - * checks if a user is enabled |
|
463 | - * |
|
464 | - * @param string $uid |
|
465 | - * @return bool |
|
466 | - */ |
|
467 | - public static function isEnabled($uid) { |
|
468 | - $user = \OC::$server->getUserManager()->get($uid); |
|
469 | - if ($user) { |
|
470 | - return $user->isEnabled(); |
|
471 | - } else { |
|
472 | - return false; |
|
473 | - } |
|
474 | - } |
|
475 | - |
|
476 | - /** |
|
477 | - * Returns the first active backend from self::$_usedBackends. |
|
478 | - * |
|
479 | - * @return OCP\Authentication\IApacheBackend|null if no backend active, otherwise OCP\Authentication\IApacheBackend |
|
480 | - */ |
|
481 | - private static function findFirstActiveUsedBackend() { |
|
482 | - foreach (self::$_usedBackends as $backend) { |
|
483 | - if ($backend instanceof OCP\Authentication\IApacheBackend) { |
|
484 | - if ($backend->isSessionActive()) { |
|
485 | - return $backend; |
|
486 | - } |
|
487 | - } |
|
488 | - } |
|
489 | - |
|
490 | - return null; |
|
491 | - } |
|
60 | + /** |
|
61 | + * @return \OC\User\Session |
|
62 | + */ |
|
63 | + public static function getUserSession() { |
|
64 | + return OC::$server->getUserSession(); |
|
65 | + } |
|
66 | + |
|
67 | + private static $_usedBackends = array(); |
|
68 | + |
|
69 | + private static $_setupedBackends = array(); |
|
70 | + |
|
71 | + // bool, stores if a user want to access a resource anonymously, e.g if they open a public link |
|
72 | + private static $incognitoMode = false; |
|
73 | + |
|
74 | + /** |
|
75 | + * Adds the backend to the list of used backends |
|
76 | + * |
|
77 | + * @param string|\OCP\UserInterface $backend default: database The backend to use for user management |
|
78 | + * @return bool |
|
79 | + * |
|
80 | + * Set the User Authentication Module |
|
81 | + */ |
|
82 | + public static function useBackend($backend = 'database') { |
|
83 | + if ($backend instanceof \OCP\UserInterface) { |
|
84 | + self::$_usedBackends[get_class($backend)] = $backend; |
|
85 | + \OC::$server->getUserManager()->registerBackend($backend); |
|
86 | + } else { |
|
87 | + // You'll never know what happens |
|
88 | + if (null === $backend OR !is_string($backend)) { |
|
89 | + $backend = 'database'; |
|
90 | + } |
|
91 | + |
|
92 | + // Load backend |
|
93 | + switch ($backend) { |
|
94 | + case 'database': |
|
95 | + case 'mysql': |
|
96 | + case 'sqlite': |
|
97 | + \OCP\Util::writeLog('core', 'Adding user backend ' . $backend . '.', \OCP\Util::DEBUG); |
|
98 | + self::$_usedBackends[$backend] = new \OC\User\Database(); |
|
99 | + \OC::$server->getUserManager()->registerBackend(self::$_usedBackends[$backend]); |
|
100 | + break; |
|
101 | + case 'dummy': |
|
102 | + self::$_usedBackends[$backend] = new \Test\Util\User\Dummy(); |
|
103 | + \OC::$server->getUserManager()->registerBackend(self::$_usedBackends[$backend]); |
|
104 | + break; |
|
105 | + default: |
|
106 | + \OCP\Util::writeLog('core', 'Adding default user backend ' . $backend . '.', \OCP\Util::DEBUG); |
|
107 | + $className = 'OC_USER_' . strtoupper($backend); |
|
108 | + self::$_usedBackends[$backend] = new $className(); |
|
109 | + \OC::$server->getUserManager()->registerBackend(self::$_usedBackends[$backend]); |
|
110 | + break; |
|
111 | + } |
|
112 | + } |
|
113 | + return true; |
|
114 | + } |
|
115 | + |
|
116 | + /** |
|
117 | + * remove all used backends |
|
118 | + */ |
|
119 | + public static function clearBackends() { |
|
120 | + self::$_usedBackends = array(); |
|
121 | + \OC::$server->getUserManager()->clearBackends(); |
|
122 | + } |
|
123 | + |
|
124 | + /** |
|
125 | + * setup the configured backends in config.php |
|
126 | + */ |
|
127 | + public static function setupBackends() { |
|
128 | + OC_App::loadApps(['prelogin']); |
|
129 | + $backends = \OC::$server->getSystemConfig()->getValue('user_backends', []); |
|
130 | + if (isset($backends['default']) && !$backends['default']) { |
|
131 | + // clear default backends |
|
132 | + self::clearBackends(); |
|
133 | + } |
|
134 | + foreach ($backends as $i => $config) { |
|
135 | + if (!is_array($config)) { |
|
136 | + continue; |
|
137 | + } |
|
138 | + $class = $config['class']; |
|
139 | + $arguments = $config['arguments']; |
|
140 | + if (class_exists($class)) { |
|
141 | + if (array_search($i, self::$_setupedBackends) === false) { |
|
142 | + // make a reflection object |
|
143 | + $reflectionObj = new ReflectionClass($class); |
|
144 | + |
|
145 | + // use Reflection to create a new instance, using the $args |
|
146 | + $backend = $reflectionObj->newInstanceArgs($arguments); |
|
147 | + self::useBackend($backend); |
|
148 | + self::$_setupedBackends[] = $i; |
|
149 | + } else { |
|
150 | + \OCP\Util::writeLog('core', 'User backend ' . $class . ' already initialized.', \OCP\Util::DEBUG); |
|
151 | + } |
|
152 | + } else { |
|
153 | + \OCP\Util::writeLog('core', 'User backend ' . $class . ' not found.', \OCP\Util::ERROR); |
|
154 | + } |
|
155 | + } |
|
156 | + } |
|
157 | + |
|
158 | + /** |
|
159 | + * Try to login a user, assuming authentication |
|
160 | + * has already happened (e.g. via Single Sign On). |
|
161 | + * |
|
162 | + * Log in a user and regenerate a new session. |
|
163 | + * |
|
164 | + * @param \OCP\Authentication\IApacheBackend $backend |
|
165 | + * @return bool |
|
166 | + */ |
|
167 | + public static function loginWithApache(\OCP\Authentication\IApacheBackend $backend) { |
|
168 | + |
|
169 | + $uid = $backend->getCurrentUserId(); |
|
170 | + $run = true; |
|
171 | + OC_Hook::emit("OC_User", "pre_login", array("run" => &$run, "uid" => $uid)); |
|
172 | + |
|
173 | + if ($uid) { |
|
174 | + if (self::getUser() !== $uid) { |
|
175 | + self::setUserId($uid); |
|
176 | + $userSession = self::getUserSession(); |
|
177 | + $userSession->setLoginName($uid); |
|
178 | + $request = OC::$server->getRequest(); |
|
179 | + $userSession->createSessionToken($request, $uid, $uid); |
|
180 | + // setup the filesystem |
|
181 | + OC_Util::setupFS($uid); |
|
182 | + // first call the post_login hooks, the login-process needs to be |
|
183 | + // completed before we can safely create the users folder. |
|
184 | + // For example encryption needs to initialize the users keys first |
|
185 | + // before we can create the user folder with the skeleton files |
|
186 | + OC_Hook::emit("OC_User", "post_login", array("uid" => $uid, 'password' => '')); |
|
187 | + //trigger creation of user home and /files folder |
|
188 | + \OC::$server->getUserFolder($uid); |
|
189 | + } |
|
190 | + return true; |
|
191 | + } |
|
192 | + return false; |
|
193 | + } |
|
194 | + |
|
195 | + /** |
|
196 | + * Verify with Apache whether user is authenticated. |
|
197 | + * |
|
198 | + * @return boolean|null |
|
199 | + * true: authenticated |
|
200 | + * false: not authenticated |
|
201 | + * null: not handled / no backend available |
|
202 | + */ |
|
203 | + public static function handleApacheAuth() { |
|
204 | + $backend = self::findFirstActiveUsedBackend(); |
|
205 | + if ($backend) { |
|
206 | + OC_App::loadApps(); |
|
207 | + |
|
208 | + //setup extra user backends |
|
209 | + self::setupBackends(); |
|
210 | + self::getUserSession()->unsetMagicInCookie(); |
|
211 | + |
|
212 | + return self::loginWithApache($backend); |
|
213 | + } |
|
214 | + |
|
215 | + return null; |
|
216 | + } |
|
217 | + |
|
218 | + |
|
219 | + /** |
|
220 | + * Sets user id for session and triggers emit |
|
221 | + * |
|
222 | + * @param string $uid |
|
223 | + */ |
|
224 | + public static function setUserId($uid) { |
|
225 | + $userSession = \OC::$server->getUserSession(); |
|
226 | + $userManager = \OC::$server->getUserManager(); |
|
227 | + if ($user = $userManager->get($uid)) { |
|
228 | + $userSession->setUser($user); |
|
229 | + } else { |
|
230 | + \OC::$server->getSession()->set('user_id', $uid); |
|
231 | + } |
|
232 | + } |
|
233 | + |
|
234 | + /** |
|
235 | + * Sets user display name for session |
|
236 | + * |
|
237 | + * @param string $uid |
|
238 | + * @param string $displayName |
|
239 | + * @return bool Whether the display name could get set |
|
240 | + */ |
|
241 | + public static function setDisplayName($uid, $displayName = null) { |
|
242 | + if (is_null($displayName)) { |
|
243 | + $displayName = $uid; |
|
244 | + } |
|
245 | + $user = \OC::$server->getUserManager()->get($uid); |
|
246 | + if ($user) { |
|
247 | + return $user->setDisplayName($displayName); |
|
248 | + } else { |
|
249 | + return false; |
|
250 | + } |
|
251 | + } |
|
252 | + |
|
253 | + /** |
|
254 | + * Check if the user is logged in, considers also the HTTP basic credentials |
|
255 | + * |
|
256 | + * @deprecated use \OC::$server->getUserSession()->isLoggedIn() |
|
257 | + * @return bool |
|
258 | + */ |
|
259 | + public static function isLoggedIn() { |
|
260 | + return \OC::$server->getUserSession()->isLoggedIn(); |
|
261 | + } |
|
262 | + |
|
263 | + /** |
|
264 | + * set incognito mode, e.g. if a user wants to open a public link |
|
265 | + * |
|
266 | + * @param bool $status |
|
267 | + */ |
|
268 | + public static function setIncognitoMode($status) { |
|
269 | + self::$incognitoMode = $status; |
|
270 | + } |
|
271 | + |
|
272 | + /** |
|
273 | + * get incognito mode status |
|
274 | + * |
|
275 | + * @return bool |
|
276 | + */ |
|
277 | + public static function isIncognitoMode() { |
|
278 | + return self::$incognitoMode; |
|
279 | + } |
|
280 | + |
|
281 | + /** |
|
282 | + * Supplies an attribute to the logout hyperlink. The default behaviour |
|
283 | + * is to return an href with '?logout=true' appended. However, it can |
|
284 | + * supply any attribute(s) which are valid for <a>. |
|
285 | + * |
|
286 | + * @return string with one or more HTML attributes. |
|
287 | + */ |
|
288 | + public static function getLogoutAttribute() { |
|
289 | + $backend = self::findFirstActiveUsedBackend(); |
|
290 | + if ($backend) { |
|
291 | + return $backend->getLogoutAttribute(); |
|
292 | + } |
|
293 | + |
|
294 | + $logoutUrl = \OC::$server->getURLGenerator()->linkToRouteAbsolute( |
|
295 | + 'core.login.logout', |
|
296 | + [ |
|
297 | + 'requesttoken' => \OCP\Util::callRegister(), |
|
298 | + ] |
|
299 | + ); |
|
300 | + |
|
301 | + return 'href="'.$logoutUrl.'"'; |
|
302 | + } |
|
303 | + |
|
304 | + /** |
|
305 | + * Check if the user is an admin user |
|
306 | + * |
|
307 | + * @param string $uid uid of the admin |
|
308 | + * @return bool |
|
309 | + */ |
|
310 | + public static function isAdminUser($uid) { |
|
311 | + $group = \OC::$server->getGroupManager()->get('admin'); |
|
312 | + $user = \OC::$server->getUserManager()->get($uid); |
|
313 | + if ($group && $user && $group->inGroup($user) && self::$incognitoMode === false) { |
|
314 | + return true; |
|
315 | + } |
|
316 | + return false; |
|
317 | + } |
|
318 | + |
|
319 | + |
|
320 | + /** |
|
321 | + * get the user id of the user currently logged in. |
|
322 | + * |
|
323 | + * @return string|bool uid or false |
|
324 | + */ |
|
325 | + public static function getUser() { |
|
326 | + $uid = \OC::$server->getSession() ? \OC::$server->getSession()->get('user_id') : null; |
|
327 | + if (!is_null($uid) && self::$incognitoMode === false) { |
|
328 | + return $uid; |
|
329 | + } else { |
|
330 | + return false; |
|
331 | + } |
|
332 | + } |
|
333 | + |
|
334 | + /** |
|
335 | + * get the display name of the user currently logged in. |
|
336 | + * |
|
337 | + * @param string $uid |
|
338 | + * @return string uid or false |
|
339 | + */ |
|
340 | + public static function getDisplayName($uid = null) { |
|
341 | + if ($uid) { |
|
342 | + $user = \OC::$server->getUserManager()->get($uid); |
|
343 | + if ($user) { |
|
344 | + return $user->getDisplayName(); |
|
345 | + } else { |
|
346 | + return $uid; |
|
347 | + } |
|
348 | + } else { |
|
349 | + $user = self::getUserSession()->getUser(); |
|
350 | + if ($user) { |
|
351 | + return $user->getDisplayName(); |
|
352 | + } else { |
|
353 | + return false; |
|
354 | + } |
|
355 | + } |
|
356 | + } |
|
357 | + |
|
358 | + /** |
|
359 | + * Set password |
|
360 | + * |
|
361 | + * @param string $uid The username |
|
362 | + * @param string $password The new password |
|
363 | + * @param string $recoveryPassword for the encryption app to reset encryption keys |
|
364 | + * @return bool |
|
365 | + * |
|
366 | + * Change the password of a user |
|
367 | + */ |
|
368 | + public static function setPassword($uid, $password, $recoveryPassword = null) { |
|
369 | + $user = \OC::$server->getUserManager()->get($uid); |
|
370 | + if ($user) { |
|
371 | + return $user->setPassword($password, $recoveryPassword); |
|
372 | + } else { |
|
373 | + return false; |
|
374 | + } |
|
375 | + } |
|
376 | + |
|
377 | + /** |
|
378 | + * Check if the password is correct |
|
379 | + * |
|
380 | + * @param string $uid The username |
|
381 | + * @param string $password The password |
|
382 | + * @return string|false user id a string on success, false otherwise |
|
383 | + * |
|
384 | + * Check if the password is correct without logging in the user |
|
385 | + * returns the user id or false |
|
386 | + */ |
|
387 | + public static function checkPassword($uid, $password) { |
|
388 | + $manager = \OC::$server->getUserManager(); |
|
389 | + $username = $manager->checkPassword($uid, $password); |
|
390 | + if ($username !== false) { |
|
391 | + return $username->getUID(); |
|
392 | + } |
|
393 | + return false; |
|
394 | + } |
|
395 | + |
|
396 | + /** |
|
397 | + * @param string $uid The username |
|
398 | + * @return string |
|
399 | + * |
|
400 | + * returns the path to the users home directory |
|
401 | + * @deprecated Use \OC::$server->getUserManager->getHome() |
|
402 | + */ |
|
403 | + public static function getHome($uid) { |
|
404 | + $user = \OC::$server->getUserManager()->get($uid); |
|
405 | + if ($user) { |
|
406 | + return $user->getHome(); |
|
407 | + } else { |
|
408 | + return \OC::$server->getSystemConfig()->getValue('datadirectory', OC::$SERVERROOT . '/data') . '/' . $uid; |
|
409 | + } |
|
410 | + } |
|
411 | + |
|
412 | + /** |
|
413 | + * Get a list of all users |
|
414 | + * |
|
415 | + * @return array an array of all uids |
|
416 | + * |
|
417 | + * Get a list of all users. |
|
418 | + * @param string $search |
|
419 | + * @param integer $limit |
|
420 | + * @param integer $offset |
|
421 | + */ |
|
422 | + public static function getUsers($search = '', $limit = null, $offset = null) { |
|
423 | + $users = \OC::$server->getUserManager()->search($search, $limit, $offset); |
|
424 | + $uids = array(); |
|
425 | + foreach ($users as $user) { |
|
426 | + $uids[] = $user->getUID(); |
|
427 | + } |
|
428 | + return $uids; |
|
429 | + } |
|
430 | + |
|
431 | + /** |
|
432 | + * Get a list of all users display name |
|
433 | + * |
|
434 | + * @param string $search |
|
435 | + * @param int $limit |
|
436 | + * @param int $offset |
|
437 | + * @return array associative array with all display names (value) and corresponding uids (key) |
|
438 | + * |
|
439 | + * Get a list of all display names and user ids. |
|
440 | + * @deprecated Use \OC::$server->getUserManager->searchDisplayName($search, $limit, $offset) instead. |
|
441 | + */ |
|
442 | + public static function getDisplayNames($search = '', $limit = null, $offset = null) { |
|
443 | + $displayNames = array(); |
|
444 | + $users = \OC::$server->getUserManager()->searchDisplayName($search, $limit, $offset); |
|
445 | + foreach ($users as $user) { |
|
446 | + $displayNames[$user->getUID()] = $user->getDisplayName(); |
|
447 | + } |
|
448 | + return $displayNames; |
|
449 | + } |
|
450 | + |
|
451 | + /** |
|
452 | + * check if a user exists |
|
453 | + * |
|
454 | + * @param string $uid the username |
|
455 | + * @return boolean |
|
456 | + */ |
|
457 | + public static function userExists($uid) { |
|
458 | + return \OC::$server->getUserManager()->userExists($uid); |
|
459 | + } |
|
460 | + |
|
461 | + /** |
|
462 | + * checks if a user is enabled |
|
463 | + * |
|
464 | + * @param string $uid |
|
465 | + * @return bool |
|
466 | + */ |
|
467 | + public static function isEnabled($uid) { |
|
468 | + $user = \OC::$server->getUserManager()->get($uid); |
|
469 | + if ($user) { |
|
470 | + return $user->isEnabled(); |
|
471 | + } else { |
|
472 | + return false; |
|
473 | + } |
|
474 | + } |
|
475 | + |
|
476 | + /** |
|
477 | + * Returns the first active backend from self::$_usedBackends. |
|
478 | + * |
|
479 | + * @return OCP\Authentication\IApacheBackend|null if no backend active, otherwise OCP\Authentication\IApacheBackend |
|
480 | + */ |
|
481 | + private static function findFirstActiveUsedBackend() { |
|
482 | + foreach (self::$_usedBackends as $backend) { |
|
483 | + if ($backend instanceof OCP\Authentication\IApacheBackend) { |
|
484 | + if ($backend->isSessionActive()) { |
|
485 | + return $backend; |
|
486 | + } |
|
487 | + } |
|
488 | + } |
|
489 | + |
|
490 | + return null; |
|
491 | + } |
|
492 | 492 | } |