74 $username = $this->
getArg( 0 );
75 $password = $this->
getArg( 1 );
80 $user = $services->getUserFactory()->newFromName( $username );
81 if ( !is_object( $user ) ) {
85 $exists = ( $user->idForName() !== 0 );
87 if ( $exists && !$force ) {
88 $this->
fatalError(
'Account exists. Perhaps you want the --force option?' );
89 } elseif ( !$exists && !$password ) {
90 $this->
error(
'Argument <password> required!' );
92 } elseif ( $exists ) {
93 $inGroups = $services->getUserGroupManager()->getUserGroups( $user );
96 $groups = array_filter( self::PERMIT_ROLES, [ $this,
'hasOption' ] );
97 if ( $this->
hasOption(
'custom-groups' ) ) {
98 $allGroups = array_fill_keys( $services->getUserGroupManager()->listAllGroups(),
true );
99 $customGroupsText = $this->
getOption(
'custom-groups' );
100 if ( $customGroupsText !==
'' ) {
101 $customGroups = explode(
',', $customGroupsText );
102 foreach ( $customGroups as $customGroup ) {
103 if ( isset( $allGroups[$customGroup] ) ) {
104 $groups[] = trim( $customGroup );
106 $this->
output(
"$customGroup is not a valid group, ignoring!\n" );
112 $promotions = array_diff(
117 if ( $exists && !$password && count( $promotions ) === 0 ) {
118 $this->
output(
"Account exists and nothing to do.\n" );
121 } elseif ( count( $promotions ) !== 0 ) {
122 $dbDomain = WikiMap::getCurrentWikiDbDomain()->getId();
123 $promoText =
"User:{$username} into " . implode(
', ', $promotions ) .
"...\n";
125 $this->
output(
"$dbDomain: Promoting $promoText" );
127 $this->
output(
"$dbDomain: Creating and promoting $promoText" );
136 $status = $user->checkPasswordValidity( $password );
138 if ( !$status->isGood() ) {
147 AuthManager::AUTOCREATE_SOURCE_MAINT,
150 if ( !$status->isGood() ) {
158 $userGroupManager = $services->getUserGroupManager();
159 $userGroupManager->addUserToMultipleGroups( $user, $promotions );
160 $reason = $this->
getOption(
'reason' ) ?:
'';
161 $this->addLogEntry( $user, $inGroups, array_merge( $inGroups, $promotions ), $reason );
165 # Try to set the password
167 $status = $user->changeAuthenticationData( [
168 'username' => $user->getName(),
169 'password' => $password,
170 'retype' => $password,
172 if ( !$status->isGood() ) {
173 throw new PasswordError( $status->getMessage(
false,
false,
'en' )->text() );
176 $this->
output(
"Password set.\n" );
177 $user->saveSettings();
180 $this->
fatalError(
'Setting the password failed: ' . $pwe->getMessage() );
185 # Increment site_stats.ss_users
186 $ssu = SiteStatsUpdate::factory( [
'users' => 1 ] );
190 $this->
output(
"done.\n" );
201 private function addLogEntry( $user, array $oldGroups, array $newGroups,
string $reason ) {