72 $username = $this->
getArg( 0 );
73 $password = $this->
getArg( 1 );
78 $user = $services->getUserFactory()->newFromName( $username );
79 if ( !is_object( $user ) ) {
83 $exists = ( $user->idForName() !== 0 );
85 if ( $exists && !$force ) {
86 $this->
fatalError(
'Account exists. Perhaps you want the --force option?' );
87 } elseif ( !$exists && !$password ) {
88 $this->
error(
'Argument <password> required!' );
90 } elseif ( $exists ) {
91 $inGroups = $services->getUserGroupManager()->getUserGroups( $user );
94 $groups = array_filter( self::$permitRoles, [ $this,
'hasOption' ] );
95 if ( $this->
hasOption(
'custom-groups' ) ) {
96 $allGroups = array_fill_keys( $services->getUserGroupManager()->listAllGroups(),
true );
97 $customGroupsText = $this->
getOption(
'custom-groups' );
98 if ( $customGroupsText !==
'' ) {
99 $customGroups = explode(
',', $customGroupsText );
100 foreach ( $customGroups as $customGroup ) {
101 if ( isset( $allGroups[$customGroup] ) ) {
102 $groups[] = trim( $customGroup );
104 $this->
output(
"$customGroup is not a valid group, ignoring!\n" );
110 $promotions = array_diff(
115 if ( $exists && !$password && count( $promotions ) === 0 ) {
116 $this->
output(
"Account exists and nothing to do.\n" );
119 } elseif ( count( $promotions ) !== 0 ) {
120 $dbDomain = WikiMap::getCurrentWikiDbDomain()->getId();
121 $promoText =
"User:{$username} into " . implode(
', ', $promotions ) .
"...\n";
123 $this->
output(
"$dbDomain: Promoting $promoText" );
125 $this->
output(
"$dbDomain: Creating and promoting $promoText" );
134 $status = $user->checkPasswordValidity( $password );
136 if ( !$status->isGood() ) {
137 $this->
fatalError( $status->getMessage(
false,
false,
'en' )->text() );
145 AuthManager::AUTOCREATE_SOURCE_MAINT,
148 if ( !$status->isGood() ) {
149 $this->
fatalError( $status->getMessage(
false,
false,
'en' )->text() );
156 $userGroupManager = $services->getUserGroupManager();
157 $userGroupManager->addUserToMultipleGroups( $user, $promotions );
158 $reason = $this->
getOption(
'reason' ) ?:
'';
159 $this->addLogEntry( $user, $inGroups, array_merge( $inGroups, $promotions ), $reason );
163 # Try to set the password
165 $status = $user->changeAuthenticationData( [
166 'username' => $user->getName(),
167 'password' => $password,
168 'retype' => $password,
170 if ( !$status->isGood() ) {
171 throw new PasswordError( $status->getMessage(
false,
false,
'en' )->text() );
174 $this->
output(
"Password set.\n" );
175 $user->saveSettings();
178 $this->
fatalError(
'Setting the password failed: ' . $pwe->getMessage() );
183 # Increment site_stats.ss_users
184 $ssu = SiteStatsUpdate::factory( [
'users' => 1 ] );
188 $this->
output(
"done.\n" );
199 private function addLogEntry( $user, array $oldGroups, array $newGroups,
string $reason ) {