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