MediaWiki  master
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 = MediaWikiServices::getInstance()->getAuthManager()
70  ->checkAccountCreatePermissions( $user );
71  if ( !$status->isGood() ) {
72  throw new ErrorPageError( 'createacct-error', $status->getMessage() );
73  }
74  }
75 
76  protected function getLoginSecurityLevel() {
77  return false;
78  }
79 
80  protected function getDefaultAction( $subPage ) {
81  return AuthManager::ACTION_CREATE;
82  }
83 
84  public function getDescription() {
85  return $this->msg( 'createaccount' )->text();
86  }
87 
88  protected function isSignup() {
89  return true;
90  }
91 
99  protected function successfulAction( $direct = false, $extraMessages = null ) {
100  $session = $this->getRequest()->getSession();
101  $user = $this->targetUser ?: $this->getUser();
102 
103  if ( $direct ) {
104  # Only save preferences if the user is not creating an account for someone else.
105  if ( !$this->proxyAccountCreation ) {
106  $this->getHookRunner()->onAddNewAccount( $user, false );
107 
108  // If the user does not have a session cookie at this point, they probably need to
109  // do something to their browser.
110  if ( !$this->hasSessionCookie() ) {
111  $this->mainLoginForm( [ /*?*/ ], $session->getProvider()->whyNoSession() );
112  // TODO something more specific? This used to use nocookiesnew
113  // FIXME should redirect to login page instead?
114  return;
115  }
116  } else {
117  $byEmail = false; // FIXME no way to set this
118 
119  $this->getHookRunner()->onAddNewAccount( $user, $byEmail );
120 
121  $out = $this->getOutput();
122  // @phan-suppress-next-line PhanImpossibleCondition
123  $out->setPageTitle( $this->msg( $byEmail ? 'accmailtitle' : 'accountcreated' ) );
124  // @phan-suppress-next-line PhanImpossibleCondition
125  if ( $byEmail ) {
126  $out->addWikiMsg( 'accmailtext', $user->getName(), $user->getEmail() );
127  } else {
128  $out->addWikiMsg( 'accountcreatedtext', $user->getName() );
129  }
130 
131  $rt = Title::newFromText( $this->mReturnTo );
132  $out->addReturnTo(
133  ( $rt && !$rt->isExternal() ) ? $rt : $this->getPageTitle(),
134  wfCgiToArray( $this->mReturnToQuery )
135  );
136  return;
137  }
138  }
139 
140  $this->clearToken();
141 
142  # Run any hooks; display injected HTML
143  $injected_html = '';
144  $welcome_creation_msg = 'welcomecreation-msg';
145  $this->getHookRunner()->onUserLoginComplete( $user, $injected_html, $direct );
146 
152  $this->getHookRunner()->onBeforeWelcomeCreation( $welcome_creation_msg, $injected_html );
153 
154  $this->showSuccessPage( 'signup', $this->msg( 'welcomeuser', $this->getUser()->getName() ),
155  $welcome_creation_msg, $injected_html, $extraMessages );
156  }
157 
158  protected function getToken() {
159  return $this->getRequest()->getSession()->getToken( '', 'createaccount' );
160  }
161 
162  protected function clearToken() {
163  return $this->getRequest()->getSession()->resetToken( 'createaccount' );
164  }
165 
166  protected function getTokenName() {
167  return 'wpCreateaccountToken';
168  }
169 
170  protected function getGroupName() {
171  return 'login';
172  }
173 
174  protected function logAuthResult( $success, $status = null ) {
175  LoggerFactory::getInstance( 'authevents' )->info( 'Account creation attempt', [
176  'event' => 'accountcreation',
177  'successful' => $success,
178  'status' => $status,
179  ] );
180  }
181 }
SpecialPage\msg
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
Definition: SpecialPage.php:828
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:329
SpecialCreateAccount\getDefaultAction
getDefaultAction( $subPage)
Get the default action for this special page, if none is given via URL/POST data.
Definition: SpecialCreateAccount.php:80
SpecialPage\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: SpecialPage.php:744
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:152
LoginSignupSpecialPage\showSuccessPage
showSuccessPage( $type, $title, $msgname, $injected_html, $extraMessages)
Show the success page.
Definition: LoginSignupSpecialPage.php:440
SpecialCreateAccount\$messages
static $messages
Definition: SpecialCreateAccount.php:39
SpecialCreateAccount\isSignup
isSignup()
Definition: SpecialCreateAccount.php:88
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:166
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:164
SpecialCreateAccount\successfulAction
successfulAction( $direct=false, $extraMessages=null)
Run any hooks registered for logins, then display a message welcoming the user.
Definition: SpecialCreateAccount.php:99
SpecialCreateAccount\clearToken
clearToken()
Definition: SpecialCreateAccount.php:162
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:84
AuthManagerSpecialPage\$subPage
string $subPage
Subpage of the special page.
Definition: AuthManagerSpecialPage.php:38
SpecialPage\getHookRunner
getHookRunner()
Definition: SpecialPage.php:1010
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()
Stable for overriding.
Definition: SpecialCreateAccount.php:76
wfCgiToArray
wfCgiToArray( $query)
This is the logical opposite of wfArrayToCgi(): it accepts a query string as its argument and returns...
Definition: GlobalFunctions.php:391
LoginSignupSpecialPage\mainLoginForm
mainLoginForm(array $requests, $msg='', $msgtype='error')
Definition: LoginSignupSpecialPage.php:519
SpecialPage\getUser
getUser()
Shortcut to get the User executing this instance.
Definition: SpecialPage.php:754
LoginSignupSpecialPage\hasSessionCookie
hasSessionCookie()
Check if a session cookie is present.
Definition: LoginSignupSpecialPage.php:1067
SpecialCreateAccount\__construct
__construct()
Definition: SpecialCreateAccount.php:45
SpecialCreateAccount\logAuthResult
logAuthResult( $success, $status=null)
Logs to the authmanager-stats channel.
Definition: SpecialCreateAccount.php:174
SpecialCreateAccount\$allowedActions
static $allowedActions
Definition: SpecialCreateAccount.php:34
MediaWiki\Auth\AuthManager
This serves as the entry point to the authentication system.
Definition: AuthManager.php:88
SpecialCreateAccount\getGroupName
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
Definition: SpecialCreateAccount.php:170
SpecialCreateAccount\getToken
getToken()
Returns the CSRF token.
Definition: SpecialCreateAccount.php:158
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:73
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:30
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:59