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;
45 * @param PasswordReset $passwordReset
47 public function __construct( PasswordReset
$passwordReset ) {
48 parent
::__construct( 'PasswordReset', 'editmyprivateinfo' );
50 $this->passwordReset
= $passwordReset;
53 public function doesWrites() {
58 public function userCanExecute( User
$user ) {
59 return $this->passwordReset
->isAllowed( $user )->isGood();
62 public function checkExecutePermissions( User
$user ) {
63 $status = Status
::wrap( $this->passwordReset
->isAllowed( $user ) );
64 if ( !$status->isGood() ) {
65 throw new ErrorPageError( 'internalerror', $status->getMessage() );
68 parent
::checkExecutePermissions( $user );
72 * @param string|null $par
74 public function execute( $par ) {
75 $out = $this->getOutput();
76 $out->disallowUserJs();
77 parent
::execute( $par );
81 protected function getFormFields() {
82 $resetRoutes = $this->getConfig()->get( MainConfigNames
::PasswordResetRoutes
);
84 if ( isset( $resetRoutes['username'] ) && $resetRoutes['username'] ) {
87 'default' => $this->getRequest()->getSession()->suggestLoginUsername(),
88 'label-message' => 'passwordreset-username',
89 'excludetemp' => true,
92 if ( $this->getUser()->isRegistered() ) {
93 $a['Username']['default'] = $this->getUser()->getName();
97 if ( isset( $resetRoutes['email'] ) && $resetRoutes['email'] ) {
100 'label-message' => 'passwordreset-email',
108 protected function getDisplayFormat() {
112 public function alterForm( HTMLForm
$form ) {
113 $resetRoutes = $this->getConfig()->get( MainConfigNames
::PasswordResetRoutes
);
115 $form->setSubmitDestructive();
117 $form->addHiddenFields( $this->getRequest()->getValues( 'returnto', 'returntoquery' ) );
120 if ( isset( $resetRoutes['username'] ) && $resetRoutes['username'] ) {
123 if ( isset( $resetRoutes['email'] ) && $resetRoutes['email'] ) {
127 $message = ( $i > 1 ) ?
'passwordreset-text-many' : 'passwordreset-text-one';
129 $form->setHeaderHtml( $this->msg( $message, $i )->parseAsBlock() );
130 $form->setSubmitTextMsg( 'mailmypassword' );
135 * At this point, we know that the user passes all the criteria in
136 * userCanExecute(), and if the data array contains 'Username', etc., then Username
137 * resets are allowed.
141 public function onSubmit( array $data ) {
142 $username = $data['Username'] ??
null;
143 $email = $data['Email'] ??
null;
145 $result = Status
::wrap(
146 $this->passwordReset
->execute( $this->getUser(), $username, $email ) );
148 if ( $result->hasMessage( 'actionthrottledtext' ) ) {
149 throw new ThrottledError
;
156 * Show a message on the successful processing of the form.
157 * This doesn't necessarily mean a reset email was sent.
159 public function onSuccess() {
160 $output = $this->getOutput();
162 // Information messages.
163 $output->addWikiMsg( 'passwordreset-success' );
164 $output->addWikiMsg( 'passwordreset-success-details-generic',
165 $this->getConfig()->get( MainConfigNames
::PasswordReminderResendTime
) );
167 // Confirmation of what the user has just submitted.
169 $postVals = $this->getRequest()->getPostValues();
170 if ( isset( $postVals['wpUsername'] ) && $postVals['wpUsername'] !== '' ) {
171 $info .= "* " . $this->msg( 'passwordreset-username' ) . ' '
172 . wfEscapeWikiText( $postVals['wpUsername'] ) . "\n";
174 if ( isset( $postVals['wpEmail'] ) && $postVals['wpEmail'] !== '' ) {
175 $info .= "* " . $this->msg( 'passwordreset-email' ) . ' '
176 . wfEscapeWikiText( $postVals['wpEmail'] ) . "\n";
178 $output->addWikiMsg( 'passwordreset-success-info', $info );
180 // Add a return to link to the main page.
181 $output->returnToMain();
185 * Hide the password reset page if resets are disabled.
188 public function isListed() {
189 if ( !$this->passwordReset
->isEnabled()->isGood() ) {
193 return parent
::isListed();
197 protected function getGroupName() {
203 * Retain the old class name for backwards compatibility.
204 * @deprecated since 1.41
206 class_alias( SpecialPasswordReset
::class, 'SpecialPasswordReset' );