Add partial support for running Parsoid selser tests
[mediawiki.git] / maintenance / createBotPassword.php
blob3cb1c4c0c6e2dbc3630a40893d18866a33b5152e
1 <?php
2 /**
3 * Creates a bot password for an existing user account.
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
20 * @file
21 * @ingroup Maintenance
22 * @author Alex Dean <wikimedia@mostlyalex.com>
25 require_once __DIR__ . '/Maintenance.php';
27 class CreateBotPassword extends Maintenance {
28 /**
29 * Width of initial column of --showgrants output
31 private const SHOWGRANTS_COLUMN_WIDTH = 20;
33 public function __construct() {
34 parent::__construct();
35 $this->addDescription(
36 'Create a bot password for a user. ' .
37 'See https://www.mediawiki.org/wiki/Manual:Bot_passwords for more information.'
40 $this->addOption( "showgrants",
41 "Prints a description of available grants and exits."
43 $this->addOption( "appid",
44 "App id for the new bot password.", false, true
46 $this->addOption( "grants",
47 "CSV list of permissions to grant.", false, true
49 $this->addArg( "user",
50 "The username to create a bot password for.", false
52 $this->addArg( "password",
53 "A password will be generated if this is omitted." .
54 " If supplied, it must be exactly 32 characters.", false
58 public function execute() {
59 if ( $this->hasOption( 'showgrants' ) ) {
60 $this->showGrants();
61 return;
64 $username = $this->getArg( 0 );
65 $password = $this->getArg( 1 );
66 $appId = $this->getOption( 'appid' );
67 $grants = explode( ',', $this->getOption( 'grants' ) );
69 $errors = [];
70 if ( $username === null ) {
71 $errors[] = "Argument <user> required!";
73 if ( $appId == null ) {
74 $errors[] = "Param appid required!";
76 if ( $this->getOption( 'grants' ) === null ) {
77 $errors[] = "Param grants required!";
79 if ( count( $errors ) > 0 ) {
80 $this->fatalError( implode( "\n", $errors ) );
83 $invalidGrants = array_diff( $grants, MWGrants::getValidGrants() );
84 if ( count( $invalidGrants ) > 0 ) {
85 $this->fatalError(
86 "These grants are invalid: " . implode( ', ', $invalidGrants ) . "\n" .
87 "Use the --showgrants option for a full list of valid grant names."
91 $passwordFactory = MediaWiki\MediaWikiServices::getInstance()->getPasswordFactory();
93 $userId = User::idFromName( $username );
94 if ( $userId === null ) {
95 $this->fatalError( "Cannot create bot password for non-existent user '$username'." );
98 if ( $password === null ) {
99 $password = BotPassword::generatePassword( $this->getConfig() );
100 } else {
101 $passwordLength = strlen( $password );
102 if ( $passwordLength < BotPassword::PASSWORD_MINLENGTH ) {
103 $message = "Bot passwords must have at least " . BotPassword::PASSWORD_MINLENGTH .
104 " characters. Given password is $passwordLength characters.";
105 $this->fatalError( $message );
109 $bp = BotPassword::newUnsaved( [
110 'username' => $username,
111 'appId' => $appId,
112 'grants' => $grants
113 ] );
115 if ( $bp === null ) {
116 $this->fatalError( "Bot password creation failed." );
119 $passwordInstance = $passwordFactory->newFromPlaintext( $password );
120 $status = $bp->save( 'insert', $passwordInstance );
122 if ( $status->isGood() ) {
123 $this->output( "Success.\n" );
124 $this->output( "Log in using username:'${username}@${appId}' and password:'${password}'.\n" );
125 } else {
126 $this->fatalError(
127 "Bot password creation failed. Does this appid already exist for the user perhaps?\n\nErrors:\n" .
128 print_r( $status->getErrors(), true )
133 public function showGrants() {
134 $permissions = MWGrants::getValidGrants();
135 sort( $permissions );
137 $this->output( str_pad( 'GRANT', self::SHOWGRANTS_COLUMN_WIDTH ) . " DESCRIPTION\n" );
138 foreach ( $permissions as $permission ) {
139 $this->output(
140 str_pad( $permission, self::SHOWGRANTS_COLUMN_WIDTH ) . " " .
141 User::getRightDescription( $permission ) . "\n"
147 $maintClass = CreateBotPassword::class;
148 require_once RUN_MAINTENANCE_IF_MAIN;