Merge "docs: Fix typo"
[mediawiki.git] / maintenance / changePassword.php
blob87b08c25cf4e891656a4561572b2bbfaf0379e88
1 <?php
2 /**
3 * Change the password of a given user
5 * Copyright © 2005, Ævar Arnfjörð Bjarmason
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
22 * @file
23 * @author Ævar Arnfjörð Bjarmason <avarab@gmail.com>
24 * @ingroup Maintenance
27 // @codeCoverageIgnoreStart
28 require_once __DIR__ . '/Maintenance.php';
29 // @codeCoverageIgnoreEnd
31 use MediaWiki\Maintenance\Maintenance;
33 /**
34 * Maintenance script to change the password of a given user.
36 * @ingroup Maintenance
38 class ChangePassword extends Maintenance {
39 public function __construct() {
40 parent::__construct();
41 $this->addOption( "user", "The username to operate on", false, true );
42 $this->addOption( "userid", "The user id to operate on", false, true );
43 $this->addOption( "password", "The password to use", false, true );
44 // phpcs:ignore Generic.Files.LineLength.TooLong
45 $this->addOption( "passwordstdin", "Makes the script read the password from stdin instead. Cannot be used alongside --password", false, false );
46 $this->addDescription( "Change a user's password" );
49 public function execute() {
50 $user = $this->validateUserOption( "A \"user\" or \"userid\" must be set to change the password for" );
51 // phpcs:ignore Generic.Files.LineLength.TooLong
52 $password = $this->validatePasswordOption( 'Set either --password or --passwordstdin', 'Either --password or --passwordstdin must be set, not both at once' );
53 $status = $user->changeAuthenticationData( [
54 'username' => $user->getName(),
55 'password' => $password,
56 'retype' => $password,
57 ] );
58 if ( $status->isGood() ) {
59 $this->output( "Password set for " . $user->getName() . "\n" );
60 } else {
61 $this->fatalError( $status );
65 /**
66 * @param string $errorMsgNoPassword Error message to be displayed if neither --password or --stdin are set
67 * @param string $errorMsgBothPasswords Error message to be displayed if both --password and --stdin are set
69 * @since 1.44
71 * @return string The new password
73 private function validatePasswordOption( $errorMsgNoPassword, $errorMsgBothPasswords ) {
74 if ( $this->hasOption( 'password' ) && $this->hasOption( 'passwordstdin' ) ) {
75 $this->fatalError( $errorMsgBothPasswords );
77 if ( $this->hasOption( 'password' ) ) {
78 return $this->getOption( 'password' );
79 } elseif ( $this->hasOption( 'passwordstdin' ) ) {
80 return $this->getStdin( Maintenance::STDIN_ALL );
81 } else {
82 $this->fatalError( $errorMsgNoPassword );
87 // @codeCoverageIgnoreStart
88 $maintClass = ChangePassword::class;
89 require_once RUN_MAINTENANCE_IF_MAIN;
90 // @codeCoverageIgnoreEnd