3 use MediaWiki\Auth\AuthenticationRequest
;
4 use MediaWiki\Auth\AuthenticationResponse
;
5 use MediaWiki\Auth\AuthManager
;
6 use MediaWiki\Session\SessionManager
;
9 * Special change to change credentials (such as the password).
11 * Also does most of the work for SpecialRemoveCredentials.
13 class SpecialChangeCredentials
extends AuthManagerSpecialPage
{
14 protected static $allowedActions = [ AuthManager
::ACTION_CHANGE
];
16 protected static $messagePrefix = 'changecredentials';
18 /** Change action needs user data; remove action does not */
19 protected static $loadUserData = true;
21 public function __construct( $name = 'ChangeCredentials' ) {
22 parent
::__construct( $name, 'editmyprivateinfo' );
25 protected function getGroupName() {
29 public function isListed() {
30 $this->loadAuth( '' );
31 return (bool)$this->authRequests
;
34 public function doesWrites() {
38 protected function getDefaultAction( $subPage ) {
39 return AuthManager
::ACTION_CHANGE
;
42 protected function getPreservedParams( $withToken = false ) {
43 $request = $this->getRequest();
44 $params = parent
::getPreservedParams( $withToken );
46 'returnto' => $request->getVal( 'returnto' ),
47 'returntoquery' => $request->getVal( 'returntoquery' ),
52 public function onAuthChangeFormFields(
53 array $requests, array $fieldInfo, array &$formDescriptor, $action
55 // This method is never called for remove actions.
58 Hooks
::run( 'ChangePasswordForm', [ &$extraFields ], '1.27' );
59 foreach ( $extraFields as $extra ) {
60 list( $name, $label, $type, $default ) = $extra;
61 $formDescriptor[$name] = [
64 'label-message' => $label,
65 'default' => $default,
70 return parent
::onAuthChangeFormFields( $requests, $fieldInfo, $formDescriptor, $action );
73 public function execute( $subPage ) {
75 $this->outputHeader();
77 $this->loadAuth( $subPage );
80 $this->showSubpageList();
84 if ( !$this->authRequests
) {
85 // messages used: changecredentials-invalidsubpage, removecredentials-invalidsubpage
86 $this->showSubpageList( $this->msg( static::$messagePrefix . '-invalidsubpage', $subPage ) );
90 $this->getOutput()->addBacklinkSubtitle( $this->getPageTitle() );
92 $status = $this->trySubmit();
94 if ( $status === false ||
!$status->isOK() ) {
95 $this->displayForm( $status );
99 $response = $status->getValue();
101 switch ( $response->status
) {
102 case AuthenticationResponse
::PASS
:
105 case AuthenticationResponse
::FAIL
:
106 $this->displayForm( Status
::newFatal( $response->message
) );
109 throw new LogicException( 'invalid AuthenticationResponse' );
113 protected function loadAuth( $subPage, $authAction = null, $reset = false ) {
114 parent
::loadAuth( $subPage, $authAction );
116 $this->authRequests
= array_filter( $this->authRequests
, function ( $req ) use ( $subPage ) {
117 return $req->getUniqueId() === $subPage;
119 if ( count( $this->authRequests
) > 1 ) {
120 throw new LogicException( 'Multiple AuthenticationRequest objects with same ID!' );
125 protected function getAuthFormDescriptor( $requests, $action ) {
126 if ( !static::$loadUserData ) {
129 return parent
::getAuthFormDescriptor( $requests, $action );
133 protected function getAuthForm( array $requests, $action ) {
134 $form = parent
::getAuthForm( $requests, $action );
135 $req = reset( $requests );
136 $info = $req->describeCredentials();
139 Html
::openElement( 'dl' )
140 . Html
::element( 'dt', [], wfMessage( 'credentialsform-provider' )->text() )
141 . Html
::element( 'dd', [], $info['provider'] )
142 . Html
::element( 'dt', [], wfMessage( 'credentialsform-account' )->text() )
143 . Html
::element( 'dd', [], $info['account'] )
144 . Html
::closeElement( 'dl' )
147 // messages used: changecredentials-submit removecredentials-submit
148 $form->setSubmitTextMsg( static::$messagePrefix . '-submit' );
149 $form->showCancel()->setCancelTarget( $this->getReturnUrl() ?
: Title
::newMainPage() );
154 protected function needsSubmitButton( array $requests ) {
155 // Change/remove forms show are built from a single AuthenticationRequest and do not allow
156 // for redirect flow; they always need a submit button.
160 public function handleFormSubmit( $data ) {
161 // remove requests do not accept user input
162 $requests = $this->authRequests
;
163 if ( static::$loadUserData ) {
164 $requests = AuthenticationRequest
::loadRequestsFromSubmission( $this->authRequests
, $data );
167 $response = $this->performAuthenticationStep( $this->authAction
, $requests );
169 // we can't handle FAIL or similar as failure here since it might require changing the form
170 return Status
::newGood( $response );
174 * @param Message|null $error
176 protected function showSubpageList( $error = null ) {
177 $out = $this->getOutput();
180 $out->addHTML( $error->parse() );
183 $groupedRequests = [];
184 foreach ( $this->authRequests
as $req ) {
185 $info = $req->describeCredentials();
186 $groupedRequests[(string)$info['provider']][] = $req;
189 $linkRenderer = $this->getLinkRenderer();
190 $out->addHTML( Html
::openElement( 'dl' ) );
191 foreach ( $groupedRequests as $group => $members ) {
192 $out->addHTML( Html
::element( 'dt', [], $group ) );
193 foreach ( $members as $req ) {
194 /** @var AuthenticationRequest $req */
195 $info = $req->describeCredentials();
196 $out->addHTML( Html
::rawElement( 'dd', [],
197 $linkRenderer->makeLink(
198 $this->getPageTitle( $req->getUniqueId() ),
204 $out->addHTML( Html
::closeElement( 'dl' ) );
207 protected function success() {
208 $session = $this->getRequest()->getSession();
209 $user = $this->getUser();
210 $out = $this->getOutput();
211 $returnUrl = $this->getReturnUrl();
213 // change user token and update the session
214 SessionManager
::singleton()->invalidateSessionsForUser( $user );
215 $session->setUser( $user );
219 $out->redirect( $returnUrl );
221 // messages used: changecredentials-success removecredentials-success
222 $out->wrapWikiMsg( "<div class=\"successbox\">\n$1\n</div>", static::$messagePrefix
224 $out->returnToMain();
229 * @return string|null
231 protected function getReturnUrl() {
232 $request = $this->getRequest();
233 $returnTo = $request->getText( 'returnto' );
234 $returnToQuery = $request->getText( 'returntoquery', '' );
240 $title = Title
::newFromText( $returnTo );
241 return $title->getFullURL( $returnToQuery );
244 protected function getRequestBlacklist() {
245 return $this->getConfig()->get( 'ChangeCredentialsBlacklist' );