SECURITY: Special:BotPasswords should reauthenticate
[lhc/web/wiklou.git] / includes / specials / SpecialBotPasswords.php
1 <?php
2 /**
3 * Implements Special:BotPasswords
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 /**
25 * Let users manage bot passwords
26 *
27 * @ingroup SpecialPage
28 */
29 class SpecialBotPasswords extends FormSpecialPage {
30
31 /** @var int Central user ID */
32 private $userId = 0;
33
34 /** @var BotPassword|null Bot password being edited, if any */
35 private $botPassword = null;
36
37 /** @var string Operation being performed: create, update, delete */
38 private $operation = null;
39
40 /** @var string New password set, for communication between onSubmit() and onSuccess() */
41 private $password = null;
42
43 public function __construct() {
44 parent::__construct( 'BotPasswords', 'editmyprivateinfo' );
45 }
46
47 /**
48 * @return bool
49 */
50 public function isListed() {
51 return $this->getConfig()->get( 'EnableBotPasswords' );
52 }
53
54 protected function getLoginSecurityLevel() {
55 return $this->getName();
56 }
57
58 /**
59 * Main execution point
60 * @param string|null $par
61 */
62 function execute( $par ) {
63 $this->getOutput()->disallowUserJs();
64 $this->requireLogin();
65
66 $par = trim( $par );
67 if ( strlen( $par ) === 0 ) {
68 $par = null;
69 } elseif ( strlen( $par ) > BotPassword::APPID_MAXLENGTH ) {
70 throw new ErrorPageError( 'botpasswords', 'botpasswords-bad-appid',
71 [ htmlspecialchars( $par ) ] );
72 }
73
74 parent::execute( $par );
75 }
76
77 protected function checkExecutePermissions( User $user ) {
78 parent::checkExecutePermissions( $user );
79
80 if ( !$this->getConfig()->get( 'EnableBotPasswords' ) ) {
81 throw new ErrorPageError( 'botpasswords', 'botpasswords-disabled' );
82 }
83
84 $this->userId = CentralIdLookup::factory()->centralIdFromLocalUser( $this->getUser() );
85 if ( !$this->userId ) {
86 throw new ErrorPageError( 'botpasswords', 'botpasswords-no-central-id' );
87 }
88 }
89
90 protected function getFormFields() {
91 $fields = [];
92
93 if ( $this->par !== null ) {
94 $this->botPassword = BotPassword::newFromCentralId( $this->userId, $this->par );
95 if ( !$this->botPassword ) {
96 $this->botPassword = BotPassword::newUnsaved( [
97 'centralId' => $this->userId,
98 'appId' => $this->par,
99 ] );
100 }
101
102 $sep = BotPassword::getSeparator();
103 $fields[] = [
104 'type' => 'info',
105 'label-message' => 'username',
106 'default' => $this->getUser()->getName() . $sep . $this->par
107 ];
108
109 if ( $this->botPassword->isSaved() ) {
110 $fields['resetPassword'] = [
111 'type' => 'check',
112 'label-message' => 'botpasswords-label-resetpassword',
113 ];
114 }
115
116 $lang = $this->getLanguage();
117 $showGrants = MWGrants::getValidGrants();
118 $fields['grants'] = [
119 'type' => 'checkmatrix',
120 'label-message' => 'botpasswords-label-grants',
121 'help-message' => 'botpasswords-help-grants',
122 'columns' => [
123 $this->msg( 'botpasswords-label-grants-column' )->escaped() => 'grant'
124 ],
125 'rows' => array_combine(
126 array_map( 'MWGrants::getGrantsLink', $showGrants ),
127 $showGrants
128 ),
129 'default' => array_map(
130 function ( $g ) {
131 return "grant-$g";
132 },
133 $this->botPassword->getGrants()
134 ),
135 'tooltips' => array_combine(
136 array_map( 'MWGrants::getGrantsLink', $showGrants ),
137 array_map(
138 function ( $rights ) use ( $lang ) {
139 return $lang->semicolonList( array_map( 'User::getRightDescription', $rights ) );
140 },
141 array_intersect_key( MWGrants::getRightsByGrant(), array_flip( $showGrants ) )
142 )
143 ),
144 'force-options-on' => array_map(
145 function ( $g ) {
146 return "grant-$g";
147 },
148 MWGrants::getHiddenGrants()
149 ),
150 ];
151
152 $fields['restrictions'] = [
153 'class' => HTMLRestrictionsField::class,
154 'required' => true,
155 'default' => $this->botPassword->getRestrictions(),
156 ];
157
158 } else {
159 $linkRenderer = $this->getLinkRenderer();
160 $dbr = BotPassword::getDB( DB_REPLICA );
161 $res = $dbr->select(
162 'bot_passwords',
163 [ 'bp_app_id' ],
164 [ 'bp_user' => $this->userId ],
165 __METHOD__
166 );
167 foreach ( $res as $row ) {
168 $fields[] = [
169 'section' => 'existing',
170 'type' => 'info',
171 'raw' => true,
172 'default' => $linkRenderer->makeKnownLink(
173 $this->getPageTitle( $row->bp_app_id ),
174 $row->bp_app_id
175 ),
176 ];
177 }
178
179 $fields['appId'] = [
180 'section' => 'createnew',
181 'type' => 'textwithbutton',
182 'label-message' => 'botpasswords-label-appid',
183 'buttondefault' => $this->msg( 'botpasswords-label-create' )->text(),
184 'buttonflags' => [ 'progressive', 'primary' ],
185 'required' => true,
186 'size' => BotPassword::APPID_MAXLENGTH,
187 'maxlength' => BotPassword::APPID_MAXLENGTH,
188 'validation-callback' => function ( $v ) {
189 $v = trim( $v );
190 return $v !== '' && strlen( $v ) <= BotPassword::APPID_MAXLENGTH;
191 },
192 ];
193
194 $fields[] = [
195 'type' => 'hidden',
196 'default' => 'new',
197 'name' => 'op',
198 ];
199 }
200
201 return $fields;
202 }
203
204 protected function alterForm( HTMLForm $form ) {
205 $form->setId( 'mw-botpasswords-form' );
206 $form->setTableId( 'mw-botpasswords-table' );
207 $form->addPreText( $this->msg( 'botpasswords-summary' )->parseAsBlock() );
208 $form->suppressDefaultSubmit();
209
210 if ( $this->par !== null ) {
211 if ( $this->botPassword->isSaved() ) {
212 $form->setWrapperLegendMsg( 'botpasswords-editexisting' );
213 $form->addButton( [
214 'name' => 'op',
215 'value' => 'update',
216 'label-message' => 'botpasswords-label-update',
217 'flags' => [ 'primary', 'progressive' ],
218 ] );
219 $form->addButton( [
220 'name' => 'op',
221 'value' => 'delete',
222 'label-message' => 'botpasswords-label-delete',
223 'flags' => [ 'destructive' ],
224 ] );
225 } else {
226 $form->setWrapperLegendMsg( 'botpasswords-createnew' );
227 $form->addButton( [
228 'name' => 'op',
229 'value' => 'create',
230 'label-message' => 'botpasswords-label-create',
231 'flags' => [ 'primary', 'progressive' ],
232 ] );
233 }
234
235 $form->addButton( [
236 'name' => 'op',
237 'value' => 'cancel',
238 'label-message' => 'botpasswords-label-cancel'
239 ] );
240 }
241 }
242
243 public function onSubmit( array $data ) {
244 $op = $this->getRequest()->getVal( 'op', '' );
245
246 switch ( $op ) {
247 case 'new':
248 $this->getOutput()->redirect( $this->getPageTitle( $data['appId'] )->getFullURL() );
249 return false;
250
251 case 'create':
252 $this->operation = 'insert';
253 return $this->save( $data );
254
255 case 'update':
256 $this->operation = 'update';
257 return $this->save( $data );
258
259 case 'delete':
260 $this->operation = 'delete';
261 $bp = BotPassword::newFromCentralId( $this->userId, $this->par );
262 if ( $bp ) {
263 $bp->delete();
264 }
265 return Status::newGood();
266
267 case 'cancel':
268 $this->getOutput()->redirect( $this->getPageTitle()->getFullURL() );
269 return false;
270 }
271
272 return false;
273 }
274
275 private function save( array $data ) {
276 $bp = BotPassword::newUnsaved( [
277 'centralId' => $this->userId,
278 'appId' => $this->par,
279 'restrictions' => $data['restrictions'],
280 'grants' => array_merge(
281 MWGrants::getHiddenGrants(),
282 preg_replace( '/^grant-/', '', $data['grants'] )
283 )
284 ] );
285
286 if ( $this->operation === 'insert' || !empty( $data['resetPassword'] ) ) {
287 $this->password = BotPassword::generatePassword( $this->getConfig() );
288 $passwordFactory = new PasswordFactory();
289 $passwordFactory->init( RequestContext::getMain()->getConfig() );
290 $password = $passwordFactory->newFromPlaintext( $this->password );
291 } else {
292 $password = null;
293 }
294
295 if ( $bp->save( $this->operation, $password ) ) {
296 return Status::newGood();
297 } else {
298 // Messages: botpasswords-insert-failed, botpasswords-update-failed
299 return Status::newFatal( "botpasswords-{$this->operation}-failed", $this->par );
300 }
301 }
302
303 public function onSuccess() {
304 $out = $this->getOutput();
305
306 $username = $this->getUser()->getName();
307 switch ( $this->operation ) {
308 case 'insert':
309 $out->setPageTitle( $this->msg( 'botpasswords-created-title' )->text() );
310 $out->addWikiMsg( 'botpasswords-created-body', $this->par, $username );
311 break;
312
313 case 'update':
314 $out->setPageTitle( $this->msg( 'botpasswords-updated-title' )->text() );
315 $out->addWikiMsg( 'botpasswords-updated-body', $this->par, $username );
316 break;
317
318 case 'delete':
319 $out->setPageTitle( $this->msg( 'botpasswords-deleted-title' )->text() );
320 $out->addWikiMsg( 'botpasswords-deleted-body', $this->par, $username );
321 $this->password = null;
322 break;
323 }
324
325 if ( $this->password !== null ) {
326 $sep = BotPassword::getSeparator();
327 $out->addWikiMsg(
328 'botpasswords-newpassword',
329 htmlspecialchars( $username . $sep . $this->par ),
330 htmlspecialchars( $this->password ),
331 htmlspecialchars( $username ),
332 htmlspecialchars( $this->par . $sep . $this->password )
333 );
334 $this->password = null;
335 }
336
337 $out->addReturnTo( $this->getPageTitle() );
338 }
339
340 protected function getGroupName() {
341 return 'users';
342 }
343
344 protected function getDisplayFormat() {
345 return 'ooui';
346 }
347 }