| Total Complexity | 56 |
| Total Lines | 588 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| 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 |
||
| 26 | class PageUserManagement extends InternalPageBase |
||
| 27 | { |
||
| 28 | // FIXME: domains |
||
| 29 | /** @var string */ |
||
| 30 | private $adminMailingList = '[email protected]'; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Main function for this page, when no specific actions are called. |
||
| 34 | */ |
||
| 35 | protected function main() |
||
| 36 | { |
||
| 37 | $this->setHtmlTitle('User Management'); |
||
| 38 | |||
| 39 | $database = $this->getDatabase(); |
||
| 40 | $currentUser = User::getCurrent($database); |
||
| 41 | |||
| 42 | $userSearchRequest = WebRequest::getString('usersearch'); |
||
| 43 | if ($userSearchRequest !== null) { |
||
| 44 | $searchedUser = User::getByUsername($userSearchRequest, $database); |
||
| 45 | if ($searchedUser !== false) { |
||
| 46 | $this->redirect('statistics/users', 'detail', ['user' => $searchedUser->getId()]); |
||
| 47 | return; |
||
| 48 | } |
||
| 49 | } |
||
| 50 | |||
| 51 | // A bit hacky, but it's better than my last solution of creating an object for each user and passing that to |
||
| 52 | // the template. I still don't have a particularly good way of handling this. |
||
| 53 | OAuthUserHelper::prepareTokenCountStatement($database); |
||
| 54 | |||
| 55 | if (WebRequest::getBoolean("showAll")) { |
||
| 56 | $this->assign("showAll", true); |
||
| 57 | |||
| 58 | $suspendedUsers = UserSearchHelper::get($database)->byStatus(User::STATUS_SUSPENDED)->fetch(); |
||
| 59 | $this->assign("suspendedUsers", $suspendedUsers); |
||
| 60 | |||
| 61 | $declinedUsers = UserSearchHelper::get($database)->byStatus(User::STATUS_DECLINED)->fetch(); |
||
| 62 | $this->assign("declinedUsers", $declinedUsers); |
||
| 63 | |||
| 64 | UserSearchHelper::get($database)->getRoleMap($roleMap); |
||
| 65 | } |
||
| 66 | else { |
||
| 67 | $this->assign("showAll", false); |
||
| 68 | $this->assign("suspendedUsers", array()); |
||
| 69 | $this->assign("declinedUsers", array()); |
||
| 70 | |||
| 71 | UserSearchHelper::get($database)->statusIn(array('New', 'Active'))->getRoleMap($roleMap); |
||
| 72 | } |
||
| 73 | |||
| 74 | $newUsers = UserSearchHelper::get($database)->byStatus(User::STATUS_NEW)->fetch(); |
||
| 75 | $normalUsers = UserSearchHelper::get($database)->byStatus(User::STATUS_ACTIVE)->byRole('user')->fetch(); |
||
| 76 | $adminUsers = UserSearchHelper::get($database)->byStatus(User::STATUS_ACTIVE)->byRole('admin')->fetch(); |
||
| 77 | $checkUsers = UserSearchHelper::get($database)->byStatus(User::STATUS_ACTIVE)->byRole('checkuser')->fetch(); |
||
| 78 | $toolRoots = UserSearchHelper::get($database)->byStatus(User::STATUS_ACTIVE)->byRole('toolRoot')->fetch(); |
||
| 79 | $this->assign('newUsers', $newUsers); |
||
| 80 | $this->assign('normalUsers', $normalUsers); |
||
| 81 | $this->assign('adminUsers', $adminUsers); |
||
| 82 | $this->assign('checkUsers', $checkUsers); |
||
| 83 | $this->assign('toolRoots', $toolRoots); |
||
| 84 | |||
| 85 | $this->assign('roles', $roleMap); |
||
| 86 | |||
| 87 | $this->addJs("/api.php?action=users&all=true&targetVariable=typeaheaddata"); |
||
| 88 | |||
| 89 | $this->assign('canApprove', $this->barrierTest('approve', $currentUser)); |
||
| 90 | $this->assign('canDecline', $this->barrierTest('decline', $currentUser)); |
||
| 91 | $this->assign('canRename', $this->barrierTest('rename', $currentUser)); |
||
| 92 | $this->assign('canEditUser', $this->barrierTest('editUser', $currentUser)); |
||
| 93 | $this->assign('canSuspend', $this->barrierTest('suspend', $currentUser)); |
||
| 94 | $this->assign('canEditRoles', $this->barrierTest('editRoles', $currentUser)); |
||
| 95 | |||
| 96 | // FIXME: domains! |
||
| 97 | /** @var Domain $domain */ |
||
| 98 | $domain = Domain::getById(1, $this->getDatabase()); |
||
| 99 | $this->assign('mediawikiScriptPath', $domain->getWikiArticlePath()); |
||
| 100 | |||
| 101 | $this->setTemplate("usermanagement/main.tpl"); |
||
| 102 | } |
||
| 103 | |||
| 104 | #region Access control |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Action target for editing the roles assigned to a user |
||
| 108 | */ |
||
| 109 | protected function editRoles() |
||
| 110 | { |
||
| 111 | $this->setHtmlTitle('User Management'); |
||
| 112 | $database = $this->getDatabase(); |
||
| 113 | $userId = WebRequest::getInt('user'); |
||
| 114 | |||
| 115 | /** @var User $user */ |
||
| 116 | $user = User::getById($userId, $database); |
||
| 117 | |||
| 118 | if ($user === false) { |
||
|
|
|||
| 119 | throw new ApplicationLogicException('Sorry, the user you are trying to edit could not be found.'); |
||
| 120 | } |
||
| 121 | |||
| 122 | $roleData = $this->getRoleData(UserRole::getForUser($user->getId(), $database)); |
||
| 123 | |||
| 124 | // Dual-mode action |
||
| 125 | if (WebRequest::wasPosted()) { |
||
| 126 | $this->validateCSRFToken(); |
||
| 127 | |||
| 128 | $reason = WebRequest::postString('reason'); |
||
| 129 | if ($reason === false || trim($reason) === '') { |
||
| 130 | throw new ApplicationLogicException('No reason specified for roles change'); |
||
| 131 | } |
||
| 132 | |||
| 133 | /** @var UserRole[] $delete */ |
||
| 134 | $delete = array(); |
||
| 135 | /** @var string[] $delete */ |
||
| 136 | $add = array(); |
||
| 137 | |||
| 138 | foreach ($roleData as $name => $r) { |
||
| 139 | if ($r['allowEdit'] !== 1) { |
||
| 140 | // not allowed, to touch this, so ignore it |
||
| 141 | continue; |
||
| 142 | } |
||
| 143 | |||
| 144 | $newValue = WebRequest::postBoolean('role-' . $name) ? 1 : 0; |
||
| 145 | if ($newValue !== $r['active']) { |
||
| 146 | if ($newValue === 0) { |
||
| 147 | $delete[] = $r['object']; |
||
| 148 | } |
||
| 149 | |||
| 150 | if ($newValue === 1) { |
||
| 151 | $add[] = $name; |
||
| 152 | } |
||
| 153 | } |
||
| 154 | } |
||
| 155 | |||
| 156 | // Check there's something to do |
||
| 157 | if ((count($add) + count($delete)) === 0) { |
||
| 158 | $this->redirect('statistics/users', 'detail', array('user' => $user->getId())); |
||
| 159 | SessionAlert::warning('No changes made to roles.'); |
||
| 160 | |||
| 161 | return; |
||
| 162 | } |
||
| 163 | |||
| 164 | $removed = array(); |
||
| 165 | |||
| 166 | /** @var UserRole $d */ |
||
| 167 | foreach ($delete as $d) { |
||
| 168 | $removed[] = $d->getRole(); |
||
| 169 | $d->delete(); |
||
| 170 | } |
||
| 171 | |||
| 172 | foreach ($add as $x) { |
||
| 173 | $a = new UserRole(); |
||
| 174 | $a->setUser($user->getId()); |
||
| 175 | $a->setRole($x); |
||
| 176 | $a->setDatabase($database); |
||
| 177 | $a->save(); |
||
| 178 | } |
||
| 179 | |||
| 180 | Logger::userRolesEdited($database, $user, $reason, $add, $removed); |
||
| 181 | |||
| 182 | // dummy save for optimistic locking. If this fails, the entire txn will roll back. |
||
| 183 | $user->setUpdateVersion(WebRequest::postInt('updateversion')); |
||
| 184 | $user->save(); |
||
| 185 | |||
| 186 | $this->getNotificationHelper()->userRolesEdited($user, $reason); |
||
| 187 | SessionAlert::quick('Roles changed for user ' . htmlentities($user->getUsername(), ENT_COMPAT, 'UTF-8')); |
||
| 188 | |||
| 189 | $this->redirect('statistics/users', 'detail', array('user' => $user->getId())); |
||
| 190 | |||
| 191 | return; |
||
| 192 | } |
||
| 193 | else { |
||
| 194 | $this->assignCSRFToken(); |
||
| 195 | $this->setTemplate('usermanagement/roleedit.tpl'); |
||
| 196 | $this->assign('user', $user); |
||
| 197 | $this->assign('roleData', $roleData); |
||
| 198 | } |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Action target for suspending users |
||
| 203 | * |
||
| 204 | * @throws ApplicationLogicException |
||
| 205 | */ |
||
| 206 | protected function suspend() |
||
| 264 | } |
||
| 265 | } |
||
| 266 | } |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Entry point for the decline action |
||
| 270 | * |
||
| 271 | * @throws ApplicationLogicException |
||
| 272 | */ |
||
| 273 | protected function decline() |
||
| 274 | { |
||
| 275 | $this->setHtmlTitle('User Management'); |
||
| 276 | |||
| 277 | $database = $this->getDatabase(); |
||
| 278 | |||
| 279 | $userId = WebRequest::getInt('user'); |
||
| 280 | $user = User::getById($userId, $database); |
||
| 281 | |||
| 282 | if ($user === false) { |
||
| 283 | throw new ApplicationLogicException('Sorry, the user you are trying to decline could not be found.'); |
||
| 284 | } |
||
| 285 | |||
| 286 | if (!$user->isNewUser()) { |
||
| 287 | throw new ApplicationLogicException('Sorry, the user you are trying to decline is not new.'); |
||
| 288 | } |
||
| 289 | |||
| 290 | // Dual-mode action |
||
| 291 | if (WebRequest::wasPosted()) { |
||
| 292 | $this->validateCSRFToken(); |
||
| 293 | $reason = WebRequest::postString('reason'); |
||
| 294 | |||
| 295 | if ($reason === null || trim($reason) === "") { |
||
| 296 | throw new ApplicationLogicException('No reason provided'); |
||
| 297 | } |
||
| 298 | |||
| 299 | $user->setStatus(User::STATUS_DECLINED); |
||
| 300 | $user->setUpdateVersion(WebRequest::postInt('updateversion')); |
||
| 301 | $user->save(); |
||
| 302 | Logger::declinedUser($database, $user, $reason); |
||
| 303 | |||
| 304 | $this->getNotificationHelper()->userDeclined($user, $reason); |
||
| 305 | SessionAlert::quick('Declined user ' . htmlentities($user->getUsername(), ENT_COMPAT, 'UTF-8')); |
||
| 306 | |||
| 307 | // send email |
||
| 308 | $this->sendStatusChangeEmail( |
||
| 309 | 'Your WP:ACC account has been declined', |
||
| 310 | 'usermanagement/emails/declined.tpl', |
||
| 311 | $reason, |
||
| 312 | $user, |
||
| 313 | User::getCurrent($database)->getUsername() |
||
| 314 | ); |
||
| 315 | |||
| 316 | $this->redirect('userManagement'); |
||
| 317 | |||
| 318 | return; |
||
| 319 | } |
||
| 320 | else { |
||
| 321 | $this->assignCSRFToken(); |
||
| 322 | $this->setTemplate('usermanagement/changelevel-reason.tpl'); |
||
| 323 | $this->assign('user', $user); |
||
| 324 | $this->assign('status', 'Declined'); |
||
| 325 | $this->assign("showReason", true); |
||
| 326 | } |
||
| 327 | } |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Entry point for the approve action |
||
| 331 | * |
||
| 332 | * @throws ApplicationLogicException |
||
| 333 | */ |
||
| 334 | protected function approve() |
||
| 335 | { |
||
| 336 | $this->setHtmlTitle('User Management'); |
||
| 337 | |||
| 338 | $database = $this->getDatabase(); |
||
| 339 | |||
| 340 | $userId = WebRequest::getInt('user'); |
||
| 341 | $user = User::getById($userId, $database); |
||
| 342 | |||
| 343 | if ($user === false) { |
||
| 344 | throw new ApplicationLogicException('Sorry, the user you are trying to approve could not be found.'); |
||
| 345 | } |
||
| 346 | |||
| 347 | if ($user->isActive()) { |
||
| 348 | throw new ApplicationLogicException('Sorry, the user you are trying to approve is already an active user.'); |
||
| 349 | } |
||
| 350 | |||
| 351 | // Dual-mode action |
||
| 352 | if (WebRequest::wasPosted()) { |
||
| 353 | $this->validateCSRFToken(); |
||
| 354 | $user->setStatus(User::STATUS_ACTIVE); |
||
| 355 | $user->setUpdateVersion(WebRequest::postInt('updateversion')); |
||
| 356 | $user->save(); |
||
| 357 | Logger::approvedUser($database, $user); |
||
| 358 | |||
| 359 | $this->getNotificationHelper()->userApproved($user); |
||
| 360 | SessionAlert::quick('Approved user ' . htmlentities($user->getUsername(), ENT_COMPAT, 'UTF-8')); |
||
| 361 | |||
| 362 | // send email |
||
| 363 | $this->sendStatusChangeEmail( |
||
| 364 | 'Your WP:ACC account has been approved', |
||
| 365 | 'usermanagement/emails/approved.tpl', |
||
| 366 | null, |
||
| 367 | $user, |
||
| 368 | User::getCurrent($database)->getUsername() |
||
| 369 | ); |
||
| 370 | |||
| 371 | $this->redirect("userManagement"); |
||
| 372 | |||
| 373 | return; |
||
| 374 | } |
||
| 375 | else { |
||
| 376 | $this->assignCSRFToken(); |
||
| 377 | $this->setTemplate("usermanagement/changelevel-reason.tpl"); |
||
| 378 | $this->assign("user", $user); |
||
| 379 | $this->assign("status", "Active"); |
||
| 380 | $this->assign("showReason", false); |
||
| 381 | } |
||
| 382 | } |
||
| 383 | |||
| 384 | #endregion |
||
| 385 | |||
| 386 | #region Renaming / Editing |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Entry point for the rename action |
||
| 390 | * |
||
| 391 | * @throws ApplicationLogicException |
||
| 392 | */ |
||
| 393 | protected function rename() |
||
| 463 | } |
||
| 464 | } |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Entry point for the edit action |
||
| 468 | * |
||
| 469 | * @throws ApplicationLogicException |
||
| 470 | */ |
||
| 471 | protected function editUser() |
||
| 472 | { |
||
| 473 | $this->setHtmlTitle('User Management'); |
||
| 474 | |||
| 475 | $database = $this->getDatabase(); |
||
| 476 | |||
| 477 | $userId = WebRequest::getInt('user'); |
||
| 478 | $user = User::getById($userId, $database); |
||
| 479 | $oauth = new OAuthUserHelper($user, $database, $this->getOAuthProtocolHelper(), $this->getSiteConfiguration()); |
||
| 480 | |||
| 481 | if ($user === false) { |
||
| 482 | throw new ApplicationLogicException('Sorry, the user you are trying to edit could not be found.'); |
||
| 483 | } |
||
| 484 | |||
| 485 | // Dual-mode action |
||
| 486 | if (WebRequest::wasPosted()) { |
||
| 487 | $this->validateCSRFToken(); |
||
| 488 | $newEmail = WebRequest::postEmail('user_email'); |
||
| 489 | $newOnWikiName = WebRequest::postString('user_onwikiname'); |
||
| 490 | |||
| 491 | if ($newEmail === null) { |
||
| 492 | throw new ApplicationLogicException('Invalid email address'); |
||
| 493 | } |
||
| 494 | |||
| 495 | if ($this->validateUnusedEmail($newEmail, $userId)) { |
||
| 496 | throw new ApplicationLogicException('The specified email address is already in use.'); |
||
| 497 | } |
||
| 498 | |||
| 499 | if (!($oauth->isFullyLinked() || $oauth->isPartiallyLinked())) { |
||
| 500 | if (trim($newOnWikiName) == "") { |
||
| 501 | throw new ApplicationLogicException('New on-wiki username cannot be blank'); |
||
| 502 | } |
||
| 503 | |||
| 504 | $user->setOnWikiName($newOnWikiName); |
||
| 505 | $user->setWelcomeSig(WebRequest::postString('sig')); |
||
| 506 | } |
||
| 507 | |||
| 508 | $user->setEmail($newEmail); |
||
| 509 | $user->setCreationMode(WebRequest::postInt('creationmode')); |
||
| 510 | |||
| 511 | $user->setUpdateVersion(WebRequest::postInt('updateversion')); |
||
| 512 | |||
| 513 | $user->save(); |
||
| 514 | |||
| 515 | Logger::userPreferencesChange($database, $user); |
||
| 516 | $this->getNotificationHelper()->userPrefChange($user); |
||
| 517 | SessionAlert::quick('Changes to user\'s preferences have been saved'); |
||
| 518 | |||
| 519 | $this->redirect("userManagement"); |
||
| 520 | |||
| 521 | return; |
||
| 522 | } |
||
| 523 | else { |
||
| 524 | $this->assignCSRFToken(); |
||
| 525 | $oauth = new OAuthUserHelper($user, $database, $this->getOAuthProtocolHelper(), |
||
| 526 | $this->getSiteConfiguration()); |
||
| 527 | $this->setTemplate('usermanagement/edituser.tpl'); |
||
| 528 | $this->assign('user', $user); |
||
| 529 | $this->assign('oauth', $oauth); |
||
| 530 | |||
| 531 | $this->assign('canManualCreate', |
||
| 532 | $this->barrierTest(User::CREATION_MANUAL, $user, 'RequestCreation')); |
||
| 533 | $this->assign('canOauthCreate', |
||
| 534 | $this->barrierTest(User::CREATION_OAUTH, $user, 'RequestCreation')); |
||
| 535 | $this->assign('canBotCreate', |
||
| 536 | $this->barrierTest(User::CREATION_BOT, $user, 'RequestCreation')); |
||
| 537 | } |
||
| 538 | } |
||
| 539 | |||
| 540 | #endregion |
||
| 541 | |||
| 542 | private function validateUnusedEmail(string $email, int $userId) : bool { |
||
| 550 | } |
||
| 551 | |||
| 552 | /** |
||
| 553 | * Sends a status change email to the user. |
||
| 554 | * |
||
| 555 | * @param string $subject The subject of the email |
||
| 556 | * @param string $template The smarty template to use |
||
| 557 | * @param string|null $reason The reason for performing the status change |
||
| 558 | * @param User $user The user affected |
||
| 559 | * @param string $toolAdminUsername The tool admin's username who is making the edit |
||
| 560 | */ |
||
| 561 | private function sendStatusChangeEmail($subject, $template, $reason, $user, $toolAdminUsername) |
||
| 562 | { |
||
| 563 | $this->assign('targetUsername', $user->getUsername()); |
||
| 564 | $this->assign('toolAdmin', $toolAdminUsername); |
||
| 565 | $this->assign('actionReason', $reason); |
||
| 566 | $this->assign('mailingList', $this->adminMailingList); |
||
| 567 | |||
| 568 | // FIXME: domains! |
||
| 569 | /** @var Domain $domain */ |
||
| 570 | $domain = Domain::getById(1, $this->getDatabase()); |
||
| 571 | $this->getEmailHelper()->sendMail( |
||
| 572 | $this->adminMailingList, |
||
| 573 | $user->getEmail(), |
||
| 574 | $subject, |
||
| 575 | $this->fetchTemplate($template) |
||
| 576 | ); |
||
| 577 | } |
||
| 578 | |||
| 579 | /** |
||
| 580 | * @param UserRole[] $activeRoles |
||
| 581 | * |
||
| 582 | * @return array |
||
| 583 | */ |
||
| 584 | private function getRoleData($activeRoles) |
||
| 614 | } |
||
| 615 | } |
||
| 616 |