| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
namespace LoginNotify; |
| 10 |
|
| 11 |
use MediaWiki\Auth\AuthenticationResponse; |
| 12 |
use MediaWiki\Auth\Hook\AuthManagerLoginAuthenticateAuditHook; |
| 13 |
use MediaWiki\Auth\Hook\LocalUserCreatedHook; |
| 14 |
use MediaWiki\Hook\RecentChange_saveHook; |
| 15 |
use MediaWiki\User\User; |
| 16 |
use MediaWiki\User\UserFactory; |
| 17 |
use RecentChange; |
| 18 |
|
| 19 |
class Hooks implements |
| 20 |
AuthManagerLoginAuthenticateAuditHook, |
| 21 |
LocalUserCreatedHook, |
| 22 |
RecentChange_saveHook |
| 23 |
{ |
| 24 |
private UserFactory $userFactory; |
| 25 |
|
| 26 |
public function __construct( UserFactory $userFactory ) { |
| 27 |
$this->userFactory = $userFactory; |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public function onAuthManagerLoginAuthenticateAudit( |
| 39 |
$ret, $user, $username, $extraData |
| 40 |
) { |
| 41 |
if ( !$user && $username !== null ) { |
| 42 |
$user = $this->userFactory->newFromName( $username, UserFactory::RIGOR_USABLE ); |
| 43 |
} |
| 44 |
|
| 45 |
if ( !$user ) { |
| 46 |
return; |
| 47 |
} |
| 48 |
|
| 49 |
if ( $ret->status === AuthenticationResponse::PASS ) { |
| 50 |
self::doSuccessfulLogin( $user ); |
| 51 |
} elseif ( |
| 52 |
$ret->status === AuthenticationResponse::FAIL |
| 53 |
&& $ret->message->getKey() !== 'login-throttled' |
| 54 |
) { |
| 55 |
self::doFailedLogin( $user ); |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
public static function doSuccessfulLogin( User $user ) { |
| 68 |
$loginNotify = LoginNotify::getInstance(); |
| 69 |
$loginNotify->clearCounters( $user ); |
| 70 |
$loginNotify->sendSuccessNotice( $user ); |
| 71 |
$loginNotify->recordKnownWithCookie( $user ); |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
public static function doFailedLogin( User $user ) { |
| 80 |
$loginNotify = LoginNotify::getInstance(); |
| 81 |
$loginNotify->recordFailure( $user ); |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
public function onLocalUserCreated( $user, $autocreated ) { |
| 94 |
if ( !$autocreated ) { |
| 95 |
$loginNotify = LoginNotify::getInstance(); |
| 96 |
$loginNotify->recordKnownWithCookie( $user ); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
public function onRecentChange_save( $recentChange ) { |
| 104 |
$loginNotify = LoginNotify::getInstance(); |
| 105 |
$user = $this->userFactory->newFromUserIdentity( $recentChange->getPerformerIdentity() ); |
| 106 |
$loginNotify->recordKnown( $user ); |
| 107 |
} |
| 108 |
} |