MediaWiki master
SpecialChangeEmail.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Specials;
22
34
44 private $status;
45
46 public function __construct( AuthManager $authManager ) {
47 parent::__construct( 'ChangeEmail', 'editmyprivateinfo' );
48
49 $this->setAuthManager( $authManager );
50 }
51
52 public function doesWrites() {
53 return true;
54 }
55
59 public function isListed() {
60 return $this->getAuthManager()->allowsPropertyChange( 'emailaddress' );
61 }
62
67 public function execute( $par ) {
68 $out = $this->getOutput();
69 $out->disallowUserJs();
70 $out->addModules( 'mediawiki.special.changeemail' );
71 parent::execute( $par );
72 }
73
74 protected function getLoginSecurityLevel() {
75 return $this->getName();
76 }
77
78 protected function checkExecutePermissions( User $user ) {
79 if ( !$this->getAuthManager()->allowsPropertyChange( 'emailaddress' ) ) {
80 throw new ErrorPageError( 'changeemail', 'cannotchangeemail' );
81 }
82
83 $this->requireNamedUser( 'changeemail-no-info', 'exception-nologin', true );
84
85 // This could also let someone check the current email address, so
86 // require both permissions.
87 if ( !$this->getAuthority()->isAllowed( 'viewmyprivateinfo' ) ) {
88 throw new PermissionsError( 'viewmyprivateinfo' );
89 }
90
91 parent::checkExecutePermissions( $user );
92 }
93
94 protected function getFormFields() {
95 $user = $this->getUser();
96
97 return [
98 'Name' => [
99 'type' => 'info',
100 'label-message' => 'username',
101 'default' => $user->getName(),
102 ],
103 'OldEmail' => [
104 'type' => 'info',
105 'label-message' => 'changeemail-oldemail',
106 'default' => $user->getEmail() ?: $this->msg( 'changeemail-none' )->text(),
107 ],
108 'NewEmail' => [
109 'type' => 'email',
110 'label-message' => 'changeemail-newemail',
111 'autofocus' => true,
112 'maxlength' => 255,
113 'help-message' => 'changeemail-newemail-help',
114 ],
115 ];
116 }
117
118 protected function getDisplayFormat() {
119 return 'ooui';
120 }
121
122 protected function alterForm( HTMLForm $form ) {
123 $form->setId( 'mw-changeemail-form' );
124 $form->setTableId( 'mw-changeemail-table' );
125 $form->setSubmitTextMsg( 'changeemail-submit' );
126 $form->addHiddenFields( $this->getRequest()->getValues( 'returnto', 'returntoquery' ) );
127
128 $form->addHeaderHtml( $this->msg( 'changeemail-header' )->parseAsBlock() );
129 $form->setSubmitID( 'change_email_submit' );
130 }
131
132 public function onSubmit( array $data ) {
133 $this->status = $this->attemptChange( $this->getUser(), $data['NewEmail'] );
134
135 return $this->status;
136 }
137
138 public function onSuccess() {
139 $request = $this->getRequest();
140
141 $returnTo = $request->getVal( 'returnto' );
142 $titleObj = $returnTo !== null ? Title::newFromText( $returnTo ) : null;
143 if ( !$titleObj instanceof Title ) {
144 $titleObj = Title::newMainPage();
145 }
146 $query = $request->getVal( 'returntoquery', '' );
147
148 if ( $this->status->value === true ) {
149 $this->getOutput()->redirect( $titleObj->getFullUrlForRedirect( $query ) );
150 } elseif ( $this->status->value === 'eauth' ) {
151 # Notify user that a confirmation email has been sent...
152 $out = $this->getOutput();
153 $out->addModuleStyles( 'mediawiki.codex.messagebox.styles' );
154 $out->addHTML(
155 Html::warningBox(
156 $out->msg( 'eauthentsent', $this->getUser()->getName() )->parse()
157 )
158 );
159 // just show the link to go back
160 $this->getOutput()->addReturnTo( $titleObj, wfCgiToArray( $query ) );
161 }
162 }
163
170 private function attemptChange( User $user, $newAddr ) {
171 if ( $newAddr !== '' && !Sanitizer::validateEmail( $newAddr ) ) {
172 return Status::newFatal( 'invalidemailaddress' );
173 }
174
175 $oldAddr = $user->getEmail();
176 if ( $newAddr === $oldAddr ) {
177 return Status::newFatal( 'changeemail-nochange' );
178 }
179
180 if ( strlen( $newAddr ) > 255 ) {
181 return Status::newFatal( 'changeemail-maxlength' );
182 }
183
184 // To prevent spam, rate limit adding a new address, but do
185 // not rate limit removing an address.
186 if ( $newAddr !== '' ) {
187 // Enforce permissions, user blocks, and rate limits
188 $status = $this->authorizeAction( 'changeemail' );
189 if ( !$status->isGood() ) {
190 return Status::wrap( $status );
191 }
192 }
193
194 $userLatest = $user->getInstanceForUpdate();
195 $status = $userLatest->setEmailWithConfirmation( $newAddr );
196 if ( !$status->isGood() ) {
197 return $status;
198 }
199
200 LoggerFactory::getInstance( 'authentication' )->info(
201 'Changing email address for {user} from {oldemail} to {newemail}', [
202 'user' => $userLatest->getName(),
203 'oldemail' => $oldAddr,
204 'newemail' => $newAddr,
205 ]
206 );
207
208 $this->getHookRunner()->onPrefsEmailAudit( $userLatest, $oldAddr, $newAddr );
209
210 $userLatest->saveSettings();
211
212 return $status;
213 }
214
215 public function requiresUnblock() {
216 return false;
217 }
218
219 protected function getGroupName() {
220 return 'login';
221 }
222}
223
225class_alias( SpecialChangeEmail::class, 'SpecialChangeEmail' );
wfCgiToArray( $query)
This is the logical opposite of wfArrayToCgi(): it accepts a query string as its argument and returns...
This serves as the entry point to the authentication system.
An error page which can definitely be safely rendered using the OutputPage.
Show an error when a user tries to do something they do not have the necessary permissions for.
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition HTMLForm.php:209
setSubmitTextMsg( $msg)
Set the text for the submit button to a message.
setTableId( $id)
Set the id of the <table> or outermost <div> element.
addHiddenFields(array $fields)
Add an array of hidden fields to the output Array values are discarded for security reasons (per WebR...
setSubmitID( $t)
Set the id for the submit button.
addHeaderHtml( $html, $section=null)
Add HTML to the header, inside the form.
Definition HTMLForm.php:927
This class is a collection of static functions that serve two purposes:
Definition Html.php:57
Create PSR-3 logger objects.
HTML sanitizer for MediaWiki.
Definition Sanitizer.php:46
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.
authorizeAction(?string $action=null)
Utility function for authorizing an action to be performed by the special page.
requireNamedUser( $reasonMsg='exception-nologin-text', $titleMsg='exception-nologin', bool $alwaysRedirectToLoginPage=false)
If the user is not logged in or is a temporary user, throws UserNotLoggedIn.
setAuthManager(AuthManager $authManager)
Set the injected AuthManager from the special page constructor.
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.
getAuthority()
Shortcut to get the Authority executing this instance.
getName()
Get the canonical, unlocalized name of this special page without namespace.
Let users change their email address.
doesWrites()
Indicates whether POST requests to this special page require write access to the wiki.
onSuccess()
Do something exciting on successful processing of the form, most likely to show a confirmation messag...
getFormFields()
Get an HTMLForm descriptor array.
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
alterForm(HTMLForm $form)
Play with the HTMLForm if you need to more substantially.
checkExecutePermissions(User $user)
Called from execute() to check if the given user can perform this action.
getDisplayFormat()
Get display format for the form.
onSubmit(array $data)
Process the form on submission.
requiresUnblock()
Whether this action cannot be executed by a blocked user, default to requiresPost()
getLoginSecurityLevel()
Tells if the special page does something security-sensitive and needs extra defense against a stolen ...
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:54
Represents a title within MediaWiki.
Definition Title.php:78
User class for the MediaWiki software.
Definition User.php:121
getInstanceForUpdate()
Get a new instance of this user that was loaded from the primary DB via a locking read.
Definition User.php:3252
getEmail()
Get the user's e-mail address.
Definition User.php:1930
getName()
Get the user name, or the IP of an anonymous user.
Definition User.php:1612
isGood()
Returns whether the operation completed and didn't have any error or warnings.