Merge "Special:Upload should not crash on failing previews"
[mediawiki.git] / includes / specials / SpecialPasswordReset.php
bloba4f16bd7c97649cd6eb19d75b37f1ae28eaf2dbc
1 <?php
2 /**
3 * Implements Special:PasswordReset
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 use MediaWiki\Auth\AuthManager;
26 /**
27 * Special page for requesting a password reset email.
29 * Requires the TemporaryPasswordPrimaryAuthenticationProvider and the
30 * EmailNotificationSecondaryAuthenticationProvider (or something providing equivalent
31 * functionality) to be enabled.
33 * @ingroup SpecialPage
35 class SpecialPasswordReset extends FormSpecialPage {
36 /** @var PasswordReset */
37 private $passwordReset = null;
39 /**
40 * @var string[] Temporary storage for the passwords which have been sent out, keyed by username.
42 private $passwords = [];
44 /**
45 * @var Status
47 private $result;
49 /**
50 * @var string $method Identifies which password reset field was specified by the user.
52 private $method;
54 public function __construct() {
55 parent::__construct( 'PasswordReset', 'editmyprivateinfo' );
58 private function getPasswordReset() {
59 if ( $this->passwordReset === null ) {
60 $this->passwordReset = new PasswordReset( $this->getConfig(), AuthManager::singleton() );
62 return $this->passwordReset;
65 public function doesWrites() {
66 return true;
69 public function userCanExecute( User $user ) {
70 return $this->getPasswordReset()->isAllowed( $user )->isGood();
73 public function checkExecutePermissions( User $user ) {
74 $status = Status::wrap( $this->getPasswordReset()->isAllowed( $user ) );
75 if ( !$status->isGood() ) {
76 throw new ErrorPageError( 'internalerror', $status->getMessage() );
79 parent::checkExecutePermissions( $user );
82 protected function getFormFields() {
83 $resetRoutes = $this->getConfig()->get( 'PasswordResetRoutes' );
84 $a = [];
85 if ( isset( $resetRoutes['username'] ) && $resetRoutes['username'] ) {
86 $a['Username'] = [
87 'type' => 'text',
88 'label-message' => 'passwordreset-username',
91 if ( $this->getUser()->isLoggedIn() ) {
92 $a['Username']['default'] = $this->getUser()->getName();
96 if ( isset( $resetRoutes['email'] ) && $resetRoutes['email'] ) {
97 $a['Email'] = [
98 'type' => 'email',
99 'label-message' => 'passwordreset-email',
103 return $a;
106 protected function getDisplayFormat() {
107 return 'ooui';
110 public function alterForm( HTMLForm $form ) {
111 $resetRoutes = $this->getConfig()->get( 'PasswordResetRoutes' );
113 $form->addHiddenFields( $this->getRequest()->getValues( 'returnto', 'returntoquery' ) );
115 $i = 0;
116 if ( isset( $resetRoutes['username'] ) && $resetRoutes['username'] ) {
117 $i++;
119 if ( isset( $resetRoutes['email'] ) && $resetRoutes['email'] ) {
120 $i++;
123 $message = ( $i > 1 ) ? 'passwordreset-text-many' : 'passwordreset-text-one';
125 $form->setHeaderText( $this->msg( $message, $i )->parseAsBlock() );
126 $form->setSubmitTextMsg( 'mailmypassword' );
130 * Process the form. At this point we know that the user passes all the criteria in
131 * userCanExecute(), and if the data array contains 'Username', etc, then Username
132 * resets are allowed.
133 * @param array $data
134 * @throws MWException
135 * @throws ThrottledError|PermissionsError
136 * @return Status
138 public function onSubmit( array $data ) {
139 $username = isset( $data['Username'] ) ? $data['Username'] : null;
140 $email = isset( $data['Email'] ) ? $data['Email'] : null;
142 $this->method = $username ? 'username' : 'email';
143 $this->result = Status::wrap(
144 $this->getPasswordReset()->execute( $this->getUser(), $username, $email ) );
146 if ( $this->result->hasMessage( 'actionthrottledtext' ) ) {
147 throw new ThrottledError;
150 return $this->result;
153 public function onSuccess() {
154 if ( $this->method === 'email' ) {
155 $this->getOutput()->addWikiMsg( 'passwordreset-emailsentemail' );
156 } else {
157 $this->getOutput()->addWikiMsg( 'passwordreset-emailsentusername' );
160 $this->getOutput()->returnToMain();
164 * Hide the password reset page if resets are disabled.
165 * @return bool
167 public function isListed() {
168 if ( $this->getPasswordReset()->isAllowed( $this->getUser() )->isGood() ) {
169 return parent::isListed();
172 return false;
175 protected function getGroupName() {
176 return 'users';