2d0d020cacd74766844d65922de87908f6e7f174
3 * Implements Special:ChangePassword
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup SpecialPage
25 * Let users recover their password.
27 * @ingroup SpecialPage
29 class SpecialChangePassword
extends FormSpecialPage
{
33 // Optional Wikitext Message to show above the password change form
34 protected $mPreTextMessage = null;
36 // label for old password input
37 protected $mOldPassMsg = null;
39 public function __construct() {
40 parent
::__construct( 'ChangePassword', 'editmyprivateinfo' );
41 $this->listed( false );
44 public function doesWrites() {
49 * Main execution point
50 * @param string|null $par
52 function execute( $par ) {
53 $this->getOutput()->disallowUserJs();
55 parent
::execute( $par );
58 protected function checkExecutePermissions( User
$user ) {
59 parent
::checkExecutePermissions( $user );
61 if ( !$this->getRequest()->wasPosted() ) {
62 $this->requireLogin( 'resetpass-no-info' );
67 * Set a message at the top of the Change Password form
69 * @param Message $msg Message to parse and add to the form header
71 public function setChangeMessage( Message
$msg ) {
72 $this->mPreTextMessage
= $msg;
76 * Set a message at the top of the Change Password form
78 * @param string $msg Message label for old/temp password field
80 public function setOldPasswordMessage( $msg ) {
81 $this->mOldPassMsg
= $msg;
84 protected function getFormFields() {
85 $user = $this->getUser();
86 $request = $this->getRequest();
88 $oldpassMsg = $this->mOldPassMsg
;
89 if ( $oldpassMsg === null ) {
90 $oldpassMsg = $user->isLoggedIn() ?
'oldpassword' : 'resetpass-temp-password';
96 'label-message' => 'username',
97 'default' => $request->getVal( 'wpName', $user->getName() ),
100 'type' => 'password',
101 'label-message' => $oldpassMsg,
104 'type' => 'password',
105 'label-message' => 'newpassword',
108 'type' => 'password',
109 'label-message' => 'retypenew',
113 if ( !$this->getUser()->isLoggedIn() ) {
114 $fields['LoginOnChangeToken'] = [
116 'label' => 'Change Password Token',
117 'default' => LoginForm
::getLoginToken()->toString(),
122 Hooks
::run( 'ChangePasswordForm', [ &$extraFields ] );
123 foreach ( $extraFields as $extra ) {
124 list( $name, $label, $type, $default ) = $extra;
128 'label-message' => $label,
129 'default' => $default,
133 if ( !$user->isLoggedIn() ) {
134 $fields['Remember'] = [
136 'label' => $this->msg( 'remembermypassword' )
138 ceil( $this->getConfig()->get( 'CookieExpiration' ) / ( 3600 * 24 ) )
140 'default' => $request->getVal( 'wpRemember' ),
147 protected function alterForm( HTMLForm
$form ) {
148 $form->setId( 'mw-resetpass-form' );
149 $form->setTableId( 'mw-resetpass-table' );
150 $form->setWrapperLegendMsg( 'resetpass_header' );
151 $form->setSubmitTextMsg(
152 $this->getUser()->isLoggedIn()
153 ?
'resetpass-submit-loggedin'
157 'name' => 'wpCancel',
158 'value' => $this->msg( 'resetpass-submit-cancel' )->text()
160 $form->setHeaderText( $this->msg( 'resetpass_text' )->parseAsBlock() );
161 if ( $this->mPreTextMessage
instanceof Message
) {
162 $form->addPreText( $this->mPreTextMessage
->parseAsBlock() );
164 $form->addHiddenFields(
165 $this->getRequest()->getValues( 'wpName', 'wpDomain', 'returnto', 'returntoquery' ) );
168 public function onSubmit( array $data ) {
171 $request = $this->getRequest();
173 if ( $request->getCheck( 'wpLoginToken' ) ) {
174 // This comes from Special:Userlogin when logging in with a temporary password
178 if ( !$this->getUser()->isLoggedIn()
179 && !LoginForm
::getLoginToken()->match( $request->getVal( 'wpLoginOnChangeToken' ) )
181 // Potential CSRF (bug 62497)
185 if ( $request->getCheck( 'wpCancel' ) ) {
186 $returnto = $request->getVal( 'returnto' );
187 $titleObj = $returnto !== null ? Title
::newFromText( $returnto ) : null;
188 if ( !$titleObj instanceof Title
) {
189 $titleObj = Title
::newMainPage();
191 $query = $request->getVal( 'returntoquery' );
192 $this->getOutput()->redirect( $titleObj->getFullURL( $query ) );
197 $this->mUserName
= $request->getVal( 'wpName', $this->getUser()->getName() );
198 $this->mDomain
= $wgAuth->getDomain();
200 if ( !$wgAuth->allowPasswordChange() ) {
201 throw new ErrorPageError( 'changepassword', 'resetpass_forbidden' );
204 $status = $this->attemptReset( $data['Password'], $data['NewPassword'], $data['Retype'] );
209 public function onSuccess() {
210 if ( $this->getUser()->isLoggedIn() ) {
211 $this->getOutput()->wrapWikiMsg(
212 "<div class=\"successbox\">\n$1\n</div>",
213 'changepassword-success'
215 $this->getOutput()->returnToMain();
217 $request = $this->getRequest();
218 LoginForm
::clearLoginToken();
219 $token = LoginForm
::getLoginToken()->toString();
221 'action' => 'submitlogin',
222 'wpName' => $this->mUserName
,
223 'wpDomain' => $this->mDomain
,
224 'wpLoginToken' => $token,
225 'wpPassword' => $request->getVal( 'wpNewPassword' ),
226 ] +
$request->getValues( 'wpRemember', 'returnto', 'returntoquery' );
227 $login = new LoginForm( new DerivativeRequest( $request, $data, true ) );
228 $login->setContext( $this->getContext() );
229 $login->execute( null );
234 * Checks the new password if it meets the requirements for passwords and set
235 * it as a current password, otherwise set the passed Status object to fatal
236 * and doesn't change anything
238 * @param string $oldpass The current (temporary) password.
239 * @param string $newpass The password to set.
240 * @param string $retype The string of the retype password field to check with newpass
243 protected function attemptReset( $oldpass, $newpass, $retype ) {
244 $isSelf = ( $this->mUserName
=== $this->getUser()->getName() );
246 $user = $this->getUser();
248 $user = User
::newFromName( $this->mUserName
);
251 if ( !$user ||
$user->isAnon() ) {
252 return Status
::newFatal( $this->msg( 'nosuchusershort', $this->mUserName
) );
255 if ( $newpass !== $retype ) {
256 Hooks
::run( 'PrefsPasswordAudit', [ $user, $newpass, 'badretype' ] );
257 return Status
::newFatal( $this->msg( 'badretype' ) );
260 $throttleInfo = LoginForm
::incrementLoginThrottle( $this->mUserName
);
261 if ( $throttleInfo ) {
262 return Status
::newFatal( $this->msg( 'changepassword-throttled' )
263 ->durationParams( $throttleInfo['wait'] )
267 // @todo Make these separate messages, since the message is written for both cases
268 if ( !$user->checkTemporaryPassword( $oldpass ) && !$user->checkPassword( $oldpass ) ) {
269 Hooks
::run( 'PrefsPasswordAudit', [ $user, $newpass, 'wrongpassword' ] );
270 return Status
::newFatal( $this->msg( 'resetpass-wrong-oldpass' ) );
273 // User is resetting their password to their old password
274 if ( $oldpass === $newpass ) {
275 return Status
::newFatal( $this->msg( 'resetpass-recycled' ) );
278 // Do AbortChangePassword after checking mOldpass, so we don't leak information
279 // by possibly aborting a new password before verifying the old password.
280 $abortMsg = 'resetpass-abort-generic';
281 if ( !Hooks
::run( 'AbortChangePassword', [ $user, $oldpass, $newpass, &$abortMsg ] ) ) {
282 Hooks
::run( 'PrefsPasswordAudit', [ $user, $newpass, 'abortreset' ] );
283 return Status
::newFatal( $this->msg( $abortMsg ) );
286 // Please reset throttle for successful logins, thanks!
287 LoginForm
::clearLoginThrottle( $this->mUserName
);
290 $user->setPassword( $newpass );
291 Hooks
::run( 'PrefsPasswordAudit', [ $user, $newpass, 'success' ] );
292 } catch ( PasswordError
$e ) {
293 Hooks
::run( 'PrefsPasswordAudit', [ $user, $newpass, 'error' ] );
294 return Status
::newFatal( new RawMessage( $e->getMessage() ) );
298 // This is needed to keep the user connected since
299 // changing the password also modifies the user's token.
300 $remember = $this->getRequest()->getCookie( 'Token' ) !== null;
301 $user->setCookies( null, null, $remember );
303 $user->saveSettings();
304 $this->resetPasswordExpiration( $user );
305 return Status
::newGood();
308 public function requiresUnblock() {
312 protected function getGroupName() {
317 * For resetting user password expiration, until AuthManager comes along
320 private function resetPasswordExpiration( User
$user ) {
321 global $wgPasswordExpirationDays;
323 if ( $wgPasswordExpirationDays ) {
324 $newExpire = wfTimestamp(
326 time() +
( $wgPasswordExpirationDays * 24 * 3600 )
329 // Give extensions a chance to force an expiration
330 Hooks
::run( 'ResetPasswordExpiration', [ $this, &$newExpire ] );
331 $dbw = wfGetDB( DB_MASTER
);
334 [ 'user_password_expires' => $dbw->timestampOrNull( $newExpire ) ],
335 [ 'user_id' => $user->getID() ],
340 protected function getDisplayFormat() {