mediawiki-extensions-LoginN.../maintenance/loginAttempt.php
libraryupgrader 3103915e78 build: Updating mediawiki/mediawiki-codesniffer to 29.0.0
The following sniffs are failing and were disabled:
* MediaWiki.Commenting.FunctionComment.MissingParamName
* MediaWiki.Commenting.FunctionComment.MissingParamTag
* MediaWiki.Commenting.FunctionComment.ParamNameNoMatch

Additional changes:
* Also sorted "composer fix" command to run phpcbf last.

Change-Id: Ie0f3c44edd654994ca1aa3c55a40cbef22465c0b
2020-01-14 07:37:43 +00:00

84 lines
2.5 KiB
PHP

<?php
namespace LoginNotify\Maintenance;
use FauxRequest;
use Hooks;
use Maintenance;
use MediaWiki\Auth\AuthenticationResponse;
use RawMessage;
use User;
$IP = getenv( 'MW_INSTALL_PATH' );
if ( $IP === false ) {
$IP = __DIR__ . '/../../..';
}
require_once "$IP/maintenance/Maintenance.php";
/**
* A maintenance script to programatically generate successful or failed login attempts for any
* user, from any given IP address and with any given user-agent string. This script makes testing
* LoginNotify and its interaction with other extensions (such as CheckUser) much easier for the
* developers.
*/
class LoginAttempt extends Maintenance {
/**
* Constructor
*
* Retrieves the arguments from the command line
*/
public function __construct() {
parent::__construct();
$this->addDescription( 'Registers a login attempt for a given user' );
$this->addArg( 'user', 'Target user', true );
$this->addArg( 'success', 'Whether login attempt was successful (true/false)', false );
$this->addArg( 'ip', 'IP address of the login attempt', false );
$this->addArg( 'ua', 'User-agent string of the login attempt', false );
$this->addArg( 'repetitions', 'How many times the attempt should be made', false );
$this->requireExtension( 'LoginNotify' );
}
/**
* Main function
*
* Registers a failed or successful login attempt for a given user
*/
public function execute() {
global $wgRequest;
$username = $this->getArg( 0 );
$success = $this->getArg( 1, false ) === 'true';
$ip = $this->getArg( 2, '127.0.0.1' );
$ua = $this->getArg( 3, 'Login attempt by LoginNotify maintenance script' );
$reps = intval( $this->getArg( 4, 1 ) );
$wgRequest = new FauxRequest();
$wgRequest->setIP( $ip );
$wgRequest->setHeader( 'User-Agent', $ua );
if ( !User::idFromName( $username ) ) {
$this->output( "User {$username} does not exist!\n" );
return;
}
$user = User::newFromName( $username, 'usable' );
for ( $i = 0; $i < $reps; $i++ ) {
if ( $success ) {
$res = AuthenticationResponse::newPass( $username );
Hooks::run( 'AuthManagerLoginAuthenticateAudit', [ $res, $user, $username, [] ] );
$this->output( "A successful login attempt was registered!\n" );
} else {
$res = AuthenticationResponse::newFail( new RawMessage( 'Well, it failed' ) );
Hooks::run( 'AuthManagerLoginAuthenticateAudit', [ $res, null, $username, [] ] );
$this->output( "A failed login attempt was registered!\n" );
}
}
}
}
$maintClass = LoginAttempt::class;
require_once RUN_MAINTENANCE_IF_MAIN;