SpecialSearch: Simplify result info
[mediawiki.git] / includes / specials / SpecialChangePassword.php
blobdcd244365690229f37f4360a381ee5884aeae94f
1 <?php
2 /**
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
20 * @file
21 * @ingroup SpecialPage
24 /**
25 * Let users recover their password.
27 * @ingroup SpecialPage
29 class SpecialChangePassword extends FormSpecialPage {
30 protected $mUserName;
31 protected $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 );
44 /**
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' );
61 /**
62 * Set a message at the top of the Change Password form
63 * @since 1.23
64 * @param Message $msg to parse and add to the form header
66 public function setChangeMessage( Message $msg ) {
67 $this->mPreTextMessage = $msg;
70 /**
71 * Set a message at the top of the Change Password form
72 * @since 1.23
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 ( $oldpassMsg === null ) {
87 $oldpassMsg = $user->isLoggedIn() ? 'oldpassword' : 'resetpass-temp-password';
90 $fields = array(
91 'Name' => array(
92 'type' => 'info',
93 'label-message' => 'username',
94 'default' => $request->getVal( 'wpName', $user->getName() ),
96 'Password' => array(
97 'type' => 'password',
98 'label-message' => $oldpassMsg,
100 'NewPassword' => array(
101 'type' => 'password',
102 'label-message' => 'newpassword',
104 'Retype' => array(
105 'type' => 'password',
106 'label-message' => 'retypenew',
110 if ( !$this->getUser()->isLoggedIn() ) {
111 if ( !LoginForm::getLoginToken() ) {
112 LoginForm::setLoginToken();
114 $fields['LoginOnChangeToken'] = array(
115 'type' => 'hidden',
116 'label' => 'Change Password Token',
117 'default' => LoginForm::getLoginToken(),
121 $extraFields = array();
122 wfRunHooks( 'ChangePasswordForm', array( &$extraFields ) );
123 foreach ( $extraFields as $extra ) {
124 list( $name, $label, $type, $default ) = $extra;
125 $fields[$name] = array(
126 'type' => $type,
127 'name' => $name,
128 'label-message' => $label,
129 'default' => $default,
133 if ( !$user->isLoggedIn() ) {
134 $fields['Remember'] = array(
135 'type' => 'check',
136 'label' => $this->msg( 'remembermypassword' )
137 ->numParams( ceil( $wgCookieExpiration / ( 3600 * 24 ) ) )
138 ->text(),
139 'default' => $request->getVal( 'wpRemember' ),
143 return $fields;
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'
153 : 'resetpass_submit'
155 $form->addButton( 'wpCancel', $this->msg( 'resetpass-submit-cancel' )->text() );
156 $form->setHeaderText( $this->msg( 'resetpass_text' )->parseAsBlock() );
157 if ( $this->mPreTextMessage instanceof Message ) {
158 $form->addPreText( $this->mPreTextMessage->parseAsBlock() );
160 $form->addHiddenFields(
161 $this->getRequest()->getValues( 'wpName', 'wpDomain', 'returnto', 'returntoquery' ) );
164 public function onSubmit( array $data ) {
165 global $wgAuth;
167 $request = $this->getRequest();
169 if ( $request->getCheck( 'wpLoginToken' ) ) {
170 // This comes from Special:Userlogin when logging in with a temporary password
171 return false;
174 if ( !$this->getUser()->isLoggedIn()
175 && $request->getVal( 'wpLoginOnChangeToken' ) !== LoginForm::getLoginToken()
177 // Potential CSRF (bug 62497)
178 return false;
181 if ( $request->getCheck( 'wpCancel' ) ) {
182 $titleObj = Title::newFromText( $request->getVal( 'returnto' ) );
183 if ( !$titleObj instanceof Title ) {
184 $titleObj = Title::newMainPage();
186 $query = $request->getVal( 'returntoquery' );
187 $this->getOutput()->redirect( $titleObj->getFullURL( $query ) );
189 return true;
192 try {
193 $this->mUserName = $request->getVal( 'wpName', $this->getUser()->getName() );
194 $this->mDomain = $wgAuth->getDomain();
196 if ( !$wgAuth->allowPasswordChange() ) {
197 throw new ErrorPageError( 'changepassword', 'resetpass_forbidden' );
200 $this->attemptReset( $data['Password'], $data['NewPassword'], $data['Retype'] );
202 return true;
203 } catch ( PasswordError $e ) {
204 return $e->getMessage();
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();
215 } else {
216 $request = $this->getRequest();
217 LoginForm::setLoginToken();
218 $token = LoginForm::getLoginToken();
219 $data = array(
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 * @throws PasswordError when cannot set the new password because requirements not met.
235 protected function attemptReset( $oldpass, $newpass, $retype ) {
236 global $wgPasswordAttemptThrottle;
238 $isSelf = ( $this->mUserName === $this->getUser()->getName() );
239 if ( $isSelf ) {
240 $user = $this->getUser();
241 } else {
242 $user = User::newFromName( $this->mUserName );
245 if ( !$user || $user->isAnon() ) {
246 throw new PasswordError( $this->msg( 'nosuchusershort', $this->mUserName )->text() );
249 if ( $newpass !== $retype ) {
250 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'badretype' ) );
251 throw new PasswordError( $this->msg( 'badretype' )->text() );
254 $throttleCount = LoginForm::incLoginThrottle( $this->mUserName );
255 if ( $throttleCount === true ) {
256 $lang = $this->getLanguage();
257 throw new PasswordError( $this->msg( 'changepassword-throttled' )
258 ->params( $lang->formatDuration( $wgPasswordAttemptThrottle['seconds'] ) )
259 ->text()
263 // @TODO Make these separate messages, since the message is written for both cases
264 if ( !$user->checkTemporaryPassword( $oldpass ) && !$user->checkPassword( $oldpass ) ) {
265 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'wrongpassword' ) );
266 throw new PasswordError( $this->msg( 'resetpass-wrong-oldpass' )->text() );
269 // User is resetting their password to their old password
270 if ( $oldpass === $newpass ) {
271 throw new PasswordError( $this->msg( 'resetpass-recycled' )->text() );
274 // Do AbortChangePassword after checking mOldpass, so we don't leak information
275 // by possibly aborting a new password before verifying the old password.
276 $abortMsg = 'resetpass-abort-generic';
277 if ( !wfRunHooks( 'AbortChangePassword', array( $user, $oldpass, $newpass, &$abortMsg ) ) ) {
278 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'abortreset' ) );
279 throw new PasswordError( $this->msg( $abortMsg )->text() );
282 // Please reset throttle for successful logins, thanks!
283 if ( $throttleCount ) {
284 LoginForm::clearLoginThrottle( $this->mUserName );
287 try {
288 $user->setPassword( $newpass );
289 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'success' ) );
290 } catch ( PasswordError $e ) {
291 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'error' ) );
292 throw new PasswordError( $e->getMessage() );
295 if ( $isSelf ) {
296 // This is needed to keep the user connected since
297 // changing the password also modifies the user's token.
298 $remember = $this->getRequest()->getCookie( 'Token' ) !== null;
299 $user->setCookies( null, null, $remember );
301 $user->resetPasswordExpiration();
302 $user->saveSettings();
305 public function requiresUnblock() {
306 return false;
309 protected function getGroupName() {
310 return 'users';