SECURITY: Do not allow user scripts on Special:PasswordReset
[lhc/web/wiklou.git] / includes / specials / SpecialPasswordReset.php
1 <?php
2 /**
3 * Implements Special:PasswordReset
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\MediaWikiServices;
25
26 /**
27 * Special page for requesting a password reset email.
28 *
29 * Requires the TemporaryPasswordPrimaryAuthenticationProvider and the
30 * EmailNotificationSecondaryAuthenticationProvider (or something providing equivalent
31 * functionality) to be enabled.
32 *
33 * @ingroup SpecialPage
34 */
35 class SpecialPasswordReset extends FormSpecialPage {
36 /** @var PasswordReset */
37 private $passwordReset = null;
38
39 /**
40 * @var Status
41 */
42 private $result;
43
44 /**
45 * @var string $method Identifies which password reset field was specified by the user.
46 */
47 private $method;
48
49 public function __construct() {
50 parent::__construct( 'PasswordReset', 'editmyprivateinfo' );
51 }
52
53 private function getPasswordReset() {
54 if ( $this->passwordReset === null ) {
55 $this->passwordReset = MediaWikiServices::getInstance()->getPasswordReset();
56 }
57 return $this->passwordReset;
58 }
59
60 public function doesWrites() {
61 return true;
62 }
63
64 public function userCanExecute( User $user ) {
65 return $this->getPasswordReset()->isAllowed( $user )->isGood();
66 }
67
68 public function checkExecutePermissions( User $user ) {
69 $status = Status::wrap( $this->getPasswordReset()->isAllowed( $user ) );
70 if ( !$status->isGood() ) {
71 throw new ErrorPageError( 'internalerror', $status->getMessage() );
72 }
73
74 parent::checkExecutePermissions( $user );
75 }
76
77 /**
78 * @param string $par
79 */
80 public function execute( $par ) {
81 $out = $this->getOutput();
82 $out->disallowUserJs();
83 parent::execute( $par );
84 }
85
86 protected function getFormFields() {
87 $resetRoutes = $this->getConfig()->get( 'PasswordResetRoutes' );
88 $a = [];
89 if ( isset( $resetRoutes['username'] ) && $resetRoutes['username'] ) {
90 $a['Username'] = [
91 'type' => 'text',
92 'default' => $this->getRequest()->getSession()->suggestLoginUsername(),
93 'label-message' => 'passwordreset-username',
94 ];
95
96 if ( $this->getUser()->isLoggedIn() ) {
97 $a['Username']['default'] = $this->getUser()->getName();
98 }
99 }
100
101 if ( isset( $resetRoutes['email'] ) && $resetRoutes['email'] ) {
102 $a['Email'] = [
103 'type' => 'email',
104 'label-message' => 'passwordreset-email',
105 ];
106 }
107
108 return $a;
109 }
110
111 protected function getDisplayFormat() {
112 return 'ooui';
113 }
114
115 public function alterForm( HTMLForm $form ) {
116 $resetRoutes = $this->getConfig()->get( 'PasswordResetRoutes' );
117
118 $form->setSubmitDestructive();
119
120 $form->addHiddenFields( $this->getRequest()->getValues( 'returnto', 'returntoquery' ) );
121
122 $i = 0;
123 if ( isset( $resetRoutes['username'] ) && $resetRoutes['username'] ) {
124 $i++;
125 }
126 if ( isset( $resetRoutes['email'] ) && $resetRoutes['email'] ) {
127 $i++;
128 }
129
130 $message = ( $i > 1 ) ? 'passwordreset-text-many' : 'passwordreset-text-one';
131
132 $form->setHeaderText( $this->msg( $message, $i )->parseAsBlock() );
133 $form->setSubmitTextMsg( 'mailmypassword' );
134 }
135
136 /**
137 * Process the form. At this point we know that the user passes all the criteria in
138 * userCanExecute(), and if the data array contains 'Username', etc, then Username
139 * resets are allowed.
140 * @param array $data
141 * @throws MWException
142 * @throws ThrottledError|PermissionsError
143 * @return Status
144 */
145 public function onSubmit( array $data ) {
146 $username = $data['Username'] ?? null;
147 $email = $data['Email'] ?? null;
148
149 $this->method = $username ? 'username' : 'email';
150 $this->result = Status::wrap(
151 $this->getPasswordReset()->execute( $this->getUser(), $username, $email ) );
152
153 if ( $this->result->hasMessage( 'actionthrottledtext' ) ) {
154 throw new ThrottledError;
155 }
156
157 return $this->result;
158 }
159
160 public function onSuccess() {
161 if ( $this->method === 'email' ) {
162 $this->getOutput()->addWikiMsg( 'passwordreset-emailsentemail' );
163 } else {
164 $this->getOutput()->addWikiMsg( 'passwordreset-emailsentusername' );
165 }
166
167 $this->getOutput()->returnToMain();
168 }
169
170 /**
171 * Hide the password reset page if resets are disabled.
172 * @return bool
173 */
174 public function isListed() {
175 if ( $this->getPasswordReset()->isAllowed( $this->getUser() )->isGood() ) {
176 return parent::isListed();
177 }
178
179 return false;
180 }
181
182 protected function getGroupName() {
183 return 'users';
184 }
185 }