Debian Codegraph @

extensions/LoginNotify/includes/Hooks.php

raw symbols SCIP
1 <?php
2 /**
3 * @file
4 * @ingroup Extensions
5 */
6
7 // phpcs:disable MediaWiki.NamingConventions.LowerCamelFunctionsName
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 * Hook for login auditing
32 *
33 * @param AuthenticationResponse $ret Is login successful?
34 * @param User|null $user User object on successful auth
35 * @param string|null $username Username for failed attempts.
36 * @param string[] $extraData
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 // Other statuses include Abstain, Redirect, or UI. We ignore such
58 // statuses.
59 }
60
61 /**
62 * Handle a successful login (clear the attempt counter, send a notice, and record the
63 * current IP address as known).
64 *
65 * @param User $user The user who logged in.
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 * Handle a failed login (record the failure).
76 *
77 * @param User $user The user that failed to log in.
78 */
79 public static function doFailedLogin( User $user ) {
80 $loginNotify = LoginNotify::getInstance();
81 $loginNotify->recordFailure( $user );
82 }
83
84 /**
85 * Hook handler for new account creation.
86 *
87 * Called immediately after a local user has been created and saved to the database
88 *
89 * @todo This still sets cookies if user creates account well logged in as someone else.
90 * @param User $user User created
91 * @param bool $autocreated Whether this was an auto-created account
92 */
93 public function onLocalUserCreated( $user, $autocreated ) {
94 if ( !$autocreated ) {
95 $loginNotify = LoginNotify::getInstance();
96 $loginNotify->recordKnownWithCookie( $user );
97 }
98 }
99
100 /**
101 * @param RecentChange $recentChange
102 */
103 public function onRecentChange_save( $recentChange ) {
104 $loginNotify = LoginNotify::getInstance();
105 $user = $this->userFactory->newFromUserIdentity( $recentChange->getPerformerIdentity() );
106 $loginNotify->recordKnown( $user );
107 }
108 }