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,
103 'NewPassword' => array(
104 'type' => 'password',
105 'label-message' => 'newpassword',
108 'type' => 'password',
109 'label-message' => 'retypenew',
113 if ( !$this->getUser()->isLoggedIn() ) {
114 $fields['LoginOnChangeToken'] = array(
116 'label' => 'Change Password Token',
117 'default' => LoginForm
::getLoginToken()->toString(),
121 $extraFields = array();
122 Hooks
::run( 'ChangePasswordForm', array( &$extraFields ) );
123 foreach ( $extraFields as $extra ) {
124 list( $name, $label, $type, $default ) = $extra;
125 $fields[$name] = array(
128 'label-message' => $label,
129 'default' => $default,
133 if ( !$user->isLoggedIn() ) {
134 $fields['Remember'] = array(
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'
156 $form->addButton( array(
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', array( $user, $newpass, 'badretype' ) );
257 return Status
::newFatal( $this->msg( 'badretype' ) );
260 $throttleCount = LoginForm
::incLoginThrottle( $this->mUserName
);
261 if ( $throttleCount === true ) {
262 $lang = $this->getLanguage();
263 $throttleInfo = $this->getConfig()->get( 'PasswordAttemptThrottle' );
264 return Status
::newFatal( $this->msg( 'changepassword-throttled' )
265 ->params( $lang->formatDuration( $throttleInfo['seconds'] ) )
269 // @todo Make these separate messages, since the message is written for both cases
270 if ( !$user->checkTemporaryPassword( $oldpass ) && !$user->checkPassword( $oldpass ) ) {
271 Hooks
::run( 'PrefsPasswordAudit', array( $user, $newpass, 'wrongpassword' ) );
272 return Status
::newFatal( $this->msg( 'resetpass-wrong-oldpass' ) );
275 // User is resetting their password to their old password
276 if ( $oldpass === $newpass ) {
277 return Status
::newFatal( $this->msg( 'resetpass-recycled' ) );
280 // Do AbortChangePassword after checking mOldpass, so we don't leak information
281 // by possibly aborting a new password before verifying the old password.
282 $abortMsg = 'resetpass-abort-generic';
283 if ( !Hooks
::run( 'AbortChangePassword', array( $user, $oldpass, $newpass, &$abortMsg ) ) ) {
284 Hooks
::run( 'PrefsPasswordAudit', array( $user, $newpass, 'abortreset' ) );
285 return Status
::newFatal( $this->msg( $abortMsg ) );
288 // Please reset throttle for successful logins, thanks!
289 if ( $throttleCount ) {
290 LoginForm
::clearLoginThrottle( $this->mUserName
);
294 $user->setPassword( $newpass );
295 Hooks
::run( 'PrefsPasswordAudit', array( $user, $newpass, 'success' ) );
296 } catch ( PasswordError
$e ) {
297 Hooks
::run( 'PrefsPasswordAudit', array( $user, $newpass, 'error' ) );
298 return Status
::newFatal( new RawMessage( $e->getMessage() ) );
302 // This is needed to keep the user connected since
303 // changing the password also modifies the user's token.
304 $remember = $this->getRequest()->getCookie( 'Token' ) !== null;
305 $user->setCookies( null, null, $remember );
307 $user->saveSettings();
308 $this->resetPasswordExpiration( $user );
309 return Status
::newGood();
312 public function requiresUnblock() {
316 protected function getGroupName() {
321 * For resetting user password expiration, until AuthManager comes along
324 private function resetPasswordExpiration( User
$user ) {
325 global $wgPasswordExpirationDays;
327 if ( $wgPasswordExpirationDays ) {
328 $newExpire = wfTimestamp(
330 time() +
( $wgPasswordExpirationDays * 24 * 3600 )
333 // Give extensions a chance to force an expiration
334 Hooks
::run( 'ResetPasswordExpiration', array( $this, &$newExpire ) );
335 $dbw = wfGetDB( DB_MASTER
);
338 array( 'user_password_expires' => $dbw->timestampOrNull( $newExpire ) ),
339 array( 'user_id' => $user->getID() ),
344 protected function getDisplayFormat() {