MediaWiki master
SpecialCreateAccount.php
Go to the documentation of this file.
1<?php
7namespace MediaWiki\Specials;
8
18use StatusValue;
19
28 protected static $allowedActions = [
29 AuthManager::ACTION_CREATE,
30 AuthManager::ACTION_CREATE_CONTINUE
31 ];
32
34 protected static $messages = [
35 'authform-newtoken' => 'nocookiesfornew',
36 'authform-notoken' => 'sessionfailure',
37 'authform-wrongtoken' => 'sessionfailure',
38 ];
39
40 private FormatterFactory $formatterFactory;
41
42 private UserIdentityUtils $identityUtils;
43
44 public function __construct(
45 AuthManager $authManager,
46 FormatterFactory $formatterFactory,
47 UserIdentityUtils $identityUtils
48 ) {
49 parent::__construct( 'CreateAccount', 'createaccount' );
50
51 $this->setAuthManager( $authManager );
52 $this->formatterFactory = $formatterFactory;
53 $this->identityUtils = $identityUtils;
54 }
55
57 public function doesWrites() {
58 return true;
59 }
60
61 public function checkPermissions() {
62 parent::checkPermissions();
63
64 $performer = $this->getAuthority();
65 $authManager = $this->getAuthManager();
66
67 $status = $this->mPosted ?
68 $authManager->authorizeCreateAccount( $performer ) :
69 $authManager->probablyCanCreateAccount( $performer );
70
71 if ( !$status->isGood() ) {
72 $formatter = $this->formatterFactory->getStatusFormatter( $this->getContext() );
73 $messages = [];
74 foreach ( $status->getMessages() as $message ) {
75 $messages[] = $message->getKey();
76 }
77 $this->logAuthResult(
78 false, $performer->getUser(),
79 implode( '|', $messages )
80 );
81 throw new ErrorPageError(
82 'createacct-error',
83 $formatter->getMessage( $status )
84 );
85 }
86 }
87
89 protected function getLoginSecurityLevel() {
90 return false;
91 }
92
94 protected function getDefaultAction( $subPage ) {
95 return AuthManager::ACTION_CREATE;
96 }
97
99 public function getDescription() {
100 return $this->msg( 'createaccount' );
101 }
102
104 protected function isSignup() {
105 return true;
106 }
107
115 protected function successfulAction( $direct = false, $extraMessages = null ) {
116 $session = $this->getRequest()->getSession();
117 $user = $this->targetUser ?: $this->getUser();
118
119 $injected_html = '';
120 if ( $direct ) {
121 # Only save preferences if the user is not creating an account for someone else.
122 if ( !$this->proxyAccountCreation ) {
123 $this->getHookRunner()->onAddNewAccount( $user, false );
124 } else {
125 $byEmail = false; // FIXME no way to set this
126
127 $this->getHookRunner()->onAddNewAccount( $user, $byEmail );
128
129 $out = $this->getOutput();
130 // @phan-suppress-next-line PhanImpossibleCondition
131 $out->setPageTitleMsg( $this->msg( $byEmail ? 'accmailtitle' : 'accountcreated' ) );
132 // @phan-suppress-next-line PhanImpossibleCondition
133 if ( $byEmail ) {
134 $out->addWikiMsg( 'accmailtext', $user->getName(), $user->getEmail() );
135 } else {
136 $out->addWikiMsg( 'accountcreatedtext', $user->getName() );
137 }
138
139 $rt = Title::newFromText( $this->mReturnTo );
140 $out->addReturnTo(
141 ( $rt && !$rt->isExternal() ) ? $rt : $this->getPageTitle(),
142 wfCgiToArray( $this->mReturnToQuery )
143 );
144 return;
145 }
146 $this->getHookRunner()->onUserLoginComplete( $user, $injected_html, $direct );
147 }
148
149 $this->clearToken();
150
151 # Run any hooks; display injected HTML
152 $welcome_creation_msg = 'welcomecreation-msg';
158 $this->getHookRunner()->onBeforeWelcomeCreation( $welcome_creation_msg, $injected_html );
159
160 $this->showSuccessPage( 'signup',
161 // T308471: ensure username is plaintext (aka escaped)
162 $this->msg( 'welcomeuser' )->plaintextParams( $this->getUser()->getName() ),
163 $welcome_creation_msg, $injected_html, $extraMessages );
164 }
165
167 protected function getToken() {
168 return $this->getRequest()->getSession()->getToken( '', 'createaccount' );
169 }
170
171 protected function clearToken() {
172 $this->getRequest()->getSession()->resetToken( 'createaccount' );
173 }
174
176 protected function getTokenName() {
177 return 'wpCreateaccountToken';
178 }
179
181 protected function getGroupName() {
182 return 'users';
183 }
184
186 protected function logAuthResult( $success, UserIdentity $performer, $status = null ) {
187 LoggerFactory::getInstance( 'authevents' )->info( 'Account creation attempt', [
188 'event' => 'accountcreation',
189 'successful' => $success,
190 'accountType' => $this->identityUtils->getShortUserTypeInternal( $performer ),
191 'status' => strval( $status )
192 ] + RequestContext::getMain()->getRequest()->getSecurityLogContext( $performer ) );
193 }
194}
195
197class_alias( SpecialCreateAccount::class, 'SpecialCreateAccount' );
wfCgiToArray( $query)
This is the logical opposite of wfArrayToCgi(): it accepts a query string as its argument and returns...
AuthManager is the authentication system in MediaWiki and serves entry point for authentication.
Group all the pieces relevant to the context of a request into one instance.
An error page which can definitely be safely rendered using the OutputPage.
Factory for formatters of common complex objects.
Create PSR-3 logger objects.
getRequest()
Get the WebRequest being used for this instance.WebRequest 1.18
Holds shared logic for login and account creation pages.
showSuccessPage( $type, $title, $msgname, $injected_html, $extraMessages)
Show the success page.
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.
logAuthResult( $success, UserIdentity $performer, $status=null)
Logs to the authmanager-stats channel.
getToken()
Returns the CSRF token.to override Token
getDefaultAction( $subPage)
Get the default action for this special page if none is given via URL/POST data.Subclasses should ove...
getDescription()
Returns the name that goes in the <h1> in the special page itself, and also the name that will be l...
__construct(AuthManager $authManager, FormatterFactory $formatterFactory, UserIdentityUtils $identityUtils)
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 POST requests to this special page require write access to the wiki....
Represents a title within MediaWiki.
Definition Title.php:70
Convenience functions for interpreting UserIdentity objects using additional services or config.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Interface for objects representing user identity.