Complex classes like BackendSystem 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 BackendSystem, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class BackendSystem extends Backend { |
||
| 24 | |||
| 25 | |||
| 26 | protected $needRefreshUserCache = false; |
||
| 27 | protected $needRefreshGroupCache = false; |
||
| 28 | |||
| 29 | |||
| 30 | /** |
||
| 31 | * Warn system that the user lists has been updated (new user) |
||
| 32 | * This is required so that nscd (name service caching daemon) knows that it needs |
||
| 33 | * to update its user data. |
||
| 34 | * |
||
| 35 | * NOTE: Don't need to update cache on deleted user since shadow information are not cached, |
||
| 36 | * so even if the cache is not refreshed, a deleted user won't be able to login |
||
| 37 | * |
||
| 38 | * @return true on success, false otherwise |
||
| 39 | */ |
||
| 40 | public function refreshUserCache() { |
||
| 43 | |||
| 44 | /** |
||
| 45 | * set if we need to refresh the user cache |
||
| 46 | * |
||
| 47 | * @return null |
||
| 48 | */ |
||
| 49 | public function setNeedRefreshUserCache() { |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Return if we need to refresh the user cache |
||
| 55 | * |
||
| 56 | * @return boolean |
||
| 57 | */ |
||
| 58 | public function getNeedRefreshUserCache() { |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Warn system that the group lists has been updated (new group, new member to group or memeber removed from group) |
||
| 64 | * This is required so that nscd (name service caching daemon) knows that it needs |
||
| 65 | * to update its group data. |
||
| 66 | * |
||
| 67 | * NOTE: Currently, we don't update group cache on deleted group, new user and deleted user |
||
| 68 | * |
||
| 69 | * @return true on success, false otherwise |
||
| 70 | */ |
||
| 71 | public function refreshGroupCache() { |
||
| 74 | |||
| 75 | /** |
||
| 76 | * set if we need to refresh the group cache |
||
| 77 | * |
||
| 78 | * @return null |
||
| 79 | */ |
||
| 80 | public function setNeedRefreshGroupCache() { |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Return if we need to refresh the groupo cache |
||
| 86 | * |
||
| 87 | * @return boolean |
||
| 88 | */ |
||
| 89 | public function getNeedRefreshGroupCache() { |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Hard reset of system related stuff (nscd for uid/gid and fs cache). |
||
| 95 | * |
||
| 96 | * Should be used before modification of system (new user, project, etc) |
||
| 97 | * |
||
| 98 | * @return null |
||
| 99 | */ |
||
| 100 | public function flushNscdAndFsCache() { |
||
| 101 | $this->refreshGroupCache(); |
||
| 102 | $this->refreshUserCache(); |
||
| 103 | clearstatcache(); |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Ensure user home directory is created and has the right uid |
||
| 108 | * |
||
| 109 | * @param PFUser $user the user we want to sanitize his home |
||
| 110 | * |
||
| 111 | * @return null |
||
| 112 | */ |
||
| 113 | public function userHomeSanityCheck(PFUser $user) { |
||
| 114 | if (!$this->userHomeExists($user->getUserName())) { |
||
| 115 | $this->createUserHome($user); |
||
| 116 | } |
||
| 117 | if (!$this->isUserHomeOwnedByUser($user)) { |
||
| 118 | $this->setUserHomeOwnership($user); |
||
| 119 | } |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Create user home directory |
||
| 124 | * |
||
| 125 | * Also copy files from the skel directory to the new home directory. |
||
| 126 | * If the directory already exists, nothing is done. |
||
| 127 | * |
||
| 128 | * @param PFUser $user the user we want to create a home |
||
| 129 | * |
||
| 130 | * @return true if directory is successfully created, false otherwise |
||
| 131 | */ |
||
| 132 | public function createUserHome(PFUser $user) { |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Verify if given name exists as user home directory |
||
| 155 | * |
||
| 156 | * @param String $username the user name to test if home exists |
||
| 157 | * |
||
| 158 | * @return boolean |
||
| 159 | */ |
||
| 160 | public function userHomeExists($username) { |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Verify is user home directory has the right uid |
||
| 166 | * |
||
| 167 | * @param PFUser $user the user needed to verify his home directory |
||
| 168 | * |
||
| 169 | * @return boolean |
||
| 170 | */ |
||
| 171 | private function isUserHomeOwnedByUser(PFUser $user) { |
||
| 172 | $stat = stat($user->getUnixHomeDir()); |
||
| 173 | if ($stat) { |
||
|
|
|||
| 174 | if ($stat['uid'] != $user->getRealUnixUID()) { |
||
| 175 | return false; |
||
| 176 | } |
||
| 177 | } |
||
| 178 | return true; |
||
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Set user's uid/gid on its home directory (recursively) |
||
| 183 | * |
||
| 184 | * @param PFUser $user user to set uid/gid |
||
| 185 | * |
||
| 186 | * @return null |
||
| 187 | */ |
||
| 188 | private function setUserHomeOwnership(PFUser $user) { |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Create project home directory |
||
| 194 | * If the directory already exists, nothing is done. |
||
| 195 | * |
||
| 196 | * @param int $group_id a group id |
||
| 197 | * |
||
| 198 | * @return true if directory is successfully created, false otherwise |
||
| 199 | */ |
||
| 200 | public function createProjectHome($group_id) { |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Archive the user home directory |
||
| 338 | * |
||
| 339 | * @param int $user_id a user id needed to find the home dir to archive |
||
| 340 | * |
||
| 341 | * @return true if directory is successfully archived, false otherwise |
||
| 342 | */ |
||
| 343 | public function archiveUserHome($user_id) { |
||
| 344 | $user=$this->getUserManager()->getUserById($user_id); |
||
| 345 | if (!$user) return false; |
||
| 346 | $homedir=$GLOBALS['homedir_prefix']."/".$user->getUserName(); |
||
| 347 | $backupfile=ForgeConfig::get('sys_project_backup_path')."/".$user->getUserName().".tgz"; |
||
| 348 | |||
| 349 | if (is_dir($homedir)) { |
||
| 350 | system("cd ".$GLOBALS['homedir_prefix']."; tar cfz $backupfile ".$user->getUserName()); |
||
| 351 | chmod($backupfile,0600); |
||
| 352 | $this->recurseDeleteInDir($homedir); |
||
| 353 | rmdir($homedir); |
||
| 354 | } |
||
| 355 | return true; |
||
| 356 | |||
| 357 | } |
||
| 358 | |||
| 359 | |||
| 360 | /** |
||
| 361 | * Archive the project directory |
||
| 362 | * |
||
| 363 | * @param int $group_id the group id used to find the home directory to archive |
||
| 364 | * |
||
| 365 | * @return true if directory is successfully archived, false otherwise |
||
| 366 | */ |
||
| 367 | public function archiveProjectHome($group_id) { |
||
| 368 | $project=$this->getProjectManager()->getProject($group_id); |
||
| 369 | if (!$project) return false; |
||
| 370 | $mydir=$GLOBALS['grpdir_prefix']."/".$project->getUnixName(false); |
||
| 371 | $backupfile=ForgeConfig::get('sys_project_backup_path')."/".$project->getUnixName(false).".tgz"; |
||
| 372 | |||
| 373 | if (is_dir($mydir)) { |
||
| 374 | system("cd ".$GLOBALS['grpdir_prefix']."; tar cfz $backupfile ".$project->getUnixName(false)); |
||
| 375 | chmod($backupfile,0600); |
||
| 376 | $this->recurseDeleteInDir($mydir); |
||
| 377 | rmdir($mydir); |
||
| 378 | |||
| 379 | |||
| 380 | // Remove lower-case symlink if it exists |
||
| 381 | if ($project->getUnixName(true) != $project->getUnixName(false)) { |
||
| 382 | if (is_link($GLOBALS['grpdir_prefix']."/".$project->getUnixName(true))) { |
||
| 383 | unlink($GLOBALS['grpdir_prefix']."/".$project->getUnixName(true)); |
||
| 384 | } |
||
| 385 | } |
||
| 386 | } |
||
| 387 | return true; |
||
| 388 | } |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Archive ftp elements for a given project. |
||
| 392 | * It would delete FTP directory content of the project |
||
| 393 | * and create a Tarball in temp dir. |
||
| 394 | * |
||
| 395 | * @param Integer $group_id the group id |
||
| 396 | * |
||
| 397 | * @return boolean |
||
| 398 | */ |
||
| 399 | function archiveProjectFtp($group_id) { |
||
| 400 | $project = $this->getProjectManager()->getProject($group_id); |
||
| 401 | if (!$project) { |
||
| 402 | return false; |
||
| 403 | } |
||
| 404 | $anonymousFTP = $GLOBALS['ftp_anon_dir_prefix']."/".$project->getUnixName(false); |
||
| 405 | $backupfile = ForgeConfig::get('sys_project_backup_path')."/".$project->getUnixName(false)."-ftp.tgz"; |
||
| 406 | if (is_dir($anonymousFTP)) { |
||
| 407 | system("cd ".$GLOBALS['ftp_anon_dir_prefix']."; tar cfz $backupfile ".$project->getUnixName(false)); |
||
| 408 | chmod($backupfile, 0600); |
||
| 409 | $this->recurseDeleteInDir($anonymousFTP); |
||
| 410 | } |
||
| 411 | return true; |
||
| 412 | } |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Remove deleted releases and released files |
||
| 416 | * |
||
| 417 | * @return bool the status |
||
| 418 | */ |
||
| 419 | public function cleanupFRS() { |
||
| 420 | // Purge all deleted files older than 3 days old |
||
| 421 | if (!isset($GLOBALS['sys_file_deletion_delay'])) { |
||
| 422 | $delay = 3; |
||
| 423 | } else { |
||
| 424 | $delay = intval($GLOBALS['sys_file_deletion_delay']); |
||
| 425 | } |
||
| 426 | $time = $_SERVER['REQUEST_TIME'] - (3600*24*$delay); |
||
| 427 | |||
| 428 | $frs = $this->getFRSFileFactory(); |
||
| 429 | $status = $frs->moveFiles($time, $this); |
||
| 430 | // {{{ /!\ WARNING HACK /!\ |
||
| 431 | // We keep the good old purge mecanism for at least one release to clean |
||
| 432 | // the previously deleted files |
||
| 433 | // Delete all files under DELETE that are older than 10 days |
||
| 434 | //$delete_dir = $GLOBALS['ftp_frs_dir_prefix']."/DELETED"; |
||
| 435 | //system("find $delete_dir -type f -mtime +10 -exec rm {} \\;"); |
||
| 436 | //system("find $delete_dir -mindepth 1 -type d -empty -exec rm -R {} \\;"); |
||
| 437 | // }}} /!\ WARNING HACK /!\ |
||
| 438 | |||
| 439 | //Manage the purge of wiki attachments |
||
| 440 | $wiki = $this->getWikiAttachment(); |
||
| 441 | $status = $status && $wiki->purgeAttachments($time); |
||
| 442 | |||
| 443 | $em = EventManager::instance(); |
||
| 444 | $em->processEvent('backend_system_purge_files', array('time' => $time)); |
||
| 445 | |||
| 446 | return ($status); |
||
| 447 | } |
||
| 448 | |||
| 449 | /** |
||
| 450 | * dumps SSH authorized_keys into all users homedirs |
||
| 451 | * |
||
| 452 | * @return boolean always true |
||
| 453 | */ |
||
| 454 | public function dumpSSHKeys() { |
||
| 455 | $sshkey_dumper = new User_SSHKeyDumper($this); |
||
| 456 | $user_manager = $this->getUserManager(); |
||
| 457 | foreach($user_manager->getUsersWithSshKey() as $user) { |
||
| 458 | $sshkey_dumper->writeSSHKeys($user); |
||
| 459 | } |
||
| 460 | EventManager::instance()->processEvent(Event::DUMP_SSH_KEYS, array()); |
||
| 461 | return true; |
||
| 462 | } |
||
| 463 | |||
| 464 | /** |
||
| 465 | * dumps SSH authorized_keys for a user in its homedir |
||
| 466 | * |
||
| 467 | * @param PFUser $user the user we want to dump his key |
||
| 468 | * @param string $original_keys the original keys of the user |
||
| 469 | * |
||
| 470 | * @return boolean if the ssh key was written |
||
| 471 | */ |
||
| 472 | public function dumpSSHKeysForUser(PFUser $user, $original_keys) { |
||
| 476 | |||
| 477 | /** |
||
| 478 | * Check if repository of given project exists |
||
| 479 | * |
||
| 480 | * @param Project $project project to test if home exist |
||
| 481 | * |
||
| 482 | * @return true is repository already exists, false otherwise |
||
| 483 | */ |
||
| 484 | public function projectHomeExists($project) { |
||
| 485 | $unix_group_name = $project->getUnixName(false); // May contain upper-case letters |
||
| 486 | $home_dir=$GLOBALS['grpdir_prefix']."/".$unix_group_name; |
||
| 487 | if (is_dir($home_dir)) { |
||
| 488 | return true; |
||
| 489 | } else return false; |
||
| 490 | } |
||
| 491 | |||
| 492 | /** |
||
| 493 | * Check if given name is not used by a repository or a file or a link under project directories |
||
| 494 | * |
||
| 495 | * Return false if repository or file or link already exists: |
||
| 496 | ** with the same name under the grp_dir |
||
| 497 | ** with its lower case name under the grp_dir |
||
| 498 | ** under FRS |
||
| 499 | ** under ftp anon |
||
| 500 | * true otherwise |
||
| 501 | * |
||
| 502 | * @param String $name the project name to test |
||
| 503 | * |
||
| 504 | * @return boolean |
||
| 505 | */ |
||
| 506 | public function isProjectNameAvailable($name) { |
||
| 507 | $dir = $GLOBALS['grpdir_prefix']."/".$name; |
||
| 508 | $frs = $GLOBALS['ftp_frs_dir_prefix']."/".$name; |
||
| 509 | $ftp = $GLOBALS['ftp_anon_dir_prefix']."/".$name; |
||
| 510 | |||
| 511 | if ($this->fileExists($dir)) { |
||
| 512 | return false; |
||
| 513 | } else if ($name != strtolower ($name)) { |
||
| 514 | $link = $GLOBALS['grpdir_prefix']."/".strtolower($name); |
||
| 515 | if ($this->fileExists($link)) { |
||
| 516 | return false; |
||
| 517 | } |
||
| 518 | } |
||
| 519 | if ($this->fileExists($frs)) { |
||
| 520 | return false; |
||
| 521 | } else if ($this->fileExists($ftp)) { |
||
| 522 | return false; |
||
| 523 | } |
||
| 524 | return true; |
||
| 525 | } |
||
| 526 | |||
| 527 | /** |
||
| 528 | * Check if given name is not used by a repository or a file or a link under user directory |
||
| 529 | * |
||
| 530 | * @param String $name a user name to test availability |
||
| 531 | * |
||
| 532 | * @return boolean false if repository or file or link already exists, true otherwise |
||
| 533 | */ |
||
| 534 | function isUserNameAvailable($name) { |
||
| 538 | |||
| 539 | |||
| 540 | /** |
||
| 541 | * Rename project home directory (following project unix_name change) |
||
| 542 | * |
||
| 543 | * @param Project $project a project to rename |
||
| 544 | * @param String $newName the new name of the project |
||
| 545 | * |
||
| 546 | * @return boolean |
||
| 547 | */ |
||
| 548 | public function renameProjectHomeDirectory($project, $newName) { |
||
| 549 | if (is_link($GLOBALS['grpdir_prefix'].'/'.$newName)) { |
||
| 550 | unlink($GLOBALS['grpdir_prefix'].'/'.$newName); |
||
| 551 | return rename($GLOBALS['grpdir_prefix'].'/'.$project->getUnixName(false), $GLOBALS['grpdir_prefix'].'/'.$newName); |
||
| 552 | } else { |
||
| 553 | $renamed = rename($GLOBALS['grpdir_prefix'].'/'.$project->getUnixName(false), $GLOBALS['grpdir_prefix'].'/'.$newName); |
||
| 554 | if ($renamed) { |
||
| 555 | if (is_link($GLOBALS['grpdir_prefix'].'/'.$project->getUnixName(true))) { |
||
| 556 | unlink($GLOBALS['grpdir_prefix'].'/'.$project->getUnixName(true)); |
||
| 557 | } |
||
| 558 | if (strtolower($newName) != $newName) { |
||
| 559 | return symlink($GLOBALS['grpdir_prefix'].'/'.$newName,$GLOBALS['grpdir_prefix'].'/'.strtolower($newName)); |
||
| 560 | } else { |
||
| 561 | return true; |
||
| 562 | } |
||
| 563 | } |
||
| 564 | return $renamed; |
||
| 565 | } |
||
| 566 | } |
||
| 567 | |||
| 568 | /** |
||
| 569 | * Rename Directory where the released files are located (following project unix_name change) |
||
| 570 | * |
||
| 571 | * @param Project $project a project |
||
| 572 | * @param String $newName a new name |
||
| 573 | * |
||
| 574 | * @return boolean |
||
| 575 | */ |
||
| 576 | public function renameFileReleasedDirectory($project, $newName) { |
||
| 577 | if (is_dir($GLOBALS['ftp_frs_dir_prefix'].'/'.$project->getUnixName(false))) { |
||
| 578 | return rename($GLOBALS['ftp_frs_dir_prefix'].'/'.$project->getUnixName(false), $GLOBALS['ftp_frs_dir_prefix'].'/'.$newName); |
||
| 579 | } else { |
||
| 580 | return true; |
||
| 581 | } |
||
| 582 | } |
||
| 583 | |||
| 584 | /** |
||
| 585 | * Rename anon ftp project homedir (following project unix_name change) |
||
| 586 | * |
||
| 587 | * @param Project $project a project |
||
| 588 | * @param String $newName a new name |
||
| 589 | * |
||
| 590 | * @return boolean |
||
| 591 | */ |
||
| 592 | public function renameAnonFtpDirectory($project, $newName) { |
||
| 593 | if (is_dir($GLOBALS['ftp_anon_dir_prefix'].'/'.$project->getUnixName(false))){ |
||
| 594 | return rename($GLOBALS['ftp_anon_dir_prefix'].'/'.$project->getUnixName(false), $GLOBALS['ftp_anon_dir_prefix'].'/'.$newName); |
||
| 595 | } else { |
||
| 596 | return true; |
||
| 597 | } |
||
| 598 | } |
||
| 599 | |||
| 600 | /** |
||
| 601 | * Rename User home directory |
||
| 602 | * |
||
| 603 | * @param PFUser $user a user |
||
| 604 | * @param String $newName the new name of user home directory |
||
| 605 | * |
||
| 606 | * @return boolean |
||
| 607 | */ |
||
| 608 | public function renameUserHomeDirectory($user, $newName) { |
||
| 611 | |||
| 612 | /** |
||
| 613 | * Wrapper for getFRSFileFactory |
||
| 614 | * |
||
| 615 | * @return FRSFileFactory |
||
| 616 | */ |
||
| 617 | protected function getFRSFileFactory() { |
||
| 620 | |||
| 621 | /** |
||
| 622 | * Wrapper for getWikiAttachment |
||
| 623 | * |
||
| 624 | * @return WikiAttachment |
||
| 625 | */ |
||
| 626 | protected function getWikiAttachment() { |
||
| 629 | |||
| 630 | |||
| 631 | } |
||
| 632 | |||
| 633 | ?> |
||
| 634 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.