MediaWiki master
SpecialCreateAccount.php
Go to the documentation of this file.
1<?php
24namespace MediaWiki\Specials;
25
32use StatusValue;
33
40 protected static $allowedActions = [
41 AuthManager::ACTION_CREATE,
42 AuthManager::ACTION_CREATE_CONTINUE
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
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
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 # Only save preferences if the user is not creating an account for someone else.
117 if ( !$this->proxyAccountCreation ) {
118 $this->getHookRunner()->onAddNewAccount( $user, false );
119
120 // If the user does not have a session cookie at this point, they probably need to
121 // do something to their browser.
122 if ( !$this->hasSessionCookie() ) {
123 $this->mainLoginForm( [ /*?*/ ], $session->getProvider()->whyNoSession() );
124 // TODO something more specific? This used to use nocookiesnew
125 // FIXME should redirect to login page instead?
126 return;
127 }
128 } else {
129 $byEmail = false; // FIXME no way to set this
130
131 $this->getHookRunner()->onAddNewAccount( $user, $byEmail );
132
133 $out = $this->getOutput();
134 // @phan-suppress-next-line PhanImpossibleCondition
135 $out->setPageTitleMsg( $this->msg( $byEmail ? 'accmailtitle' : 'accountcreated' ) );
136 // @phan-suppress-next-line PhanImpossibleCondition
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 # Run any hooks; display injected HTML
156 $welcome_creation_msg = 'welcomecreation-msg';
162 $this->getHookRunner()->onBeforeWelcomeCreation( $welcome_creation_msg, $injected_html );
163
164 $this->showSuccessPage( 'signup',
165 // T308471: ensure username is plaintext (aka escaped)
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
196class_alias( SpecialCreateAccount::class, 'SpecialCreateAccount' );
wfCgiToArray( $query)
This is the logical opposite of wfArrayToCgi(): it accepts a query string as its argument and returns...
An error page which can definitely be safely rendered using the OutputPage.
This serves as the entry point to the authentication system.
Factory for formatters of common complex objects.
Create PSR-3 logger objects.
getRequest()
Get the WebRequest being used for this instance.
Holds shared logic for login and account creation pages.
showSuccessPage( $type, $title, $msgname, $injected_html, $extraMessages)
Show the success page.
mainLoginForm(array $requests, $msg='', $msgtype='error')
hasSessionCookie()
Check if a session cookie is present.
getUser()
Shortcut to get the User executing this instance.
setAuthManager(AuthManager $authManager)
Set the injected AuthManager from the special page constructor.
getPageTitle( $subpage=false)
Get a self-referential title object.
getContext()
Gets the context this SpecialPage is executed in.
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
getOutput()
Get the OutputPage being used for this instance.
getAuthority()
Shortcut to get the Authority executing this instance.
getName()
Get the canonical, unlocalized name of this special page without namespace.
Implements Special:CreateAccount.
getTokenName()
Returns the name of the CSRF token (under which it should be found in the POST or GET data).
__construct(AuthManager $authManager, FormatterFactory $formatterFactory)
getDefaultAction( $subPage)
Get the default action for this special page if none is given via URL/POST data.
logAuthResult( $success, $status=null)
Logs to the authmanager-stats channel.
getDescription()
Returns the name that goes in the <h1> in the special page itself, and also the name that will be l...
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
checkPermissions()
Checks if userCanExecute, and if not throws a PermissionsError.
successfulAction( $direct=false, $extraMessages=null)
Run any hooks registered for logins, then display a message welcoming the user.
doesWrites()
Indicates whether this special page may perform database writes.
Represents a title within MediaWiki.
Definition Title.php:78
Generic operation result class Has warning/error list, boolean status and arbitrary value.
This program is free software; you can redistribute it and/or modify it under the terms of the GNU Ge...