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
;
25 use MediaWiki\MediaWikiServices
;
28 * Special page for requesting a password reset email.
30 * Requires the TemporaryPasswordPrimaryAuthenticationProvider and the
31 * EmailNotificationSecondaryAuthenticationProvider (or something providing equivalent
32 * functionality) to be enabled.
34 * @ingroup SpecialPage
36 class SpecialPasswordReset
extends FormSpecialPage
{
37 /** @var PasswordReset */
38 private $passwordReset = null;
46 * @var string $method Identifies which password reset field was specified by the user.
50 public function __construct() {
51 parent
::__construct( 'PasswordReset', 'editmyprivateinfo' );
54 private function getPasswordReset() {
55 if ( $this->passwordReset
=== null ) {
56 $this->passwordReset
= new PasswordReset(
58 AuthManager
::singleton(),
59 MediaWikiServices
::getInstance()->getPermissionManager()
62 return $this->passwordReset
;
65 public function doesWrites() {
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' );
85 if ( isset( $resetRoutes['username'] ) && $resetRoutes['username'] ) {
88 'default' => $this->getRequest()->getSession()->suggestLoginUsername(),
89 'label-message' => 'passwordreset-username',
92 if ( $this->getUser()->isLoggedIn() ) {
93 $a['Username']['default'] = $this->getUser()->getName();
97 if ( isset( $resetRoutes['email'] ) && $resetRoutes['email'] ) {
100 'label-message' => 'passwordreset-email',
107 protected function getDisplayFormat() {
111 public function alterForm( HTMLForm
$form ) {
112 $resetRoutes = $this->getConfig()->get( 'PasswordResetRoutes' );
114 $form->setSubmitDestructive();
116 $form->addHiddenFields( $this->getRequest()->getValues( 'returnto', 'returntoquery' ) );
119 if ( isset( $resetRoutes['username'] ) && $resetRoutes['username'] ) {
122 if ( isset( $resetRoutes['email'] ) && $resetRoutes['email'] ) {
126 $message = ( $i > 1 ) ?
'passwordreset-text-many' : 'passwordreset-text-one';
128 $form->setHeaderText( $this->msg( $message, $i )->parseAsBlock() );
129 $form->setSubmitTextMsg( 'mailmypassword' );
133 * Process the form. At this point we know that the user passes all the criteria in
134 * userCanExecute(), and if the data array contains 'Username', etc, then Username
135 * resets are allowed.
137 * @throws MWException
138 * @throws ThrottledError|PermissionsError
141 public function onSubmit( array $data ) {
142 $username = $data['Username'] ??
null;
143 $email = $data['Email'] ??
null;
145 $this->method
= $username ?
'username' : 'email';
146 $this->result
= Status
::wrap(
147 $this->getPasswordReset()->execute( $this->getUser(), $username, $email ) );
149 if ( $this->result
->hasMessage( 'actionthrottledtext' ) ) {
150 throw new ThrottledError
;
153 return $this->result
;
156 public function onSuccess() {
157 if ( $this->method
=== 'email' ) {
158 $this->getOutput()->addWikiMsg( 'passwordreset-emailsentemail' );
160 $this->getOutput()->addWikiMsg( 'passwordreset-emailsentusername' );
163 $this->getOutput()->returnToMain();
167 * Hide the password reset page if resets are disabled.
170 public function isListed() {
171 if ( $this->getPasswordReset()->isAllowed( $this->getUser() )->isGood() ) {
172 return parent
::isListed();
178 protected function getGroupName() {