SpecialLinkAccounts.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. use MediaWiki\Auth\AuthenticationRequest;
  3. use MediaWiki\Auth\AuthenticationResponse;
  4. use MediaWiki\Auth\AuthManager;
  5. /**
  6. * Links/unlinks external accounts to the current user.
  7. *
  8. * To interact with this page, account providers need to register themselves with AuthManager.
  9. */
  10. class SpecialLinkAccounts extends AuthManagerSpecialPage {
  11. protected static $allowedActions = [
  12. AuthManager::ACTION_LINK, AuthManager::ACTION_LINK_CONTINUE,
  13. ];
  14. public function __construct() {
  15. parent::__construct( 'LinkAccounts' );
  16. }
  17. protected function getGroupName() {
  18. return 'users';
  19. }
  20. public function isListed() {
  21. return AuthManager::singleton()->canLinkAccounts();
  22. }
  23. protected function getRequestBlacklist() {
  24. return $this->getConfig()->get( 'ChangeCredentialsBlacklist' );
  25. }
  26. /**
  27. * @param null|string $subPage
  28. * @throws ErrorPageError
  29. * @throws LogicException
  30. */
  31. public function execute( $subPage ) {
  32. $this->setHeaders();
  33. $this->loadAuth( $subPage );
  34. if ( !$this->isActionAllowed( $this->authAction ) ) {
  35. if ( $this->authAction === AuthManager::ACTION_LINK ) {
  36. // looks like no linking provider is installed or willing to take this user
  37. $titleMessage = $this->msg( 'cannotlink-no-provider-title' );
  38. $errorMessage = $this->msg( 'cannotlink-no-provider' );
  39. throw new ErrorPageError( $titleMessage, $errorMessage );
  40. } else {
  41. // user probably back-button-navigated into an auth session that no longer exists
  42. // FIXME would be nice to show a message
  43. $this->getOutput()->redirect( $this->getPageTitle()->getFullURL( '', false,
  44. PROTO_HTTPS ) );
  45. return;
  46. }
  47. }
  48. $this->outputHeader();
  49. $status = $this->trySubmit();
  50. if ( $status === false || !$status->isOK() ) {
  51. $this->displayForm( $status );
  52. return;
  53. }
  54. $response = $status->getValue();
  55. switch ( $response->status ) {
  56. case AuthenticationResponse::PASS:
  57. $this->success();
  58. break;
  59. case AuthenticationResponse::FAIL:
  60. $this->loadAuth( '', AuthManager::ACTION_LINK, true );
  61. $this->displayForm( StatusValue::newFatal( $response->message ) );
  62. break;
  63. case AuthenticationResponse::REDIRECT:
  64. $this->getOutput()->redirect( $response->redirectTarget );
  65. break;
  66. case AuthenticationResponse::UI:
  67. $this->authAction = AuthManager::ACTION_LINK_CONTINUE;
  68. $this->authRequests = $response->neededRequests;
  69. $this->displayForm( StatusValue::newFatal( $response->message ) );
  70. break;
  71. default:
  72. throw new LogicException( 'invalid AuthenticationResponse' );
  73. }
  74. }
  75. protected function getDefaultAction( $subPage ) {
  76. return AuthManager::ACTION_LINK;
  77. }
  78. /**
  79. * @param AuthenticationRequest[] $requests
  80. * @param string $action AuthManager action name, should be ACTION_LINK or ACTION_LINK_CONTINUE
  81. * @return HTMLForm
  82. */
  83. protected function getAuthForm( array $requests, $action ) {
  84. $form = parent::getAuthForm( $requests, $action );
  85. $form->setSubmitTextMsg( 'linkaccounts-submit' );
  86. return $form;
  87. }
  88. /**
  89. * Show a success message.
  90. */
  91. protected function success() {
  92. $this->loadAuth( '', AuthManager::ACTION_LINK, true );
  93. $this->displayForm( StatusValue::newFatal( $this->msg( 'linkaccounts-success-text' ) ) );
  94. }
  95. }