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