Merge "Use {{int:}} on MediaWiki:Blockedtext and MediaWiki:Autoblockedtext"
[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\Auth\AuthManager;
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 = new PasswordReset( $this->getConfig(), AuthManager::singleton() );
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 protected function getFormFields() {
78 $resetRoutes = $this->getConfig()->get( 'PasswordResetRoutes' );
79 $a = [];
80 if ( isset( $resetRoutes['username'] ) && $resetRoutes['username'] ) {
81 $a['Username'] = [
82 'type' => 'text',
83 'label-message' => 'passwordreset-username',
84 ];
85
86 if ( $this->getUser()->isLoggedIn() ) {
87 $a['Username']['default'] = $this->getUser()->getName();
88 }
89 }
90
91 if ( isset( $resetRoutes['email'] ) && $resetRoutes['email'] ) {
92 $a['Email'] = [
93 'type' => 'email',
94 'label-message' => 'passwordreset-email',
95 ];
96 }
97
98 return $a;
99 }
100
101 protected function getDisplayFormat() {
102 return 'ooui';
103 }
104
105 public function alterForm( HTMLForm $form ) {
106 $resetRoutes = $this->getConfig()->get( 'PasswordResetRoutes' );
107
108 $form->setSubmitDestructive();
109
110 $form->addHiddenFields( $this->getRequest()->getValues( 'returnto', 'returntoquery' ) );
111
112 $i = 0;
113 if ( isset( $resetRoutes['username'] ) && $resetRoutes['username'] ) {
114 $i++;
115 }
116 if ( isset( $resetRoutes['email'] ) && $resetRoutes['email'] ) {
117 $i++;
118 }
119
120 $message = ( $i > 1 ) ? 'passwordreset-text-many' : 'passwordreset-text-one';
121
122 $form->setHeaderText( $this->msg( $message, $i )->parseAsBlock() );
123 $form->setSubmitTextMsg( 'mailmypassword' );
124 }
125
126 /**
127 * Process the form. At this point we know that the user passes all the criteria in
128 * userCanExecute(), and if the data array contains 'Username', etc, then Username
129 * resets are allowed.
130 * @param array $data
131 * @throws MWException
132 * @throws ThrottledError|PermissionsError
133 * @return Status
134 */
135 public function onSubmit( array $data ) {
136 $username = isset( $data['Username'] ) ? $data['Username'] : null;
137 $email = isset( $data['Email'] ) ? $data['Email'] : null;
138
139 $this->method = $username ? 'username' : 'email';
140 $this->result = Status::wrap(
141 $this->getPasswordReset()->execute( $this->getUser(), $username, $email ) );
142
143 if ( $this->result->hasMessage( 'actionthrottledtext' ) ) {
144 throw new ThrottledError;
145 }
146
147 return $this->result;
148 }
149
150 public function onSuccess() {
151 if ( $this->method === 'email' ) {
152 $this->getOutput()->addWikiMsg( 'passwordreset-emailsentemail' );
153 } else {
154 $this->getOutput()->addWikiMsg( 'passwordreset-emailsentusername' );
155 }
156
157 $this->getOutput()->returnToMain();
158 }
159
160 /**
161 * Hide the password reset page if resets are disabled.
162 * @return bool
163 */
164 public function isListed() {
165 if ( $this->getPasswordReset()->isAllowed( $this->getUser() )->isGood() ) {
166 return parent::isListed();
167 }
168
169 return false;
170 }
171
172 protected function getGroupName() {
173 return 'users';
174 }
175 }