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
21 * @ingroup SpecialPage
24 use MediaWiki\Auth\AuthManager
;
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;
40 * @var string[] Temporary storage for the passwords which have been sent out, keyed by username.
42 private $passwords = [];
50 * @var string $method Identifies which password reset field was specified by the user.
54 public function __construct() {
55 parent
::__construct( 'PasswordReset', 'editmyprivateinfo' );
56 $this->passwordReset
= new PasswordReset( $this->getConfig(), AuthManager
::singleton() );
59 public function doesWrites() {
63 public function userCanExecute( User
$user ) {
64 return $this->passwordReset
->isAllowed( $user )->isGood();
67 public function checkExecutePermissions( User
$user ) {
68 $status = Status
::wrap( $this->passwordReset
->isAllowed( $user ) );
69 if ( !$status->isGood() ) {
70 throw new ErrorPageError( 'internalerror', $status->getMessage() );
73 parent
::checkExecutePermissions( $user );
76 protected function getFormFields() {
77 $resetRoutes = $this->getConfig()->get( 'PasswordResetRoutes' );
79 if ( isset( $resetRoutes['username'] ) && $resetRoutes['username'] ) {
82 'label-message' => 'passwordreset-username',
85 if ( $this->getUser()->isLoggedIn() ) {
86 $a['Username']['default'] = $this->getUser()->getName();
90 if ( isset( $resetRoutes['email'] ) && $resetRoutes['email'] ) {
93 'label-message' => 'passwordreset-email',
97 if ( $this->getUser()->isAllowed( 'passwordreset' ) ) {
100 'label-message' => 'passwordreset-capture',
101 'help-message' => 'passwordreset-capture-help',
108 protected function getDisplayFormat() {
112 public function alterForm( HTMLForm
$form ) {
113 $resetRoutes = $this->getConfig()->get( 'PasswordResetRoutes' );
115 $form->addHiddenFields( $this->getRequest()->getValues( 'returnto', 'returntoquery' ) );
118 if ( isset( $resetRoutes['username'] ) && $resetRoutes['username'] ) {
121 if ( isset( $resetRoutes['email'] ) && $resetRoutes['email'] ) {
125 $message = ( $i > 1 ) ?
'passwordreset-text-many' : 'passwordreset-text-one';
127 $form->setHeaderText( $this->msg( $message, $i )->parseAsBlock() );
128 $form->setSubmitTextMsg( 'mailmypassword' );
132 * Process the form. 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.
136 * @throws MWException
137 * @throws ThrottledError|PermissionsError
140 public function onSubmit( array $data ) {
141 if ( isset( $data['Capture'] ) && !$this->getUser()->isAllowed( 'passwordreset' ) ) {
142 // The user knows they don't have the passwordreset permission,
143 // but they tried to spoof the form. That's naughty
144 throw new PermissionsError( 'passwordreset' );
147 $username = isset( $data['Username'] ) ?
$data['Username'] : null;
148 $email = isset( $data['Email'] ) ?
$data['Email'] : null;
149 $capture = !empty( $data['Capture'] );
151 $this->method
= $username ?
'username' : 'email';
152 $this->result
= Status
::wrap(
153 $this->passwordReset
->execute( $this->getUser(), $username, $email, $capture ) );
154 if ( $capture && $this->result
->isOK() ) {
155 $this->passwords
= $this->result
->getValue();
158 if ( $this->result
->hasMessage( 'actionthrottledtext' ) ) {
159 throw new ThrottledError
;
162 return $this->result
;
165 public function onSuccess() {
166 if ( $this->getUser()->isAllowed( 'passwordreset' ) && $this->passwords
) {
169 if ( $this->result
->isGood() ) {
170 $this->getOutput()->addWikiMsg( 'passwordreset-emailsent-capture2',
171 count( $this->passwords
) );
173 $this->getOutput()->addWikiMsg( 'passwordreset-emailerror-capture2',
174 $this->result
->getMessage(), key( $this->passwords
), count( $this->passwords
) );
177 $this->getOutput()->addHTML( Html
::openElement( 'ul' ) );
178 foreach ( $this->passwords
as $username => $pwd ) {
179 $this->getOutput()->addHTML( Html
::rawElement( 'li', [],
180 htmlspecialchars( $username, ENT_QUOTES
)
181 . $this->msg( 'colon-separator' )->text()
182 . htmlspecialchars( $pwd, ENT_QUOTES
)
185 $this->getOutput()->addHTML( Html
::closeElement( 'ul' ) );
188 if ( $this->method
=== 'email' ) {
189 $this->getOutput()->addWikiMsg( 'passwordreset-emailsentemail' );
191 $this->getOutput()->addWikiMsg( 'passwordreset-emailsentusername' );
194 $this->getOutput()->returnToMain();
198 * Hide the password reset page if resets are disabled.
201 public function isListed() {
202 if ( $this->passwordReset
->isAllowed( $this->getUser() )->isGood() ) {
203 return parent
::isListed();
209 protected function getGroupName() {