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
{
31 protected $mUserName, $mDomain;
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 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 global $wgCookieExpiration;
82 $user = $this->getUser();
83 $request = $this->getRequest();
85 $oldpassMsg = $this->mOldPassMsg
;
86 if ( !isset( $oldpassMsg ) ) {
87 $oldpassMsg = $user->isLoggedIn() ?
'oldpassword' : 'resetpass-temp-password';
93 'label-message' => 'username',
94 'default' => $request->getVal( 'wpName', $user->getName() ),
98 'label-message' => $oldpassMsg,
100 'NewPassword' => array(
101 'type' => 'password',
102 'label-message' => 'newpassword',
105 'type' => 'password',
106 'label-message' => 'retypenew',
110 $extraFields = array();
111 wfRunHooks( 'ChangePasswordForm', array( &$extraFields ) );
112 foreach ( $extraFields as $extra ) {
113 list( $name, $label, $type, $default ) = $extra;
114 $fields[$name] = array(
117 'label-message' => $label,
118 'default' => $default,
122 if ( !$user->isLoggedIn() ) {
123 $fields['Remember'] = array(
125 'label' => $this->msg( 'remembermypassword' )
126 ->numParams( ceil( $wgCookieExpiration / ( 3600 * 24 ) ) )
128 'default' => $request->getVal( 'wpRemember' ),
135 protected function alterForm( HTMLForm
$form ) {
136 $form->setId( 'mw-resetpass-form' );
137 $form->setTableId( 'mw-resetpass-table' );
138 $form->setWrapperLegendMsg( 'resetpass_header' );
139 $form->setSubmitTextMsg(
140 $this->getUser()->isLoggedIn()
141 ?
'resetpass-submit-loggedin'
144 $form->addButton( 'wpCancel', $this->msg( 'resetpass-submit-cancel' )->text() );
145 $form->setHeaderText( $this->msg( 'resetpass_text' )->parseAsBlock() );
146 if ( $this->mPreTextMessage
instanceof Message
) {
147 $form->addPreText( $this->mPreTextMessage
->parseAsBlock() );
149 $form->addHiddenFields(
150 $this->getRequest()->getValues( 'wpName', 'wpDomain', 'returnto', 'returntoquery' ) );
153 public function onSubmit( array $data ) {
156 $request = $this->getRequest();
158 if ( $request->getCheck( 'wpLoginToken' ) ) {
159 // This comes from Special:Userlogin when logging in with a temporary password
163 if ( $request->getCheck( 'wpCancel' ) ) {
164 $titleObj = Title
::newFromText( $request->getVal( 'returnto' ) );
165 if ( !$titleObj instanceof Title
) {
166 $titleObj = Title
::newMainPage();
168 $query = $request->getVal( 'returntoquery' );
169 $this->getOutput()->redirect( $titleObj->getFullURL( $query ) );
175 $this->mUserName
= $request->getVal( 'wpName', $this->getUser()->getName() );
176 $this->mDomain
= $wgAuth->getDomain();
178 if ( !$wgAuth->allowPasswordChange() ) {
179 throw new ErrorPageError( 'changepassword', 'resetpass_forbidden' );
182 $this->attemptReset( $data['Password'], $data['NewPassword'], $data['Retype'] );
185 } catch ( PasswordError
$e ) {
186 return $e->getMessage();
190 public function onSuccess() {
191 if ( $this->getUser()->isLoggedIn() ) {
192 $this->getOutput()->wrapWikiMsg(
193 "<div class=\"successbox\">\n$1\n</div>",
194 'changepassword-success'
196 $this->getOutput()->returnToMain();
198 $request = $this->getRequest();
199 LoginForm
::setLoginToken();
200 $token = LoginForm
::getLoginToken();
202 'action' => 'submitlogin',
203 'wpName' => $this->mUserName
,
204 'wpDomain' => $this->mDomain
,
205 'wpLoginToken' => $token,
206 'wpPassword' => $request->getVal( 'wpNewPassword' ),
207 ) +
$request->getValues( 'wpRemember', 'returnto', 'returntoquery' );
208 $login = new LoginForm( new DerivativeRequest( $request, $data, true ) );
209 $login->setContext( $this->getContext() );
210 $login->execute( null );
215 * @throws PasswordError when cannot set the new password because requirements not met.
217 protected function attemptReset( $oldpass, $newpass, $retype ) {
218 global $wgPasswordAttemptThrottle;
220 $isSelf = ( $this->mUserName
=== $this->getUser()->getName() );
222 $user = $this->getUser();
224 $user = User
::newFromName( $this->mUserName
);
227 if ( !$user ||
$user->isAnon() ) {
228 throw new PasswordError( $this->msg( 'nosuchusershort', $this->mUserName
)->text() );
231 if ( $newpass !== $retype ) {
232 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'badretype' ) );
233 throw new PasswordError( $this->msg( 'badretype' )->text() );
236 $throttleCount = LoginForm
::incLoginThrottle( $this->mUserName
);
237 if ( $throttleCount === true ) {
238 $lang = $this->getLanguage();
239 throw new PasswordError( $this->msg( 'changepassword-throttled' )
240 ->params( $lang->formatDuration( $wgPasswordAttemptThrottle['seconds'] ) )
245 // @TODO Make these separate messages, since the message is written for both cases
246 if ( !$user->checkTemporaryPassword( $oldpass ) && !$user->checkPassword( $oldpass ) ) {
247 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'wrongpassword' ) );
248 throw new PasswordError( $this->msg( 'resetpass-wrong-oldpass' )->text() );
251 // User is resetting their password to their old password
252 if ( $oldpass === $newpass ) {
253 throw new PasswordError( $this->msg( 'resetpass-recycled' )->text() );
256 // Do AbortChangePassword after checking mOldpass, so we don't leak information
257 // by possibly aborting a new password before verifying the old password.
258 $abortMsg = 'resetpass-abort-generic';
259 if ( !wfRunHooks( 'AbortChangePassword', array( $user, $oldpass, $newpass, &$abortMsg ) ) ) {
260 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'abortreset' ) );
261 throw new PasswordError( $this->msg( $abortMsg )->text() );
264 // Please reset throttle for successful logins, thanks!
265 if ( $throttleCount ) {
266 LoginForm
::clearLoginThrottle( $this->mUserName
);
270 $user->setPassword( $newpass );
271 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'success' ) );
272 } catch ( PasswordError
$e ) {
273 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'error' ) );
274 throw new PasswordError( $e->getMessage() );
278 // This is needed to keep the user connected since
279 // changing the password also modifies the user's token.
282 $user->resetPasswordExpiration();
283 $user->saveSettings();
286 public function requiresUnblock() {
290 protected function getGroupName() {