AuthManager: Break AuthPlugin::addUser more explicitly
[mediawiki.git] / includes / specials / pre-authmanager / SpecialChangePassword.php
blob3955feeba21287344f562d758839b46714f24545
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 SpecialChangePasswordPreAuthManager 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 public function doesWrites() {
45 return true;
48 /**
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' );
66 /**
67 * Set a message at the top of the Change Password form
68 * @since 1.23
69 * @param Message $msg Message to parse and add to the form header
71 public function setChangeMessage( Message $msg ) {
72 $this->mPreTextMessage = $msg;
75 /**
76 * Set a message at the top of the Change Password form
77 * @since 1.23
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';
93 $fields = [
94 'Name' => [
95 'type' => 'info',
96 'label-message' => 'username',
97 'default' => $request->getVal( 'wpName', $user->getName() ),
99 'Password' => [
100 'type' => 'password',
101 'label-message' => $oldpassMsg,
103 'NewPassword' => [
104 'type' => 'password',
105 'label-message' => 'newpassword',
107 'Retype' => [
108 'type' => 'password',
109 'label-message' => 'retypenew',
113 if ( !$this->getUser()->isLoggedIn() ) {
114 $fields['LoginOnChangeToken'] = [
115 'type' => 'hidden',
116 'label' => 'Change Password Token',
117 'default' => LoginForm::getLoginToken()->toString(),
121 $extraFields = [];
122 Hooks::run( 'ChangePasswordForm', [ &$extraFields ] );
123 foreach ( $extraFields as $extra ) {
124 list( $name, $label, $type, $default ) = $extra;
125 $fields[$name] = [
126 'type' => $type,
127 'name' => $name,
128 'label-message' => $label,
129 'default' => $default,
133 if ( !$user->isLoggedIn() ) {
134 $fields['Remember'] = [
135 'type' => 'check',
136 'label' => $this->msg( 'remembermypassword' )
137 ->numParams(
138 ceil( $this->getConfig()->get( 'CookieExpiration' ) / ( 3600 * 24 ) )
139 )->text(),
140 'default' => $request->getVal( 'wpRemember' ),
144 return $fields;
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'
154 : 'resetpass_submit'
156 $form->addButton( [
157 'name' => 'wpCancel',
158 'value' => $this->msg( 'resetpass-submit-cancel' )->text()
159 ] );
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 ) {
169 global $wgAuth;
171 $request = $this->getRequest();
173 if ( $request->getCheck( 'wpLoginToken' ) ) {
174 // This comes from Special:Userlogin when logging in with a temporary password
175 return false;
178 if ( !$this->getUser()->isLoggedIn()
179 && !LoginForm::getLoginToken()->match( $request->getVal( 'wpLoginOnChangeToken' ) )
181 // Potential CSRF (bug 62497)
182 return false;
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 ) );
194 return true;
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'] );
206 return $status;
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();
216 } else {
217 $request = $this->getRequest();
218 LoginForm::clearLoginToken();
219 $token = LoginForm::getLoginToken()->toString();
220 $data = [
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
241 * @return Status
243 protected function attemptReset( $oldpass, $newpass, $retype ) {
244 $isSelf = ( $this->mUserName === $this->getUser()->getName() );
245 if ( $isSelf ) {
246 $user = $this->getUser();
247 } else {
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', [ $user, $newpass, 'badretype' ] );
257 return Status::newFatal( $this->msg( 'badretype' ) );
260 $throttleInfo = LoginForm::incrementLoginThrottle( $this->mUserName );
261 if ( $throttleInfo ) {
262 return Status::newFatal( $this->msg( 'changepassword-throttled' )
263 ->durationParams( $throttleInfo['wait'] )
267 // @todo Make these separate messages, since the message is written for both cases
268 if ( !$user->checkTemporaryPassword( $oldpass ) && !$user->checkPassword( $oldpass ) ) {
269 Hooks::run( 'PrefsPasswordAudit', [ $user, $newpass, 'wrongpassword' ] );
270 return Status::newFatal( $this->msg( 'resetpass-wrong-oldpass' ) );
273 // User is resetting their password to their old password
274 if ( $oldpass === $newpass ) {
275 return Status::newFatal( $this->msg( 'resetpass-recycled' ) );
278 // Do AbortChangePassword after checking mOldpass, so we don't leak information
279 // by possibly aborting a new password before verifying the old password.
280 $abortMsg = 'resetpass-abort-generic';
281 if ( !Hooks::run( 'AbortChangePassword', [ $user, $oldpass, $newpass, &$abortMsg ] ) ) {
282 Hooks::run( 'PrefsPasswordAudit', [ $user, $newpass, 'abortreset' ] );
283 return Status::newFatal( $this->msg( $abortMsg ) );
286 // Please reset throttle for successful logins, thanks!
287 LoginForm::clearLoginThrottle( $this->mUserName );
289 try {
290 $user->setPassword( $newpass );
291 Hooks::run( 'PrefsPasswordAudit', [ $user, $newpass, 'success' ] );
292 } catch ( PasswordError $e ) {
293 Hooks::run( 'PrefsPasswordAudit', [ $user, $newpass, 'error' ] );
294 return Status::newFatal( new RawMessage( $e->getMessage() ) );
297 if ( $isSelf ) {
298 // This is needed to keep the user connected since
299 // changing the password also modifies the user's token.
300 $remember = $this->getRequest()->getCookie( 'Token' ) !== null;
301 $user->setCookies( null, null, $remember );
303 $user->saveSettings();
304 $this->resetPasswordExpiration( $user );
305 return Status::newGood();
308 public function requiresUnblock() {
309 return false;
312 protected function getGroupName() {
313 return 'users';
317 * For resetting user password expiration, until AuthManager comes along
318 * @param User $user
320 private function resetPasswordExpiration( User $user ) {
321 global $wgPasswordExpirationDays;
322 $newExpire = null;
323 if ( $wgPasswordExpirationDays ) {
324 $newExpire = wfTimestamp(
325 TS_MW,
326 time() + ( $wgPasswordExpirationDays * 24 * 3600 )
329 // Give extensions a chance to force an expiration
330 Hooks::run( 'ResetPasswordExpiration', [ $this, &$newExpire ] );
331 $dbw = wfGetDB( DB_MASTER );
332 $dbw->update(
333 'user',
334 [ 'user_password_expires' => $dbw->timestampOrNull( $newExpire ) ],
335 [ 'user_id' => $user->getId() ],
336 __METHOD__
340 protected function getDisplayFormat() {
341 return 'ooui';