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