Blame view
sources/lib/private/user.php
16 KB
|
31b7f2792
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
<?php
/**
* ownCloud
*
* @author Frank Karlitschek
* @copyright 2012 Frank Karlitschek frank@owncloud.org
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/
/**
* This class provides wrapper methods for user management. Multiple backends are
* supported. User management operations are delegated to the configured backend for
* execution.
*
* Hooks provided:
* pre_createUser(&run, uid, password)
* post_createUser(uid, password)
* pre_deleteUser(&run, uid)
* post_deleteUser(uid)
* pre_setPassword(&run, uid, password, recoveryPassword)
* post_setPassword(uid, password, recoveryPassword)
* pre_login(&run, uid, password)
* post_login(uid)
* logout()
*/
class OC_User {
|
|
6d9380f96
|
40 41 42 43 |
/** * @return \OC\User\Session */ |
|
31b7f2792
|
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
public static function getUserSession() {
return OC::$server->getUserSession();
}
/**
* @return \OC\User\Manager
*/
public static function getManager() {
return OC::$server->getUserManager();
}
private static $_backends = array();
private static $_usedBackends = array();
private static $_setupedBackends = array();
// bool, stores if a user want to access a resource anonymously, e.g if he opens a public link
private static $incognitoMode = false;
/**
|
|
6d9380f96
|
65 |
* registers backend |
|
31b7f2792
|
66 67 68 69 70 71 72 73 74 75 76 77 |
* @param string $backend name of the backend
* @deprecated Add classes by calling useBackend with a class instance instead
* @return bool
*
* Makes a list of backends that can be used by other modules
*/
public static function registerBackend($backend) {
self::$_backends[] = $backend;
return true;
}
/**
|
|
6d9380f96
|
78 |
* gets available backends |
|
31b7f2792
|
79 |
* @deprecated |
|
6d9380f96
|
80 |
* @return array an array of backends |
|
31b7f2792
|
81 82 83 84 85 86 87 88 |
*
* Returns the names of all backends.
*/
public static function getBackends() {
return self::$_backends;
}
/**
|
|
6d9380f96
|
89 |
* gets used backends |
|
31b7f2792
|
90 |
* @deprecated |
|
6d9380f96
|
91 |
* @return array an array of backends |
|
31b7f2792
|
92 93 94 95 96 97 98 99 |
*
* Returns the names of all used backends.
*/
public static function getUsedBackends() {
return array_keys(self::$_usedBackends);
}
/**
|
|
6d9380f96
|
100 101 |
* Adds the backend to the list of used backends * @param string|OC_User_Interface $backend default: database The backend to use for user management |
|
31b7f2792
|
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
* @return bool
*
* Set the User Authentication Module
*/
public static function useBackend($backend = 'database') {
if ($backend instanceof OC_User_Interface) {
self::$_usedBackends[get_class($backend)] = $backend;
self::getManager()->registerBackend($backend);
} else {
// You'll never know what happens
if (null === $backend OR !is_string($backend)) {
$backend = 'database';
}
// Load backend
switch ($backend) {
case 'database':
case 'mysql':
case 'sqlite':
OC_Log::write('core', 'Adding user backend ' . $backend . '.', OC_Log::DEBUG);
self::$_usedBackends[$backend] = new OC_User_Database();
self::getManager()->registerBackend(self::$_usedBackends[$backend]);
break;
default:
OC_Log::write('core', 'Adding default user backend ' . $backend . '.', OC_Log::DEBUG);
$className = 'OC_USER_' . strToUpper($backend);
self::$_usedBackends[$backend] = new $className();
self::getManager()->registerBackend(self::$_usedBackends[$backend]);
break;
}
}
return true;
}
/**
* remove all used backends
*/
public static function clearBackends() {
self::$_usedBackends = array();
self::getManager()->clearBackends();
}
/**
* setup the configured backends in config.php
*/
public static function setupBackends() {
OC_App::loadApps(array('prelogin'));
$backends = OC_Config::getValue('user_backends', array());
foreach ($backends as $i => $config) {
$class = $config['class'];
$arguments = $config['arguments'];
if (class_exists($class)) {
if (array_search($i, self::$_setupedBackends) === false) {
// make a reflection object
$reflectionObj = new ReflectionClass($class);
// use Reflection to create a new instance, using the $args
$backend = $reflectionObj->newInstanceArgs($arguments);
self::useBackend($backend);
self::$_setupedBackends[] = $i;
} else {
OC_Log::write('core', 'User backend ' . $class . ' already initialized.', OC_Log::DEBUG);
}
} else {
OC_Log::write('core', 'User backend ' . $class . ' not found.', OC_Log::ERROR);
}
}
}
/**
|
|
6d9380f96
|
172 |
* Create a new user |
|
31b7f2792
|
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
* @param string $uid The username of the user to create
* @param string $password The password of the new user
* @throws Exception
* @return bool true/false
*
* Creates a new user. Basic checking of username is done in OC_User
* itself, not in its subclasses.
*
* Allowed characters in the username are: "a-z", "A-Z", "0-9" and "_.@-"
*/
public static function createUser($uid, $password) {
return self::getManager()->createUser($uid, $password);
}
/**
|
|
6d9380f96
|
188 |
* delete a user |
|
31b7f2792
|
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
* @param string $uid The username of the user to delete
* @return bool
*
* Deletes a user
*/
public static function deleteUser($uid) {
$user = self::getManager()->get($uid);
if ($user) {
$result = $user->delete();
// if delete was successful we clean-up the rest
if ($result) {
// We have to delete the user from all groups
foreach (OC_Group::getUserGroups($uid) as $i) {
OC_Group::removeFromGroup($uid, $i);
}
// Delete the user's keys in preferences
OC_Preferences::deleteUser($uid);
// Delete user files in /data/
|
|
6d9380f96
|
210 |
OC_Helper::rmdirr(\OC_User::getHome($uid)); |
|
a293d369c
|
211 212 213 |
// Delete the users entry in the storage table
\OC\Files\Cache\Storage::remove('home::' . $uid);
|
|
31b7f2792
|
214 215 216 217 218 219 220 221 222 223 224 225 |
// Remove it from the Cache
self::getManager()->delete($uid);
}
return true;
} else {
return false;
}
}
/**
|
|
6d9380f96
|
226 227 228 229 |
* Try to login a user * @param string $uid The username of the user to log in * @param string $password The password of the user * @return boolean|null |
|
31b7f2792
|
230 231 232 233 |
*
* Log in a user and regenerate a new session - if the password is ok
*/
public static function login($uid, $password) {
|
|
a293d369c
|
234 |
session_regenerate_id(true); |
|
31b7f2792
|
235 236 237 238 |
return self::getUserSession()->login($uid, $password); } /** |
|
6d9380f96
|
239 240 241 242 243 244 245 246 247 248 249 250 |
* Try to login a user using the magic cookie (remember login)
*
* @param string $uid The username of the user to log in
* @param string $token
* @return bool
*/
public static function loginWithCookie($uid, $token) {
return self::getUserSession()->loginWithCookie($uid, $token);
}
/**
* Try to login a user, assuming authentication
|
|
31b7f2792
|
251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
* has already happened (e.g. via Single Sign On).
*
* Log in a user and regenerate a new session.
*
* @param \OCP\Authentication\IApacheBackend $backend
* @return bool
*/
public static function loginWithApache(\OCP\Authentication\IApacheBackend $backend) {
$uid = $backend->getCurrentUserId();
$run = true;
OC_Hook::emit( "OC_User", "pre_login", array( "run" => &$run, "uid" => $uid ));
if($uid) {
|
|
31b7f2792
|
265 266 |
self::setUserId($uid); self::setDisplayName($uid); |
|
a293d369c
|
267 |
self::getUserSession()->setLoginName($uid); |
|
31b7f2792
|
268 269 270 271 272 273 274 |
OC_Hook::emit( "OC_User", "post_login", array( "uid" => $uid, 'password'=>'' )); return true; } return false; } /** |
|
6d9380f96
|
275 |
* Verify with Apache whether user is authenticated. |
|
31b7f2792
|
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
*
* @return boolean|null
* true: authenticated
* false: not authenticated
* null: not handled / no backend available
*/
public static function handleApacheAuth() {
$backend = self::findFirstActiveUsedBackend();
if ($backend) {
OC_App::loadApps();
//setup extra user backends
self::setupBackends();
self::unsetMagicInCookie();
return self::loginWithApache($backend);
}
return null;
}
/**
|
|
6d9380f96
|
299 |
* Sets user id for session and triggers emit |
|
31b7f2792
|
300 301 302 303 304 305 |
*/
public static function setUserId($uid) {
OC::$session->set('user_id', $uid);
}
/**
|
|
6d9380f96
|
306 307 308 309 |
* Sets user display name for session * @param string $uid * @param null $displayName * @return bool Whether the display name could get set |
|
31b7f2792
|
310 311 312 313 314 315 316 317 318 319 320 321 322 323 |
*/
public static function setDisplayName($uid, $displayName = null) {
if (is_null($displayName)) {
$displayName = $uid;
}
$user = self::getManager()->get($uid);
if ($user) {
return $user->setDisplayName($displayName);
} else {
return false;
}
}
/**
|
|
6d9380f96
|
324 |
* Logs the current user out and kills all the session data |
|
31b7f2792
|
325 326 327 328 329 330 331 332 |
*
* Logout, destroys session
*/
public static function logout() {
self::getUserSession()->logout();
}
/**
|
|
6d9380f96
|
333 334 |
* Check if the user is logged in * @return bool |
|
31b7f2792
|
335 336 337 338 339 |
*
* Checks if the user is logged in
*/
public static function isLoggedIn() {
if (\OC::$session->get('user_id') && self::$incognitoMode === false) {
|
|
31b7f2792
|
340 341 342 343 344 345 |
return self::userExists(\OC::$session->get('user_id'));
}
return false;
}
/**
|
|
6d9380f96
|
346 |
* set incognito mode, e.g. if a user wants to open a public link |
|
31b7f2792
|
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 |
* @param bool $status
*/
public static function setIncognitoMode($status) {
self::$incognitoMode = $status;
}
/**
* Supplies an attribute to the logout hyperlink. The default behaviour
* is to return an href with '?logout=true' appended. However, it can
* supply any attribute(s) which are valid for <a>.
*
* @return string with one or more HTML attributes.
*/
public static function getLogoutAttribute() {
$backend = self::findFirstActiveUsedBackend();
if ($backend) {
return $backend->getLogoutAttribute();
}
|
|
6d9380f96
|
366 |
return 'href="' . link_to('', 'index.php') . '?logout=true&requesttoken=' . OC_Util::callRegister() . '"';
|
|
31b7f2792
|
367 368 369 |
} /** |
|
6d9380f96
|
370 |
* Check if the user is an admin user |
|
31b7f2792
|
371 372 373 374 375 376 377 378 379 380 381 382 |
* @param string $uid uid of the admin
* @return bool
*/
public static function isAdminUser($uid) {
if (OC_Group::inGroup($uid, 'admin') && self::$incognitoMode === false) {
return true;
}
return false;
}
/**
|
|
6d9380f96
|
383 |
* get the user id of the user currently logged in. |
|
31b7f2792
|
384 385 386 387 388 389 390 391 392 393 394 395 |
* @return string uid or false
*/
public static function getUser() {
$uid = OC::$session ? OC::$session->get('user_id') : null;
if (!is_null($uid) && self::$incognitoMode === false) {
return $uid;
} else {
return false;
}
}
/**
|
|
6d9380f96
|
396 |
* get the display name of the user currently logged in. |
|
31b7f2792
|
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 |
* @param string $uid
* @return string uid or false
*/
public static function getDisplayName($uid = null) {
if ($uid) {
$user = self::getManager()->get($uid);
if ($user) {
return $user->getDisplayName();
} else {
return $uid;
}
} else {
$user = self::getUserSession()->getUser();
if ($user) {
return $user->getDisplayName();
} else {
return false;
}
}
}
/**
|
|
6d9380f96
|
419 |
* Autogenerate a password |
|
31b7f2792
|
420 421 422 423 424 425 426 427 428 |
* @return string
*
* generates a password
*/
public static function generatePassword() {
return OC_Util::generateRandomBytes(30);
}
/**
|
|
6d9380f96
|
429 |
* Set password |
|
31b7f2792
|
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 |
* @param string $uid The username
* @param string $password The new password
* @param string $recoveryPassword for the encryption app to reset encryption keys
* @return bool
*
* Change the password of a user
*/
public static function setPassword($uid, $password, $recoveryPassword = null) {
$user = self::getManager()->get($uid);
if ($user) {
return $user->setPassword($password, $recoveryPassword);
} else {
return false;
}
}
/**
|
|
6d9380f96
|
447 |
* Check whether user can change his avatar |
|
31b7f2792
|
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 |
* @param string $uid The username
* @return bool
*
* Check whether a specified user can change his avatar
*/
public static function canUserChangeAvatar($uid) {
$user = self::getManager()->get($uid);
if ($user) {
return $user->canChangeAvatar();
} else {
return false;
}
}
/**
|
|
6d9380f96
|
463 |
* Check whether user can change his password |
|
31b7f2792
|
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 |
* @param string $uid The username
* @return bool
*
* Check whether a specified user can change his password
*/
public static function canUserChangePassword($uid) {
$user = self::getManager()->get($uid);
if ($user) {
return $user->canChangePassword();
} else {
return false;
}
}
/**
|
|
6d9380f96
|
479 |
* Check whether user can change his display name |
|
31b7f2792
|
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 |
* @param string $uid The username
* @return bool
*
* Check whether a specified user can change his display name
*/
public static function canUserChangeDisplayName($uid) {
$user = self::getManager()->get($uid);
if ($user) {
return $user->canChangeDisplayName();
} else {
return false;
}
}
/**
|
|
6d9380f96
|
495 |
* Check if the password is correct |
|
31b7f2792
|
496 497 |
* @param string $uid The username * @param string $password The password |
|
6d9380f96
|
498 |
* @return string|false user id a string on success, false otherwise |
|
31b7f2792
|
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 |
*
* Check if the password is correct without logging in the user
* returns the user id or false
*/
public static function checkPassword($uid, $password) {
$manager = self::getManager();
$username = $manager->checkPassword($uid, $password);
if ($username !== false) {
return $username->getUID();
}
return false;
}
/**
* @param string $uid The username
* @return string
*
* returns the path to the users home directory
*/
public static function getHome($uid) {
$user = self::getManager()->get($uid);
if ($user) {
return $user->getHome();
} else {
return OC_Config::getValue('datadirectory', OC::$SERVERROOT . '/data') . '/' . $uid;
}
}
/**
|
|
6d9380f96
|
528 529 |
* Get a list of all users * @return array an array of all uids |
|
31b7f2792
|
530 531 |
* * Get a list of all users. |
|
6d9380f96
|
532 533 534 |
* @param string $search * @param integer $limit * @param integer $offset |
|
31b7f2792
|
535 536 537 538 539 540 541 542 543 544 545 |
*/
public static function getUsers($search = '', $limit = null, $offset = null) {
$users = self::getManager()->search($search, $limit, $offset);
$uids = array();
foreach ($users as $user) {
$uids[] = $user->getUID();
}
return $uids;
}
/**
|
|
6d9380f96
|
546 |
* Get a list of all users display name |
|
31b7f2792
|
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 |
* @param string $search
* @param int $limit
* @param int $offset
* @return array associative array with all display names (value) and corresponding uids (key)
*
* Get a list of all display names and user ids.
*/
public static function getDisplayNames($search = '', $limit = null, $offset = null) {
$displayNames = array();
$users = self::getManager()->searchDisplayName($search, $limit, $offset);
foreach ($users as $user) {
$displayNames[$user->getUID()] = $user->getDisplayName();
}
return $displayNames;
}
/**
|
|
6d9380f96
|
564 |
* check if a user exists |
|
31b7f2792
|
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 |
* @param string $uid the username
* @return boolean
*/
public static function userExists($uid) {
return self::getManager()->userExists($uid);
}
/**
* disables a user
*
* @param string $uid the user to disable
*/
public static function disableUser($uid) {
$user = self::getManager()->get($uid);
if ($user) {
$user->setEnabled(false);
}
}
/**
* enable a user
*
* @param string $uid
*/
public static function enableUser($uid) {
$user = self::getManager()->get($uid);
if ($user) {
$user->setEnabled(true);
}
}
/**
* checks if a user is enabled
*
* @param string $uid
* @return bool
*/
public static function isEnabled($uid) {
$user = self::getManager()->get($uid);
if ($user) {
return $user->isEnabled();
} else {
return false;
}
}
/**
|
|
6d9380f96
|
612 |
* Set cookie value to use in next page load |
|
31b7f2792
|
613 614 615 616 617 618 619 620 |
* @param string $username username to be set
* @param string $token
*/
public static function setMagicInCookie($username, $token) {
self::getUserSession()->setMagicInCookie($username, $token);
}
/**
|
|
6d9380f96
|
621 |
* Remove cookie for "remember username" |
|
31b7f2792
|
622 623 624 625 626 627 |
*/
public static function unsetMagicInCookie() {
self::getUserSession()->unsetMagicInCookie();
}
/**
|
|
6d9380f96
|
628 629 |
* Returns the first active backend from self::$_usedBackends. * @return OCP\Authentication\IApacheBackend|null if no backend active, otherwise OCP\Authentication\IApacheBackend |
|
31b7f2792
|
630 631 632 633 634 635 636 637 638 639 640 641 642 |
*/
private static function findFirstActiveUsedBackend() {
foreach (self::$_usedBackends as $backend) {
if ($backend instanceof OCP\Authentication\IApacheBackend) {
if ($backend->isSessionActive()) {
return $backend;
}
}
}
return null;
}
}
|