SpecialChangeEmail.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. /**
  3. * Implements Special:ChangeEmail
  4. *
  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.
  9. *
  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.
  14. *
  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
  19. *
  20. * @file
  21. * @ingroup SpecialPage
  22. */
  23. use MediaWiki\Auth\AuthManager;
  24. use MediaWiki\Logger\LoggerFactory;
  25. use MediaWiki\MediaWikiServices;
  26. /**
  27. * Let users change their email address.
  28. *
  29. * @ingroup SpecialPage
  30. */
  31. class SpecialChangeEmail extends FormSpecialPage {
  32. /**
  33. * @var Status
  34. */
  35. private $status;
  36. public function __construct() {
  37. parent::__construct( 'ChangeEmail', 'editmyprivateinfo' );
  38. }
  39. public function doesWrites() {
  40. return true;
  41. }
  42. /**
  43. * @return bool
  44. */
  45. public function isListed() {
  46. return AuthManager::singleton()->allowsPropertyChange( 'emailaddress' );
  47. }
  48. /**
  49. * Main execution point
  50. * @param string $par
  51. */
  52. function execute( $par ) {
  53. $out = $this->getOutput();
  54. $out->disallowUserJs();
  55. parent::execute( $par );
  56. }
  57. protected function getLoginSecurityLevel() {
  58. return $this->getName();
  59. }
  60. protected function checkExecutePermissions( User $user ) {
  61. if ( !AuthManager::singleton()->allowsPropertyChange( 'emailaddress' ) ) {
  62. throw new ErrorPageError( 'changeemail', 'cannotchangeemail' );
  63. }
  64. $this->requireLogin( 'changeemail-no-info' );
  65. // This could also let someone check the current email address, so
  66. // require both permissions.
  67. if ( !MediaWikiServices::getInstance()
  68. ->getPermissionManager()
  69. ->userHasRight( $this->getUser(), 'viewmyprivateinfo' )
  70. ) {
  71. throw new PermissionsError( 'viewmyprivateinfo' );
  72. }
  73. parent::checkExecutePermissions( $user );
  74. }
  75. protected function getFormFields() {
  76. $user = $this->getUser();
  77. $fields = [
  78. 'Name' => [
  79. 'type' => 'info',
  80. 'label-message' => 'username',
  81. 'default' => $user->getName(),
  82. ],
  83. 'OldEmail' => [
  84. 'type' => 'info',
  85. 'label-message' => 'changeemail-oldemail',
  86. 'default' => $user->getEmail() ?: $this->msg( 'changeemail-none' )->text(),
  87. ],
  88. 'NewEmail' => [
  89. 'type' => 'email',
  90. 'label-message' => 'changeemail-newemail',
  91. 'autofocus' => true,
  92. 'help-message' => 'changeemail-newemail-help',
  93. ],
  94. ];
  95. return $fields;
  96. }
  97. protected function getDisplayFormat() {
  98. return 'ooui';
  99. }
  100. protected function alterForm( HTMLForm $form ) {
  101. $form->setId( 'mw-changeemail-form' );
  102. $form->setTableId( 'mw-changeemail-table' );
  103. $form->setSubmitTextMsg( 'changeemail-submit' );
  104. $form->addHiddenFields( $this->getRequest()->getValues( 'returnto', 'returntoquery' ) );
  105. $form->addHeaderText( $this->msg( 'changeemail-header' )->parseAsBlock() );
  106. }
  107. public function onSubmit( array $data ) {
  108. $status = $this->attemptChange( $this->getUser(), $data['NewEmail'] );
  109. $this->status = $status;
  110. return $status;
  111. }
  112. public function onSuccess() {
  113. $request = $this->getRequest();
  114. $returnto = $request->getVal( 'returnto' );
  115. $titleObj = $returnto !== null ? Title::newFromText( $returnto ) : null;
  116. if ( !$titleObj instanceof Title ) {
  117. $titleObj = Title::newMainPage();
  118. }
  119. $query = $request->getVal( 'returntoquery' );
  120. if ( $this->status->value === true ) {
  121. $this->getOutput()->redirect( $titleObj->getFullUrlForRedirect( $query ) );
  122. } elseif ( $this->status->value === 'eauth' ) {
  123. # Notify user that a confirmation email has been sent...
  124. $this->getOutput()->wrapWikiMsg( "<div class='error' style='clear: both;'>\n$1\n</div>",
  125. 'eauthentsent', $this->getUser()->getName() );
  126. // just show the link to go back
  127. $this->getOutput()->addReturnTo( $titleObj, wfCgiToArray( $query ) );
  128. }
  129. }
  130. /**
  131. * @param User $user
  132. * @param string $newaddr
  133. * @return Status
  134. */
  135. private function attemptChange( User $user, $newaddr ) {
  136. if ( $newaddr != '' && !Sanitizer::validateEmail( $newaddr ) ) {
  137. return Status::newFatal( 'invalidemailaddress' );
  138. }
  139. if ( $newaddr === $user->getEmail() ) {
  140. return Status::newFatal( 'changeemail-nochange' );
  141. }
  142. // To prevent spam, rate limit adding a new address, but do
  143. // not rate limit removing an address.
  144. if ( $newaddr !== '' && $user->pingLimiter( 'changeemail' ) ) {
  145. return Status::newFatal( 'actionthrottledtext' );
  146. }
  147. $oldaddr = $user->getEmail();
  148. $status = $user->setEmailWithConfirmation( $newaddr );
  149. if ( !$status->isGood() ) {
  150. return $status;
  151. }
  152. LoggerFactory::getInstance( 'authentication' )->info(
  153. 'Changing email address for {user} from {oldemail} to {newemail}', [
  154. 'user' => $user->getName(),
  155. 'oldemail' => $oldaddr,
  156. 'newemail' => $newaddr,
  157. ]
  158. );
  159. Hooks::run( 'PrefsEmailAudit', [ $user, $oldaddr, $newaddr ] );
  160. $user->saveSettings();
  161. return $status;
  162. }
  163. public function requiresUnblock() {
  164. return false;
  165. }
  166. protected function getGroupName() {
  167. return 'users';
  168. }
  169. }