Add countUnreadNotifications to WatchedItemStore
[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 'required' => true,
196 'size' => BotPassword::APPID_MAXLENGTH,
197 'maxlength' => BotPassword::APPID_MAXLENGTH,
198 'validation-callback' => function ( $v ) {
199 $v = trim( $v );
200 return $v !== '' && strlen( $v ) <= BotPassword::APPID_MAXLENGTH;
201 },
202 ];
203
204 $fields[] = [
205 'type' => 'hidden',
206 'default' => 'new',
207 'name' => 'op',
208 ];
209 }
210
211 return $fields;
212 }
213
214 protected function alterForm( HTMLForm $form ) {
215 $form->setId( 'mw-botpasswords-form' );
216 $form->setTableId( 'mw-botpasswords-table' );
217 $form->addPreText( $this->msg( 'botpasswords-summary' )->parseAsBlock() );
218 $form->suppressDefaultSubmit();
219
220 if ( $this->par !== null ) {
221 if ( $this->botPassword->isSaved() ) {
222 $form->setWrapperLegendMsg( 'botpasswords-editexisting' );
223 $form->addButton( [
224 'name' => 'op',
225 'value' => 'update',
226 'label-message' => 'botpasswords-label-update',
227 'flags' => [ 'primary', 'progressive' ],
228 ] );
229 $form->addButton( [
230 'name' => 'op',
231 'value' => 'delete',
232 'label-message' => 'botpasswords-label-delete',
233 'flags' => [ 'destructive' ],
234 ] );
235 } else {
236 $form->setWrapperLegendMsg( 'botpasswords-createnew' );
237 $form->addButton( [
238 'name' => 'op',
239 'value' => 'create',
240 'label-message' => 'botpasswords-label-create',
241 'flags' => [ 'primary', 'constructive' ],
242 ] );
243 }
244
245 $form->addButton( [
246 'name' => 'op',
247 'value' => 'cancel',
248 'label-message' => 'botpasswords-label-cancel'
249 ] );
250 }
251 }
252
253 public function onSubmit( array $data ) {
254 $op = $this->getRequest()->getVal( 'op', '' );
255
256 switch ( $op ) {
257 case 'new':
258 $this->getOutput()->redirect( $this->getPageTitle( $data['appId'] )->getFullURL() );
259 return false;
260
261 case 'create':
262 $this->operation = 'insert';
263 return $this->save( $data );
264
265 case 'update':
266 $this->operation = 'update';
267 return $this->save( $data );
268
269 case 'delete':
270 $this->operation = 'delete';
271 $bp = BotPassword::newFromCentralId( $this->userId, $this->par );
272 if ( $bp ) {
273 $bp->delete();
274 }
275 return Status::newGood();
276
277 case 'cancel':
278 $this->getOutput()->redirect( $this->getPageTitle()->getFullURL() );
279 return false;
280 }
281
282 return false;
283 }
284
285 private function save( array $data ) {
286 $bp = BotPassword::newUnsaved( [
287 'centralId' => $this->userId,
288 'appId' => $this->par,
289 'restrictions' => MWRestrictions::newFromJson( $data['restrictions'] ),
290 'grants' => array_merge(
291 MWGrants::getHiddenGrants(),
292 preg_replace( '/^grant-/', '', $data['grants'] )
293 )
294 ] );
295
296 if ( $this->operation === 'insert' || !empty( $data['resetPassword'] ) ) {
297 $this->password = PasswordFactory::generateRandomPasswordString(
298 max( 32, $this->getConfig()->get( 'MinimalPasswordLength' ) )
299 );
300 $passwordFactory = new PasswordFactory();
301 $passwordFactory->init( RequestContext::getMain()->getConfig() );
302 $password = $passwordFactory->newFromPlaintext( $this->password );
303 } else {
304 $password = null;
305 }
306
307 if ( $bp->save( $this->operation, $password ) ) {
308 return Status::newGood();
309 } else {
310 // Messages: botpasswords-insert-failed, botpasswords-update-failed
311 return Status::newFatal( "botpasswords-{$this->operation}-failed", $this->par );
312 }
313 }
314
315 public function onSuccess() {
316 $out = $this->getOutput();
317
318 switch ( $this->operation ) {
319 case 'insert':
320 $out->setPageTitle( $this->msg( 'botpasswords-created-title' )->text() );
321 $out->addWikiMsg( 'botpasswords-created-body', $this->par );
322 break;
323
324 case 'update':
325 $out->setPageTitle( $this->msg( 'botpasswords-updated-title' )->text() );
326 $out->addWikiMsg( 'botpasswords-updated-body', $this->par );
327 break;
328
329 case 'delete':
330 $out->setPageTitle( $this->msg( 'botpasswords-deleted-title' )->text() );
331 $out->addWikiMsg( 'botpasswords-deleted-body', $this->par );
332 $this->password = null;
333 break;
334 }
335
336 if ( $this->password !== null ) {
337 $sep = BotPassword::getSeparator();
338 $out->addWikiMsg(
339 'botpasswords-newpassword',
340 htmlspecialchars( $this->getUser()->getName() . $sep . $this->par ),
341 htmlspecialchars( $this->password )
342 );
343 $this->password = null;
344 }
345
346 $out->addReturnTo( $this->getPageTitle() );
347 }
348
349 protected function getGroupName() {
350 return 'users';
351 }
352
353 protected function getDisplayFormat() {
354 return 'ooui';
355 }
356 }