72 $username = $this->
getArg( 0 );
73 $password = $this->
getArg( 1 );
78 $user = $services->getUserFactory()->newFromName( $username );
79 if ( !is_object( $user ) ) {
83 if ( $services->getUserNameUtils()->isTemp( $user->getName() ) ) {
85 'Temporary accounts cannot have groups or a password, so this script should not be used ' .
86 'to create a temporary account. Temporary accounts can be created by making an edit while logged out.'
90 $exists = ( $user->idForName() !== 0 );
92 if ( $exists && !$force ) {
93 $this->
fatalError(
'Account exists. Perhaps you want the --force option?' );
94 } elseif ( !$exists && !$password ) {
95 $this->
error(
'Argument <password> required!' );
97 } elseif ( $exists ) {
98 $inGroups = $services->getUserGroupManager()->getUserGroups( $user );
101 $groups = array_filter( self::PERMIT_ROLES, $this->
hasOption( ... ) );
102 if ( $this->
hasOption(
'custom-groups' ) ) {
103 $allGroups = array_fill_keys( $services->getUserGroupManager()->listAllGroups(),
true );
104 $customGroupsText = $this->
getOption(
'custom-groups' );
105 if ( $customGroupsText !==
'' ) {
106 $customGroups = explode(
',', $customGroupsText );
107 foreach ( $customGroups as $customGroup ) {
108 if ( isset( $allGroups[$customGroup] ) ) {
109 $groups[] = trim( $customGroup );
111 $this->
output(
"$customGroup is not a valid group, ignoring!\n" );
117 $promotions = array_diff(
122 if ( $exists && !$password && count( $promotions ) === 0 ) {
123 $this->
output(
"Account exists and nothing to do.\n" );
126 } elseif ( count( $promotions ) !== 0 ) {
127 $dbDomain = WikiMap::getCurrentWikiDbDomain()->getId();
128 $promoText =
"User:{$username} into " . implode(
', ', $promotions ) .
"...\n";
130 $this->
output(
"$dbDomain: Promoting $promoText" );
132 $this->
output(
"$dbDomain: Creating and promoting $promoText" );
141 $status = $user->checkPasswordValidity( $password );
143 if ( !$status->isGood() ) {
152 AuthManager::AUTOCREATE_SOURCE_MAINT,
155 new UltimateAuthority( User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [
'steal' =>
true ] ) )
157 if ( !$status->isGood() ) {
165 $userGroupManager = $services->getUserGroupManager();
166 $userGroupManager->addUserToMultipleGroups( $user, $promotions );
167 $reason = $this->
getOption(
'reason' ) ?:
'';
168 $this->addLogEntry( $user, $inGroups, array_merge( $inGroups, $promotions ), $reason );
172 # Try to set the password
174 $status = $user->changeAuthenticationData( [
175 'username' => $user->getName(),
176 'password' => $password,
177 'retype' => $password,
180 $this->
fatalError(
'Unexpected PasswordError: ' . $pwe->getMessage() );
182 if ( !$status->isGood() ) {
183 $this->
output(
"Setting the password failed.\n" );
187 $this->
output(
"Password set.\n" );
188 $user->saveSettings();
193 $resetEmail = $this->
createChild( ResetUserEmail::class );
194 $resetEmail->setArg( 0, $user->getName() );
195 $resetEmail->setArg( 1, $this->
getOption(
'email' ) );
196 $resetEmail->setOption(
'no-reset-password',
true );
197 $resetEmail->execute();
201 # Increment site_stats.ss_users
202 $ssu = SiteStatsUpdate::factory( [
'users' => 1 ] );
206 $this->
output(
"done.\n" );
217 private function addLogEntry( $user, array $oldGroups, array $newGroups,
string $reason ) {