2020-07-18 15:07:10 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Creates a bot password for an existing user account.
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
* @ingroup Maintenance
|
|
|
|
|
* @author Alex Dean <wikimedia@mostlyalex.com>
|
|
|
|
|
*/
|
|
|
|
|
|
2023-09-19 16:42:44 +00:00
|
|
|
use MediaWiki\User\BotPassword;
|
2023-09-19 12:13:45 +00:00
|
|
|
use MediaWiki\User\User;
|
|
|
|
|
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreStart
|
2020-07-18 15:07:10 +00:00
|
|
|
require_once __DIR__ . '/Maintenance.php';
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreEnd
|
2020-07-18 15:07:10 +00:00
|
|
|
|
|
|
|
|
class CreateBotPassword extends Maintenance {
|
|
|
|
|
/**
|
2020-10-04 00:33:43 +00:00
|
|
|
* Width of initial column of --showgrants output
|
2020-07-18 15:07:10 +00:00
|
|
|
*/
|
|
|
|
|
private const SHOWGRANTS_COLUMN_WIDTH = 20;
|
|
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
|
parent::__construct();
|
|
|
|
|
$this->addDescription(
|
|
|
|
|
'Create a bot password for a user. ' .
|
|
|
|
|
'See https://www.mediawiki.org/wiki/Manual:Bot_passwords for more information.'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->addOption( "showgrants",
|
|
|
|
|
"Prints a description of available grants and exits."
|
|
|
|
|
);
|
|
|
|
|
$this->addOption( "appid",
|
|
|
|
|
"App id for the new bot password.", false, true
|
|
|
|
|
);
|
|
|
|
|
$this->addOption( "grants",
|
|
|
|
|
"CSV list of permissions to grant.", false, true
|
|
|
|
|
);
|
|
|
|
|
$this->addArg( "user",
|
|
|
|
|
"The username to create a bot password for.", false
|
|
|
|
|
);
|
|
|
|
|
$this->addArg( "password",
|
|
|
|
|
"A password will be generated if this is omitted." .
|
|
|
|
|
" If supplied, it must be exactly 32 characters.", false
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function execute() {
|
|
|
|
|
if ( $this->hasOption( 'showgrants' ) ) {
|
|
|
|
|
$this->showGrants();
|
2020-10-04 00:33:43 +00:00
|
|
|
return;
|
2020-07-18 15:07:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$username = $this->getArg( 0 );
|
|
|
|
|
$password = $this->getArg( 1 );
|
|
|
|
|
$appId = $this->getOption( 'appid' );
|
2024-08-30 18:16:53 +00:00
|
|
|
$grants = explode( ',', $this->getOption( 'grants', '' ) );
|
2020-07-18 15:07:10 +00:00
|
|
|
|
|
|
|
|
$errors = [];
|
|
|
|
|
if ( $username === null ) {
|
|
|
|
|
$errors[] = "Argument <user> required!";
|
|
|
|
|
}
|
|
|
|
|
if ( $appId == null ) {
|
|
|
|
|
$errors[] = "Param appid required!";
|
|
|
|
|
}
|
|
|
|
|
if ( $this->getOption( 'grants' ) === null ) {
|
|
|
|
|
$errors[] = "Param grants required!";
|
|
|
|
|
}
|
|
|
|
|
if ( count( $errors ) > 0 ) {
|
|
|
|
|
$this->fatalError( implode( "\n", $errors ) );
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-31 09:21:12 +00:00
|
|
|
$services = $this->getServiceContainer();
|
2022-07-07 14:41:24 +00:00
|
|
|
$grantsInfo = $services->getGrantsInfo();
|
|
|
|
|
$invalidGrants = array_diff( $grants, $grantsInfo->getValidGrants() );
|
2020-07-18 15:07:10 +00:00
|
|
|
if ( count( $invalidGrants ) > 0 ) {
|
|
|
|
|
$this->fatalError(
|
|
|
|
|
"These grants are invalid: " . implode( ', ', $invalidGrants ) . "\n" .
|
|
|
|
|
"Use the --showgrants option for a full list of valid grant names."
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-07 14:41:24 +00:00
|
|
|
$passwordFactory = $services->getPasswordFactory();
|
2020-07-18 15:07:10 +00:00
|
|
|
|
2022-12-21 19:12:14 +00:00
|
|
|
$userIdentity = $services->getUserIdentityLookup()->getUserIdentityByName( $username );
|
|
|
|
|
if ( !$userIdentity || !$userIdentity->isRegistered() ) {
|
2020-07-18 15:07:10 +00:00
|
|
|
$this->fatalError( "Cannot create bot password for non-existent user '$username'." );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $password === null ) {
|
|
|
|
|
$password = BotPassword::generatePassword( $this->getConfig() );
|
|
|
|
|
} else {
|
|
|
|
|
$passwordLength = strlen( $password );
|
|
|
|
|
if ( $passwordLength < BotPassword::PASSWORD_MINLENGTH ) {
|
|
|
|
|
$message = "Bot passwords must have at least " . BotPassword::PASSWORD_MINLENGTH .
|
2021-09-03 22:52:31 +00:00
|
|
|
" characters. Given password is $passwordLength characters.";
|
2020-07-18 15:07:10 +00:00
|
|
|
$this->fatalError( $message );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$bp = BotPassword::newUnsaved( [
|
|
|
|
|
'username' => $username,
|
|
|
|
|
'appId' => $appId,
|
|
|
|
|
'grants' => $grants
|
|
|
|
|
] );
|
|
|
|
|
|
2020-10-04 00:33:43 +00:00
|
|
|
if ( $bp === null ) {
|
|
|
|
|
$this->fatalError( "Bot password creation failed." );
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-18 15:07:10 +00:00
|
|
|
$passwordInstance = $passwordFactory->newFromPlaintext( $password );
|
2020-10-04 00:33:43 +00:00
|
|
|
$status = $bp->save( 'insert', $passwordInstance );
|
2020-07-18 15:07:10 +00:00
|
|
|
|
2020-10-04 00:33:43 +00:00
|
|
|
if ( $status->isGood() ) {
|
2020-07-18 15:07:10 +00:00
|
|
|
$this->output( "Success.\n" );
|
2022-07-29 00:45:09 +00:00
|
|
|
$this->output( "Log in using username:'{$username}@{$appId}' and password:'{$password}'.\n" );
|
2020-07-18 15:07:10 +00:00
|
|
|
} else {
|
Maintenance: Print errors from StatusValue objects in a consistent way
Allow Maintenance::error() and Maintenance::fatalError() to take
StatusValue objects. They now print each error message from the
status on a separate line, in English, ignoring on-wiki message
overrides, as wikitext but after parser function expansion.
Thoughts on the previously commonly used methods:
- $status->getMessage( false, false, 'en' )->text()
Almost the same as the new output, but it allows on-wiki message
overrides, and if there is more than one error, it prefixes each
line with a '*' (like a wikitext list).
- $status->getMessage( false, false, 'en' )->plain()
- $status->getWikiText( false, false, 'en' )
As above, but these forms do not expand parser functions
such as {{GENDER:}}.
- print_r( $status->getErrorsArray(), true )
- print_r( $status->getErrors(), true )
These forms output the message keys instead of the message text,
which is not very human-readable.
The error messages are now always printed using error() rather
than output(), which means they go to STDERR rather than STDOUT
and they're printed even with the --quiet flag.
Change-Id: I5b8e7c7ed2a896a1029f58857a478d3f1b4b0589
2024-06-06 23:50:00 +00:00
|
|
|
$this->error( "Bot password creation failed. Does this appid already exist for the user perhaps?" );
|
|
|
|
|
$this->fatalError( $status );
|
2020-07-18 15:07:10 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function showGrants() {
|
2023-08-31 09:21:12 +00:00
|
|
|
$permissions = $this->getServiceContainer()->getGrantsInfo()->getValidGrants();
|
2020-07-18 15:07:10 +00:00
|
|
|
sort( $permissions );
|
|
|
|
|
|
|
|
|
|
$this->output( str_pad( 'GRANT', self::SHOWGRANTS_COLUMN_WIDTH ) . " DESCRIPTION\n" );
|
|
|
|
|
foreach ( $permissions as $permission ) {
|
|
|
|
|
$this->output(
|
|
|
|
|
str_pad( $permission, self::SHOWGRANTS_COLUMN_WIDTH ) . " " .
|
2024-02-05 02:36:46 +00:00
|
|
|
User::getRightDescription( $permission ) . "\n"
|
2020-07-18 15:07:10 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreStart
|
2020-07-18 15:07:10 +00:00
|
|
|
$maintClass = CreateBotPassword::class;
|
|
|
|
|
require_once RUN_MAINTENANCE_IF_MAIN;
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreEnd
|