75 $username = $this->
getArg( 0 );
76 $password = $this->
getArg( 1 );
81 $user = $services->getUserFactory()->newFromName( $username );
82 if ( !is_object( $user ) ) {
86 $exists = ( $user->idForName() !== 0 );
88 if ( $exists && !$force ) {
89 $this->
fatalError(
'Account exists. Perhaps you want the --force option?' );
90 } elseif ( !$exists && !$password ) {
91 $this->
error(
'Argument <password> required!' );
93 } elseif ( $exists ) {
94 $inGroups = $services->getUserGroupManager()->getUserGroups( $user );
97 $groups = array_filter( self::PERMIT_ROLES, [ $this,
'hasOption' ] );
98 if ( $this->
hasOption(
'custom-groups' ) ) {
99 $allGroups = array_fill_keys( $services->getUserGroupManager()->listAllGroups(),
true );
100 $customGroupsText = $this->
getOption(
'custom-groups' );
101 if ( $customGroupsText !==
'' ) {
102 $customGroups = explode(
',', $customGroupsText );
103 foreach ( $customGroups as $customGroup ) {
104 if ( isset( $allGroups[$customGroup] ) ) {
105 $groups[] = trim( $customGroup );
107 $this->
output(
"$customGroup is not a valid group, ignoring!\n" );
113 $promotions = array_diff(
118 if ( $exists && !$password && count( $promotions ) === 0 ) {
119 $this->
output(
"Account exists and nothing to do.\n" );
122 } elseif ( count( $promotions ) !== 0 ) {
123 $dbDomain = WikiMap::getCurrentWikiDbDomain()->getId();
124 $promoText =
"User:{$username} into " . implode(
', ', $promotions ) .
"...\n";
126 $this->
output(
"$dbDomain: Promoting $promoText" );
128 $this->
output(
"$dbDomain: Creating and promoting $promoText" );
137 $status = $user->checkPasswordValidity( $password );
139 if ( !$status->isGood() ) {
148 AuthManager::AUTOCREATE_SOURCE_MAINT,
151 if ( !$status->isGood() ) {
159 $userGroupManager = $services->getUserGroupManager();
160 $userGroupManager->addUserToMultipleGroups( $user, $promotions );
161 $reason = $this->
getOption(
'reason' ) ?:
'';
162 $this->addLogEntry( $user, $inGroups, array_merge( $inGroups, $promotions ), $reason );
166 # Try to set the password
168 $status = $user->changeAuthenticationData( [
169 'username' => $user->getName(),
170 'password' => $password,
171 'retype' => $password,
173 if ( !$status->isGood() ) {
174 throw new PasswordError( $status->getMessage(
false,
false,
'en' )->text() );
177 $this->
output(
"Password set.\n" );
178 $user->saveSettings();
181 $this->
fatalError(
'Setting the password failed: ' . $pwe->getMessage() );
186 # Increment site_stats.ss_users
187 $ssu = SiteStatsUpdate::factory( [
'users' => 1 ] );
191 $this->
output(
"done.\n" );
202 private function addLogEntry( $user, array $oldGroups, array $newGroups,
string $reason ) {