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
47 function execute( $par ) {
48 $this->getOutput()->disallowUserJs();
50 parent
::execute( $par );
53 protected function checkExecutePermissions( User
$user ) {
54 parent
::checkExecutePermissions( $user );
56 if ( !$this->getRequest()->wasPosted() ) {
57 $this->requireLogin( 'resetpass-no-info' );
62 * Set a message at the top of the Change Password form
64 * @param Message $msg Message to parse and add to the form header
66 public function setChangeMessage( Message
$msg ) {
67 $this->mPreTextMessage
= $msg;
71 * Set a message at the top of the Change Password form
73 * @param string $msg Message label for old/temp password field
75 public function setOldPasswordMessage( $msg ) {
76 $this->mOldPassMsg
= $msg;
79 protected function getFormFields() {
80 $user = $this->getUser();
81 $request = $this->getRequest();
83 $oldpassMsg = $this->mOldPassMsg
;
84 if ( $oldpassMsg === null ) {
85 $oldpassMsg = $user->isLoggedIn() ?
'oldpassword' : 'resetpass-temp-password';
91 'label-message' => 'username',
92 'default' => $request->getVal( 'wpName', $user->getName() ),
96 'label-message' => $oldpassMsg,
98 'NewPassword' => array(
100 'label-message' => 'newpassword',
103 'type' => 'password',
104 'label-message' => 'retypenew',
108 if ( !$this->getUser()->isLoggedIn() ) {
109 if ( !LoginForm
::getLoginToken() ) {
110 LoginForm
::setLoginToken();
112 $fields['LoginOnChangeToken'] = array(
114 'label' => 'Change Password Token',
115 'default' => LoginForm
::getLoginToken(),
119 $extraFields = array();
120 wfRunHooks( 'ChangePasswordForm', array( &$extraFields ) );
121 foreach ( $extraFields as $extra ) {
122 list( $name, $label, $type, $default ) = $extra;
123 $fields[$name] = array(
126 'label-message' => $label,
127 'default' => $default,
131 if ( !$user->isLoggedIn() ) {
132 $fields['Remember'] = array(
134 'label' => $this->msg( 'remembermypassword' )
136 ceil( $this->getConfig()->get( 'CookieExpiration' ) / ( 3600 * 24 ) )
138 'default' => $request->getVal( 'wpRemember' ),
145 protected function alterForm( HTMLForm
$form ) {
146 $form->setId( 'mw-resetpass-form' );
147 $form->setTableId( 'mw-resetpass-table' );
148 $form->setWrapperLegendMsg( 'resetpass_header' );
149 $form->setSubmitTextMsg(
150 $this->getUser()->isLoggedIn()
151 ?
'resetpass-submit-loggedin'
154 $form->addButton( 'wpCancel', $this->msg( 'resetpass-submit-cancel' )->text() );
155 $form->setHeaderText( $this->msg( 'resetpass_text' )->parseAsBlock() );
156 if ( $this->mPreTextMessage
instanceof Message
) {
157 $form->addPreText( $this->mPreTextMessage
->parseAsBlock() );
159 $form->addHiddenFields(
160 $this->getRequest()->getValues( 'wpName', 'wpDomain', 'returnto', 'returntoquery' ) );
163 public function onSubmit( array $data ) {
166 $request = $this->getRequest();
168 if ( $request->getCheck( 'wpLoginToken' ) ) {
169 // This comes from Special:Userlogin when logging in with a temporary password
173 if ( !$this->getUser()->isLoggedIn()
174 && $request->getVal( 'wpLoginOnChangeToken' ) !== LoginForm
::getLoginToken()
176 // Potential CSRF (bug 62497)
180 if ( $request->getCheck( 'wpCancel' ) ) {
181 $titleObj = Title
::newFromText( $request->getVal( 'returnto' ) );
182 if ( !$titleObj instanceof Title
) {
183 $titleObj = Title
::newMainPage();
185 $query = $request->getVal( 'returntoquery' );
186 $this->getOutput()->redirect( $titleObj->getFullURL( $query ) );
192 $this->mUserName
= $request->getVal( 'wpName', $this->getUser()->getName() );
193 $this->mDomain
= $wgAuth->getDomain();
195 if ( !$wgAuth->allowPasswordChange() ) {
196 throw new ErrorPageError( 'changepassword', 'resetpass_forbidden' );
199 $this->attemptReset( $data['Password'], $data['NewPassword'], $data['Retype'] );
202 } catch ( PasswordError
$e ) {
203 return $e->getMessage();
207 public function onSuccess() {
208 if ( $this->getUser()->isLoggedIn() ) {
209 $this->getOutput()->wrapWikiMsg(
210 "<div class=\"successbox\">\n$1\n</div>",
211 'changepassword-success'
213 $this->getOutput()->returnToMain();
215 $request = $this->getRequest();
216 LoginForm
::setLoginToken();
217 $token = LoginForm
::getLoginToken();
219 'action' => 'submitlogin',
220 'wpName' => $this->mUserName
,
221 'wpDomain' => $this->mDomain
,
222 'wpLoginToken' => $token,
223 'wpPassword' => $request->getVal( 'wpNewPassword' ),
224 ) +
$request->getValues( 'wpRemember', 'returnto', 'returntoquery' );
225 $login = new LoginForm( new DerivativeRequest( $request, $data, true ) );
226 $login->setContext( $this->getContext() );
227 $login->execute( null );
232 * @throws PasswordError When cannot set the new password because requirements not met.
234 protected function attemptReset( $oldpass, $newpass, $retype ) {
235 $isSelf = ( $this->mUserName
=== $this->getUser()->getName() );
237 $user = $this->getUser();
239 $user = User
::newFromName( $this->mUserName
);
242 if ( !$user ||
$user->isAnon() ) {
243 throw new PasswordError( $this->msg( 'nosuchusershort', $this->mUserName
)->text() );
246 if ( $newpass !== $retype ) {
247 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'badretype' ) );
248 throw new PasswordError( $this->msg( 'badretype' )->text() );
251 $throttleCount = LoginForm
::incLoginThrottle( $this->mUserName
);
252 if ( $throttleCount === true ) {
253 $lang = $this->getLanguage();
254 $throttleInfo = $this->getConfig()->get( 'PasswordAttemptThrottle' );
255 throw new PasswordError( $this->msg( 'changepassword-throttled' )
256 ->params( $lang->formatDuration( $throttleInfo['seconds'] ) )
261 // @todo Make these separate messages, since the message is written for both cases
262 if ( !$user->checkTemporaryPassword( $oldpass ) && !$user->checkPassword( $oldpass ) ) {
263 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'wrongpassword' ) );
264 throw new PasswordError( $this->msg( 'resetpass-wrong-oldpass' )->text() );
267 // User is resetting their password to their old password
268 if ( $oldpass === $newpass ) {
269 throw new PasswordError( $this->msg( 'resetpass-recycled' )->text() );
272 // Do AbortChangePassword after checking mOldpass, so we don't leak information
273 // by possibly aborting a new password before verifying the old password.
274 $abortMsg = 'resetpass-abort-generic';
275 if ( !wfRunHooks( 'AbortChangePassword', array( $user, $oldpass, $newpass, &$abortMsg ) ) ) {
276 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'abortreset' ) );
277 throw new PasswordError( $this->msg( $abortMsg )->text() );
280 // Please reset throttle for successful logins, thanks!
281 if ( $throttleCount ) {
282 LoginForm
::clearLoginThrottle( $this->mUserName
);
286 $user->setPassword( $newpass );
287 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'success' ) );
288 } catch ( PasswordError
$e ) {
289 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'error' ) );
290 throw new PasswordError( $e->getMessage() );
294 // This is needed to keep the user connected since
295 // changing the password also modifies the user's token.
296 $remember = $this->getRequest()->getCookie( 'Token' ) !== null;
297 $user->setCookies( null, null, $remember );
299 $user->resetPasswordExpiration();
300 $user->saveSettings();
303 public function requiresUnblock() {
307 protected function getGroupName() {