70 $username = $this->
getArg( 0 );
71 $password = $this->
getArg( 1 );
76 $user = $services->getUserFactory()->newFromName( $username );
77 if ( !is_object( $user ) ) {
81 $exists = ( $user->idForName() !== 0 );
83 if ( $exists && !$force ) {
84 $this->
fatalError(
'Account exists. Perhaps you want the --force option?' );
85 } elseif ( !$exists && !$password ) {
86 $this->
error(
'Argument <password> required!' );
88 } elseif ( $exists ) {
89 $inGroups = $services->getUserGroupManager()->getUserGroups( $user );
92 $groups = array_filter( self::$permitRoles, [ $this,
'hasOption' ] );
93 if ( $this->
hasOption(
'custom-groups' ) ) {
94 $allGroups = array_fill_keys( $services->getUserGroupManager()->listAllGroups(),
true );
95 $customGroupsText = $this->
getOption(
'custom-groups' );
96 if ( $customGroupsText !==
'' ) {
97 $customGroups = explode(
',', $customGroupsText );
98 foreach ( $customGroups as $customGroup ) {
99 if ( isset( $allGroups[$customGroup] ) ) {
100 $groups[] = trim( $customGroup );
102 $this->
output(
"$customGroup is not a valid group, ignoring!\n" );
108 $promotions = array_diff(
113 if ( $exists && !$password && count( $promotions ) === 0 ) {
114 $this->
output(
"Account exists and nothing to do.\n" );
117 } elseif ( count( $promotions ) !== 0 ) {
118 $dbDomain = WikiMap::getCurrentWikiDbDomain()->getId();
119 $promoText =
"User:{$username} into " . implode(
', ', $promotions ) .
"...\n";
121 $this->
output(
"$dbDomain: Promoting $promoText" );
123 $this->
output(
"$dbDomain: Creating and promoting $promoText" );
132 $status = $user->checkPasswordValidity( $password );
134 if ( !$status->isGood() ) {
135 $this->
fatalError( $status->getMessage(
false,
false,
'en' )->text() );
143 MediaWiki\Auth\AuthManager::AUTOCREATE_SOURCE_MAINT,
146 if ( !$status->isGood() ) {
147 $this->
fatalError( $status->getMessage(
false,
false,
'en' )->text() );
154 $userGroupManager = $services->getUserGroupManager();
155 $userGroupManager->addUserToMultipleGroups( $user, $promotions );
156 $reason = $this->
getOption(
'reason' ) ?:
'';
157 $this->addLogEntry( $user, $inGroups, array_merge( $inGroups, $promotions ), $reason );
161 # Try to set the password
163 $status = $user->changeAuthenticationData( [
164 'username' => $user->getName(),
165 'password' => $password,
166 'retype' => $password,
168 if ( !$status->isGood() ) {
169 throw new PasswordError( $status->getMessage(
false,
false,
'en' )->text() );
172 $this->
output(
"Password set.\n" );
173 $user->saveSettings();
176 $this->
fatalError(
'Setting the password failed: ' . $pwe->getMessage() );
181 # Increment site_stats.ss_users
182 $ssu = SiteStatsUpdate::factory( [
'users' => 1 ] );
186 $this->
output(
"done.\n" );
197 private function addLogEntry( $user, array $oldGroups, array $newGroups,
string $reason ) {