MediaWiki 1.43.1
SpecialPasswordReset.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Specials;
22
31
42 private PasswordReset $passwordReset;
43
47 public function __construct( PasswordReset $passwordReset ) {
48 parent::__construct( 'PasswordReset', 'editmyprivateinfo' );
49
50 $this->passwordReset = $passwordReset;
51 }
52
53 public function doesWrites() {
54 return true;
55 }
56
58 public function userCanExecute( User $user ) {
59 return $this->passwordReset->isAllowed( $user )->isGood();
60 }
61
62 public function checkExecutePermissions( User $user ) {
63 $status = Status::wrap( $this->passwordReset->isAllowed( $user ) );
64 if ( !$status->isGood() ) {
65 throw new ErrorPageError( 'internalerror', $status->getMessage() );
66 }
67
68 parent::checkExecutePermissions( $user );
69 }
70
74 public function execute( $par ) {
75 $out = $this->getOutput();
76 $out->disallowUserJs();
77 parent::execute( $par );
78 }
79
81 protected function getFormFields() {
82 $resetRoutes = $this->getConfig()->get( MainConfigNames::PasswordResetRoutes );
83 $a = [];
84 if ( isset( $resetRoutes['username'] ) && $resetRoutes['username'] ) {
85 $a['Username'] = [
86 'type' => 'user',
87 'default' => $this->getRequest()->getSession()->suggestLoginUsername(),
88 'label-message' => 'passwordreset-username',
89 'excludetemp' => true,
90 ];
91
92 if ( $this->getUser()->isRegistered() ) {
93 $a['Username']['default'] = $this->getUser()->getName();
94 }
95 }
96
97 if ( isset( $resetRoutes['email'] ) && $resetRoutes['email'] ) {
98 $a['Email'] = [
99 'type' => 'email',
100 'label-message' => 'passwordreset-email',
101 ];
102 }
103
104 return $a;
105 }
106
108 protected function getDisplayFormat() {
109 return 'ooui';
110 }
111
112 public function alterForm( HTMLForm $form ) {
113 $resetRoutes = $this->getConfig()->get( MainConfigNames::PasswordResetRoutes );
114
115 $form->setSubmitDestructive();
116
117 $form->addHiddenFields( $this->getRequest()->getValues( 'returnto', 'returntoquery' ) );
118
119 $i = 0;
120 if ( isset( $resetRoutes['username'] ) && $resetRoutes['username'] ) {
121 $i++;
122 }
123 if ( isset( $resetRoutes['email'] ) && $resetRoutes['email'] ) {
124 $i++;
125 }
126
127 $message = ( $i > 1 ) ? 'passwordreset-text-many' : 'passwordreset-text-one';
128
129 $form->setHeaderHtml( $this->msg( $message, $i )->parseAsBlock() );
130 $form->setSubmitTextMsg( 'mailmypassword' );
131 }
132
141 public function onSubmit( array $data ) {
142 $username = $data['Username'] ?? null;
143 $email = $data['Email'] ?? null;
144
145 $result = Status::wrap(
146 $this->passwordReset->execute( $this->getUser(), $username, $email ) );
147
148 if ( $result->hasMessage( 'actionthrottledtext' ) ) {
149 throw new ThrottledError;
150 }
151
152 return $result;
153 }
154
159 public function onSuccess() {
160 $output = $this->getOutput();
161
162 // Information messages.
163 $output->addWikiMsg( 'passwordreset-success' );
164 $output->addWikiMsg( 'passwordreset-success-details-generic',
166
167 // Confirmation of what the user has just submitted.
168 $info = "\n";
169 $postVals = $this->getRequest()->getPostValues();
170 if ( isset( $postVals['wpUsername'] ) && $postVals['wpUsername'] !== '' ) {
171 $info .= "* " . $this->msg( 'passwordreset-username' ) . ' '
172 . wfEscapeWikiText( $postVals['wpUsername'] ) . "\n";
173 }
174 if ( isset( $postVals['wpEmail'] ) && $postVals['wpEmail'] !== '' ) {
175 $info .= "* " . $this->msg( 'passwordreset-email' ) . ' '
176 . wfEscapeWikiText( $postVals['wpEmail'] ) . "\n";
177 }
178 $output->addWikiMsg( 'passwordreset-success-info', $info );
179
180 // Add a return to link to the main page.
181 $output->returnToMain();
182 }
183
188 public function isListed() {
189 if ( !$this->passwordReset->isEnabled()->isGood() ) {
190 return false;
191 }
192
193 return parent::isListed();
194 }
195
197 protected function getGroupName() {
198 return 'login';
199 }
200}
201
206class_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:209
setHeaderHtml( $html, $section=null)
Set header HTML, inside the form.
Definition HTMLForm.php:997
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 subpage 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 POST requests to this special page require write access to the wiki.
alterForm(HTMLForm $form)
Play with the HTMLForm if you need to more substantially.
getFormFields()
Get an HTMLForm descriptor array.array
getDisplayFormat()
Get display format for the form.See HTMLForm documentation for available values.1....
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:94
Show an error when the user hits a rate limit.