Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
33 / 33 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| InvalidateUserSessions | |
100.00% |
33 / 33 |
|
100.00% |
2 / 2 |
13 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
| execute | |
100.00% |
26 / 26 |
|
100.00% |
1 / 1 |
12 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Invalidate the sessions of certain users on the wiki. |
| 4 | * If you want to invalidate all sessions, use $wgAuthenticationTokenVersion instead. |
| 5 | * |
| 6 | * @license GPL-2.0-or-later |
| 7 | * @file |
| 8 | * @ingroup Maintenance |
| 9 | */ |
| 10 | |
| 11 | use MediaWiki\Maintenance\Maintenance; |
| 12 | use MediaWiki\Session\SessionManager; |
| 13 | use MediaWiki\User\User; |
| 14 | |
| 15 | // @codeCoverageIgnoreStart |
| 16 | require_once __DIR__ . '/Maintenance.php'; |
| 17 | // @codeCoverageIgnoreEnd |
| 18 | |
| 19 | /** |
| 20 | * Invalidate the sessions of certain users on the wiki. |
| 21 | * If you want to invalidate all sessions, use $wgAuthenticationTokenVersion instead. |
| 22 | * |
| 23 | * @ingroup Maintenance |
| 24 | */ |
| 25 | class InvalidateUserSessions extends Maintenance { |
| 26 | public function __construct() { |
| 27 | parent::__construct(); |
| 28 | $this->addDescription( |
| 29 | 'Invalidate the sessions of certain users on the wiki.' |
| 30 | ); |
| 31 | $this->addOption( 'user', 'Username', false, true, 'u' ); |
| 32 | $this->addOption( 'file', 'File with one username per line', false, true, 'f' ); |
| 33 | $this->setBatchSize( 1000 ); |
| 34 | } |
| 35 | |
| 36 | public function execute() { |
| 37 | $username = $this->getOption( 'user' ); |
| 38 | $file = $this->getOption( 'file' ); |
| 39 | |
| 40 | if ( $username === null && $file === null ) { |
| 41 | $this->fatalError( 'Either --user or --file is required' ); |
| 42 | } elseif ( $username !== null && $file !== null ) { |
| 43 | $this->fatalError( 'Cannot use both --user and --file' ); |
| 44 | } |
| 45 | |
| 46 | if ( $username !== null ) { |
| 47 | $usernames = [ $username ]; |
| 48 | } else { |
| 49 | $usernames = is_readable( $file ) ? |
| 50 | file( $file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES ) : false; |
| 51 | if ( $usernames === false ) { |
| 52 | $this->fatalError( "Could not open $file", 2 ); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | $i = 0; |
| 57 | $sessionManager = SessionManager::singleton(); |
| 58 | foreach ( $usernames as $username ) { |
| 59 | $i++; |
| 60 | $user = User::newFromName( $username ); |
| 61 | try { |
| 62 | $sessionManager->invalidateSessionsForUser( $user ); |
| 63 | if ( $user->isRegistered() ) { |
| 64 | $this->output( "Invalidated sessions for user $username\n" ); |
| 65 | } else { |
| 66 | # session invalidation might still work if there is a central identity provider |
| 67 | $this->output( "Could not find user $username, tried to invalidate anyway\n" ); |
| 68 | } |
| 69 | } catch ( Exception $e ) { |
| 70 | $this->output( "Failed to invalidate sessions for user $username | " |
| 71 | . str_replace( [ "\r", "\n" ], ' ', $e->getMessage() ) . "\n" ); |
| 72 | } |
| 73 | |
| 74 | if ( $i % $this->getBatchSize() ) { |
| 75 | $this->waitForReplication(); |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | // @codeCoverageIgnoreStart |
| 82 | $maintClass = InvalidateUserSessions::class; |
| 83 | require_once RUN_MAINTENANCE_IF_MAIN; |
| 84 | // @codeCoverageIgnoreEnd |