SpecialCreateAccount.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. /**
  3. * Implements Special:CreateAccount
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. * @ingroup SpecialPage
  22. */
  23. use MediaWiki\Auth\AuthManager;
  24. use MediaWiki\Logger\LoggerFactory;
  25. use MediaWiki\MediaWikiServices;
  26. /**
  27. * Implements Special:CreateAccount
  28. *
  29. * @ingroup SpecialPage
  30. */
  31. class SpecialCreateAccount extends LoginSignupSpecialPage {
  32. protected static $allowedActions = [
  33. AuthManager::ACTION_CREATE,
  34. AuthManager::ACTION_CREATE_CONTINUE
  35. ];
  36. protected static $messages = [
  37. 'authform-newtoken' => 'nocookiesfornew',
  38. 'authform-notoken' => 'sessionfailure',
  39. 'authform-wrongtoken' => 'sessionfailure',
  40. ];
  41. public function __construct() {
  42. parent::__construct( 'CreateAccount' );
  43. }
  44. public function doesWrites() {
  45. return true;
  46. }
  47. public function isRestricted() {
  48. return !MediaWikiServices::getInstance()
  49. ->getPermissionManager()
  50. ->groupHasPermission( '*', 'createaccount' );
  51. }
  52. public function userCanExecute( User $user ) {
  53. return MediaWikiServices::getInstance()
  54. ->getPermissionManager()
  55. ->userHasRight( $user, 'createaccount' );
  56. }
  57. public function checkPermissions() {
  58. parent::checkPermissions();
  59. $user = $this->getUser();
  60. $status = AuthManager::singleton()->checkAccountCreatePermissions( $user );
  61. if ( !$status->isGood() ) {
  62. // Track block with a cookie if it doesn't exist already
  63. if ( $user->isBlockedFromCreateAccount() ) {
  64. MediaWikiServices::getInstance()->getBlockManager()->trackBlockWithCookie( $user );
  65. }
  66. throw new ErrorPageError( 'createacct-error', $status->getMessage() );
  67. }
  68. }
  69. protected function getLoginSecurityLevel() {
  70. return false;
  71. }
  72. protected function getDefaultAction( $subPage ) {
  73. return AuthManager::ACTION_CREATE;
  74. }
  75. public function getDescription() {
  76. return $this->msg( 'createaccount' )->text();
  77. }
  78. protected function isSignup() {
  79. return true;
  80. }
  81. /**
  82. * Run any hooks registered for logins, then display a message welcoming
  83. * the user.
  84. * @param bool $direct True if the action was successful just now; false if that happened
  85. * pre-redirection (so this handler was called already)
  86. * @param StatusValue|null $extraMessages
  87. */
  88. protected function successfulAction( $direct = false, $extraMessages = null ) {
  89. $session = $this->getRequest()->getSession();
  90. $user = $this->targetUser ?: $this->getUser();
  91. if ( $direct ) {
  92. # Only save preferences if the user is not creating an account for someone else.
  93. if ( !$this->proxyAccountCreation ) {
  94. Hooks::run( 'AddNewAccount', [ $user, false ] );
  95. // If the user does not have a session cookie at this point, they probably need to
  96. // do something to their browser.
  97. if ( !$this->hasSessionCookie() ) {
  98. $this->mainLoginForm( [ /*?*/ ], $session->getProvider()->whyNoSession() );
  99. // TODO something more specific? This used to use nocookiesnew
  100. // FIXME should redirect to login page instead?
  101. return;
  102. }
  103. } else {
  104. $byEmail = false; // FIXME no way to set this
  105. Hooks::run( 'AddNewAccount', [ $user, $byEmail ] );
  106. $out = $this->getOutput();
  107. $out->setPageTitle( $this->msg( $byEmail ? 'accmailtitle' : 'accountcreated' ) );
  108. if ( $byEmail ) {
  109. $out->addWikiMsg( 'accmailtext', $user->getName(), $user->getEmail() );
  110. } else {
  111. $out->addWikiMsg( 'accountcreatedtext', $user->getName() );
  112. }
  113. $rt = Title::newFromText( $this->mReturnTo );
  114. $out->addReturnTo(
  115. ( $rt && !$rt->isExternal() ) ? $rt : $this->getPageTitle(),
  116. wfCgiToArray( $this->mReturnToQuery )
  117. );
  118. return;
  119. }
  120. }
  121. $this->clearToken();
  122. # Run any hooks; display injected HTML
  123. $injected_html = '';
  124. $welcome_creation_msg = 'welcomecreation-msg';
  125. Hooks::run( 'UserLoginComplete', [ &$user, &$injected_html, $direct ] );
  126. /**
  127. * Let any extensions change what message is shown.
  128. * @see https://www.mediawiki.org/wiki/Manual:Hooks/BeforeWelcomeCreation
  129. * @since 1.18
  130. */
  131. Hooks::run( 'BeforeWelcomeCreation', [ &$welcome_creation_msg, &$injected_html ] );
  132. $this->showSuccessPage( 'signup', $this->msg( 'welcomeuser', $this->getUser()->getName() ),
  133. $welcome_creation_msg, $injected_html, $extraMessages );
  134. }
  135. protected function getToken() {
  136. return $this->getRequest()->getSession()->getToken( '', 'createaccount' );
  137. }
  138. protected function clearToken() {
  139. return $this->getRequest()->getSession()->resetToken( 'createaccount' );
  140. }
  141. protected function getTokenName() {
  142. return 'wpCreateaccountToken';
  143. }
  144. protected function getGroupName() {
  145. return 'login';
  146. }
  147. protected function logAuthResult( $success, $status = null ) {
  148. LoggerFactory::getInstance( 'authevents' )->info( 'Account creation attempt', [
  149. 'event' => 'accountcreation',
  150. 'successful' => $success,
  151. 'status' => $status,
  152. ] );
  153. }
  154. }