28 require_once __DIR__ .
'/Maintenance.php';
34 private const SHOWGRANTS_COLUMN_WIDTH = 20;
37 parent::__construct();
39 'Create a bot password for a user. ' .
40 'See https://www.mediawiki.org/wiki/Manual:Bot_passwords for more information.'
44 "Prints a description of available grants and exits."
47 "App id for the new bot password.",
false,
true
50 "CSV list of permissions to grant.",
false,
true
53 "The username to create a bot password for.",
false
56 "A password will be generated if this is omitted." .
57 " If supplied, it must be exactly 32 characters.",
false
67 $username = $this->
getArg( 0 );
68 $password = $this->
getArg( 1 );
70 $grants = explode(
',', $this->
getOption(
'grants' ) );
73 if ( $username ===
null ) {
74 $errors[] =
"Argument <user> required!";
76 if ( $appId ==
null ) {
77 $errors[] =
"Param appid required!";
79 if ( $this->
getOption(
'grants' ) ===
null ) {
80 $errors[] =
"Param grants required!";
82 if ( count( $errors ) > 0 ) {
87 $grantsInfo = $services->getGrantsInfo();
88 $invalidGrants = array_diff( $grants, $grantsInfo->getValidGrants() );
89 if ( count( $invalidGrants ) > 0 ) {
91 "These grants are invalid: " . implode(
', ', $invalidGrants ) .
"\n" .
92 "Use the --showgrants option for a full list of valid grant names."
96 $passwordFactory = $services->getPasswordFactory();
98 $userIdentity = $services->getUserIdentityLookup()->getUserIdentityByName( $username );
99 if ( !$userIdentity || !$userIdentity->isRegistered() ) {
100 $this->
fatalError(
"Cannot create bot password for non-existent user '$username'." );
103 if ( $password ===
null ) {
104 $password = BotPassword::generatePassword( $this->
getConfig() );
106 $passwordLength = strlen( $password );
107 if ( $passwordLength < BotPassword::PASSWORD_MINLENGTH ) {
108 $message =
"Bot passwords must have at least " . BotPassword::PASSWORD_MINLENGTH .
109 " characters. Given password is $passwordLength characters.";
114 $bp = BotPassword::newUnsaved( [
115 'username' => $username,
120 if ( $bp ===
null ) {
121 $this->
fatalError(
"Bot password creation failed." );
124 $passwordInstance = $passwordFactory->newFromPlaintext( $password );
125 $status = $bp->save(
'insert', $passwordInstance );
127 if ( $status->isGood() ) {
128 $this->
output(
"Success.\n" );
129 $this->
output(
"Log in using username:'{$username}@{$appId}' and password:'{$password}'.\n" );
132 "Bot password creation failed. Does this appid already exist for the user perhaps?\n\nErrors:\n" .
133 print_r( $status->getErrors(),
true )
140 sort( $permissions );
142 $this->
output( str_pad(
'GRANT', self::SHOWGRANTS_COLUMN_WIDTH ) .
" DESCRIPTION\n" );
143 foreach ( $permissions as $permission ) {
145 str_pad( $permission, self::SHOWGRANTS_COLUMN_WIDTH ) .
" " .
146 User::getRightDescription( $permission ) .
"\n"
153 require_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.
output( $out, $channel=null)
Throw some output to the user.
hasOption( $name)
Checks to see if a particular option was set.
getServiceContainer()
Returns the main service container.
getArg( $argId=0, $default=null)
Get an argument.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.