Debian Codegraph @

maintenance/resetUserEmail.php

raw symbols SCIP
1 <?php
2 /**
3 * Reset user email.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Maintenance
22 */
23
24 // @codeCoverageIgnoreStart
25 require_once __DIR__ . '/Maintenance.php';
26 // @codeCoverageIgnoreEnd
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 * Maintenance script that resets user email.
36 *
37 * @since 1.27
38 * @ingroup Maintenance
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 // Code from https://wikitech.wikimedia.org/wiki/Password_reset
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 // Kick whomever is currently controlling the account off if possible
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 // @codeCoverageIgnoreStart
133 $maintClass = ResetUserEmail::class;
134 require_once RUN_MAINTENANCE_IF_MAIN;
135 // @codeCoverageIgnoreEnd