| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\Specials; |
| 22 |
|
| 23 |
use ErrorPageError; |
| 24 |
use MediaWiki\Auth\AuthManager; |
| 25 |
use MediaWiki\Language\FormatterFactory; |
| 26 |
use MediaWiki\Logger\LoggerFactory; |
| 27 |
use MediaWiki\SpecialPage\LoginSignupSpecialPage; |
| 28 |
use MediaWiki\Title\Title; |
| 29 |
use StatusValue; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
class SpecialCreateAccount extends LoginSignupSpecialPage { |
| 38 |
|
| 39 |
protected static $allowedActions = [ |
| 40 |
AuthManager::ACTION_CREATE, |
| 41 |
AuthManager::ACTION_CREATE_CONTINUE |
| 42 |
]; |
| 43 |
|
| 44 |
|
| 45 |
protected static $messages = [ |
| 46 |
'authform-newtoken' => 'nocookiesfornew', |
| 47 |
'authform-notoken' => 'sessionfailure', |
| 48 |
'authform-wrongtoken' => 'sessionfailure', |
| 49 |
]; |
| 50 |
|
| 51 |
private FormatterFactory $formatterFactory; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public function __construct( AuthManager $authManager, FormatterFactory $formatterFactory ) { |
| 58 |
parent::__construct( 'CreateAccount', 'createaccount' ); |
| 59 |
|
| 60 |
$this->setAuthManager( $authManager ); |
| 61 |
$this->formatterFactory = $formatterFactory; |
| 62 |
} |
| 63 |
|
| 64 |
public function doesWrites() { |
| 65 |
return true; |
| 66 |
} |
| 67 |
|
| 68 |
public function checkPermissions() { |
| 69 |
parent::checkPermissions(); |
| 70 |
|
| 71 |
$performer = $this->getAuthority(); |
| 72 |
$authManager = $this->getAuthManager(); |
| 73 |
|
| 74 |
$status = $this->mPosted ? |
| 75 |
$authManager->authorizeCreateAccount( $performer ) : |
| 76 |
$authManager->probablyCanCreateAccount( $performer ); |
| 77 |
|
| 78 |
if ( !$status->isGood() ) { |
| 79 |
$formatter = $this->formatterFactory->getStatusFormatter( $this->getContext() ); |
| 80 |
throw new ErrorPageError( |
| 81 |
'createacct-error', |
| 82 |
$formatter->getMessage( $status ) |
| 83 |
); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
protected function getLoginSecurityLevel() { |
| 88 |
return false; |
| 89 |
} |
| 90 |
|
| 91 |
protected function getDefaultAction( $subPage ) { |
| 92 |
return AuthManager::ACTION_CREATE; |
| 93 |
} |
| 94 |
|
| 95 |
public function getDescription() { |
| 96 |
return $this->msg( 'createaccount' ); |
| 97 |
} |
| 98 |
|
| 99 |
protected function isSignup() { |
| 100 |
return true; |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
protected function successfulAction( $direct = false, $extraMessages = null ) { |
| 111 |
$session = $this->getRequest()->getSession(); |
| 112 |
$user = $this->targetUser ?: $this->getUser(); |
| 113 |
|
| 114 |
$injected_html = ''; |
| 115 |
if ( $direct ) { |
| 116 |
|
| 117 |
if ( !$this->proxyAccountCreation ) { |
| 118 |
$this->getHookRunner()->onAddNewAccount( $user, false ); |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
if ( !$this->hasSessionCookie() ) { |
| 123 |
$this->mainLoginForm( [ ], $session->getProvider()->whyNoSession() ); |
| 124 |
|
| 125 |
|
| 126 |
return; |
| 127 |
} |
| 128 |
} else { |
| 129 |
$byEmail = false; |
| 130 |
|
| 131 |
$this->getHookRunner()->onAddNewAccount( $user, $byEmail ); |
| 132 |
|
| 133 |
$out = $this->getOutput(); |
| 134 |
|
| 135 |
$out->setPageTitleMsg( $this->msg( $byEmail ? 'accmailtitle' : 'accountcreated' ) ); |
| 136 |
|
| 137 |
if ( $byEmail ) { |
| 138 |
$out->addWikiMsg( 'accmailtext', $user->getName(), $user->getEmail() ); |
| 139 |
} else { |
| 140 |
$out->addWikiMsg( 'accountcreatedtext', $user->getName() ); |
| 141 |
} |
| 142 |
|
| 143 |
$rt = Title::newFromText( $this->mReturnTo ); |
| 144 |
$out->addReturnTo( |
| 145 |
( $rt && !$rt->isExternal() ) ? $rt : $this->getPageTitle(), |
| 146 |
wfCgiToArray( $this->mReturnToQuery ) |
| 147 |
); |
| 148 |
return; |
| 149 |
} |
| 150 |
$this->getHookRunner()->onUserLoginComplete( $user, $injected_html, $direct ); |
| 151 |
} |
| 152 |
|
| 153 |
$this->clearToken(); |
| 154 |
|
| 155 |
|
| 156 |
$welcome_creation_msg = 'welcomecreation-msg'; |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
$this->getHookRunner()->onBeforeWelcomeCreation( $welcome_creation_msg, $injected_html ); |
| 163 |
|
| 164 |
$this->showSuccessPage( 'signup', |
| 165 |
|
| 166 |
$this->msg( 'welcomeuser' )->plaintextParams( $this->getUser()->getName() ), |
| 167 |
$welcome_creation_msg, $injected_html, $extraMessages ); |
| 168 |
} |
| 169 |
|
| 170 |
protected function getToken() { |
| 171 |
return $this->getRequest()->getSession()->getToken( '', 'createaccount' ); |
| 172 |
} |
| 173 |
|
| 174 |
protected function clearToken() { |
| 175 |
$this->getRequest()->getSession()->resetToken( 'createaccount' ); |
| 176 |
} |
| 177 |
|
| 178 |
protected function getTokenName() { |
| 179 |
return 'wpCreateaccountToken'; |
| 180 |
} |
| 181 |
|
| 182 |
protected function getGroupName() { |
| 183 |
return 'users'; |
| 184 |
} |
| 185 |
|
| 186 |
protected function logAuthResult( $success, $status = null ) { |
| 187 |
LoggerFactory::getInstance( 'authevents' )->info( 'Account creation attempt', [ |
| 188 |
'event' => 'accountcreation', |
| 189 |
'successful' => $success, |
| 190 |
'status' => strval( $status ), |
| 191 |
] ); |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
class_alias( SpecialCreateAccount::class, 'SpecialCreateAccount' ); |