MediaWiki master
resetUserEmail.php
Go to the documentation of this file.
1<?php
24// @codeCoverageIgnoreStart
25require_once __DIR__ . '/Maintenance.php';
26// @codeCoverageIgnoreEnd
27
31
39 public function __construct() {
40 parent::__construct();
41
42 $this->addDescription( "Resets a user's email" );
43
44 $this->addArg( 'user', 'Username or user ID, if starts with #' );
45 $this->addArg( 'email', 'Email to assign' );
46
47 $this->addOption( 'no-reset-password', 'Don\'t reset the user\'s password' );
48 $this->addOption( 'email-password', 'Send a temporary password to the user\'s new email address' );
49 }
50
51 public function execute() {
52 $userName = $this->getArg( 0 );
53 if ( preg_match( '/^#\d+$/', $userName ) ) {
54 $user = User::newFromId( (int)substr( $userName, 1 ) );
55 } else {
56 $user = User::newFromName( $userName );
57 }
58 if ( !$user || !$user->isRegistered() || !$user->loadFromId() ) {
59 $this->fatalError( "Error: user '$userName' does not exist\n" );
60 }
61
62 $email = $this->getArg( 1, '' );
63 if ( $email !== '' && !Sanitizer::validateEmail( $email ) ) {
64 $this->fatalError( "Error: email '$email' is not valid\n" );
65 }
66
67 // Code from https://wikitech.wikimedia.org/wiki/Password_reset
68 $user->setEmail( $email );
69 $user->setEmailAuthenticationTimestamp( wfTimestampNow() );
70 $user->saveSettings();
71
72 if ( !$this->hasOption( 'no-reset-password' ) ) {
73 // Kick whomever is currently controlling the account off if possible
74 $password = PasswordFactory::generateRandomPasswordString( 128 );
75 $status = $user->changeAuthenticationData( [
76 'username' => $user->getName(),
77 'password' => $password,
78 'retype' => $password,
79 ] );
80 if ( !$status->isGood() ) {
81 $this->error( "Password couldn't be reset because:" );
82 $this->error( $status );
83 }
84 }
85
86 if ( $this->hasOption( 'email-password' ) ) {
87 $passReset = $this->getServiceContainer()->getPasswordReset();
88 $sysUser = User::newSystemUser( 'Maintenance script', [ 'steal' => true ] );
89 $status = $passReset->execute( $sysUser, $user->getName(), $email );
90 if ( !$status->isGood() ) {
91 $this->error( "Email couldn't be sent because:" );
92 $this->error( $status );
93 }
94 }
95 $this->output( "Done!\n" );
96 }
97}
98
99// @codeCoverageIgnoreStart
100$maintClass = ResetUserEmail::class;
101require_once RUN_MAINTENANCE_IF_MAIN;
102// @codeCoverageIgnoreEnd
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
error( $err, $die=0)
Throw an error to the user.
addArg( $arg, $description, $required=true, $multi=false)
Add some args that are needed.
output( $out, $channel=null)
Throw some output to the user.
hasOption( $name)
Checks to see if a particular option was set.
getServiceContainer()
Returns the main service container.
getArg( $argId=0, $default=null)
Get an argument.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
HTML sanitizer for MediaWiki.
Definition Sanitizer.php:46
Factory class for creating and checking Password objects.
internal since 1.36
Definition User.php:93
Maintenance script that resets user email.
__construct()
Default constructor.
execute()
Do the actual work.
$maintClass