3 * Implements Special:ChangeEmail
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\Logger\LoggerFactory
;
28 * Let users change their email address.
30 * @ingroup SpecialPage
32 class SpecialChangeEmail
extends FormSpecialPage
{
38 public function __construct() {
39 parent
::__construct( 'ChangeEmail', 'editmyprivateinfo' );
42 public function doesWrites() {
49 public function isListed() {
50 return AuthManager
::singleton()->allowsPropertyChange( 'emailaddress' );
54 * Main execution point
57 function execute( $par ) {
58 $out = $this->getOutput();
59 $out->disallowUserJs();
61 parent
::execute( $par );
64 protected function getLoginSecurityLevel() {
65 return $this->getName();
68 protected function checkExecutePermissions( User
$user ) {
69 if ( !AuthManager
::singleton()->allowsPropertyChange( 'emailaddress' ) ) {
70 throw new ErrorPageError( 'changeemail', 'cannotchangeemail' );
73 $this->requireLogin( 'changeemail-no-info' );
75 // This could also let someone check the current email address, so
76 // require both permissions.
77 if ( !$this->getUser()->isAllowed( 'viewmyprivateinfo' ) ) {
78 throw new PermissionsError( 'viewmyprivateinfo' );
81 if ( $user->isBlockedFromEmailuser() ) {
82 throw new UserBlockedError( $user->getBlock() );
85 parent
::checkExecutePermissions( $user );
88 protected function getFormFields() {
89 $user = $this->getUser();
94 'label-message' => 'username',
95 'default' => $user->getName(),
99 'label-message' => 'changeemail-oldemail',
100 'default' => $user->getEmail() ?
: $this->msg( 'changeemail-none' )->text(),
104 'label-message' => 'changeemail-newemail',
106 'help-message' => 'changeemail-newemail-help',
113 protected function getDisplayFormat() {
117 protected function alterForm( HTMLForm
$form ) {
118 $form->setId( 'mw-changeemail-form' );
119 $form->setTableId( 'mw-changeemail-table' );
120 $form->setSubmitTextMsg( 'changeemail-submit' );
121 $form->addHiddenFields( $this->getRequest()->getValues( 'returnto', 'returntoquery' ) );
123 $form->addHeaderText( $this->msg( 'changeemail-header' )->parseAsBlock() );
126 public function onSubmit( array $data ) {
127 $status = $this->attemptChange( $this->getUser(), $data['NewEmail'] );
129 $this->status
= $status;
134 public function onSuccess() {
135 $request = $this->getRequest();
137 $returnto = $request->getVal( 'returnto' );
138 $titleObj = $returnto !== null ? Title
::newFromText( $returnto ) : null;
139 if ( !$titleObj instanceof Title
) {
140 $titleObj = Title
::newMainPage();
142 $query = $request->getVal( 'returntoquery' );
144 if ( $this->status
->value
=== true ) {
145 $this->getOutput()->redirect( $titleObj->getFullUrlForRedirect( $query ) );
146 } elseif ( $this->status
->value
=== 'eauth' ) {
147 # Notify user that a confirmation email has been sent...
148 $this->getOutput()->wrapWikiMsg( "<div class='error' style='clear: both;'>\n$1\n</div>",
149 'eauthentsent', $this->getUser()->getName() );
150 // just show the link to go back
151 $this->getOutput()->addReturnTo( $titleObj, wfCgiToArray( $query ) );
157 * @param string $newaddr
160 private function attemptChange( User
$user, $newaddr ) {
161 if ( $newaddr != '' && !Sanitizer
::validateEmail( $newaddr ) ) {
162 return Status
::newFatal( 'invalidemailaddress' );
165 if ( $newaddr === $user->getEmail() ) {
166 return Status
::newFatal( 'changeemail-nochange' );
169 // To prevent spam, rate limit adding a new address, but do
170 // not rate limit removing an address.
171 if ( $newaddr !== '' && $user->pingLimiter( 'changeemail' ) ) {
172 return Status
::newFatal( 'actionthrottledtext' );
175 $oldaddr = $user->getEmail();
176 $status = $user->setEmailWithConfirmation( $newaddr );
177 if ( !$status->isGood() ) {
181 LoggerFactory
::getInstance( 'authentication' )->info(
182 'Changing email address for {user} from {oldemail} to {newemail}', [
183 'user' => $user->getName(),
184 'oldemail' => $oldaddr,
185 'newemail' => $newaddr,
189 Hooks
::run( 'PrefsEmailAudit', [ $user, $oldaddr, $newaddr ] );
191 $user->saveSettings();
196 public function requiresUnblock() {
200 protected function getGroupName() {