3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
21 namespace MediaWiki\Specials
;
24 use MediaWiki\Auth\AuthManager
;
25 use MediaWiki\Html\Html
;
26 use MediaWiki\HTMLForm\HTMLForm
;
27 use MediaWiki\Logger\LoggerFactory
;
28 use MediaWiki\Parser\Sanitizer
;
29 use MediaWiki\SpecialPage\FormSpecialPage
;
30 use MediaWiki\Status\Status
;
31 use MediaWiki\Title\Title
;
32 use MediaWiki\User\User
;
36 * Let users change their email address.
38 * @ingroup SpecialPage
40 class SpecialChangeEmail
extends FormSpecialPage
{
47 * @param AuthManager $authManager
49 public function __construct( AuthManager
$authManager ) {
50 parent
::__construct( 'ChangeEmail', 'editmyprivateinfo' );
52 $this->setAuthManager( $authManager );
55 public function doesWrites() {
62 public function isListed() {
63 return $this->getAuthManager()->allowsPropertyChange( 'emailaddress' );
67 * Main execution point
68 * @param string|null $par
70 public function execute( $par ) {
71 $out = $this->getOutput();
72 $out->disallowUserJs();
73 $out->addModules( 'mediawiki.special.changeemail' );
74 parent
::execute( $par );
77 protected function getLoginSecurityLevel() {
78 return $this->getName();
81 protected function checkExecutePermissions( User
$user ) {
82 if ( !$this->getAuthManager()->allowsPropertyChange( 'emailaddress' ) ) {
83 throw new ErrorPageError( 'changeemail', 'cannotchangeemail' );
86 $this->requireNamedUser( 'changeemail-no-info', 'exception-nologin', true );
88 // This could also let someone check the current email address, so
89 // require both permissions.
90 if ( !$this->getAuthority()->isAllowed( 'viewmyprivateinfo' ) ) {
91 throw new PermissionsError( 'viewmyprivateinfo' );
94 parent
::checkExecutePermissions( $user );
97 protected function getFormFields() {
98 $user = $this->getUser();
103 'label-message' => 'username',
104 'default' => $user->getName(),
108 'label-message' => 'changeemail-oldemail',
109 'default' => $user->getEmail() ?
: $this->msg( 'changeemail-none' )->text(),
113 'label-message' => 'changeemail-newemail',
116 'help-message' => 'changeemail-newemail-help',
121 protected function getDisplayFormat() {
125 protected function alterForm( HTMLForm
$form ) {
126 $form->setId( 'mw-changeemail-form' );
127 $form->setTableId( 'mw-changeemail-table' );
128 $form->setSubmitTextMsg( 'changeemail-submit' );
129 $form->addHiddenFields( $this->getRequest()->getValues( 'returnto', 'returntoquery' ) );
131 $form->addHeaderHtml( $this->msg( 'changeemail-header' )->parseAsBlock() );
132 $form->setSubmitID( 'change_email_submit' );
135 public function onSubmit( array $data ) {
136 $this->status
= $this->attemptChange( $this->getUser(), $data['NewEmail'] );
138 return $this->status
;
141 public function onSuccess() {
142 $request = $this->getRequest();
144 $returnTo = $request->getVal( 'returnto' );
145 $titleObj = $returnTo !== null ? Title
::newFromText( $returnTo ) : null;
146 if ( !$titleObj instanceof Title
) {
147 $titleObj = Title
::newMainPage();
149 $query = $request->getVal( 'returntoquery', '' );
151 if ( $this->status
->value
=== true ) {
152 $this->getOutput()->redirect( $titleObj->getFullUrlForRedirect( $query ) );
153 } elseif ( $this->status
->value
=== 'eauth' ) {
154 # Notify user that a confirmation email has been sent...
155 $out = $this->getOutput();
158 $out->msg( 'eauthentsent', $this->getUser()->getName() )->parse()
161 // just show the link to go back
162 $this->getOutput()->addReturnTo( $titleObj, wfCgiToArray( $query ) );
168 * @param string $newAddr
172 private function attemptChange( User
$user, $newAddr ) {
173 if ( $newAddr !== '' && !Sanitizer
::validateEmail( $newAddr ) ) {
174 return Status
::newFatal( 'invalidemailaddress' );
177 $oldAddr = $user->getEmail();
178 if ( $newAddr === $oldAddr ) {
179 return Status
::newFatal( 'changeemail-nochange' );
182 if ( strlen( $newAddr ) > 255 ) {
183 return Status
::newFatal( 'changeemail-maxlength' );
186 // To prevent spam, rate limit adding a new address, but do
187 // not rate limit removing an address.
188 if ( $newAddr !== '' ) {
189 // Enforce permissions, user blocks, and rate limits
190 $status = $this->authorizeAction( 'changeemail' );
191 if ( !$status->isGood() ) {
192 return Status
::wrap( $status );
196 $userLatest = $user->getInstanceForUpdate();
197 $status = $userLatest->setEmailWithConfirmation( $newAddr );
198 if ( !$status->isGood() ) {
202 LoggerFactory
::getInstance( 'authentication' )->info(
203 'Changing email address for {user} from {oldemail} to {newemail}', [
204 'user' => $userLatest->getName(),
205 'oldemail' => $oldAddr,
206 'newemail' => $newAddr,
210 $this->getHookRunner()->onPrefsEmailAudit( $userLatest, $oldAddr, $newAddr );
212 $userLatest->saveSettings();
217 public function requiresUnblock() {
221 protected function getGroupName() {
226 /** @deprecated class alias since 1.41 */
227 class_alias( SpecialChangeEmail
::class, 'SpecialChangeEmail' );