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
;
25 use MediaWiki\MediaWikiServices
;
28 * Let users manage bot passwords
30 * @ingroup SpecialPage
32 class SpecialBotPasswords
extends FormSpecialPage
{
34 /** @var int Central user ID */
37 /** @var BotPassword|null Bot password being edited, if any */
38 private $botPassword = null;
40 /** @var string Operation being performed: create, update, delete */
41 private $operation = null;
43 /** @var string New password set, for communication between onSubmit() and onSuccess() */
44 private $password = null;
46 /** @var Psr\Log\LoggerInterface */
47 private $logger = null;
49 public function __construct() {
50 parent
::__construct( 'BotPasswords', 'editmyprivateinfo' );
51 $this->logger
= LoggerFactory
::getInstance( 'authentication' );
57 public function isListed() {
58 return $this->getConfig()->get( 'EnableBotPasswords' );
61 protected function getLoginSecurityLevel() {
62 return $this->getName();
66 * Main execution point
67 * @param string|null $par
69 function execute( $par ) {
70 $this->getOutput()->disallowUserJs();
71 $this->requireLogin();
74 if ( strlen( $par ) === 0 ) {
76 } elseif ( strlen( $par ) > BotPassword
::APPID_MAXLENGTH
) {
77 throw new ErrorPageError( 'botpasswords', 'botpasswords-bad-appid',
78 [ htmlspecialchars( $par ) ] );
81 parent
::execute( $par );
84 protected function checkExecutePermissions( User
$user ) {
85 parent
::checkExecutePermissions( $user );
87 if ( !$this->getConfig()->get( 'EnableBotPasswords' ) ) {
88 throw new ErrorPageError( 'botpasswords', 'botpasswords-disabled' );
91 $this->userId
= CentralIdLookup
::factory()->centralIdFromLocalUser( $this->getUser() );
92 if ( !$this->userId
) {
93 throw new ErrorPageError( 'botpasswords', 'botpasswords-no-central-id' );
97 protected function getFormFields() {
100 if ( $this->par
!== null ) {
101 $this->botPassword
= BotPassword
::newFromCentralId( $this->userId
, $this->par
);
102 if ( !$this->botPassword
) {
103 $this->botPassword
= BotPassword
::newUnsaved( [
104 'centralId' => $this->userId
,
105 'appId' => $this->par
,
109 $sep = BotPassword
::getSeparator();
112 'label-message' => 'username',
113 'default' => $this->getUser()->getName() . $sep . $this->par
116 if ( $this->botPassword
->isSaved() ) {
117 $fields['resetPassword'] = [
119 'label-message' => 'botpasswords-label-resetpassword',
121 if ( $this->botPassword
->isInvalid() ) {
122 $fields['resetPassword']['default'] = true;
126 $lang = $this->getLanguage();
127 $showGrants = MWGrants
::getValidGrants();
128 $fields['grants'] = [
129 'type' => 'checkmatrix',
130 'label-message' => 'botpasswords-label-grants',
131 'help-message' => 'botpasswords-help-grants',
133 $this->msg( 'botpasswords-label-grants-column' )->escaped() => 'grant'
135 'rows' => array_combine(
136 array_map( 'MWGrants::getGrantsLink', $showGrants ),
139 'default' => array_map(
143 $this->botPassword
->getGrants()
145 'tooltips' => array_combine(
146 array_map( 'MWGrants::getGrantsLink', $showGrants ),
148 function ( $rights ) use ( $lang ) {
149 return $lang->semicolonList( array_map( 'User::getRightDescription', $rights ) );
151 array_intersect_key( MWGrants
::getRightsByGrant(), array_flip( $showGrants ) )
154 'force-options-on' => array_map(
158 MWGrants
::getHiddenGrants()
162 $fields['restrictions'] = [
163 'class' => HTMLRestrictionsField
::class,
165 'default' => $this->botPassword
->getRestrictions(),
169 $linkRenderer = $this->getLinkRenderer();
170 $passwordFactory = MediaWikiServices
::getInstance()->getPasswordFactory();
172 $dbr = BotPassword
::getDB( DB_REPLICA
);
175 [ 'bp_app_id', 'bp_password' ],
176 [ 'bp_user' => $this->userId
],
179 foreach ( $res as $row ) {
181 $password = $passwordFactory->newFromCiphertext( $row->bp_password
);
182 $passwordInvalid = $password instanceof InvalidPassword
;
184 } catch ( PasswordError
$ex ) {
185 $passwordInvalid = true;
188 $text = $linkRenderer->makeKnownLink(
189 $this->getPageTitle( $row->bp_app_id
),
192 if ( $passwordInvalid ) {
193 $text .= $this->msg( 'word-separator' )->escaped()
194 . $this->msg( 'botpasswords-label-needsreset' )->parse();
198 'section' => 'existing',
206 'section' => 'createnew',
207 'type' => 'textwithbutton',
208 'label-message' => 'botpasswords-label-appid',
209 'buttondefault' => $this->msg( 'botpasswords-label-create' )->text(),
210 'buttonflags' => [ 'progressive', 'primary' ],
212 'size' => BotPassword
::APPID_MAXLENGTH
,
213 'maxlength' => BotPassword
::APPID_MAXLENGTH
,
214 'validation-callback' => function ( $v ) {
216 return $v !== '' && strlen( $v ) <= BotPassword
::APPID_MAXLENGTH
;
230 protected function alterForm( HTMLForm
$form ) {
231 $form->setId( 'mw-botpasswords-form' );
232 $form->setTableId( 'mw-botpasswords-table' );
233 $form->addPreText( $this->msg( 'botpasswords-summary' )->parseAsBlock() );
234 $form->suppressDefaultSubmit();
236 if ( $this->par
!== null ) {
237 if ( $this->botPassword
->isSaved() ) {
238 $form->setWrapperLegendMsg( 'botpasswords-editexisting' );
242 'label-message' => 'botpasswords-label-update',
243 'flags' => [ 'primary', 'progressive' ],
248 'label-message' => 'botpasswords-label-delete',
249 'flags' => [ 'destructive' ],
252 $form->setWrapperLegendMsg( 'botpasswords-createnew' );
256 'label-message' => 'botpasswords-label-create',
257 'flags' => [ 'primary', 'progressive' ],
264 'label-message' => 'botpasswords-label-cancel'
269 public function onSubmit( array $data ) {
270 $op = $this->getRequest()->getVal( 'op', '' );
274 $this->getOutput()->redirect( $this->getPageTitle( $data['appId'] )->getFullURL() );
278 $this->operation
= 'insert';
279 return $this->save( $data );
282 $this->operation
= 'update';
283 return $this->save( $data );
286 $this->operation
= 'delete';
287 $bp = BotPassword
::newFromCentralId( $this->userId
, $this->par
);
291 "Bot password {op} for {user}@{app_id}",
293 'app_id' => $this->par
,
294 'user' => $this->getUser()->getName(),
295 'centralId' => $this->userId
,
297 'client_ip' => $this->getRequest()->getIP()
301 return Status
::newGood();
304 $this->getOutput()->redirect( $this->getPageTitle()->getFullURL() );
311 private function save( array $data ) {
312 $bp = BotPassword
::newUnsaved( [
313 'centralId' => $this->userId
,
314 'appId' => $this->par
,
315 'restrictions' => $data['restrictions'],
316 'grants' => array_merge(
317 MWGrants
::getHiddenGrants(),
318 preg_replace( '/^grant-/', '', $data['grants'] )
322 if ( $this->operation
=== 'insert' ||
!empty( $data['resetPassword'] ) ) {
323 $this->password
= BotPassword
::generatePassword( $this->getConfig() );
324 $passwordFactory = MediaWikiServices
::getInstance()->getPasswordFactory();
325 $password = $passwordFactory->newFromPlaintext( $this->password
);
330 if ( $bp->save( $this->operation
, $password ) ) {
332 "Bot password {op} for {user}@{app_id}",
334 'op' => $this->operation
,
335 'user' => $this->getUser()->getName(),
336 'app_id' => $this->par
,
337 'centralId' => $this->userId
,
338 'restrictions' => $data['restrictions'],
339 'grants' => $bp->getGrants(),
340 'client_ip' => $this->getRequest()->getIP()
343 return Status
::newGood();
345 // Messages: botpasswords-insert-failed, botpasswords-update-failed
346 return Status
::newFatal( "botpasswords-{$this->operation}-failed", $this->par
);
350 public function onSuccess() {
351 $out = $this->getOutput();
353 $username = $this->getUser()->getName();
354 switch ( $this->operation
) {
356 $out->setPageTitle( $this->msg( 'botpasswords-created-title' )->text() );
357 $out->addWikiMsg( 'botpasswords-created-body', $this->par
, $username );
361 $out->setPageTitle( $this->msg( 'botpasswords-updated-title' )->text() );
362 $out->addWikiMsg( 'botpasswords-updated-body', $this->par
, $username );
366 $out->setPageTitle( $this->msg( 'botpasswords-deleted-title' )->text() );
367 $out->addWikiMsg( 'botpasswords-deleted-body', $this->par
, $username );
368 $this->password
= null;
372 if ( $this->password
!== null ) {
373 $sep = BotPassword
::getSeparator();
375 'botpasswords-newpassword',
376 htmlspecialchars( $username . $sep . $this->par
),
377 htmlspecialchars( $this->password
),
378 htmlspecialchars( $username ),
379 htmlspecialchars( $this->par
. $sep . $this->password
)
381 $this->password
= null;
384 $out->addReturnTo( $this->getPageTitle() );
387 protected function getGroupName() {
391 protected function getDisplayFormat() {