AuthManager: Break AuthPlugin::addUser more explicitly
[mediawiki.git] / includes / specials / pre-authmanager / SpecialChangeEmail.php
blob786156228bab3d8bc616c5409a1cb498b0566eef
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 /**
25 * Let users change their email address.
27 * @ingroup SpecialPage
29 class SpecialChangeEmailPreAuthManager extends FormSpecialPage {
30 /**
31 * @var Status
33 private $status;
35 public function __construct() {
36 parent::__construct( 'ChangeEmail', 'editmyprivateinfo' );
39 public function doesWrites() {
40 return true;
43 /**
44 * @return bool
46 public function isListed() {
47 global $wgAuth;
49 return $wgAuth->allowPropChange( 'emailaddress' );
52 /**
53 * Main execution point
54 * @param string $par
56 function execute( $par ) {
57 $out = $this->getOutput();
58 $out->disallowUserJs();
60 parent::execute( $par );
63 protected function checkExecutePermissions( User $user ) {
64 global $wgAuth;
66 if ( !$wgAuth->allowPropChange( '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 if ( $this->getConfig()->get( 'RequirePasswordforEmailChange' ) ) {
104 $fields['Password'] = [
105 'type' => 'password',
106 'label-message' => 'changeemail-password'
110 return $fields;
113 protected function getDisplayFormat() {
114 return 'ooui';
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() );
124 if ( $this->getConfig()->get( 'RequirePasswordforEmailChange' ) ) {
125 $form->addHeaderText( $this->msg( 'changeemail-passwordrequired' )->parseAsBlock() );
129 public function onSubmit( array $data ) {
130 $password = isset( $data['Password'] ) ? $data['Password'] : null;
131 $status = $this->attemptChange( $this->getUser(), $password, $data['NewEmail'] );
133 $this->status = $status;
135 return $status;
138 public function onSuccess() {
139 $request = $this->getRequest();
141 $returnto = $request->getVal( 'returnto' );
142 $titleObj = $returnto !== null ? Title::newFromText( $returnto ) : null;
143 if ( !$titleObj instanceof Title ) {
144 $titleObj = Title::newMainPage();
146 $query = $request->getVal( 'returntoquery' );
148 if ( $this->status->value === true ) {
149 $this->getOutput()->redirect( $titleObj->getFullURL( $query ) );
150 } elseif ( $this->status->value === 'eauth' ) {
151 # Notify user that a confirmation email has been sent...
152 $this->getOutput()->wrapWikiMsg( "<div class='error' style='clear: both;'>\n$1\n</div>",
153 'eauthentsent', $this->getUser()->getName() );
154 // just show the link to go back
155 $this->getOutput()->addReturnTo( $titleObj, wfCgiToArray( $query ) );
160 * @param User $user
161 * @param string $pass
162 * @param string $newaddr
163 * @return Status
165 private function attemptChange( User $user, $pass, $newaddr ) {
166 global $wgAuth;
168 if ( $newaddr != '' && !Sanitizer::validateEmail( $newaddr ) ) {
169 return Status::newFatal( 'invalidemailaddress' );
172 if ( $newaddr === $user->getEmail() ) {
173 return Status::newFatal( 'changeemail-nochange' );
176 $throttleInfo = LoginForm::incrementLoginThrottle( $user->getName() );
177 if ( $throttleInfo ) {
178 $lang = $this->getLanguage();
179 return Status::newFatal(
180 'changeemail-throttled',
181 $lang->formatDuration( $throttleInfo['wait'] )
185 if ( $this->getConfig()->get( 'RequirePasswordforEmailChange' )
186 && !$user->checkTemporaryPassword( $pass )
187 && !$user->checkPassword( $pass )
189 return Status::newFatal( 'wrongpassword' );
192 LoginForm::clearLoginThrottle( $user->getName() );
194 $oldaddr = $user->getEmail();
195 $status = $user->setEmailWithConfirmation( $newaddr );
196 if ( !$status->isGood() ) {
197 return $status;
200 Hooks::run( 'PrefsEmailAudit', [ $user, $oldaddr, $newaddr ] );
202 $user->saveSettings();
204 $wgAuth->updateExternalDB( $user );
206 return $status;
209 public function requiresUnblock() {
210 return false;
213 protected function getGroupName() {
214 return 'users';