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