MediaWiki  1.34.0
SpecialCreateAccount.php
Go to the documentation of this file.
1 <?php
27 
34  protected static $allowedActions = [
35  AuthManager::ACTION_CREATE,
36  AuthManager::ACTION_CREATE_CONTINUE
37  ];
38 
39  protected static $messages = [
40  'authform-newtoken' => 'nocookiesfornew',
41  'authform-notoken' => 'sessionfailure',
42  'authform-wrongtoken' => 'sessionfailure',
43  ];
44 
45  public function __construct() {
46  parent::__construct( 'CreateAccount' );
47  }
48 
49  public function doesWrites() {
50  return true;
51  }
52 
53  public function isRestricted() {
54  return !MediaWikiServices::getInstance()
55  ->getPermissionManager()
56  ->groupHasPermission( '*', 'createaccount' );
57  }
58 
59  public function userCanExecute( User $user ) {
60  return MediaWikiServices::getInstance()
61  ->getPermissionManager()
62  ->userHasRight( $user, 'createaccount' );
63  }
64 
65  public function checkPermissions() {
66  parent::checkPermissions();
67 
68  $user = $this->getUser();
69  $status = AuthManager::singleton()->checkAccountCreatePermissions( $user );
70  if ( !$status->isGood() ) {
71  // Track block with a cookie if it doesn't exist already
72  if ( $user->isBlockedFromCreateAccount() ) {
73  MediaWikiServices::getInstance()->getBlockManager()->trackBlockWithCookie( $user );
74  }
75  throw new ErrorPageError( 'createacct-error', $status->getMessage() );
76  }
77  }
78 
79  protected function getLoginSecurityLevel() {
80  return false;
81  }
82 
83  protected function getDefaultAction( $subPage ) {
84  return AuthManager::ACTION_CREATE;
85  }
86 
87  public function getDescription() {
88  return $this->msg( 'createaccount' )->text();
89  }
90 
91  protected function isSignup() {
92  return true;
93  }
94 
102  protected function successfulAction( $direct = false, $extraMessages = null ) {
103  $session = $this->getRequest()->getSession();
104  $user = $this->targetUser ?: $this->getUser();
105 
106  if ( $direct ) {
107  # Only save preferences if the user is not creating an account for someone else.
108  if ( !$this->proxyAccountCreation ) {
109  Hooks::run( 'AddNewAccount', [ $user, false ] );
110 
111  // If the user does not have a session cookie at this point, they probably need to
112  // do something to their browser.
113  if ( !$this->hasSessionCookie() ) {
114  $this->mainLoginForm( [ /*?*/ ], $session->getProvider()->whyNoSession() );
115  // TODO something more specific? This used to use nocookiesnew
116  // FIXME should redirect to login page instead?
117  return;
118  }
119  } else {
120  $byEmail = false; // FIXME no way to set this
121 
122  Hooks::run( 'AddNewAccount', [ $user, $byEmail ] );
123 
124  $out = $this->getOutput();
125  $out->setPageTitle( $this->msg( $byEmail ? 'accmailtitle' : 'accountcreated' ) );
126  if ( $byEmail ) {
127  $out->addWikiMsg( 'accmailtext', $user->getName(), $user->getEmail() );
128  } else {
129  $out->addWikiMsg( 'accountcreatedtext', $user->getName() );
130  }
131 
132  $rt = Title::newFromText( $this->mReturnTo );
133  $out->addReturnTo(
134  ( $rt && !$rt->isExternal() ) ? $rt : $this->getPageTitle(),
135  wfCgiToArray( $this->mReturnToQuery )
136  );
137  return;
138  }
139  }
140 
141  $this->clearToken();
142 
143  # Run any hooks; display injected HTML
144  $injected_html = '';
145  $welcome_creation_msg = 'welcomecreation-msg';
146  Hooks::run( 'UserLoginComplete', [ &$user, &$injected_html, $direct ] );
147 
153  Hooks::run( 'BeforeWelcomeCreation', [ &$welcome_creation_msg, &$injected_html ] );
154 
155  $this->showSuccessPage( 'signup', $this->msg( 'welcomeuser', $this->getUser()->getName() ),
156  $welcome_creation_msg, $injected_html, $extraMessages );
157  }
158 
159  protected function getToken() {
160  return $this->getRequest()->getSession()->getToken( '', 'createaccount' );
161  }
162 
163  protected function clearToken() {
164  return $this->getRequest()->getSession()->resetToken( 'createaccount' );
165  }
166 
167  protected function getTokenName() {
168  return 'wpCreateaccountToken';
169  }
170 
171  protected function getGroupName() {
172  return 'login';
173  }
174 
175  protected function logAuthResult( $success, $status = null ) {
176  LoggerFactory::getInstance( 'authevents' )->info( 'Account creation attempt', [
177  'event' => 'accountcreation',
178  'successful' => $success,
179  'status' => $status,
180  ] );
181  }
182 }
SpecialPage\getPageTitle
getPageTitle( $subpage=false)
Get a self-referential title object.
Definition: SpecialPage.php:672
SpecialPage\msg
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
Definition: SpecialPage.php:792
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:316
SpecialCreateAccount\getDefaultAction
getDefaultAction( $subPage)
Get the default action for this special page, if none is given via URL/POST data.
Definition: SpecialCreateAccount.php:83
SpecialPage\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: SpecialPage.php:719
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:117
LoginSignupSpecialPage\showSuccessPage
showSuccessPage( $type, $title, $msgname, $injected_html, $extraMessages)
Show the success page.
Definition: LoginSignupSpecialPage.php:434
SpecialCreateAccount\$messages
static $messages
Definition: SpecialCreateAccount.php:39
SpecialCreateAccount\isSignup
isSignup()
Definition: SpecialCreateAccount.php:91
SpecialCreateAccount\getTokenName
getTokenName()
Returns the name of the CSRF token (under which it should be found in the POST or GET data).
Definition: SpecialCreateAccount.php:167
SpecialCreateAccount\doesWrites
doesWrites()
Indicates whether this special page may perform database writes.
Definition: SpecialCreateAccount.php:49
$success
$success
Definition: NoLocalSettings.php:42
SpecialPage\getName
getName()
Get the name of this Special Page.
Definition: SpecialPage.php:153
SpecialCreateAccount\successfulAction
successfulAction( $direct=false, $extraMessages=null)
Run any hooks registered for logins, then display a message welcoming the user.
Definition: SpecialCreateAccount.php:102
SpecialCreateAccount\clearToken
clearToken()
Definition: SpecialCreateAccount.php:163
SpecialCreateAccount
Implements Special:CreateAccount.
Definition: SpecialCreateAccount.php:33
SpecialCreateAccount\getDescription
getDescription()
Returns the name that goes in the <h1> in the special page itself, and also the name that will be l...
Definition: SpecialCreateAccount.php:87
AuthManagerSpecialPage\$subPage
string $subPage
Subpage of the special page.
Definition: AuthManagerSpecialPage.php:35
MediaWiki\Logger\LoggerFactory
PSR-3 logger instance factory.
Definition: LoggerFactory.php:45
LoginSignupSpecialPage
Holds shared logic for login and account creation pages.
Definition: LoginSignupSpecialPage.php:37
SpecialCreateAccount\getLoginSecurityLevel
getLoginSecurityLevel()
Tells if the special page does something security-sensitive and needs extra defense against a stolen ...
Definition: SpecialCreateAccount.php:79
wfCgiToArray
wfCgiToArray( $query)
This is the logical opposite of wfArrayToCgi(): it accepts a query string as its argument and returns...
Definition: GlobalFunctions.php:392
LoginSignupSpecialPage\mainLoginForm
mainLoginForm(array $requests, $msg='', $msgtype='error')
Definition: LoginSignupSpecialPage.php:513
SpecialPage\getUser
getUser()
Shortcut to get the User executing this instance.
Definition: SpecialPage.php:729
LoginSignupSpecialPage\hasSessionCookie
hasSessionCookie()
Check if a session cookie is present.
Definition: LoginSignupSpecialPage.php:1044
SpecialCreateAccount\__construct
__construct()
Definition: SpecialCreateAccount.php:45
SpecialCreateAccount\logAuthResult
logAuthResult( $success, $status=null)
Logs to the authmanager-stats channel.
Definition: SpecialCreateAccount.php:175
SpecialCreateAccount\$allowedActions
static $allowedActions
Definition: SpecialCreateAccount.php:34
MediaWiki\Auth\AuthManager
This serves as the entry point to the authentication system.
Definition: AuthManager.php:85
SpecialCreateAccount\getGroupName
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
Definition: SpecialCreateAccount.php:171
$status
return $status
Definition: SyntaxHighlight.php:347
SpecialCreateAccount\getToken
getToken()
Returns the CSRF token.
Definition: SpecialCreateAccount.php:159
SpecialCreateAccount\checkPermissions
checkPermissions()
Checks if userCanExecute, and if not throws a PermissionsError.
Definition: SpecialCreateAccount.php:65
SpecialCreateAccount\isRestricted
isRestricted()
Can be overridden by subclasses with more complicated permissions schemes.
Definition: SpecialCreateAccount.php:53
AuthManagerSpecialPage\getRequest
getRequest()
Get the WebRequest being used for this instance.
Definition: AuthManagerSpecialPage.php:64
SpecialCreateAccount\userCanExecute
userCanExecute(User $user)
Checks if the given user (identified by an object) can execute this special page (as defined by $mRes...
Definition: SpecialCreateAccount.php:59
ErrorPageError
An error page which can definitely be safely rendered using the OutputPage.
Definition: ErrorPageError.php:27
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:51
Hooks\run
static run( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:200