3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
21 namespace MediaWiki\Specials
;
24 use MediaWiki\HTMLForm\HTMLForm
;
25 use MediaWiki\MainConfigNames
;
26 use MediaWiki\SpecialPage\FormSpecialPage
;
27 use MediaWiki\Status\Status
;
28 use MediaWiki\User\PasswordReset
;
29 use MediaWiki\User\User
;
33 * Special page for requesting a password reset email.
35 * Requires the TemporaryPasswordPrimaryAuthenticationProvider and the
36 * EmailNotificationSecondaryAuthenticationProvider (or something providing equivalent
37 * functionality) to be enabled.
39 * @ingroup SpecialPage
41 class SpecialPasswordReset
extends FormSpecialPage
{
42 private PasswordReset
$passwordReset;
44 public function __construct( PasswordReset
$passwordReset ) {
45 parent
::__construct( 'PasswordReset', 'editmyprivateinfo' );
47 $this->passwordReset
= $passwordReset;
50 public function doesWrites() {
55 public function userCanExecute( User
$user ) {
56 return $this->passwordReset
->isAllowed( $user )->isGood();
59 public function checkExecutePermissions( User
$user ) {
60 $status = Status
::wrap( $this->passwordReset
->isAllowed( $user ) );
61 if ( !$status->isGood() ) {
62 throw new ErrorPageError( 'internalerror', $status->getMessage() );
65 parent
::checkExecutePermissions( $user );
69 * @param string|null $par
71 public function execute( $par ) {
72 $out = $this->getOutput();
73 $out->disallowUserJs();
74 parent
::execute( $par );
78 protected function getFormFields() {
79 $resetRoutes = $this->getConfig()->get( MainConfigNames
::PasswordResetRoutes
);
81 if ( isset( $resetRoutes['username'] ) && $resetRoutes['username'] ) {
84 'default' => $this->getRequest()->getSession()->suggestLoginUsername(),
85 'label-message' => 'passwordreset-username',
86 'excludetemp' => true,
89 if ( $this->getUser()->isRegistered() ) {
90 $a['Username']['default'] = $this->getUser()->getName();
94 if ( isset( $resetRoutes['email'] ) && $resetRoutes['email'] ) {
97 'label-message' => 'passwordreset-email',
105 protected function getDisplayFormat() {
109 public function alterForm( HTMLForm
$form ) {
110 $resetRoutes = $this->getConfig()->get( MainConfigNames
::PasswordResetRoutes
);
112 $form->setSubmitDestructive();
114 $form->addHiddenFields( $this->getRequest()->getValues( 'returnto', 'returntoquery' ) );
117 if ( isset( $resetRoutes['username'] ) && $resetRoutes['username'] ) {
120 if ( isset( $resetRoutes['email'] ) && $resetRoutes['email'] ) {
124 $message = ( $i > 1 ) ?
'passwordreset-text-many' : 'passwordreset-text-one';
126 $form->setHeaderHtml( $this->msg( $message, $i )->parseAsBlock() );
127 $form->setSubmitTextMsg( 'mailmypassword' );
132 * At this point, we know that the user passes all the criteria in
133 * userCanExecute(), and if the data array contains 'Username', etc., then Username
134 * resets are allowed.
138 public function onSubmit( array $data ) {
139 $username = $data['Username'] ??
null;
140 $email = $data['Email'] ??
null;
142 $result = Status
::wrap(
143 $this->passwordReset
->execute( $this->getUser(), $username, $email ) );
145 if ( $result->hasMessage( 'actionthrottledtext' ) ) {
146 throw new ThrottledError
;
153 * Show a message on the successful processing of the form.
154 * This doesn't necessarily mean a reset email was sent.
156 public function onSuccess() {
157 $output = $this->getOutput();
159 // Information messages.
160 $output->addWikiMsg( 'passwordreset-success' );
161 $output->addWikiMsg( 'passwordreset-success-details-generic',
162 $this->getConfig()->get( MainConfigNames
::PasswordReminderResendTime
) );
164 // Confirmation of what the user has just submitted.
166 $postVals = $this->getRequest()->getPostValues();
167 if ( isset( $postVals['wpUsername'] ) && $postVals['wpUsername'] !== '' ) {
168 $info .= "* " . $this->msg( 'passwordreset-username' ) . ' '
169 . wfEscapeWikiText( $postVals['wpUsername'] ) . "\n";
171 if ( isset( $postVals['wpEmail'] ) && $postVals['wpEmail'] !== '' ) {
172 $info .= "* " . $this->msg( 'passwordreset-email' ) . ' '
173 . wfEscapeWikiText( $postVals['wpEmail'] ) . "\n";
175 $output->addWikiMsg( 'passwordreset-success-info', $info );
177 // Add a return to link to the main page.
178 $output->returnToMain();
182 * Hide the password reset page if resets are disabled.
185 public function isListed() {
186 if ( !$this->passwordReset
->isEnabled()->isGood() ) {
190 return parent
::isListed();
194 protected function getGroupName() {
200 * Retain the old class name for backwards compatibility.
201 * @deprecated since 1.41
203 class_alias( SpecialPasswordReset
::class, 'SpecialPasswordReset' );