SpecialUnlinkAccounts.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. use MediaWiki\Auth\AuthenticationResponse;
  3. use MediaWiki\Auth\AuthManager;
  4. use MediaWiki\Session\SessionManager;
  5. class SpecialUnlinkAccounts extends AuthManagerSpecialPage {
  6. protected static $allowedActions = [ AuthManager::ACTION_UNLINK ];
  7. public function __construct() {
  8. parent::__construct( 'UnlinkAccounts' );
  9. }
  10. protected function getLoginSecurityLevel() {
  11. return 'UnlinkAccount';
  12. }
  13. protected function getDefaultAction( $subPage ) {
  14. return AuthManager::ACTION_UNLINK;
  15. }
  16. /**
  17. * Under which header this special page is listed in Special:SpecialPages.
  18. * @return string
  19. */
  20. protected function getGroupName() {
  21. return 'users';
  22. }
  23. public function isListed() {
  24. return AuthManager::singleton()->canLinkAccounts();
  25. }
  26. protected function getRequestBlacklist() {
  27. return $this->getConfig()->get( 'RemoveCredentialsBlacklist' );
  28. }
  29. public function execute( $subPage ) {
  30. $this->setHeaders();
  31. $this->loadAuth( $subPage );
  32. if ( !$this->isActionAllowed( $this->authAction ) ) {
  33. if ( $this->authAction === AuthManager::ACTION_UNLINK ) {
  34. // Looks like there are no linked accounts to unlink
  35. $titleMessage = $this->msg( 'cannotunlink-no-provider-title' );
  36. $errorMessage = $this->msg( 'cannotunlink-no-provider' );
  37. throw new ErrorPageError( $titleMessage, $errorMessage );
  38. } else {
  39. // user probably back-button-navigated into an auth session that no longer exists
  40. // FIXME would be nice to show a message
  41. $this->getOutput()->redirect( $this->getPageTitle()->getFullURL( '', false, PROTO_HTTPS ) );
  42. return;
  43. }
  44. }
  45. $this->outputHeader();
  46. $status = $this->trySubmit();
  47. if ( $status === false || !$status->isOK() ) {
  48. $this->displayForm( $status );
  49. return;
  50. }
  51. /** @var AuthenticationResponse $response */
  52. $response = $status->getValue();
  53. if ( $response->status === AuthenticationResponse::FAIL ) {
  54. $this->displayForm( StatusValue::newFatal( $response->message ) );
  55. return;
  56. }
  57. $status = StatusValue::newGood();
  58. $status->warning( $this->msg( 'unlinkaccounts-success' ) );
  59. $this->loadAuth( $subPage, null, true ); // update requests so the unlinked one doesn't show up
  60. // Reset sessions - if the user unlinked an account because it was compromised,
  61. // log attackers out from sessions obtained via that account.
  62. $session = $this->getRequest()->getSession();
  63. $user = $this->getUser();
  64. SessionManager::singleton()->invalidateSessionsForUser( $user );
  65. $session->setUser( $user );
  66. $session->resetId();
  67. $this->displayForm( $status );
  68. }
  69. public function handleFormSubmit( $data ) {
  70. // unlink requests do not accept user input so repeat parent code but skip call to
  71. // AuthenticationRequest::loadRequestsFromSubmission
  72. $response = $this->performAuthenticationStep( $this->authAction, $this->authRequests );
  73. return Status::newGood( $response );
  74. }
  75. }