MediaWiki  master
SpecialChangeCredentials.php
Go to the documentation of this file.
1 <?php
2 
7 
14  protected static $allowedActions = [ AuthManager::ACTION_CHANGE ];
15 
16  protected static $messagePrefix = 'changecredentials';
17 
19  protected static $loadUserData = true;
20 
21  public function __construct( $name = 'ChangeCredentials' ) {
22  parent::__construct( $name, 'editmyprivateinfo' );
23  }
24 
25  protected function getGroupName() {
26  return 'users';
27  }
28 
29  public function isListed() {
30  $this->loadAuth( '' );
31  return (bool)$this->authRequests;
32  }
33 
34  public function doesWrites() {
35  return true;
36  }
37 
38  protected function getDefaultAction( $subPage ) {
39  return AuthManager::ACTION_CHANGE;
40  }
41 
42  protected function getPreservedParams( $withToken = false ) {
43  $request = $this->getRequest();
44  $params = parent::getPreservedParams( $withToken );
45  $params += [
46  'returnto' => $request->getVal( 'returnto' ),
47  'returntoquery' => $request->getVal( 'returntoquery' ),
48  ];
49  return $params;
50  }
51 
52  public function execute( $subPage ) {
53  $this->setHeaders();
54  $this->outputHeader();
55 
56  $this->loadAuth( $subPage );
57 
58  if ( !$subPage ) {
59  $this->showSubpageList();
60  return;
61  }
62 
63  if ( !$this->authRequests ) {
64  // messages used: changecredentials-invalidsubpage, removecredentials-invalidsubpage
65  $this->showSubpageList( $this->msg( static::$messagePrefix . '-invalidsubpage', $subPage ) );
66  return;
67  }
68 
69  $this->getOutput()->addBacklinkSubtitle( $this->getPageTitle() );
70 
71  $status = $this->trySubmit();
72 
73  if ( $status === false || !$status->isOK() ) {
74  $this->displayForm( $status );
75  return;
76  }
77 
78  $response = $status->getValue();
79 
80  switch ( $response->status ) {
81  case AuthenticationResponse::PASS:
82  $this->success();
83  break;
84  case AuthenticationResponse::FAIL:
85  $this->displayForm( Status::newFatal( $response->message ) );
86  break;
87  default:
88  throw new LogicException( 'invalid AuthenticationResponse' );
89  }
90  }
91 
92  protected function loadAuth( $subPage, $authAction = null, $reset = false ) {
93  parent::loadAuth( $subPage, $authAction );
94  if ( $subPage ) {
95  $foundReqs = [];
96  foreach ( $this->authRequests as $req ) {
97  if ( $req->getUniqueId() === $subPage ) {
98  $foundReqs[] = $req;
99  }
100  }
101  if ( count( $foundReqs ) > 1 ) {
102  throw new LogicException( 'Multiple AuthenticationRequest objects with same ID!' );
103  }
104  $this->authRequests = $foundReqs;
105  }
106  }
107 
108  protected function getAuthFormDescriptor( $requests, $action ) {
109  if ( !static::$loadUserData ) {
110  return [];
111  } else {
112  $descriptor = parent::getAuthFormDescriptor( $requests, $action );
113 
114  $any = false;
115  foreach ( $descriptor as &$field ) {
116  if ( $field['type'] === 'password' && $field['name'] !== 'retype' ) {
117  $any = true;
118  if ( isset( $field['cssclass'] ) ) {
119  $field['cssclass'] .= ' mw-changecredentials-validate-password';
120  } else {
121  $field['cssclass'] = 'mw-changecredentials-validate-password';
122  }
123  }
124  }
125 
126  if ( $any ) {
127  $this->getOutput()->addModules( 'mediawiki.misc-authed-ooui' );
128  }
129 
130  return $descriptor;
131  }
132  }
133 
134  protected function getAuthForm( array $requests, $action ) {
135  $form = parent::getAuthForm( $requests, $action );
136  $req = reset( $requests );
137  $info = $req->describeCredentials();
138 
139  $form->addPreText(
140  Html::openElement( 'dl' )
141  . Html::element( 'dt', [], $this->msg( 'credentialsform-provider' )->text() )
142  . Html::element( 'dd', [], $info['provider'] )
143  . Html::element( 'dt', [], $this->msg( 'credentialsform-account' )->text() )
144  . Html::element( 'dd', [], $info['account'] )
145  . Html::closeElement( 'dl' )
146  );
147 
148  // messages used: changecredentials-submit removecredentials-submit
149  $form->setSubmitTextMsg( static::$messagePrefix . '-submit' );
150  $form->showCancel()->setCancelTarget( $this->getReturnUrl() ?: Title::newMainPage() );
151 
152  return $form;
153  }
154 
155  protected function needsSubmitButton( array $requests ) {
156  // Change/remove forms show are built from a single AuthenticationRequest and do not allow
157  // for redirect flow; they always need a submit button.
158  return true;
159  }
160 
161  public function handleFormSubmit( $data ) {
162  // remove requests do not accept user input
163  $requests = $this->authRequests;
164  if ( static::$loadUserData ) {
165  $requests = AuthenticationRequest::loadRequestsFromSubmission( $this->authRequests, $data );
166  }
167 
168  $response = $this->performAuthenticationStep( $this->authAction, $requests );
169 
170  // we can't handle FAIL or similar as failure here since it might require changing the form
171  return Status::newGood( $response );
172  }
173 
177  protected function showSubpageList( $error = null ) {
178  $out = $this->getOutput();
179 
180  if ( $error ) {
181  $out->addHTML( $error->parse() );
182  }
183 
184  $groupedRequests = [];
185  foreach ( $this->authRequests as $req ) {
186  $info = $req->describeCredentials();
187  $groupedRequests[(string)$info['provider']][] = $req;
188  }
189 
190  $linkRenderer = $this->getLinkRenderer();
191  $out->addHTML( Html::openElement( 'dl' ) );
192  foreach ( $groupedRequests as $group => $members ) {
193  $out->addHTML( Html::element( 'dt', [], $group ) );
194  foreach ( $members as $req ) {
196  $info = $req->describeCredentials();
197  $out->addHTML( Html::rawElement( 'dd', [],
198  $linkRenderer->makeLink(
199  $this->getPageTitle( $req->getUniqueId() ),
200  $info['account']
201  )
202  ) );
203  }
204  }
205  $out->addHTML( Html::closeElement( 'dl' ) );
206  }
207 
208  protected function success() {
209  $session = $this->getRequest()->getSession();
210  $user = $this->getUser();
211  $out = $this->getOutput();
212  $returnUrl = $this->getReturnUrl();
213 
214  // change user token and update the session
215  SessionManager::singleton()->invalidateSessionsForUser( $user );
216  $session->setUser( $user );
217  $session->resetId();
218 
219  if ( $returnUrl ) {
220  $out->redirect( $returnUrl );
221  } else {
222  // messages used: changecredentials-success removecredentials-success
223  $out->wrapWikiMsg( "<div class=\"successbox\">\n$1\n</div>", static::$messagePrefix
224  . '-success' );
225  $out->returnToMain();
226  }
227  }
228 
232  protected function getReturnUrl() {
233  $request = $this->getRequest();
234  $returnTo = $request->getText( 'returnto' );
235  $returnToQuery = $request->getText( 'returntoquery', '' );
236 
237  if ( !$returnTo ) {
238  return null;
239  }
240 
241  $title = Title::newFromText( $returnTo );
242  return $title->getFullUrlForRedirect( $returnToQuery );
243  }
244 
245  protected function getRequestBlacklist() {
246  return $this->getConfig()->get( 'ChangeCredentialsBlacklist' );
247  }
248 }
SpecialChangeCredentials\getAuthFormDescriptor
getAuthFormDescriptor( $requests, $action)
Generates a HTMLForm descriptor array from a set of authentication requests.
Definition: SpecialChangeCredentials.php:108
SpecialPage\getPageTitle
getPageTitle( $subpage=false)
Get a self-referential title object.
Definition: SpecialPage.php:669
AuthManagerSpecialPage\displayForm
displayForm( $status)
Display the form.
Definition: AuthManagerSpecialPage.php:549
SpecialPage\msg
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
Definition: SpecialPage.php:800
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
StatusValue\newFatal
static newFatal( $message,... $parameters)
Factory function for fatal errors.
Definition: StatusValue.php:69
SpecialPage\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: SpecialPage.php:716
MediaWiki\Auth\AuthenticationRequest\describeCredentials
describeCredentials()
Describe the credentials represented by this request.
Definition: AuthenticationRequest.php:229
SpecialChangeCredentials\getAuthForm
getAuthForm(array $requests, $action)
Definition: SpecialChangeCredentials.php:134
SpecialChangeCredentials\getGroupName
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
Definition: SpecialChangeCredentials.php:25
SpecialChangeCredentials\getRequestBlacklist
getRequestBlacklist()
Allows blacklisting certain request types.
Definition: SpecialChangeCredentials.php:245
SpecialChangeCredentials\$allowedActions
static $allowedActions
Definition: SpecialChangeCredentials.php:14
Title\newMainPage
static newMainPage(MessageLocalizer $localizer=null)
Create a new Title for the Main Page.
Definition: Title.php:654
SpecialChangeCredentials\isListed
isListed()
Whether this special page is listed in Special:SpecialPages.
Definition: SpecialChangeCredentials.php:29
AuthManagerSpecialPage
A special page subclass for authentication-related special pages.
Definition: AuthManagerSpecialPage.php:15
SpecialChangeCredentials\__construct
__construct( $name='ChangeCredentials')
Definition: SpecialChangeCredentials.php:21
AuthManagerSpecialPage\$authRequests
AuthenticationRequest[] $authRequests
Definition: AuthManagerSpecialPage.php:33
SpecialChangeCredentials\handleFormSubmit
handleFormSubmit( $data)
Submit handler callback for HTMLForm.
Definition: SpecialChangeCredentials.php:161
SpecialChangeCredentials\$messagePrefix
static $messagePrefix
Definition: SpecialChangeCredentials.php:16
AuthManagerSpecialPage\trySubmit
trySubmit()
Attempts to do an authentication step with the submitted data.
Definition: AuthManagerSpecialPage.php:398
Html\closeElement
static closeElement( $element)
Returns "</$element>".
Definition: Html.php:315
AuthManagerSpecialPage\$subPage
string $subPage
Subpage of the special page.
Definition: AuthManagerSpecialPage.php:36
AuthManagerSpecialPage\performAuthenticationStep
performAuthenticationStep( $action, array $requests)
Definition: AuthManagerSpecialPage.php:343
SpecialChangeCredentials\getDefaultAction
getDefaultAction( $subPage)
Get the default action for this special page, if none is given via URL/POST data.
Definition: SpecialChangeCredentials.php:38
SpecialPage\getConfig
getConfig()
Shortcut to get main config object.
Definition: SpecialPage.php:766
AuthManagerSpecialPage\$authAction
string $authAction
one of the AuthManager::ACTION_* constants.
Definition: AuthManagerSpecialPage.php:30
SpecialChangeCredentials\getPreservedParams
getPreservedParams( $withToken=false)
Returns URL query parameters which can be used to reload the page (or leave and return) while preserv...
Definition: SpecialChangeCredentials.php:42
SpecialChangeCredentials\doesWrites
doesWrites()
Indicates whether this special page may perform database writes.
Definition: SpecialChangeCredentials.php:34
SpecialChangeCredentials\needsSubmitButton
needsSubmitButton(array $requests)
Returns true if the form built from the given AuthenticationRequests needs a submit button.
Definition: SpecialChangeCredentials.php:155
MediaWiki\Auth\AuthenticationResponse
This is a value object to hold authentication response data.
Definition: AuthenticationResponse.php:37
$title
$title
Definition: testCompression.php:38
SpecialPage\setHeaders
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
Definition: SpecialPage.php:551
SpecialPage\getUser
getUser()
Shortcut to get the User executing this instance.
Definition: SpecialPage.php:726
SpecialChangeCredentials\$loadUserData
static $loadUserData
Change action needs user data; remove action does not.
Definition: SpecialChangeCredentials.php:19
SpecialChangeCredentials\success
success()
Definition: SpecialChangeCredentials.php:208
SpecialChangeCredentials\loadAuth
loadAuth( $subPage, $authAction=null, $reset=false)
Load or initialize $authAction, $authRequests and $subPage.
Definition: SpecialChangeCredentials.php:92
StatusValue\newGood
static newGood( $value=null)
Factory function for good results.
Definition: StatusValue.php:81
MediaWiki\Session\SessionManager
This serves as the entry point to the MediaWiki session handling system.
Definition: SessionManager.php:52
SpecialChangeCredentials
Special change to change credentials (such as the password).
Definition: SpecialChangeCredentials.php:13
SpecialChangeCredentials\getReturnUrl
getReturnUrl()
Definition: SpecialChangeCredentials.php:232
MediaWiki\Auth\AuthManager
This serves as the entry point to the authentication system.
Definition: AuthManager.php:88
SpecialPage\getLinkRenderer
getLinkRenderer()
Definition: SpecialPage.php:912
SpecialChangeCredentials\execute
execute( $subPage)
Default execute method Checks user permissions.
Definition: SpecialChangeCredentials.php:52
Html\openElement
static openElement( $element, $attribs=[])
Identical to rawElement(), but has no third parameter and omits the end tag (and the self-closing '/'...
Definition: Html.php:251
Html\rawElement
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:209
AuthManagerSpecialPage\getRequest
getRequest()
Get the WebRequest being used for this instance.
Definition: AuthManagerSpecialPage.php:65
SpecialChangeCredentials\showSubpageList
showSubpageList( $error=null)
Definition: SpecialChangeCredentials.php:177
SpecialPage\$linkRenderer
MediaWiki Linker LinkRenderer null $linkRenderer
Definition: SpecialPage.php:69
Html\element
static element( $element, $attribs=[], $contents='')
Identical to rawElement(), but HTML-escapes $contents (like Xml::element()).
Definition: Html.php:231
SpecialPage\outputHeader
outputHeader( $summaryMessageKey='')
Outputs a summary message on top of special pages Per default the message key is the canonical name o...
Definition: SpecialPage.php:636
MediaWiki\Auth\AuthenticationRequest
This is a value object for authentication requests.
Definition: AuthenticationRequest.php:37