3 * Creates an account and grants it rights.
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 Maintenance
22 * @author Rob Church <robchur@gmail.com>
23 * @author Pablo Castellano <pablo@anche.no>
26 require_once __DIR__
. '/Maintenance.php';
28 use MediaWiki\MediaWikiServices
;
31 * Maintenance script to create an account and grant it rights.
33 * @ingroup Maintenance
35 class CreateAndPromote
extends Maintenance
{
36 private static $permitRoles = [ 'sysop', 'bureaucrat', 'interface-admin', 'bot' ];
38 public function __construct() {
39 parent
::__construct();
40 $this->addDescription( 'Create a new user account and/or grant it additional rights' );
43 'If acccount exists already, just grant it rights or change password.'
45 foreach ( self
::$permitRoles as $role ) {
46 $this->addOption( $role, "Add the account to the {$role} group" );
51 'Comma-separated list of groups to add the user to',
56 $this->addArg( "username", "Username of new user" );
57 $this->addArg( "password", "Password to set", false );
60 public function execute() {
61 $username = $this->getArg( 0 );
62 $password = $this->getArg( 1 );
63 $force = $this->hasOption( 'force' );
66 $user = User
::newFromName( $username );
67 if ( !is_object( $user ) ) {
68 $this->fatalError( "invalid username." );
71 $exists = ( $user->idForName() !== 0 );
73 if ( $exists && !$force ) {
74 $this->fatalError( "Account exists. Perhaps you want the --force option?" );
75 } elseif ( !$exists && !$password ) {
76 $this->error( "Argument <password> required!" );
77 $this->maybeHelp( true );
78 } elseif ( $exists ) {
79 $inGroups = $user->getGroups();
82 $groups = array_filter( self
::$permitRoles, [ $this, 'hasOption' ] );
83 if ( $this->hasOption( 'custom-groups' ) ) {
84 $allGroups = array_flip( User
::getAllGroups() );
85 $customGroupsText = $this->getOption( 'custom-groups' );
86 if ( $customGroupsText !== '' ) {
87 $customGroups = explode( ',', $customGroupsText );
88 foreach ( $customGroups as $customGroup ) {
89 if ( isset( $allGroups[$customGroup] ) ) {
90 $groups[] = trim( $customGroup );
92 $this->output( "$customGroup is not a valid group, ignoring!\n" );
98 $promotions = array_diff(
103 if ( $exists && !$password && count( $promotions ) === 0 ) {
104 $this->output( "Account exists and nothing to do.\n" );
107 } elseif ( count( $promotions ) !== 0 ) {
108 $dbDomain = WikiMap
::getCurrentWikiDbDomain()->getId();
109 $promoText = "User:{$username} into " . implode( ', ', $promotions ) . "...\n";
111 $this->output( "$dbDomain: Promoting $promoText" );
113 $this->output( "$dbDomain: Creating and promoting $promoText" );
118 // Create the user via AuthManager as there may be various side
119 // effects that are performed by the configured AuthManager chain.
120 $status = MediaWikiServices
::getInstance()->getAuthManager()->autoCreateUser(
122 MediaWiki\Auth\AuthManager
::AUTOCREATE_SOURCE_MAINT
,
125 if ( !$status->isGood() ) {
126 $this->fatalError( $status->getMessage( false, false, 'en' )->text() );
131 # Try to set the password
133 $status = $user->changeAuthenticationData( [
134 'username' => $user->getName(),
135 'password' => $password,
136 'retype' => $password,
138 if ( !$status->isGood() ) {
139 throw new PasswordError( $status->getMessage( false, false, 'en' )->text() );
142 $this->output( "Password set.\n" );
143 $user->saveSettings();
145 } catch ( PasswordError
$pwe ) {
146 $this->fatalError( $pwe->getText() );
151 array_map( [ $user, 'addGroup' ], $promotions );
154 # Increment site_stats.ss_users
155 $ssu = SiteStatsUpdate
::factory( [ 'users' => 1 ] );
159 $this->output( "done.\n" );
163 $maintClass = CreateAndPromote
::class;
164 require_once RUN_MAINTENANCE_IF_MAIN
;