Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 49
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
LockUser
0.00% covered (danger)
0.00%
0 / 47
0.00% covered (danger)
0.00%
0 / 3
90
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 1
20
 getContext
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2/**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21use MediaWiki\Extension\CentralAuth\User\CentralAuthUser;
22use MediaWiki\Permissions\UltimateAuthority;
23
24/**
25 * Locks a specific global account.
26 *
27 * @author Taavi Väänänen <taavi@wikimedia.org>
28 */
29class LockUser extends Maintenance {
30    public function __construct() {
31        parent::__construct();
32
33        $this->requireExtension( 'CentralAuth' );
34        $this->addOption( 'username', 'User to act on', true, true );
35        $this->addOption( 'actor', 'Username to log with', false, true );
36        $this->addOption( 'reason', 'Reason to use', false, true );
37        $this->addOption( 'bot', 'Mark as bot in RC', false, false );
38    }
39
40    public function execute() {
41        $user = CentralAuthUser::getPrimaryInstanceByName(
42            $this->getServiceContainer()
43                ->getUserNameUtils()
44                ->getCanonical( $this->getOption( 'username' ) )
45        );
46        $username = $user->getName();
47        $context = $this->getContext();
48
49        if ( !$user->exists() ) {
50            $this->fatalError( "User '$username' does not exist" );
51        }
52
53        if ( $user->isLocked() ) {
54            $this->output( "User '$username' is already locked" . PHP_EOL );
55            return;
56        }
57
58        $status = $user->adminLockHide(
59            /* setLocked */ true,
60            /* setHidden */ null,
61            $this->getOption( 'reason', '' ),
62            $context,
63            $this->getOption( 'bot', false )
64        );
65
66        if ( $status->isGood() ) {
67            $this->output( "Locked user '$username'" . PHP_EOL );
68            return;
69        }
70
71        $this->fatalError(
72            $this->getServiceContainer()
73                ->getFormatterFactory()
74                ->getStatusFormatter( $context )
75                ->getWikiText( $status )
76        );
77    }
78
79    private function getContext(): IContextSource {
80        $context = RequestContext::newExtraneousContext( Title::makeTitleSafe( NS_SPECIAL, 'Badtitle' ) );
81
82        $username = $this->getOption( 'actor' );
83        if ( $username ) {
84            $user = $this->getServiceContainer()
85                ->getUserFactory()
86                ->newFromName( $username );
87
88            if ( !$user || !$user->isRegistered() ) {
89                $this->fatalError( "No user '$username' found!" );
90            }
91
92            '@phan-var \MediaWiki\User\User $user';
93            $context->setUser( $user );
94        } else {
95            $context->setUser( User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [ 'steal' => true ] ) );
96        }
97
98        $context->setAuthority( new UltimateAuthority( $context->getUser(), false ) );
99        return $context;
100    }
101}
102
103$maintClass = LockUser::class;
104require_once RUN_MAINTENANCE_IF_MAIN;