rdbms: Avoid selectDB() call in LoadMonitor new connections
[mediawiki.git] / includes / specials / SpecialChangeEmail.php
blobc5143002c3f8c20f4f00e3275b97509931b55829
1 <?php
2 /**
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
20 * @file
21 * @ingroup SpecialPage
24 use MediaWiki\Auth\AuthManager;
26 /**
27 * Let users change their email address.
29 * @ingroup SpecialPage
31 class SpecialChangeEmail extends FormSpecialPage {
32 /**
33 * @var Status
35 private $status;
37 public function __construct() {
38 parent::__construct( 'ChangeEmail', 'editmyprivateinfo' );
41 public function doesWrites() {
42 return true;
45 /**
46 * @return bool
48 public function isListed() {
49 return AuthManager::singleton()->allowsPropertyChange( 'emailaddress' );
52 /**
53 * Main execution point
54 * @param string $par
56 function execute( $par ) {
57 $this->checkLoginSecurityLevel();
59 $out = $this->getOutput();
60 $out->disallowUserJs();
62 parent::execute( $par );
65 protected function checkExecutePermissions( User $user ) {
66 if ( !AuthManager::singleton()->allowsPropertyChange( 'emailaddress' ) ) {
67 throw new ErrorPageError( 'changeemail', 'cannotchangeemail' );
70 $this->requireLogin( 'changeemail-no-info' );
72 // This could also let someone check the current email address, so
73 // require both permissions.
74 if ( !$this->getUser()->isAllowed( 'viewmyprivateinfo' ) ) {
75 throw new PermissionsError( 'viewmyprivateinfo' );
78 parent::checkExecutePermissions( $user );
81 protected function getFormFields() {
82 $user = $this->getUser();
84 $fields = [
85 'Name' => [
86 'type' => 'info',
87 'label-message' => 'username',
88 'default' => $user->getName(),
90 'OldEmail' => [
91 'type' => 'info',
92 'label-message' => 'changeemail-oldemail',
93 'default' => $user->getEmail() ?: $this->msg( 'changeemail-none' )->text(),
95 'NewEmail' => [
96 'type' => 'email',
97 'label-message' => 'changeemail-newemail',
98 'autofocus' => true,
99 'help-message' => 'changeemail-newemail-help',
103 return $fields;
106 protected function getDisplayFormat() {
107 return 'ooui';
110 protected function alterForm( HTMLForm $form ) {
111 $form->setId( 'mw-changeemail-form' );
112 $form->setTableId( 'mw-changeemail-table' );
113 $form->setSubmitTextMsg( 'changeemail-submit' );
114 $form->addHiddenFields( $this->getRequest()->getValues( 'returnto', 'returntoquery' ) );
116 $form->addHeaderText( $this->msg( 'changeemail-header' )->parseAsBlock() );
119 public function onSubmit( array $data ) {
120 $status = $this->attemptChange( $this->getUser(), $data['NewEmail'] );
122 $this->status = $status;
124 return $status;
127 public function onSuccess() {
128 $request = $this->getRequest();
130 $returnto = $request->getVal( 'returnto' );
131 $titleObj = $returnto !== null ? Title::newFromText( $returnto ) : null;
132 if ( !$titleObj instanceof Title ) {
133 $titleObj = Title::newMainPage();
135 $query = $request->getVal( 'returntoquery' );
137 if ( $this->status->value === true ) {
138 $this->getOutput()->redirect( $titleObj->getFullUrlForRedirect( $query ) );
139 } elseif ( $this->status->value === 'eauth' ) {
140 # Notify user that a confirmation email has been sent...
141 $this->getOutput()->wrapWikiMsg( "<div class='error' style='clear: both;'>\n$1\n</div>",
142 'eauthentsent', $this->getUser()->getName() );
143 // just show the link to go back
144 $this->getOutput()->addReturnTo( $titleObj, wfCgiToArray( $query ) );
149 * @param User $user
150 * @param string $newaddr
151 * @return Status
153 private function attemptChange( User $user, $newaddr ) {
154 $authManager = AuthManager::singleton();
156 if ( $newaddr != '' && !Sanitizer::validateEmail( $newaddr ) ) {
157 return Status::newFatal( 'invalidemailaddress' );
160 if ( $newaddr === $user->getEmail() ) {
161 return Status::newFatal( 'changeemail-nochange' );
164 $oldaddr = $user->getEmail();
165 $status = $user->setEmailWithConfirmation( $newaddr );
166 if ( !$status->isGood() ) {
167 return $status;
170 Hooks::run( 'PrefsEmailAudit', [ $user, $oldaddr, $newaddr ] );
172 $user->saveSettings();
173 MediaWiki\Auth\AuthManager::callLegacyAuthPlugin( 'updateExternalDB', [ $user ] );
175 return $status;
178 public function requiresUnblock() {
179 return false;
182 protected function getGroupName() {
183 return 'users';