MediaWiki master
SpecialPasswordReset.php
Go to the documentation of this file.
1<?php
24namespace MediaWiki\Specials;
25
34
46 private $passwordReset;
47
51 private $result;
52
56 private $method;
57
61 public function __construct( PasswordReset $passwordReset ) {
62 parent::__construct( 'PasswordReset', 'editmyprivateinfo' );
63
64 $this->passwordReset = $passwordReset;
65 }
66
67 public function doesWrites() {
68 return true;
69 }
70
71 public function userCanExecute( User $user ) {
72 return $this->passwordReset->isAllowed( $user )->isGood();
73 }
74
75 public function checkExecutePermissions( User $user ) {
76 $status = Status::wrap( $this->passwordReset->isAllowed( $user ) );
77 if ( !$status->isGood() ) {
78 throw new ErrorPageError( 'internalerror', $status->getMessage() );
79 }
80
81 parent::checkExecutePermissions( $user );
82 }
83
87 public function execute( $par ) {
88 $out = $this->getOutput();
89 $out->disallowUserJs();
90 parent::execute( $par );
91 }
92
93 protected function getFormFields() {
94 $resetRoutes = $this->getConfig()->get( MainConfigNames::PasswordResetRoutes );
95 $a = [];
96 if ( isset( $resetRoutes['username'] ) && $resetRoutes['username'] ) {
97 $a['Username'] = [
98 'type' => 'text',
99 'default' => $this->getRequest()->getSession()->suggestLoginUsername(),
100 'label-message' => 'passwordreset-username',
101 ];
102
103 if ( $this->getUser()->isRegistered() ) {
104 $a['Username']['default'] = $this->getUser()->getName();
105 }
106 }
107
108 if ( isset( $resetRoutes['email'] ) && $resetRoutes['email'] ) {
109 $a['Email'] = [
110 'type' => 'email',
111 'label-message' => 'passwordreset-email',
112 ];
113 }
114
115 return $a;
116 }
117
118 protected function getDisplayFormat() {
119 return 'ooui';
120 }
121
122 public function alterForm( HTMLForm $form ) {
123 $resetRoutes = $this->getConfig()->get( MainConfigNames::PasswordResetRoutes );
124
125 $form->setSubmitDestructive();
126
127 $form->addHiddenFields( $this->getRequest()->getValues( 'returnto', 'returntoquery' ) );
128
129 $i = 0;
130 if ( isset( $resetRoutes['username'] ) && $resetRoutes['username'] ) {
131 $i++;
132 }
133 if ( isset( $resetRoutes['email'] ) && $resetRoutes['email'] ) {
134 $i++;
135 }
136
137 $message = ( $i > 1 ) ? 'passwordreset-text-many' : 'passwordreset-text-one';
138
139 $form->setHeaderHtml( $this->msg( $message, $i )->parseAsBlock() );
140 $form->setSubmitTextMsg( 'mailmypassword' );
141 }
142
150 public function onSubmit( array $data ) {
151 $username = $data['Username'] ?? null;
152 $email = $data['Email'] ?? null;
153
154 $this->method = $username ? 'username' : 'email';
155 $this->result = Status::wrap(
156 $this->passwordReset->execute( $this->getUser(), $username, $email ) );
157
158 if ( $this->result->hasMessage( 'actionthrottledtext' ) ) {
159 throw new ThrottledError;
160 }
161
162 return $this->result;
163 }
164
169 public function onSuccess() {
170 $output = $this->getOutput();
171
172 // Information messages.
173 $output->addWikiMsg( 'passwordreset-success' );
174 $output->addWikiMsg( 'passwordreset-success-details-generic',
176
177 // Confirmation of what the user has just submitted.
178 $info = "\n";
179 $postVals = $this->getRequest()->getPostValues();
180 if ( isset( $postVals['wpUsername'] ) && $postVals['wpUsername'] !== '' ) {
181 $info .= "* " . $this->msg( 'passwordreset-username' ) . ' '
182 . wfEscapeWikiText( $postVals['wpUsername'] ) . "\n";
183 }
184 if ( isset( $postVals['wpEmail'] ) && $postVals['wpEmail'] !== '' ) {
185 $info .= "* " . $this->msg( 'passwordreset-email' ) . ' '
186 . wfEscapeWikiText( $postVals['wpEmail'] ) . "\n";
187 }
188 $output->addWikiMsg( 'passwordreset-success-info', $info );
189
190 // Link to main page.
191 $output->returnToMain();
192 }
193
198 public function isListed() {
199 if ( !$this->passwordReset->isEnabled()->isGood() ) {
200 return false;
201 }
202
203 return parent::isListed();
204 }
205
206 protected function getGroupName() {
207 return 'login';
208 }
209}
210
215class_alias( SpecialPasswordReset::class, 'SpecialPasswordReset' );
wfEscapeWikiText( $input)
Escapes the given text so that it may be output using addWikiText() without any linking,...
An error page which can definitely be safely rendered using the OutputPage.
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition HTMLForm.php:206
setHeaderHtml( $html, $section=null)
Set header HTML, inside the form.
Definition HTMLForm.php:963
setSubmitTextMsg( $msg)
Set the text for the submit button to a message.
addHiddenFields(array $fields)
Add an array of hidden fields to the output Array values are discarded for security reasons (per WebR...
setSubmitDestructive()
Identify that the submit button in the form has a destructive action.
A class containing constants representing the names of configuration variables.
const PasswordReminderResendTime
Name constant for the PasswordReminderResendTime setting, for use with Config::get()
const PasswordResetRoutes
Name constant for the PasswordResetRoutes setting, for use with Config::get()
Special page which uses an HTMLForm to handle processing.
string null $par
The sub-page of the special page.
getUser()
Shortcut to get the User executing this instance.
getConfig()
Shortcut to get main config object.
getRequest()
Get the WebRequest being used for this instance.
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
getOutput()
Get the OutputPage being used for this instance.
Special page for requesting a password reset email.
isListed()
Hide the password reset page if resets are disabled.
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
onSuccess()
Show a message on the successful processing of the form.
userCanExecute(User $user)
Checks if the given user (identified by an object) can execute this special page (as defined by $mRes...
doesWrites()
Indicates whether this special page may perform database writes.
alterForm(HTMLForm $form)
Play with the HTMLForm if you need to more substantially.
getFormFields()
Get an HTMLForm descriptor array.
getDisplayFormat()
Get display format for the form.
checkExecutePermissions(User $user)
Called from execute() to check if the given user can perform this action.
__construct(PasswordReset $passwordReset)
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:54
Helper class for the password reset functionality shared by the web UI and the API.
internal since 1.36
Definition User.php:93
Show an error when the user hits a rate limit.
This program is free software; you can redistribute it and/or modify it under the terms of the GNU Ge...