Create HookRunner class

Only core hooks are run, but own HookRunner is recommended

Bug: T263353
Change-Id: I2506f4af21ef955a7feeb145d5173f46ca11b334
This commit is contained in:
Umherirrender 2023-06-05 22:47:00 +02:00
parent 122d47d87d
commit 222b7475fb
3 changed files with 51 additions and 3 deletions

View file

@ -0,0 +1,31 @@
<?php
namespace LoginNotify\Hooks;
use MediaWiki\HookContainer\HookContainer;
/**
* This is a hook runner class, see docs/Hooks.md in core.
* @internal
*/
class HookRunner implements
\MediaWiki\Auth\Hook\AuthManagerLoginAuthenticateAuditHook
{
private HookContainer $hookContainer;
public function __construct( HookContainer $hookContainer ) {
$this->hookContainer = $hookContainer;
}
/**
* @inheritDoc
*/
public function onAuthManagerLoginAuthenticateAudit( $response, $user,
$username, $extraData
) {
return $this->hookContainer->run(
'AuthManagerLoginAuthenticateAudit',
[ $response, $user, $username, $extraData ]
);
}
}

View file

@ -2,6 +2,7 @@
namespace LoginNotify\Maintenance;
use LoginNotify\Hooks\HookRunner;
use Maintenance;
use MediaWiki\Auth\AuthenticationResponse;
use MediaWiki\Language\RawMessage;
@ -64,15 +65,15 @@ class LoginAttempt extends Maintenance {
return;
}
$hookContainer = MediaWikiServices::getInstance()->getHookContainer();
$hookRunner = new HookRunner( MediaWikiServices::getInstance()->getHookContainer() );
for ( $i = 0; $i < $reps; $i++ ) {
if ( $success ) {
$res = AuthenticationResponse::newPass( $username );
$hookContainer->run( 'AuthManagerLoginAuthenticateAudit', [ $res, $user, $username, [] ] );
$hookRunner->onAuthManagerLoginAuthenticateAudit( $res, $user, $username, [] );
$this->output( "A successful login attempt was registered!\n" );
} else {
$res = AuthenticationResponse::newFail( new RawMessage( 'Well, it failed' ) );
$hookContainer->run( 'AuthManagerLoginAuthenticateAudit', [ $res, null, $username, [] ] );
$hookRunner->onAuthManagerLoginAuthenticateAudit( $res, null, $username, [] );
$this->output( "A failed login attempt was registered!\n" );
}
}

View file

@ -0,0 +1,16 @@
<?php
namespace LoginNotify\Tests\Unit;
use LoginNotify\Hooks\HookRunner;
use MediaWiki\Tests\HookContainer\HookRunnerTestBase;
/**
* @covers \LoginNotify\Hooks\HookRunner
*/
class HookRunnerTest extends HookRunnerTestBase {
public static function provideHookRunners() {
yield HookRunner::class => [ HookRunner::class ];
}
}