30require_once __DIR__ .
'/Maintenance.php';
37 private const SHOWGRANTS_COLUMN_WIDTH = 20;
40 parent::__construct();
42 'Create a bot password for a user. ' .
43 'See https://www.mediawiki.org/wiki/Manual:Bot_passwords for more information.'
47 "Prints a description of available grants and exits."
50 "App id for the new bot password.",
false,
true
53 "CSV list of permissions to grant.",
false,
true
56 "The username to create a bot password for.",
false
59 "A password will be generated if this is omitted." .
60 " If supplied, it must be exactly 32 characters.",
false
70 $username = $this->
getArg( 0 );
71 $password = $this->
getArg( 1 );
73 $grants = explode(
',', $this->
getOption(
'grants',
'' ) );
76 if ( $username ===
null ) {
77 $errors[] =
"Argument <user> required!";
79 if ( $appId ==
null ) {
80 $errors[] =
"Param appid required!";
82 if ( $this->
getOption(
'grants' ) ===
null ) {
83 $errors[] =
"Param grants required!";
85 if ( count( $errors ) > 0 ) {
90 $grantsInfo = $services->getGrantsInfo();
91 $invalidGrants = array_diff( $grants, $grantsInfo->getValidGrants() );
92 if ( count( $invalidGrants ) > 0 ) {
94 "These grants are invalid: " . implode(
', ', $invalidGrants ) .
"\n" .
95 "Use the --showgrants option for a full list of valid grant names."
99 $passwordFactory = $services->getPasswordFactory();
101 $userIdentity = $services->getUserIdentityLookup()->getUserIdentityByName( $username );
102 if ( !$userIdentity || !$userIdentity->isRegistered() ) {
103 $this->
fatalError(
"Cannot create bot password for non-existent user '$username'." );
106 if ( $password ===
null ) {
107 $password = BotPassword::generatePassword( $this->
getConfig() );
109 $passwordLength = strlen( $password );
110 if ( $passwordLength < BotPassword::PASSWORD_MINLENGTH ) {
111 $message =
"Bot passwords must have at least " . BotPassword::PASSWORD_MINLENGTH .
112 " characters. Given password is $passwordLength characters.";
117 $bp = BotPassword::newUnsaved( [
118 'username' => $username,
123 if ( $bp ===
null ) {
124 $this->
fatalError(
"Bot password creation failed." );
127 $passwordInstance = $passwordFactory->newFromPlaintext( $password );
128 $status = $bp->save(
'insert', $passwordInstance );
130 if ( $status->isGood() ) {
131 $this->
output(
"Success.\n" );
132 $this->
output(
"Log in using username:'{$username}@{$appId}' and password:'{$password}'.\n" );
134 $this->
error(
"Bot password creation failed. Does this appid already exist for the user perhaps?" );
141 sort( $permissions );
143 $this->
output( str_pad(
'GRANT', self::SHOWGRANTS_COLUMN_WIDTH ) .
" DESCRIPTION\n" );
144 foreach ( $permissions as $permission ) {
146 str_pad( $permission, self::SHOWGRANTS_COLUMN_WIDTH ) .
" " .
147 User::getRightDescription( $permission ) .
"\n"
155require_once RUN_MAINTENANCE_IF_MAIN;
__construct()
Default constructor.
execute()
Do the actual work.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
addArg( $arg, $description, $required=true, $multi=false)
Add some args that are needed.
getArg( $argId=0, $default=null)
Get an argument.
output( $out, $channel=null)
Throw some output to the user.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
hasOption( $name)
Checks to see if a particular option was set.
getOption( $name, $default=null)
Get an option, or return the default.
error( $err, $die=0)
Throw an error to the user.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.