79 $username = $this->
getArg( 0 );
80 $password = $this->
getArg( 1 );
85 $user = $services->getUserFactory()->newFromName( $username );
86 if ( !is_object( $user ) ) {
90 if ( $services->getUserNameUtils()->isTemp( $user->getName() ) ) {
92 'Temporary accounts cannot have groups or a password, so this script should not be used ' .
93 'to create a temporary account. Temporary accounts can be created by making an edit while logged out.'
97 $exists = ( $user->idForName() !== 0 );
99 if ( $exists && !$force ) {
100 $this->
fatalError(
'Account exists. Perhaps you want the --force option?' );
101 } elseif ( !$exists && !$password ) {
102 $this->
error(
'Argument <password> required!' );
104 } elseif ( $exists ) {
105 $inGroups = $services->getUserGroupManager()->getUserGroups( $user );
108 $groups = array_filter( self::PERMIT_ROLES, $this->
hasOption( ... ) );
109 if ( $this->
hasOption(
'custom-groups' ) ) {
110 $allGroups = array_fill_keys( $services->getUserGroupManager()->listAllGroups(),
true );
111 $customGroupsText = $this->
getOption(
'custom-groups' );
112 if ( $customGroupsText !==
'' ) {
113 $customGroups = explode(
',', $customGroupsText );
114 foreach ( $customGroups as $customGroup ) {
115 if ( isset( $allGroups[$customGroup] ) ) {
116 $groups[] = trim( $customGroup );
118 $this->
output(
"$customGroup is not a valid group, ignoring!\n" );
124 $promotions = array_diff(
129 if ( $exists && !$password && count( $promotions ) === 0 ) {
130 $this->
output(
"Account exists and nothing to do.\n" );
133 } elseif ( count( $promotions ) !== 0 ) {
134 $dbDomain = WikiMap::getCurrentWikiDbDomain()->getId();
135 $promoText =
"User:{$username} into " . implode(
', ', $promotions ) .
"...\n";
137 $this->
output(
"$dbDomain: Promoting $promoText" );
139 $this->
output(
"$dbDomain: Creating and promoting $promoText" );
148 $status = $user->checkPasswordValidity( $password );
150 if ( !$status->isGood() ) {
159 AuthManager::AUTOCREATE_SOURCE_MAINT,
162 if ( !$status->isGood() ) {
170 $userGroupManager = $services->getUserGroupManager();
171 $userGroupManager->addUserToMultipleGroups( $user, $promotions );
172 $reason = $this->
getOption(
'reason' ) ?:
'';
173 $this->addLogEntry( $user, $inGroups, array_merge( $inGroups, $promotions ), $reason );
177 # Try to set the password
179 $status = $user->changeAuthenticationData( [
180 'username' => $user->getName(),
181 'password' => $password,
182 'retype' => $password,
184 if ( !$status->isGood() ) {
185 throw new PasswordError( $status->getMessage(
false,
false,
'en' )->text() );
188 $this->
output(
"Password set.\n" );
189 $user->saveSettings();
192 $this->
fatalError(
'Setting the password failed: ' . $pwe->getMessage() );
197 # Increment site_stats.ss_users
198 $ssu = SiteStatsUpdate::factory( [
'users' => 1 ] );
202 $this->
output(
"done.\n" );
213 private function addLogEntry( $user, array $oldGroups, array $newGroups,
string $reason ) {