MediaWiki 1.43.1
SpecialChangeCredentials.php
Go to the documentation of this file.
1<?php
2
3namespace MediaWiki\Specials;
4
5use LogicException;
17
29 protected static $allowedActions = [ AuthManager::ACTION_CHANGE ];
30
32 protected static $messagePrefix = 'changecredentials';
33
35 protected static $loadUserData = true;
36
40 public function __construct( AuthManager $authManager ) {
41 parent::__construct( 'ChangeCredentials', 'editmyprivateinfo' );
42 $this->setAuthManager( $authManager );
43 }
44
45 protected function getGroupName() {
46 return 'login';
47 }
48
49 public function isListed() {
50 $this->loadAuth( '' );
51 return (bool)$this->authRequests;
52 }
53
54 public function doesWrites() {
55 return true;
56 }
57
58 protected function getDefaultAction( $subPage ) {
59 return AuthManager::ACTION_CHANGE;
60 }
61
62 public function execute( $subPage ) {
63 $this->setHeaders();
64 $this->outputHeader();
65
66 $this->loadAuth( $subPage );
67
68 if ( !$subPage ) {
69 $this->showSubpageList();
70 return;
71 }
72
73 if ( !$this->authRequests ) {
74 // messages used: changecredentials-invalidsubpage, removecredentials-invalidsubpage
75 $this->showSubpageList( $this->msg( static::$messagePrefix . '-invalidsubpage', $subPage ) );
76 return;
77 }
78
79 $out = $this->getOutput();
80 $out->addModules( 'mediawiki.special.changecredentials' );
81 $out->addBacklinkSubtitle( $this->getPageTitle() );
82 $status = $this->trySubmit();
83
84 if ( $status === false || !$status->isOK() ) {
85 $this->displayForm( $status );
86 return;
87 }
88
89 $response = $status->getValue();
90
91 switch ( $response->status ) {
92 case AuthenticationResponse::PASS:
93 $this->success();
94 break;
95 case AuthenticationResponse::FAIL:
96 $this->displayForm( Status::newFatal( $response->message ) );
97 break;
98 default:
99 throw new LogicException( 'invalid AuthenticationResponse' );
100 }
101 }
102
103 protected function loadAuth( $subPage, $authAction = null, $reset = false ) {
104 parent::loadAuth( $subPage, $authAction );
105 if ( $subPage ) {
106 $foundReqs = [];
107 foreach ( $this->authRequests as $req ) {
108 if ( $req->getUniqueId() === $subPage ) {
109 $foundReqs[] = $req;
110 }
111 }
112 if ( count( $foundReqs ) > 1 ) {
113 throw new LogicException( 'Multiple AuthenticationRequest objects with same ID!' );
114 }
115 $this->authRequests = $foundReqs;
116 }
117 }
118
120 public function onAuthChangeFormFields(
121 array $requests, array $fieldInfo, array &$formDescriptor, $action
122 ) {
123 parent::onAuthChangeFormFields( $requests, $fieldInfo, $formDescriptor, $action );
124
125 // Add some UI flair for password changes, the most common use case for this page.
126 if ( AuthenticationRequest::getRequestByClass( $this->authRequests,
127 PasswordAuthenticationRequest::class )
128 ) {
129 $formDescriptor = self::mergeDefaultFormDescriptor( $fieldInfo, $formDescriptor, [
130 'password' => [
131 'autocomplete' => 'new-password',
132 'placeholder-message' => 'createacct-yourpassword-ph',
133 'help-message' => 'createacct-useuniquepass',
134 ],
135 'retype' => [
136 'autocomplete' => 'new-password',
137 'placeholder-message' => 'createacct-yourpasswordagain-ph',
138 ],
139 // T263927 - the Chromium password form guide recommends always having a username field
140 'username' => [
141 'type' => 'text',
142 'baseField' => 'password',
143 'autocomplete' => 'username',
144 'nodata' => true,
145 'readonly' => true,
146 'cssclass' => 'mw-htmlform-hidden-field',
147 'label-message' => 'userlogin-yourname',
148 'placeholder-message' => 'userlogin-yourname-ph',
149 ],
150 ] );
151 }
152 }
153
154 protected function getAuthFormDescriptor( $requests, $action ) {
155 if ( !static::$loadUserData ) {
156 return [];
157 }
158
159 $descriptor = parent::getAuthFormDescriptor( $requests, $action );
160
161 $any = false;
162 foreach ( $descriptor as &$field ) {
163 if ( $field['type'] === 'password' && $field['name'] !== 'retype' ) {
164 $any = true;
165 if ( isset( $field['cssclass'] ) ) {
166 $field['cssclass'] .= ' mw-changecredentials-validate-password';
167 } else {
168 $field['cssclass'] = 'mw-changecredentials-validate-password';
169 }
170 }
171 }
172 unset( $field );
173
174 if ( $any ) {
175 $this->getOutput()->addModules( 'mediawiki.misc-authed-ooui' );
176 }
177
178 return $descriptor;
179 }
180
181 protected function getAuthForm( array $requests, $action ) {
182 $form = parent::getAuthForm( $requests, $action );
183 $req = reset( $requests );
184 $info = $req->describeCredentials();
185
186 $form->addPreHtml(
187 Html::openElement( 'dl' )
188 . Html::element( 'dt', [], $this->msg( 'credentialsform-provider' )->text() )
189 . Html::element( 'dd', [], $info['provider']->text() )
190 . Html::element( 'dt', [], $this->msg( 'credentialsform-account' )->text() )
191 . Html::element( 'dd', [], $info['account']->text() )
192 . Html::closeElement( 'dl' )
193 );
194
195 // messages used: changecredentials-submit removecredentials-submit
196 $form->setSubmitTextMsg( static::$messagePrefix . '-submit' );
197 $form->showCancel()->setCancelTarget( $this->getReturnUrl() ?: Title::newMainPage() );
198 $form->setSubmitID( 'change_credentials_submit' );
199 return $form;
200 }
201
202 protected function needsSubmitButton( array $requests ) {
203 // Change/remove forms show are built from a single AuthenticationRequest and do not allow
204 // for redirect flow; they always need a submit button.
205 return true;
206 }
207
208 public function handleFormSubmit( $data ) {
209 // remove requests do not accept user input
210 $requests = $this->authRequests;
211 if ( static::$loadUserData ) {
212 $requests = AuthenticationRequest::loadRequestsFromSubmission( $this->authRequests, $data );
213 }
214
215 $response = $this->performAuthenticationStep( $this->authAction, $requests );
216
217 // we can't handle FAIL or similar as failure here since it might require changing the form
218 return Status::newGood( $response );
219 }
220
224 protected function showSubpageList( $error = null ) {
225 $out = $this->getOutput();
226
227 if ( $error ) {
228 $out->addHTML( $error->parse() );
229 }
230
231 $groupedRequests = [];
232 foreach ( $this->authRequests as $req ) {
233 $info = $req->describeCredentials();
234 $groupedRequests[$info['provider']->text()][] = $req;
235 }
236
237 $linkRenderer = $this->getLinkRenderer();
238 $out->addHTML( Html::openElement( 'dl' ) );
239 foreach ( $groupedRequests as $group => $members ) {
240 $out->addHTML( Html::element( 'dt', [], $group ) );
241 foreach ( $members as $req ) {
243 $info = $req->describeCredentials();
244 $out->addHTML( Html::rawElement( 'dd', [],
245 $linkRenderer->makeLink(
246 $this->getPageTitle( $req->getUniqueId() ),
247 $info['account']->text()
248 )
249 ) );
250 }
251 }
252 $out->addHTML( Html::closeElement( 'dl' ) );
253 }
254
255 protected function success() {
256 $session = $this->getRequest()->getSession();
257 $user = $this->getUser();
258 $out = $this->getOutput();
259 $returnUrl = $this->getReturnUrl();
260
261 // change user token and update the session
262 SessionManager::singleton()->invalidateSessionsForUser( $user );
263 $session->setUser( $user );
264 $session->resetId();
265
266 if ( $returnUrl ) {
267 $out->redirect( $returnUrl );
268 } else {
269 // messages used: changecredentials-success removecredentials-success
270 $out->addHTML(
271 Html::successBox(
272 $out->msg( static::$messagePrefix . '-success' )->parse()
273 )
274 );
275 $out->returnToMain();
276 }
277 }
278
282 protected function getReturnUrl() {
283 $request = $this->getRequest();
284 $returnTo = $request->getText( 'returnto' );
285 $returnToQuery = $request->getText( 'returntoquery', '' );
286
287 if ( !$returnTo ) {
288 return null;
289 }
290
291 return Title::newFromText( $returnTo )->getFullUrlForRedirect( $returnToQuery );
292 }
293
294 protected function getRequestBlacklist() {
296 }
297}
298
300class_alias( SpecialChangeCredentials::class, 'SpecialChangeCredentials' );
This serves as the entry point to the authentication system.
This is a value object for authentication requests.
describeCredentials()
Describe the credentials represented by this request.
This is a value object to hold authentication response data.
This is a value object for authentication requests with a username and password.
This class is a collection of static functions that serve two purposes:
Definition Html.php:57
A class containing constants representing the names of configuration variables.
const ChangeCredentialsBlacklist
Name constant for the ChangeCredentialsBlacklist setting, for use with Config::get()
The Message class deals with fetching and processing of interface message into a variety of formats.
Definition Message.php:156
This serves as the entry point to the MediaWiki session handling system.
A special page subclass for authentication-related special pages.
string $authAction
one of the AuthManager::ACTION_* constants.
static mergeDefaultFormDescriptor(array $fieldInfo, array $formDescriptor, array $defaultFormDescriptor)
Apply defaults to a form descriptor, without creating non-existent fields.
trySubmit()
Attempts to do an authentication step with the submitted data.
getRequest()
Get the WebRequest being used for this instance.
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
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.
getConfig()
Shortcut to get main config object.
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
getOutput()
Get the OutputPage being used for this instance.
outputHeader( $summaryMessageKey='')
Outputs a summary message on top of special pages By default the message key is the canonical name of...
Change user credentials, such as the password.
onAuthChangeFormFields(array $requests, array $fieldInfo, array &$formDescriptor, $action)
Change the form descriptor that determines how a field will look in the authentication form....
needsSubmitButton(array $requests)
Returns true if the form built from the given AuthenticationRequests needs a submit button.
loadAuth( $subPage, $authAction=null, $reset=false)
Load or initialize $authAction, $authRequests and $subPage.
handleFormSubmit( $data)
Submit handler callback for HTMLForm.
execute( $subPage)
Default execute method Checks user permissions.
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
static bool $loadUserData
Change action needs user data; remove action does not.
isListed()
Whether this special page is listed in Special:SpecialPages.
getRequestBlacklist()
Allows blacklisting certain request types.
getDefaultAction( $subPage)
Get the default action for this special page if none is given via URL/POST data.
getAuthFormDescriptor( $requests, $action)
Generates a HTMLForm descriptor array from a set of authentication requests.
doesWrites()
Indicates whether POST requests to this special page require write access to the wiki.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:54
Represents a title within MediaWiki.
Definition Title.php:78
element(SerializerNode $parent, SerializerNode $node, $contents)