| Total Complexity | 53 |
| Total Lines | 553 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 0 |
Complex classes like PageUserManagement 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 PageUserManagement, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | class PageUserManagement extends InternalPageBase |
||
| 26 | { |
||
| 27 | /** @var string */ |
||
| 28 | private $adminMailingList = '[email protected]'; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Main function for this page, when no specific actions are called. |
||
| 32 | */ |
||
| 33 | protected function main() |
||
| 34 | { |
||
| 35 | $this->setHtmlTitle('User Management'); |
||
| 36 | |||
| 37 | $database = $this->getDatabase(); |
||
| 38 | $currentUser = User::getCurrent($database); |
||
| 39 | |||
| 40 | $userSearchRequest = WebRequest::getString('usersearch'); |
||
| 41 | if ($userSearchRequest !== null) { |
||
| 42 | $searchedUser = User::getByUsername($userSearchRequest, $database); |
||
| 43 | if($searchedUser !== false) { |
||
| 44 | $this->redirect('statistics/users', 'detail', ['user' => $searchedUser->getId()]); |
||
| 45 | return; |
||
| 46 | } |
||
| 47 | } |
||
| 48 | |||
| 49 | // A bit hacky, but it's better than my last solution of creating an object for each user and passing that to |
||
| 50 | // the template. I still don't have a particularly good way of handling this. |
||
| 51 | OAuthUserHelper::prepareTokenCountStatement($database); |
||
| 52 | |||
| 53 | if (WebRequest::getBoolean("showAll")) { |
||
| 54 | $this->assign("showAll", true); |
||
| 55 | |||
| 56 | $suspendedUsers = UserSearchHelper::get($database)->byStatus(User::STATUS_SUSPENDED)->fetch(); |
||
| 57 | $this->assign("suspendedUsers", $suspendedUsers); |
||
| 58 | |||
| 59 | $declinedUsers = UserSearchHelper::get($database)->byStatus(User::STATUS_DECLINED)->fetch(); |
||
| 60 | $this->assign("declinedUsers", $declinedUsers); |
||
| 61 | |||
| 62 | UserSearchHelper::get($database)->getRoleMap($roleMap); |
||
| 63 | } |
||
| 64 | else { |
||
| 65 | $this->assign("showAll", false); |
||
| 66 | $this->assign("suspendedUsers", array()); |
||
| 67 | $this->assign("declinedUsers", array()); |
||
| 68 | |||
| 69 | UserSearchHelper::get($database)->statusIn(array('New', 'Active'))->getRoleMap($roleMap); |
||
| 70 | } |
||
| 71 | |||
| 72 | $newUsers = UserSearchHelper::get($database)->byStatus(User::STATUS_NEW)->fetch(); |
||
| 73 | $normalUsers = UserSearchHelper::get($database)->byStatus(User::STATUS_ACTIVE)->byRole('user')->fetch(); |
||
| 74 | $adminUsers = UserSearchHelper::get($database)->byStatus(User::STATUS_ACTIVE)->byRole('admin')->fetch(); |
||
| 75 | $checkUsers = UserSearchHelper::get($database)->byStatus(User::STATUS_ACTIVE)->byRole('checkuser')->fetch(); |
||
| 76 | $toolRoots = UserSearchHelper::get($database)->byStatus(User::STATUS_ACTIVE)->byRole('toolRoot')->fetch(); |
||
| 77 | $this->assign('newUsers', $newUsers); |
||
| 78 | $this->assign('normalUsers', $normalUsers); |
||
| 79 | $this->assign('adminUsers', $adminUsers); |
||
| 80 | $this->assign('checkUsers', $checkUsers); |
||
| 81 | $this->assign('toolRoots', $toolRoots); |
||
| 82 | |||
| 83 | $this->assign('roles', $roleMap); |
||
| 84 | |||
| 85 | $this->addJs("/api.php?action=users&all=true&targetVariable=typeaheaddata"); |
||
| 86 | |||
| 87 | $this->assign('canApprove', $this->barrierTest('approve', $currentUser)); |
||
| 88 | $this->assign('canDecline', $this->barrierTest('decline', $currentUser)); |
||
| 89 | $this->assign('canRename', $this->barrierTest('rename', $currentUser)); |
||
| 90 | $this->assign('canEditUser', $this->barrierTest('editUser', $currentUser)); |
||
| 91 | $this->assign('canSuspend', $this->barrierTest('suspend', $currentUser)); |
||
| 92 | $this->assign('canEditRoles', $this->barrierTest('editRoles', $currentUser)); |
||
| 93 | |||
| 94 | $this->setTemplate("usermanagement/main.tpl"); |
||
| 95 | } |
||
| 96 | |||
| 97 | #region Access control |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Action target for editing the roles assigned to a user |
||
| 101 | */ |
||
| 102 | protected function editRoles() |
||
| 191 | } |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Action target for suspending users |
||
| 196 | * |
||
| 197 | * @throws ApplicationLogicException |
||
| 198 | */ |
||
| 199 | protected function suspend() |
||
| 257 | } |
||
| 258 | } |
||
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Entry point for the decline action |
||
| 263 | * |
||
| 264 | * @throws ApplicationLogicException |
||
| 265 | */ |
||
| 266 | protected function decline() |
||
| 267 | { |
||
| 268 | $this->setHtmlTitle('User Management'); |
||
| 269 | |||
| 270 | $database = $this->getDatabase(); |
||
| 271 | |||
| 272 | $userId = WebRequest::getInt('user'); |
||
| 273 | $user = User::getById($userId, $database); |
||
| 274 | |||
| 275 | if ($user === false) { |
||
| 276 | throw new ApplicationLogicException('Sorry, the user you are trying to decline could not be found.'); |
||
| 277 | } |
||
| 278 | |||
| 279 | if (!$user->isNewUser()) { |
||
| 280 | throw new ApplicationLogicException('Sorry, the user you are trying to decline is not new.'); |
||
| 281 | } |
||
| 282 | |||
| 283 | // Dual-mode action |
||
| 284 | if (WebRequest::wasPosted()) { |
||
| 285 | $this->validateCSRFToken(); |
||
| 286 | $reason = WebRequest::postString('reason'); |
||
| 287 | |||
| 288 | if ($reason === null || trim($reason) === "") { |
||
| 289 | throw new ApplicationLogicException('No reason provided'); |
||
| 290 | } |
||
| 291 | |||
| 292 | $user->setStatus(User::STATUS_DECLINED); |
||
| 293 | $user->setUpdateVersion(WebRequest::postInt('updateversion')); |
||
| 294 | $user->save(); |
||
| 295 | Logger::declinedUser($database, $user, $reason); |
||
| 296 | |||
| 297 | $this->getNotificationHelper()->userDeclined($user, $reason); |
||
| 298 | SessionAlert::quick('Declined user ' . htmlentities($user->getUsername(), ENT_COMPAT, 'UTF-8')); |
||
| 299 | |||
| 300 | // send email |
||
| 301 | $this->sendStatusChangeEmail( |
||
| 302 | 'Your WP:ACC account has been declined', |
||
| 303 | 'usermanagement/emails/declined.tpl', |
||
| 304 | $reason, |
||
| 305 | $user, |
||
| 306 | User::getCurrent($database)->getUsername() |
||
| 307 | ); |
||
| 308 | |||
| 309 | $this->redirect('userManagement'); |
||
| 310 | |||
| 311 | return; |
||
| 312 | } |
||
| 313 | else { |
||
| 314 | $this->assignCSRFToken(); |
||
| 315 | $this->setTemplate('usermanagement/changelevel-reason.tpl'); |
||
| 316 | $this->assign('user', $user); |
||
| 317 | $this->assign('status', 'Declined'); |
||
| 318 | $this->assign("showReason", true); |
||
| 319 | } |
||
| 320 | } |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Entry point for the approve action |
||
| 324 | * |
||
| 325 | * @throws ApplicationLogicException |
||
| 326 | */ |
||
| 327 | protected function approve() |
||
| 328 | { |
||
| 329 | $this->setHtmlTitle('User Management'); |
||
| 330 | |||
| 331 | $database = $this->getDatabase(); |
||
| 332 | |||
| 333 | $userId = WebRequest::getInt('user'); |
||
| 334 | $user = User::getById($userId, $database); |
||
| 335 | |||
| 336 | if ($user === false) { |
||
| 337 | throw new ApplicationLogicException('Sorry, the user you are trying to approve could not be found.'); |
||
| 338 | } |
||
| 339 | |||
| 340 | if ($user->isActive()) { |
||
| 341 | throw new ApplicationLogicException('Sorry, the user you are trying to approve is already an active user.'); |
||
| 342 | } |
||
| 343 | |||
| 344 | // Dual-mode action |
||
| 345 | if (WebRequest::wasPosted()) { |
||
| 346 | $this->validateCSRFToken(); |
||
| 347 | $user->setStatus(User::STATUS_ACTIVE); |
||
| 348 | $user->setUpdateVersion(WebRequest::postInt('updateversion')); |
||
| 349 | $user->save(); |
||
| 350 | Logger::approvedUser($database, $user); |
||
| 351 | |||
| 352 | $this->getNotificationHelper()->userApproved($user); |
||
| 353 | SessionAlert::quick('Approved user ' . htmlentities($user->getUsername(), ENT_COMPAT, 'UTF-8')); |
||
| 354 | |||
| 355 | // send email |
||
| 356 | $this->sendStatusChangeEmail( |
||
| 357 | 'Your WP:ACC account has been approved', |
||
| 358 | 'usermanagement/emails/approved.tpl', |
||
| 359 | null, |
||
| 360 | $user, |
||
| 361 | User::getCurrent($database)->getUsername() |
||
| 362 | ); |
||
| 363 | |||
| 364 | $this->redirect("userManagement"); |
||
| 365 | |||
| 366 | return; |
||
| 367 | } |
||
| 368 | else { |
||
| 369 | $this->assignCSRFToken(); |
||
| 370 | $this->setTemplate("usermanagement/changelevel-reason.tpl"); |
||
| 371 | $this->assign("user", $user); |
||
| 372 | $this->assign("status", "Active"); |
||
| 373 | $this->assign("showReason", false); |
||
| 374 | } |
||
| 375 | } |
||
| 376 | |||
| 377 | #endregion |
||
| 378 | |||
| 379 | #region Renaming / Editing |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Entry point for the rename action |
||
| 383 | * |
||
| 384 | * @throws ApplicationLogicException |
||
| 385 | */ |
||
| 386 | protected function rename() |
||
| 453 | } |
||
| 454 | } |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Entry point for the edit action |
||
| 458 | * |
||
| 459 | * @throws ApplicationLogicException |
||
| 460 | */ |
||
| 461 | protected function editUser() |
||
| 462 | { |
||
| 463 | $this->setHtmlTitle('User Management'); |
||
| 464 | |||
| 465 | $database = $this->getDatabase(); |
||
| 466 | |||
| 467 | $userId = WebRequest::getInt('user'); |
||
| 468 | $user = User::getById($userId, $database); |
||
| 469 | $oauth = new OAuthUserHelper($user, $database, $this->getOAuthProtocolHelper(), $this->getSiteConfiguration()); |
||
| 470 | |||
| 471 | if ($user === false) { |
||
| 472 | throw new ApplicationLogicException('Sorry, the user you are trying to edit could not be found.'); |
||
| 473 | } |
||
| 474 | |||
| 475 | // Dual-mode action |
||
| 476 | if (WebRequest::wasPosted()) { |
||
| 477 | $this->validateCSRFToken(); |
||
| 478 | $newEmail = WebRequest::postEmail('user_email'); |
||
| 479 | $newOnWikiName = WebRequest::postString('user_onwikiname'); |
||
| 480 | |||
| 481 | if ($newEmail === null) { |
||
| 482 | throw new ApplicationLogicException('Invalid email address'); |
||
| 483 | } |
||
| 484 | |||
| 485 | if (!$oauth->isFullyLinked()) { |
||
| 486 | if (trim($newOnWikiName) == "") { |
||
| 487 | throw new ApplicationLogicException('New on-wiki username cannot be blank'); |
||
| 488 | } |
||
| 489 | |||
| 490 | $user->setOnWikiName($newOnWikiName); |
||
| 491 | } |
||
| 492 | |||
| 493 | $user->setEmail($newEmail); |
||
| 494 | |||
| 495 | $user->setUpdateVersion(WebRequest::postInt('updateversion')); |
||
| 496 | |||
| 497 | $user->save(); |
||
| 498 | |||
| 499 | Logger::userPreferencesChange($database, $user); |
||
| 500 | $this->getNotificationHelper()->userPrefChange($user); |
||
| 501 | SessionAlert::quick('Changes to user\'s preferences have been saved'); |
||
| 502 | |||
| 503 | $this->redirect("userManagement"); |
||
| 504 | |||
| 505 | return; |
||
| 506 | } |
||
| 507 | else { |
||
| 508 | $this->assignCSRFToken(); |
||
| 509 | $oauth = new OAuthUserHelper($user, $database, $this->getOAuthProtocolHelper(), |
||
| 510 | $this->getSiteConfiguration()); |
||
| 511 | $this->setTemplate('usermanagement/edituser.tpl'); |
||
| 512 | $this->assign('user', $user); |
||
| 513 | $this->assign('oauth', $oauth); |
||
| 514 | } |
||
| 515 | } |
||
| 516 | |||
| 517 | #endregion |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Sends a status change email to the user. |
||
| 521 | * |
||
| 522 | * @param string $subject The subject of the email |
||
| 523 | * @param string $template The smarty template to use |
||
| 524 | * @param string|null $reason The reason for performing the status change |
||
| 525 | * @param User $user The user affected |
||
| 526 | * @param string $toolAdminUsername The tool admin's username who is making the edit |
||
| 527 | */ |
||
| 528 | private function sendStatusChangeEmail($subject, $template, $reason, $user, $toolAdminUsername) |
||
| 529 | { |
||
| 530 | $this->assign('targetUsername', $user->getUsername()); |
||
| 531 | $this->assign('toolAdmin', $toolAdminUsername); |
||
| 532 | $this->assign('actionReason', $reason); |
||
| 533 | $this->assign('mailingList', $this->adminMailingList); |
||
| 534 | |||
| 535 | $this->getEmailHelper()->sendMail( |
||
| 536 | $user->getEmail(), |
||
| 537 | $subject, |
||
| 538 | $this->fetchTemplate($template), |
||
| 539 | array('Reply-To' => $this->adminMailingList) |
||
| 540 | ); |
||
| 541 | } |
||
| 542 | |||
| 543 | /** |
||
| 544 | * @param UserRole[] $activeRoles |
||
| 545 | * |
||
| 546 | * @return array |
||
| 547 | */ |
||
| 548 | private function getRoleData($activeRoles) |
||
| 578 | } |
||
| 579 | } |
||
| 580 |