Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
82.61% covered (warning)
82.61%
19 / 23
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ChangePassword
82.61% covered (warning)
82.61%
19 / 23
66.67% covered (warning)
66.67%
2 / 3
8.34
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 execute
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
2
 validatePasswordOption
42.86% covered (danger)
42.86%
3 / 7
0.00% covered (danger)
0.00%
0 / 1
9.66
1<?php
2/**
3 * Change the password of a given user
4 *
5 * Copyright © 2005, Ævar Arnfjörð Bjarmason
6 *
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.
11 *
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.
16 *
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
21 *
22 * @file
23 * @author Ævar Arnfjörð Bjarmason <avarab@gmail.com>
24 * @ingroup Maintenance
25 */
26
27// @codeCoverageIgnoreStart
28require_once __DIR__ . '/Maintenance.php';
29// @codeCoverageIgnoreEnd
30
31use MediaWiki\Maintenance\Maintenance;
32
33/**
34 * Maintenance script to change the password of a given user.
35 *
36 * @ingroup Maintenance
37 */
38class 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" );
47    }
48
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 );
62        }
63    }
64
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
68     *
69     * @since 1.44
70     *
71     * @return string The new password
72     */
73    private function validatePasswordOption( $errorMsgNoPassword, $errorMsgBothPasswords ) {
74        if ( $this->hasOption( 'password' ) && $this->hasOption( 'passwordstdin' ) ) {
75            $this->fatalError( $errorMsgBothPasswords );
76        }
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 );
83        }
84    }
85}
86
87// @codeCoverageIgnoreStart
88$maintClass = ChangePassword::class;
89require_once RUN_MAINTENANCE_IF_MAIN;
90// @codeCoverageIgnoreEnd