3 * Implements Special:BotPasswords
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.
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.
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
21 * @ingroup SpecialPage
24 use MediaWiki\Logger\LoggerFactory
;
27 * Let users manage bot passwords
29 * @ingroup SpecialPage
31 class SpecialBotPasswords
extends FormSpecialPage
{
33 /** @var int Central user ID */
36 /** @var BotPassword|null Bot password being edited, if any */
37 private $botPassword = null;
39 /** @var string Operation being performed: create, update, delete */
40 private $operation = null;
42 /** @var string New password set, for communication between onSubmit() and onSuccess() */
43 private $password = null;
45 /** @var Psr\Log\LoggerInterface */
46 private $logger = null;
48 public function __construct() {
49 parent
::__construct( 'BotPasswords', 'editmyprivateinfo' );
50 $this->logger
= LoggerFactory
::getInstance( 'authentication' );
56 public function isListed() {
57 return $this->getConfig()->get( 'EnableBotPasswords' );
61 * Main execution point
62 * @param string|null $par
64 function execute( $par ) {
65 $this->getOutput()->disallowUserJs();
66 $this->requireLogin();
69 if ( strlen( $par ) === 0 ) {
71 } elseif ( strlen( $par ) > BotPassword
::APPID_MAXLENGTH
) {
72 throw new ErrorPageError( 'botpasswords', 'botpasswords-bad-appid',
73 [ htmlspecialchars( $par ) ] );
76 parent
::execute( $par );
79 protected function checkExecutePermissions( User
$user ) {
80 parent
::checkExecutePermissions( $user );
82 if ( !$this->getConfig()->get( 'EnableBotPasswords' ) ) {
83 throw new ErrorPageError( 'botpasswords', 'botpasswords-disabled' );
86 $this->userId
= CentralIdLookup
::factory()->centralIdFromLocalUser( $this->getUser() );
87 if ( !$this->userId
) {
88 throw new ErrorPageError( 'botpasswords', 'botpasswords-no-central-id' );
92 protected function getFormFields() {
95 if ( $this->par
!== null ) {
96 $this->botPassword
= BotPassword
::newFromCentralId( $this->userId
, $this->par
);
97 if ( !$this->botPassword
) {
98 $this->botPassword
= BotPassword
::newUnsaved( [
99 'centralId' => $this->userId
,
100 'appId' => $this->par
,
104 $sep = BotPassword
::getSeparator();
107 'label-message' => 'username',
108 'default' => $this->getUser()->getName() . $sep . $this->par
111 if ( $this->botPassword
->isSaved() ) {
112 $fields['resetPassword'] = [
114 'label-message' => 'botpasswords-label-resetpassword',
116 if ( $this->botPassword
->isInvalid() ) {
117 $fields['resetPassword']['default'] = true;
121 $lang = $this->getLanguage();
122 $showGrants = MWGrants
::getValidGrants();
123 $fields['grants'] = [
124 'type' => 'checkmatrix',
125 'label-message' => 'botpasswords-label-grants',
126 'help-message' => 'botpasswords-help-grants',
128 $this->msg( 'botpasswords-label-grants-column' )->escaped() => 'grant'
130 'rows' => array_combine(
131 array_map( 'MWGrants::getGrantsLink', $showGrants ),
134 'default' => array_map(
138 $this->botPassword
->getGrants()
140 'tooltips' => array_combine(
141 array_map( 'MWGrants::getGrantsLink', $showGrants ),
143 function ( $rights ) use ( $lang ) {
144 return $lang->semicolonList( array_map( 'User::getRightDescription', $rights ) );
146 array_intersect_key( MWGrants
::getRightsByGrant(), array_flip( $showGrants ) )
149 'force-options-on' => array_map(
153 MWGrants
::getHiddenGrants()
157 $fields['restrictions'] = [
158 'class' => HTMLRestrictionsField
::class,
160 'default' => $this->botPassword
->getRestrictions(),
164 $linkRenderer = $this->getLinkRenderer();
165 $passwordFactory = new PasswordFactory();
166 $passwordFactory->init( $this->getConfig() );
168 $dbr = BotPassword
::getDB( DB_REPLICA
);
171 [ 'bp_app_id', 'bp_password' ],
172 [ 'bp_user' => $this->userId
],
175 foreach ( $res as $row ) {
177 $password = $passwordFactory->newFromCiphertext( $row->bp_password
);
178 $passwordInvalid = $password instanceof InvalidPassword
;
180 } catch ( PasswordError
$ex ) {
181 $passwordInvalid = true;
184 $text = $linkRenderer->makeKnownLink(
185 $this->getPageTitle( $row->bp_app_id
),
188 if ( $passwordInvalid ) {
189 $text .= $this->msg( 'word-separator' )->escaped()
190 . $this->msg( 'botpasswords-label-needsreset' )->parse();
194 'section' => 'existing',
202 'section' => 'createnew',
203 'type' => 'textwithbutton',
204 'label-message' => 'botpasswords-label-appid',
205 'buttondefault' => $this->msg( 'botpasswords-label-create' )->text(),
206 'buttonflags' => [ 'progressive', 'primary' ],
208 'size' => BotPassword
::APPID_MAXLENGTH
,
209 'maxlength' => BotPassword
::APPID_MAXLENGTH
,
210 'validation-callback' => function ( $v ) {
212 return $v !== '' && strlen( $v ) <= BotPassword
::APPID_MAXLENGTH
;
226 protected function alterForm( HTMLForm
$form ) {
227 $form->setId( 'mw-botpasswords-form' );
228 $form->setTableId( 'mw-botpasswords-table' );
229 $form->addPreText( $this->msg( 'botpasswords-summary' )->parseAsBlock() );
230 $form->suppressDefaultSubmit();
232 if ( $this->par
!== null ) {
233 if ( $this->botPassword
->isSaved() ) {
234 $form->setWrapperLegendMsg( 'botpasswords-editexisting' );
238 'label-message' => 'botpasswords-label-update',
239 'flags' => [ 'primary', 'progressive' ],
244 'label-message' => 'botpasswords-label-delete',
245 'flags' => [ 'destructive' ],
248 $form->setWrapperLegendMsg( 'botpasswords-createnew' );
252 'label-message' => 'botpasswords-label-create',
253 'flags' => [ 'primary', 'progressive' ],
260 'label-message' => 'botpasswords-label-cancel'
265 public function onSubmit( array $data ) {
266 $op = $this->getRequest()->getVal( 'op', '' );
270 $this->getOutput()->redirect( $this->getPageTitle( $data['appId'] )->getFullURL() );
274 $this->operation
= 'insert';
275 return $this->save( $data );
278 $this->operation
= 'update';
279 return $this->save( $data );
282 $this->operation
= 'delete';
283 $bp = BotPassword
::newFromCentralId( $this->userId
, $this->par
);
287 "Bot password {op} for {user}@{app_id}",
289 'app_id' => $this->par
,
290 'user' => $this->getUser()->getName(),
291 'centralId' => $this->userId
,
293 'client_ip' => $this->getRequest()->getIP()
297 return Status
::newGood();
300 $this->getOutput()->redirect( $this->getPageTitle()->getFullURL() );
307 private function save( array $data ) {
308 $bp = BotPassword
::newUnsaved( [
309 'centralId' => $this->userId
,
310 'appId' => $this->par
,
311 'restrictions' => $data['restrictions'],
312 'grants' => array_merge(
313 MWGrants
::getHiddenGrants(),
314 preg_replace( '/^grant-/', '', $data['grants'] )
318 if ( $this->operation
=== 'insert' ||
!empty( $data['resetPassword'] ) ) {
319 $this->password
= BotPassword
::generatePassword( $this->getConfig() );
320 $passwordFactory = new PasswordFactory();
321 $passwordFactory->init( RequestContext
::getMain()->getConfig() );
322 $password = $passwordFactory->newFromPlaintext( $this->password
);
327 if ( $bp->save( $this->operation
, $password ) ) {
329 "Bot password {op} for {user}@{app_id}",
331 'op' => $this->operation
,
332 'user' => $this->getUser()->getName(),
333 'app_id' => $this->par
,
334 'centralId' => $this->userId
,
335 'restrictions' => $data['restrictions'],
336 'grants' => $bp->getGrants(),
337 'client_ip' => $this->getRequest()->getIP()
340 return Status
::newGood();
342 // Messages: botpasswords-insert-failed, botpasswords-update-failed
343 return Status
::newFatal( "botpasswords-{$this->operation}-failed", $this->par
);
347 public function onSuccess() {
348 $out = $this->getOutput();
350 $username = $this->getUser()->getName();
351 switch ( $this->operation
) {
353 $out->setPageTitle( $this->msg( 'botpasswords-created-title' )->text() );
354 $out->addWikiMsg( 'botpasswords-created-body', $this->par
, $username );
358 $out->setPageTitle( $this->msg( 'botpasswords-updated-title' )->text() );
359 $out->addWikiMsg( 'botpasswords-updated-body', $this->par
, $username );
363 $out->setPageTitle( $this->msg( 'botpasswords-deleted-title' )->text() );
364 $out->addWikiMsg( 'botpasswords-deleted-body', $this->par
, $username );
365 $this->password
= null;
369 if ( $this->password
!== null ) {
370 $sep = BotPassword
::getSeparator();
372 'botpasswords-newpassword',
373 htmlspecialchars( $username . $sep . $this->par
),
374 htmlspecialchars( $this->password
),
375 htmlspecialchars( $username ),
376 htmlspecialchars( $this->par
. $sep . $this->password
)
378 $this->password
= null;
381 $out->addReturnTo( $this->getPageTitle() );
384 protected function getGroupName() {
388 protected function getDisplayFormat() {