1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
|
<?php
/**
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
*/
namespace MediaWiki\Specials;
use ErrorPageError;
use MediaWiki\HTMLForm\HTMLForm;
use MediaWiki\MainConfigNames;
use MediaWiki\SpecialPage\FormSpecialPage;
use MediaWiki\Status\Status;
use MediaWiki\User\PasswordReset;
use MediaWiki\User\User;
use ThrottledError;
/**
* Special page for requesting a password reset email.
*
* Requires the TemporaryPasswordPrimaryAuthenticationProvider and the
* EmailNotificationSecondaryAuthenticationProvider (or something providing equivalent
* functionality) to be enabled.
*
* @ingroup SpecialPage
*/
class SpecialPasswordReset extends FormSpecialPage {
private PasswordReset $passwordReset;
/**
* @param PasswordReset $passwordReset
*/
public function __construct( PasswordReset $passwordReset ) {
parent::__construct( 'PasswordReset', 'editmyprivateinfo' );
$this->passwordReset = $passwordReset;
}
public function doesWrites() {
return true;
}
/** @inheritDoc */
public function userCanExecute( User $user ) {
return $this->passwordReset->isAllowed( $user )->isGood();
}
public function checkExecutePermissions( User $user ) {
$status = Status::wrap( $this->passwordReset->isAllowed( $user ) );
if ( !$status->isGood() ) {
throw new ErrorPageError( 'internalerror', $status->getMessage() );
}
parent::checkExecutePermissions( $user );
}
/**
* @param string|null $par
*/
public function execute( $par ) {
$out = $this->getOutput();
$out->disallowUserJs();
parent::execute( $par );
}
/** @inheritDoc */
protected function getFormFields() {
$resetRoutes = $this->getConfig()->get( MainConfigNames::PasswordResetRoutes );
$a = [];
if ( isset( $resetRoutes['username'] ) && $resetRoutes['username'] ) {
$a['Username'] = [
'type' => 'user',
'default' => $this->getRequest()->getSession()->suggestLoginUsername(),
'label-message' => 'passwordreset-username',
'excludetemp' => true,
];
if ( $this->getUser()->isRegistered() ) {
$a['Username']['default'] = $this->getUser()->getName();
}
}
if ( isset( $resetRoutes['email'] ) && $resetRoutes['email'] ) {
$a['Email'] = [
'type' => 'email',
'label-message' => 'passwordreset-email',
];
}
return $a;
}
/** @inheritDoc */
protected function getDisplayFormat() {
return 'ooui';
}
public function alterForm( HTMLForm $form ) {
$resetRoutes = $this->getConfig()->get( MainConfigNames::PasswordResetRoutes );
$form->setSubmitDestructive();
$form->addHiddenFields( $this->getRequest()->getValues( 'returnto', 'returntoquery' ) );
$i = 0;
if ( isset( $resetRoutes['username'] ) && $resetRoutes['username'] ) {
$i++;
}
if ( isset( $resetRoutes['email'] ) && $resetRoutes['email'] ) {
$i++;
}
$message = ( $i > 1 ) ? 'passwordreset-text-many' : 'passwordreset-text-one';
$form->setHeaderHtml( $this->msg( $message, $i )->parseAsBlock() );
$form->setSubmitTextMsg( 'mailmypassword' );
}
/**
* Process the form.
* At this point, we know that the user passes all the criteria in
* userCanExecute(), and if the data array contains 'Username', etc., then Username
* resets are allowed.
* @param array $data
* @return Status
*/
public function onSubmit( array $data ) {
$username = $data['Username'] ?? null;
$email = $data['Email'] ?? null;
$result = Status::wrap(
$this->passwordReset->execute( $this->getUser(), $username, $email ) );
if ( $result->hasMessage( 'actionthrottledtext' ) ) {
throw new ThrottledError;
}
// Show a message on the successful processing of the form.
// This doesn't necessarily mean a reset email was sent.
if ( $result->isGood() ) {
$output = $this->getOutput();
// Information messages.
$output->addWikiMsg( 'passwordreset-success' );
$output->addWikiMsg( 'passwordreset-success-details-generic',
$this->getConfig()->get( MainConfigNames::PasswordReminderResendTime ) );
// Confirmation of what the user has just submitted.
$info = "\n";
if ( $username ) {
$info .= "* " . $this->msg( 'passwordreset-username' ) . ' '
. wfEscapeWikiText( $username ) . "\n";
}
if ( $email ) {
$info .= "* " . $this->msg( 'passwordreset-email' ) . ' '
. wfEscapeWikiText( $email ) . "\n";
}
$output->addWikiMsg( 'passwordreset-success-info', $info );
// Add a return to link to the main page.
$output->returnToMain();
}
return $result;
}
/**
* Hide the password reset page if resets are disabled.
* @return bool
*/
public function isListed() {
if ( !$this->passwordReset->isEnabled()->isGood() ) {
return false;
}
return parent::isListed();
}
/** @inheritDoc */
protected function getGroupName() {
return 'login';
}
}
/**
* Retain the old class name for backwards compatibility.
* @deprecated since 1.41
*/
class_alias( SpecialPasswordReset::class, 'SpecialPasswordReset' );
|