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 );
45 * Main execution point
46 * @param string|null $par
48 function execute( $par ) {
49 $this->getOutput()->disallowUserJs();
51 parent
::execute( $par );
54 protected function checkExecutePermissions( User
$user ) {
55 parent
::checkExecutePermissions( $user );
57 if ( !$this->getRequest()->wasPosted() ) {
58 $this->requireLogin( 'resetpass-no-info' );
63 * Set a message at the top of the Change Password form
65 * @param Message $msg Message to parse and add to the form header
67 public function setChangeMessage( Message
$msg ) {
68 $this->mPreTextMessage
= $msg;
72 * Set a message at the top of the Change Password form
74 * @param string $msg Message label for old/temp password field
76 public function setOldPasswordMessage( $msg ) {
77 $this->mOldPassMsg
= $msg;
80 protected function getFormFields() {
81 $user = $this->getUser();
82 $request = $this->getRequest();
84 $oldpassMsg = $this->mOldPassMsg
;
85 if ( $oldpassMsg === null ) {
86 $oldpassMsg = $user->isLoggedIn() ?
'oldpassword' : 'resetpass-temp-password';
92 'label-message' => 'username',
93 'default' => $request->getVal( 'wpName', $user->getName() ),
97 'label-message' => $oldpassMsg,
99 'NewPassword' => array(
100 'type' => 'password',
101 'label-message' => 'newpassword',
104 'type' => 'password',
105 'label-message' => 'retypenew',
109 if ( !$this->getUser()->isLoggedIn() ) {
110 if ( !LoginForm
::getLoginToken() ) {
111 LoginForm
::setLoginToken();
113 $fields['LoginOnChangeToken'] = array(
115 'label' => 'Change Password Token',
116 'default' => LoginForm
::getLoginToken(),
120 $extraFields = array();
121 Hooks
::run( 'ChangePasswordForm', array( &$extraFields ) );
122 foreach ( $extraFields as $extra ) {
123 list( $name, $label, $type, $default ) = $extra;
124 $fields[$name] = array(
127 'label-message' => $label,
128 'default' => $default,
132 if ( !$user->isLoggedIn() ) {
133 $fields['Remember'] = array(
135 'label' => $this->msg( 'remembermypassword' )
137 ceil( $this->getConfig()->get( 'CookieExpiration' ) / ( 3600 * 24 ) )
139 'default' => $request->getVal( 'wpRemember' ),
146 protected function alterForm( HTMLForm
$form ) {
147 $form->setId( 'mw-resetpass-form' );
148 $form->setTableId( 'mw-resetpass-table' );
149 $form->setWrapperLegendMsg( 'resetpass_header' );
150 $form->setSubmitTextMsg(
151 $this->getUser()->isLoggedIn()
152 ?
'resetpass-submit-loggedin'
155 $form->addButton( array(
156 'name' => 'wpCancel',
157 'value' => $this->msg( 'resetpass-submit-cancel' )->text()
159 $form->setHeaderText( $this->msg( 'resetpass_text' )->parseAsBlock() );
160 if ( $this->mPreTextMessage
instanceof Message
) {
161 $form->addPreText( $this->mPreTextMessage
->parseAsBlock() );
163 $form->addHiddenFields(
164 $this->getRequest()->getValues( 'wpName', 'wpDomain', 'returnto', 'returntoquery' ) );
167 public function onSubmit( array $data ) {
170 $request = $this->getRequest();
172 if ( $request->getCheck( 'wpLoginToken' ) ) {
173 // This comes from Special:Userlogin when logging in with a temporary password
177 if ( !$this->getUser()->isLoggedIn()
178 && $request->getVal( 'wpLoginOnChangeToken' ) !== LoginForm
::getLoginToken()
180 // Potential CSRF (bug 62497)
184 if ( $request->getCheck( 'wpCancel' ) ) {
185 $returnto = $request->getVal( 'returnto' );
186 $titleObj = $returnto !== null ? Title
::newFromText( $returnto ) : null;
187 if ( !$titleObj instanceof Title
) {
188 $titleObj = Title
::newMainPage();
190 $query = $request->getVal( 'returntoquery' );
191 $this->getOutput()->redirect( $titleObj->getFullURL( $query ) );
196 $this->mUserName
= $request->getVal( 'wpName', $this->getUser()->getName() );
197 $this->mDomain
= $wgAuth->getDomain();
199 if ( !$wgAuth->allowPasswordChange() ) {
200 throw new ErrorPageError( 'changepassword', 'resetpass_forbidden' );
203 $status = $this->attemptReset( $data['Password'], $data['NewPassword'], $data['Retype'] );
208 public function onSuccess() {
209 if ( $this->getUser()->isLoggedIn() ) {
210 $this->getOutput()->wrapWikiMsg(
211 "<div class=\"successbox\">\n$1\n</div>",
212 'changepassword-success'
214 $this->getOutput()->returnToMain();
216 $request = $this->getRequest();
217 LoginForm
::setLoginToken();
218 $token = LoginForm
::getLoginToken();
220 'action' => 'submitlogin',
221 'wpName' => $this->mUserName
,
222 'wpDomain' => $this->mDomain
,
223 'wpLoginToken' => $token,
224 'wpPassword' => $request->getVal( 'wpNewPassword' ),
225 ) +
$request->getValues( 'wpRemember', 'returnto', 'returntoquery' );
226 $login = new LoginForm( new DerivativeRequest( $request, $data, true ) );
227 $login->setContext( $this->getContext() );
228 $login->execute( null );
233 * Checks the new password if it meets the requirements for passwords and set
234 * it as a current password, otherwise set the passed Status object to fatal
235 * and doesn't change anything
237 * @param string $oldpass The current (temporary) password.
238 * @param string $newpass The password to set.
239 * @param string $retype The string of the retype password field to check with newpass
242 protected function attemptReset( $oldpass, $newpass, $retype ) {
243 $isSelf = ( $this->mUserName
=== $this->getUser()->getName() );
245 $user = $this->getUser();
247 $user = User
::newFromName( $this->mUserName
);
250 if ( !$user ||
$user->isAnon() ) {
251 return Status
::newFatal( $this->msg( 'nosuchusershort', $this->mUserName
) );
254 if ( $newpass !== $retype ) {
255 Hooks
::run( 'PrefsPasswordAudit', array( $user, $newpass, 'badretype' ) );
256 return Status
::newFatal( $this->msg( 'badretype' ) );
259 $throttleCount = LoginForm
::incLoginThrottle( $this->mUserName
);
260 if ( $throttleCount === true ) {
261 $lang = $this->getLanguage();
262 $throttleInfo = $this->getConfig()->get( 'PasswordAttemptThrottle' );
263 return Status
::newFatal( $this->msg( 'changepassword-throttled' )
264 ->params( $lang->formatDuration( $throttleInfo['seconds'] ) )
268 // @todo Make these separate messages, since the message is written for both cases
269 if ( !$user->checkTemporaryPassword( $oldpass ) && !$user->checkPassword( $oldpass ) ) {
270 Hooks
::run( 'PrefsPasswordAudit', array( $user, $newpass, 'wrongpassword' ) );
271 return Status
::newFatal( $this->msg( 'resetpass-wrong-oldpass' ) );
274 // User is resetting their password to their old password
275 if ( $oldpass === $newpass ) {
276 return Status
::newFatal( $this->msg( 'resetpass-recycled' ) );
279 // Do AbortChangePassword after checking mOldpass, so we don't leak information
280 // by possibly aborting a new password before verifying the old password.
281 $abortMsg = 'resetpass-abort-generic';
282 if ( !Hooks
::run( 'AbortChangePassword', array( $user, $oldpass, $newpass, &$abortMsg ) ) ) {
283 Hooks
::run( 'PrefsPasswordAudit', array( $user, $newpass, 'abortreset' ) );
284 return Status
::newFatal( $this->msg( $abortMsg ) );
287 // Please reset throttle for successful logins, thanks!
288 if ( $throttleCount ) {
289 LoginForm
::clearLoginThrottle( $this->mUserName
);
293 $user->setPassword( $newpass );
294 Hooks
::run( 'PrefsPasswordAudit', array( $user, $newpass, 'success' ) );
295 } catch ( PasswordError
$e ) {
296 Hooks
::run( 'PrefsPasswordAudit', array( $user, $newpass, 'error' ) );
297 return Status
::newFatal( new RawMessage( $e->getMessage() ) );
301 // This is needed to keep the user connected since
302 // changing the password also modifies the user's token.
303 $remember = $this->getRequest()->getCookie( 'Token' ) !== null;
304 $user->setCookies( null, null, $remember );
306 $user->saveSettings();
307 $this->resetPasswordExpiration( $user );
308 return Status
::newGood();
311 public function requiresUnblock() {
315 protected function getGroupName() {
320 * For resetting user password expiration, until AuthManager comes along
323 private function resetPasswordExpiration( User
$user ) {
324 global $wgPasswordExpirationDays;
326 if ( $wgPasswordExpirationDays ) {
327 $newExpire = wfTimestamp(
329 time() +
( $wgPasswordExpirationDays * 24 * 3600 )
332 // Give extensions a chance to force an expiration
333 Hooks
::run( 'ResetPasswordExpiration', array( $this, &$newExpire ) );
334 $dbw = wfGetDB( DB_MASTER
);
337 array( 'user_password_expires' => $dbw->timestampOrNull( $newExpire ) ),
338 array( 'user_id' => $user->getID() ),
343 protected function getDisplayFormat() {