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 = null;
45 * @var string $method Identifies which password reset field was specified by the user.
49 public function __construct() {
50 parent
::__construct( 'PasswordReset', 'editmyprivateinfo' );
53 private function getPasswordReset() {
54 if ( $this->passwordReset
=== null ) {
55 $this->passwordReset
= new PasswordReset( $this->getConfig(), AuthManager
::singleton() );
57 return $this->passwordReset
;
60 public function doesWrites() {
64 public function userCanExecute( User
$user ) {
65 return $this->getPasswordReset()->isAllowed( $user )->isGood();
68 public function checkExecutePermissions( User
$user ) {
69 $status = Status
::wrap( $this->getPasswordReset()->isAllowed( $user ) );
70 if ( !$status->isGood() ) {
71 throw new ErrorPageError( 'internalerror', $status->getMessage() );
74 parent
::checkExecutePermissions( $user );
80 public function execute( $par ) {
81 $out = $this->getOutput();
82 $out->disallowUserJs();
83 parent
::execute( $par );
86 protected function getFormFields() {
87 $resetRoutes = $this->getConfig()->get( 'PasswordResetRoutes' );
89 if ( isset( $resetRoutes['username'] ) && $resetRoutes['username'] ) {
92 'label-message' => 'passwordreset-username',
95 if ( $this->getUser()->isLoggedIn() ) {
96 $a['Username']['default'] = $this->getUser()->getName();
100 if ( isset( $resetRoutes['email'] ) && $resetRoutes['email'] ) {
103 'label-message' => 'passwordreset-email',
110 protected function getDisplayFormat() {
114 public function alterForm( HTMLForm
$form ) {
115 $resetRoutes = $this->getConfig()->get( 'PasswordResetRoutes' );
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->setHeaderText( $this->msg( $message, $i )->parseAsBlock() );
130 $form->setSubmitTextMsg( 'mailmypassword' );
134 * Process the form. At this point we know that the user passes all the criteria in
135 * userCanExecute(), and if the data array contains 'Username', etc, then Username
136 * resets are allowed.
138 * @throws MWException
139 * @throws ThrottledError|PermissionsError
142 public function onSubmit( array $data ) {
143 $username = isset( $data['Username'] ) ?
$data['Username'] : null;
144 $email = isset( $data['Email'] ) ?
$data['Email'] : null;
146 $this->method
= $username ?
'username' : 'email';
147 $this->result
= Status
::wrap(
148 $this->getPasswordReset()->execute( $this->getUser(), $username, $email ) );
150 if ( $this->result
->hasMessage( 'actionthrottledtext' ) ) {
151 throw new ThrottledError
;
154 return $this->result
;
157 public function onSuccess() {
158 if ( $this->method
=== 'email' ) {
159 $this->getOutput()->addWikiMsg( 'passwordreset-emailsentemail' );
161 $this->getOutput()->addWikiMsg( 'passwordreset-emailsentusername' );
164 $this->getOutput()->returnToMain();
168 * Hide the password reset page if resets are disabled.
171 public function isListed() {
172 if ( $this->getPasswordReset()->isAllowed( $this->getUser() )->isGood() ) {
173 return parent
::isListed();
179 protected function getGroupName() {