Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
94.44% |
34 / 36 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
ResetUserEmail | |
94.44% |
34 / 36 |
|
50.00% |
1 / 2 |
12.02 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
execute | |
93.33% |
28 / 30 |
|
0.00% |
0 / 1 |
11.04 |
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\Maintenance\Maintenance; |
29 | use MediaWiki\Parser\Sanitizer; |
30 | use MediaWiki\Password\PasswordFactory; |
31 | use MediaWiki\User\User; |
32 | |
33 | /** |
34 | * Maintenance script that resets user email. |
35 | * |
36 | * @since 1.27 |
37 | * @ingroup Maintenance |
38 | */ |
39 | class ResetUserEmail extends Maintenance { |
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; |
102 | require_once RUN_MAINTENANCE_IF_MAIN; |
103 | // @codeCoverageIgnoreEnd |