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