| Total Complexity | 83 |
| Total Lines | 578 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Manager 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 Manager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 65 | class Manager extends PublicEmitter implements IUserManager { |
||
| 66 | /** |
||
| 67 | * @var \OCP\UserInterface[] $backends |
||
| 68 | */ |
||
| 69 | private $backends = array(); |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var \OC\User\User[] $cachedUsers |
||
| 73 | */ |
||
| 74 | private $cachedUsers = array(); |
||
| 75 | |||
| 76 | /** @var IConfig */ |
||
| 77 | private $config; |
||
| 78 | |||
| 79 | /** @var EventDispatcherInterface */ |
||
| 80 | private $dispatcher; |
||
| 81 | |||
| 82 | /** @var IEventDispatcher */ |
||
| 83 | private $eventDispatcher; |
||
| 84 | |||
| 85 | public function __construct(IConfig $config, |
||
| 86 | EventDispatcherInterface $oldDispatcher, |
||
| 87 | IEventDispatcher $eventDispatcher) { |
||
| 88 | $this->config = $config; |
||
| 89 | $this->dispatcher = $oldDispatcher; |
||
| 90 | $cachedUsers = &$this->cachedUsers; |
||
| 91 | $this->listen('\OC\User', 'postDelete', function ($user) use (&$cachedUsers) { |
||
| 92 | /** @var \OC\User\User $user */ |
||
| 93 | unset($cachedUsers[$user->getUID()]); |
||
| 94 | }); |
||
| 95 | $this->eventDispatcher = $eventDispatcher; |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Get the active backends |
||
| 100 | * @return \OCP\UserInterface[] |
||
| 101 | */ |
||
| 102 | public function getBackends() { |
||
| 103 | return $this->backends; |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * register a user backend |
||
| 108 | * |
||
| 109 | * @param \OCP\UserInterface $backend |
||
| 110 | */ |
||
| 111 | public function registerBackend($backend) { |
||
| 112 | $this->backends[] = $backend; |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * remove a user backend |
||
| 117 | * |
||
| 118 | * @param \OCP\UserInterface $backend |
||
| 119 | */ |
||
| 120 | public function removeBackend($backend) { |
||
| 121 | $this->cachedUsers = array(); |
||
| 122 | if (($i = array_search($backend, $this->backends)) !== false) { |
||
| 123 | unset($this->backends[$i]); |
||
| 124 | } |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * remove all user backends |
||
| 129 | */ |
||
| 130 | public function clearBackends() { |
||
| 131 | $this->cachedUsers = array(); |
||
| 132 | $this->backends = array(); |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * get a user by user id |
||
| 137 | * |
||
| 138 | * @param string $uid |
||
| 139 | * @return \OC\User\User|null Either the user or null if the specified user does not exist |
||
| 140 | */ |
||
| 141 | public function get($uid) { |
||
| 142 | if (is_null($uid) || $uid === '' || $uid === false) { |
||
| 143 | return null; |
||
| 144 | } |
||
| 145 | if (isset($this->cachedUsers[$uid])) { //check the cache first to prevent having to loop over the backends |
||
| 146 | return $this->cachedUsers[$uid]; |
||
| 147 | } |
||
| 148 | foreach ($this->backends as $backend) { |
||
| 149 | if ($backend->userExists($uid)) { |
||
| 150 | return $this->getUserObject($uid, $backend); |
||
| 151 | } |
||
| 152 | } |
||
| 153 | return null; |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * get or construct the user object |
||
| 158 | * |
||
| 159 | * @param string $uid |
||
| 160 | * @param \OCP\UserInterface $backend |
||
| 161 | * @param bool $cacheUser If false the newly created user object will not be cached |
||
| 162 | * @return \OC\User\User |
||
| 163 | */ |
||
| 164 | protected function getUserObject($uid, $backend, $cacheUser = true) { |
||
| 165 | if ($backend instanceof IGetRealUIDBackend) { |
||
| 166 | $uid = $backend->getRealUID($uid); |
||
| 167 | } |
||
| 168 | |||
| 169 | if (isset($this->cachedUsers[$uid])) { |
||
| 170 | return $this->cachedUsers[$uid]; |
||
| 171 | } |
||
| 172 | |||
| 173 | $user = new User($uid, $backend, $this->dispatcher, $this, $this->config); |
||
| 174 | if ($cacheUser) { |
||
| 175 | $this->cachedUsers[$uid] = $user; |
||
| 176 | } |
||
| 177 | return $user; |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * check if a user exists |
||
| 182 | * |
||
| 183 | * @param string $uid |
||
| 184 | * @return bool |
||
| 185 | */ |
||
| 186 | public function userExists($uid) { |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Check if the password is valid for the user |
||
| 193 | * |
||
| 194 | * @param string $loginName |
||
| 195 | * @param string $password |
||
| 196 | * @return mixed the User object on success, false otherwise |
||
| 197 | */ |
||
| 198 | public function checkPassword($loginName, $password) { |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Check if the password is valid for the user |
||
| 210 | * |
||
| 211 | * @internal |
||
| 212 | * @param string $loginName |
||
| 213 | * @param string $password |
||
| 214 | * @return IUser|false the User object on success, false otherwise |
||
| 215 | */ |
||
| 216 | public function checkPasswordNoLogging($loginName, $password) { |
||
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * search by user id |
||
| 234 | * |
||
| 235 | * @param string $pattern |
||
| 236 | * @param int $limit |
||
| 237 | * @param int $offset |
||
| 238 | * @return \OC\User\User[] |
||
| 239 | */ |
||
| 240 | public function search($pattern, $limit = null, $offset = null) { |
||
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * search by displayName |
||
| 263 | * |
||
| 264 | * @param string $pattern |
||
| 265 | * @param int $limit |
||
| 266 | * @param int $offset |
||
| 267 | * @return \OC\User\User[] |
||
| 268 | */ |
||
| 269 | public function searchDisplayName($pattern, $limit = null, $offset = null) { |
||
| 288 | } |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @param string $uid |
||
| 292 | * @param string $password |
||
| 293 | * @throws \InvalidArgumentException |
||
| 294 | * @return bool|IUser the created user or false |
||
| 295 | */ |
||
| 296 | public function createUser($uid, $password) { |
||
| 297 | $localBackends = []; |
||
| 298 | foreach ($this->backends as $backend) { |
||
| 299 | if ($backend instanceof Database) { |
||
| 300 | // First check if there is another user backend |
||
| 301 | $localBackends[] = $backend; |
||
| 302 | continue; |
||
| 303 | } |
||
| 304 | |||
| 305 | if ($backend->implementsActions(Backend::CREATE_USER)) { |
||
| 306 | return $this->createUserFromBackend($uid, $password, $backend); |
||
| 307 | } |
||
| 308 | } |
||
| 309 | |||
| 310 | foreach ($localBackends as $backend) { |
||
| 311 | if ($backend->implementsActions(Backend::CREATE_USER)) { |
||
| 312 | return $this->createUserFromBackend($uid, $password, $backend); |
||
| 313 | } |
||
| 314 | } |
||
| 315 | |||
| 316 | return false; |
||
| 317 | } |
||
| 318 | |||
| 319 | /** |
||
| 320 | * @param string $uid |
||
| 321 | * @param string $password |
||
| 322 | * @param UserInterface $backend |
||
| 323 | * @return IUser|null |
||
| 324 | * @throws \InvalidArgumentException |
||
| 325 | */ |
||
| 326 | public function createUserFromBackend($uid, $password, UserInterface $backend) { |
||
| 327 | $l = \OC::$server->getL10N('lib'); |
||
| 328 | |||
| 329 | // Check the name for bad characters |
||
| 330 | // Allowed are: "a-z", "A-Z", "0-9" and "_.@-'" |
||
| 331 | if (preg_match('/[^a-zA-Z0-9 _.@\-\']/', $uid)) { |
||
| 332 | throw new \InvalidArgumentException($l->t('Only the following characters are allowed in a username:' |
||
| 333 | . ' "a-z", "A-Z", "0-9", and "_.@-\'"')); |
||
| 334 | } |
||
| 335 | |||
| 336 | // No empty username |
||
| 337 | if (trim($uid) === '') { |
||
| 338 | throw new \InvalidArgumentException($l->t('A valid username must be provided')); |
||
| 339 | } |
||
| 340 | |||
| 341 | // No whitespace at the beginning or at the end |
||
| 342 | if (trim($uid) !== $uid) { |
||
| 343 | throw new \InvalidArgumentException($l->t('Username contains whitespace at the beginning or at the end')); |
||
| 344 | } |
||
| 345 | |||
| 346 | // Username only consists of 1 or 2 dots (directory traversal) |
||
| 347 | if ($uid === '.' || $uid === '..') { |
||
| 348 | throw new \InvalidArgumentException($l->t('Username must not consist of dots only')); |
||
| 349 | } |
||
| 350 | |||
| 351 | if (!$this->verifyUid($uid)) { |
||
| 352 | throw new \InvalidArgumentException($l->t('Username is invalid because files already exist for this user')); |
||
| 353 | } |
||
| 354 | |||
| 355 | // No empty password |
||
| 356 | if (trim($password) === '') { |
||
| 357 | throw new \InvalidArgumentException($l->t('A valid password must be provided')); |
||
| 358 | } |
||
| 359 | |||
| 360 | // Check if user already exists |
||
| 361 | if ($this->userExists($uid)) { |
||
| 362 | throw new \InvalidArgumentException($l->t('The username is already being used')); |
||
| 363 | } |
||
| 364 | |||
| 365 | $this->emit('\OC\User', 'preCreateUser', [$uid, $password]); |
||
| 366 | $this->eventDispatcher->dispatchTyped(new CreateUserEvent($uid, $password)); |
||
| 367 | $state = $backend->createUser($uid, $password); |
||
| 368 | if($state === false) { |
||
| 369 | throw new \InvalidArgumentException($l->t('Could not create user')); |
||
| 370 | } |
||
| 371 | $user = $this->getUserObject($uid, $backend); |
||
| 372 | if ($user instanceof IUser) { |
||
| 373 | $this->emit('\OC\User', 'postCreateUser', [$user, $password]); |
||
| 374 | $this->eventDispatcher->dispatchTyped(new UserCreatedEvent($user, $password)); |
||
| 375 | } |
||
| 376 | return $user; |
||
| 377 | } |
||
| 378 | |||
| 379 | /** |
||
| 380 | * returns how many users per backend exist (if supported by backend) |
||
| 381 | * |
||
| 382 | * @param boolean $hasLoggedIn when true only users that have a lastLogin |
||
| 383 | * entry in the preferences table will be affected |
||
| 384 | * @return array|int an array of backend class as key and count number as value |
||
| 385 | * if $hasLoggedIn is true only an int is returned |
||
| 386 | */ |
||
| 387 | public function countUsers($hasLoggedIn = false) { |
||
| 388 | if ($hasLoggedIn) { |
||
| 389 | return $this->countSeenUsers(); |
||
| 390 | } |
||
| 391 | $userCountStatistics = []; |
||
| 392 | foreach ($this->backends as $backend) { |
||
| 393 | if ($backend->implementsActions(Backend::COUNT_USERS)) { |
||
| 394 | $backendUsers = $backend->countUsers(); |
||
| 395 | if($backendUsers !== false) { |
||
| 396 | if($backend instanceof IUserBackend) { |
||
| 397 | $name = $backend->getBackendName(); |
||
| 398 | } else { |
||
| 399 | $name = get_class($backend); |
||
| 400 | } |
||
| 401 | if(isset($userCountStatistics[$name])) { |
||
| 402 | $userCountStatistics[$name] += $backendUsers; |
||
| 403 | } else { |
||
| 404 | $userCountStatistics[$name] = $backendUsers; |
||
| 405 | } |
||
| 406 | } |
||
| 407 | } |
||
| 408 | } |
||
| 409 | return $userCountStatistics; |
||
| 410 | } |
||
| 411 | |||
| 412 | /** |
||
| 413 | * returns how many users per backend exist in the requested groups (if supported by backend) |
||
| 414 | * |
||
| 415 | * @param IGroup[] $groups an array of gid to search in |
||
| 416 | * @return array|int an array of backend class as key and count number as value |
||
| 417 | * if $hasLoggedIn is true only an int is returned |
||
| 418 | */ |
||
| 419 | public function countUsersOfGroups(array $groups) { |
||
| 420 | $users = []; |
||
| 421 | foreach($groups as $group) { |
||
| 422 | $usersIds = array_map(function($user) { |
||
| 423 | return $user->getUID(); |
||
| 424 | }, $group->getUsers()); |
||
| 425 | $users = array_merge($users, $usersIds); |
||
| 426 | } |
||
| 427 | return count(array_unique($users)); |
||
| 428 | } |
||
| 429 | |||
| 430 | /** |
||
| 431 | * The callback is executed for each user on each backend. |
||
| 432 | * If the callback returns false no further users will be retrieved. |
||
| 433 | * |
||
| 434 | * @param \Closure $callback |
||
| 435 | * @param string $search |
||
| 436 | * @param boolean $onlySeen when true only users that have a lastLogin entry |
||
| 437 | * in the preferences table will be affected |
||
| 438 | * @since 9.0.0 |
||
| 439 | */ |
||
| 440 | public function callForAllUsers(\Closure $callback, $search = '', $onlySeen = false) { |
||
| 441 | if ($onlySeen) { |
||
| 442 | $this->callForSeenUsers($callback); |
||
| 443 | } else { |
||
| 444 | foreach ($this->getBackends() as $backend) { |
||
| 445 | $limit = 500; |
||
| 446 | $offset = 0; |
||
| 447 | do { |
||
| 448 | $users = $backend->getUsers($search, $limit, $offset); |
||
| 449 | foreach ($users as $uid) { |
||
| 450 | if (!$backend->userExists($uid)) { |
||
| 451 | continue; |
||
| 452 | } |
||
| 453 | $user = $this->getUserObject($uid, $backend, false); |
||
| 454 | $return = $callback($user); |
||
| 455 | if ($return === false) { |
||
| 456 | break; |
||
| 457 | } |
||
| 458 | } |
||
| 459 | $offset += $limit; |
||
| 460 | } while (count($users) >= $limit); |
||
| 461 | } |
||
| 462 | } |
||
| 463 | } |
||
| 464 | |||
| 465 | /** |
||
| 466 | * returns how many users are disabled |
||
| 467 | * |
||
| 468 | * @return int |
||
| 469 | * @since 12.0.0 |
||
| 470 | */ |
||
| 471 | public function countDisabledUsers(): int { |
||
| 472 | $queryBuilder = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
||
| 473 | $queryBuilder->select($queryBuilder->func()->count('*')) |
||
| 474 | ->from('preferences') |
||
| 475 | ->where($queryBuilder->expr()->eq('appid', $queryBuilder->createNamedParameter('core'))) |
||
| 476 | ->andWhere($queryBuilder->expr()->eq('configkey', $queryBuilder->createNamedParameter('enabled'))) |
||
| 477 | ->andWhere($queryBuilder->expr()->eq('configvalue', $queryBuilder->createNamedParameter('false'), IQueryBuilder::PARAM_STR)); |
||
| 478 | |||
| 479 | |||
| 480 | $result = $queryBuilder->execute(); |
||
| 481 | $count = $result->fetchColumn(); |
||
| 482 | $result->closeCursor(); |
||
| 483 | |||
| 484 | if ($count !== false) { |
||
| 485 | $count = (int)$count; |
||
| 486 | } else { |
||
| 487 | $count = 0; |
||
| 488 | } |
||
| 489 | |||
| 490 | return $count; |
||
| 491 | } |
||
| 492 | |||
| 493 | /** |
||
| 494 | * returns how many users are disabled in the requested groups |
||
| 495 | * |
||
| 496 | * @param array $groups groupids to search |
||
| 497 | * @return int |
||
| 498 | * @since 14.0.0 |
||
| 499 | */ |
||
| 500 | public function countDisabledUsersOfGroups(array $groups): int { |
||
| 501 | $queryBuilder = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
||
| 502 | $queryBuilder->select($queryBuilder->createFunction('COUNT(DISTINCT ' . $queryBuilder->getColumnName('uid') . ')')) |
||
| 503 | ->from('preferences', 'p') |
||
| 504 | ->innerJoin('p', 'group_user', 'g', $queryBuilder->expr()->eq('p.userid', 'g.uid')) |
||
| 505 | ->where($queryBuilder->expr()->eq('appid', $queryBuilder->createNamedParameter('core'))) |
||
| 506 | ->andWhere($queryBuilder->expr()->eq('configkey', $queryBuilder->createNamedParameter('enabled'))) |
||
| 507 | ->andWhere($queryBuilder->expr()->eq('configvalue', $queryBuilder->createNamedParameter('false'), IQueryBuilder::PARAM_STR)) |
||
| 508 | ->andWhere($queryBuilder->expr()->in('gid', $queryBuilder->createNamedParameter($groups, IQueryBuilder::PARAM_STR_ARRAY))); |
||
| 509 | |||
| 510 | $result = $queryBuilder->execute(); |
||
| 511 | $count = $result->fetchColumn(); |
||
| 512 | $result->closeCursor(); |
||
| 513 | |||
| 514 | if ($count !== false) { |
||
| 515 | $count = (int)$count; |
||
| 516 | } else { |
||
| 517 | $count = 0; |
||
| 518 | } |
||
| 519 | |||
| 520 | return $count; |
||
| 521 | } |
||
| 522 | |||
| 523 | /** |
||
| 524 | * returns how many users have logged in once |
||
| 525 | * |
||
| 526 | * @return int |
||
| 527 | * @since 11.0.0 |
||
| 528 | */ |
||
| 529 | public function countSeenUsers() { |
||
| 530 | $queryBuilder = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
||
| 531 | $queryBuilder->select($queryBuilder->func()->count('*')) |
||
| 532 | ->from('preferences') |
||
| 533 | ->where($queryBuilder->expr()->eq('appid', $queryBuilder->createNamedParameter('login'))) |
||
| 534 | ->andWhere($queryBuilder->expr()->eq('configkey', $queryBuilder->createNamedParameter('lastLogin'))) |
||
| 535 | ->andWhere($queryBuilder->expr()->isNotNull('configvalue')); |
||
| 536 | |||
| 537 | $query = $queryBuilder->execute(); |
||
| 538 | |||
| 539 | $result = (int)$query->fetchColumn(); |
||
| 540 | $query->closeCursor(); |
||
| 541 | |||
| 542 | return $result; |
||
| 543 | } |
||
| 544 | |||
| 545 | /** |
||
| 546 | * @param \Closure $callback |
||
| 547 | * @since 11.0.0 |
||
| 548 | */ |
||
| 549 | public function callForSeenUsers(\Closure $callback) { |
||
| 550 | $limit = 1000; |
||
| 551 | $offset = 0; |
||
| 552 | do { |
||
| 553 | $userIds = $this->getSeenUserIds($limit, $offset); |
||
| 554 | $offset += $limit; |
||
| 555 | foreach ($userIds as $userId) { |
||
| 556 | foreach ($this->backends as $backend) { |
||
| 557 | if ($backend->userExists($userId)) { |
||
| 558 | $user = $this->getUserObject($userId, $backend, false); |
||
| 559 | $return = $callback($user); |
||
| 560 | if ($return === false) { |
||
| 561 | return; |
||
| 562 | } |
||
| 563 | break; |
||
| 564 | } |
||
| 565 | } |
||
| 566 | } |
||
| 567 | } while (count($userIds) >= $limit); |
||
| 568 | } |
||
| 569 | |||
| 570 | /** |
||
| 571 | * Getting all userIds that have a listLogin value requires checking the |
||
| 572 | * value in php because on oracle you cannot use a clob in a where clause, |
||
| 573 | * preventing us from doing a not null or length(value) > 0 check. |
||
| 574 | * |
||
| 575 | * @param int $limit |
||
| 576 | * @param int $offset |
||
| 577 | * @return string[] with user ids |
||
| 578 | */ |
||
| 579 | private function getSeenUserIds($limit = null, $offset = null) { |
||
| 580 | $queryBuilder = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
||
| 581 | $queryBuilder->select(['userid']) |
||
| 582 | ->from('preferences') |
||
| 583 | ->where($queryBuilder->expr()->eq( |
||
| 584 | 'appid', $queryBuilder->createNamedParameter('login')) |
||
| 585 | ) |
||
| 586 | ->andWhere($queryBuilder->expr()->eq( |
||
| 587 | 'configkey', $queryBuilder->createNamedParameter('lastLogin')) |
||
| 588 | ) |
||
| 589 | ->andWhere($queryBuilder->expr()->isNotNull('configvalue') |
||
| 590 | ); |
||
| 591 | |||
| 592 | if ($limit !== null) { |
||
| 593 | $queryBuilder->setMaxResults($limit); |
||
| 594 | } |
||
| 595 | if ($offset !== null) { |
||
| 596 | $queryBuilder->setFirstResult($offset); |
||
| 597 | } |
||
| 598 | $query = $queryBuilder->execute(); |
||
| 599 | $result = []; |
||
| 600 | |||
| 601 | while ($row = $query->fetch()) { |
||
| 602 | $result[] = $row['userid']; |
||
| 603 | } |
||
| 604 | |||
| 605 | $query->closeCursor(); |
||
| 606 | |||
| 607 | return $result; |
||
| 608 | } |
||
| 609 | |||
| 610 | /** |
||
| 611 | * @param string $email |
||
| 612 | * @return IUser[] |
||
| 613 | * @since 9.1.0 |
||
| 614 | */ |
||
| 615 | public function getByEmail($email) { |
||
| 616 | $userIds = $this->config->getUsersForUserValueCaseInsensitive('settings', 'email', $email); |
||
| 617 | |||
| 618 | $users = array_map(function($uid) { |
||
| 619 | return $this->get($uid); |
||
| 620 | }, $userIds); |
||
| 621 | |||
| 622 | return array_values(array_filter($users, function($u) { |
||
| 623 | return ($u instanceof IUser); |
||
| 624 | })); |
||
| 625 | } |
||
| 626 | |||
| 627 | private function verifyUid(string $uid): bool { |
||
| 643 | } |
||
| 644 | } |
||
| 645 |