| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
require_once __DIR__ . '/Maintenance.php'; |
| 26 |
|
| 27 |
|
| 28 |
use MediaWiki\Logger\LoggerFactory; |
| 29 |
use MediaWiki\Maintenance\Maintenance; |
| 30 |
use MediaWiki\Parser\Sanitizer; |
| 31 |
use MediaWiki\Password\PasswordFactory; |
| 32 |
use MediaWiki\User\User; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
class ResetUserEmail extends Maintenance { |
| 41 |
public function __construct() { |
| 42 |
parent::__construct(); |
| 43 |
|
| 44 |
$this->addDescription( "Resets a user's email" ); |
| 45 |
|
| 46 |
$this->addArg( 'user', 'Username or user ID, if starts with #' ); |
| 47 |
$this->addArg( 'email', 'Email to assign' ); |
| 48 |
|
| 49 |
$this->addOption( 'no-reset-password', 'Don\'t reset the user\'s password' ); |
| 50 |
$this->addOption( 'email-password', 'Send a temporary password to the user\'s new email address' ); |
| 51 |
$this->addOption( 'reason', 'Reason for the email change (ticket number etc)', false, true ); |
| 52 |
} |
| 53 |
|
| 54 |
public function execute() { |
| 55 |
$userName = $this->getArg( 0 ); |
| 56 |
if ( preg_match( '/^#\d+$/', $userName ) ) { |
| 57 |
$user = User::newFromId( (int)substr( $userName, 1 ) ); |
| 58 |
} else { |
| 59 |
$user = User::newFromName( $userName ); |
| 60 |
} |
| 61 |
if ( !$user || !$user->isRegistered() || !$user->loadFromId() ) { |
| 62 |
$this->fatalError( "Error: user '$userName' does not exist\n" ); |
| 63 |
} |
| 64 |
|
| 65 |
$email = $this->getArg( 1, '' ); |
| 66 |
if ( $email !== '' && !Sanitizer::validateEmail( $email ) ) { |
| 67 |
$this->fatalError( "Error: email '$email' is not valid\n" ); |
| 68 |
} |
| 69 |
|
| 70 |
$oldAddr = $user->getEmail(); |
| 71 |
|
| 72 |
|
| 73 |
$user->setEmail( $email ); |
| 74 |
$user->setEmailAuthenticationTimestamp( wfTimestampNow() ); |
| 75 |
$user->saveSettings(); |
| 76 |
|
| 77 |
$logger = LoggerFactory::getInstance( 'authentication' ); |
| 78 |
$logger->info( |
| 79 |
'Changing email address for {user} from {oldemail} to {newemail} via resetUserEmail.php', [ |
| 80 |
'user' => $user->getName(), |
| 81 |
'oldemail' => $oldAddr, |
| 82 |
'newemail' => $email, |
| 83 |
'reason' => $this->getOption( 'reason', '' ), |
| 84 |
] |
| 85 |
); |
| 86 |
|
| 87 |
if ( !$this->hasOption( 'no-reset-password' ) ) { |
| 88 |
|
| 89 |
$password = PasswordFactory::generateRandomPasswordString( 128 ); |
| 90 |
$status = $user->changeAuthenticationData( [ |
| 91 |
'username' => $user->getName(), |
| 92 |
'password' => $password, |
| 93 |
'retype' => $password, |
| 94 |
] ); |
| 95 |
if ( !$status->isGood() ) { |
| 96 |
$this->error( "Password couldn't be reset because:" ); |
| 97 |
$this->error( $status ); |
| 98 |
} else { |
| 99 |
$logger->info( |
| 100 |
'Scrambling password for {user} via resetUserEmail.php', [ |
| 101 |
'user' => $user->getName(), |
| 102 |
'reason' => $this->getOption( 'reason', '' ), |
| 103 |
] |
| 104 |
); |
| 105 |
|
| 106 |
$invalidator = $this->createChild( InvalidateUserSessions::class ); |
| 107 |
$invalidator->setOption( 'user', $user->getName() ); |
| 108 |
$invalidator->execute(); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
if ( $this->hasOption( 'email-password' ) ) { |
| 113 |
$passReset = $this->getServiceContainer()->getPasswordReset(); |
| 114 |
$sysUser = User::newSystemUser( 'Maintenance script', [ 'steal' => true ] ); |
| 115 |
$status = $passReset->execute( $sysUser, $user->getName(), $email ); |
| 116 |
if ( !$status->isGood() ) { |
| 117 |
$this->error( "Email couldn't be sent because:" ); |
| 118 |
$this->error( $status ); |
| 119 |
} else { |
| 120 |
$logger->info( |
| 121 |
'Password reset email sent for {user} via resetUserEmail.php', [ |
| 122 |
'user' => $user->getName(), |
| 123 |
'reason' => $this->getOption( 'reason', '' ), |
| 124 |
] |
| 125 |
); |
| 126 |
} |
| 127 |
} |
| 128 |
$this->output( "Done!\n" ); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
$maintClass = ResetUserEmail::class; |
| 134 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 135 |
|