Skip to content

Commit a693c58

Browse files
refactor: rename trigger to detector
1 parent 97a2eb1 commit a693c58

7 files changed

Lines changed: 113 additions & 113 deletions

File tree

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,17 @@
1919
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2020
*/
2121

22-
namespace MoveElevator\Typo3LoginWarning\Trigger;
22+
namespace MoveElevator\Typo3LoginWarning\Detector;
2323

2424
use TYPO3\CMS\Core\Authentication\AbstractUserAuthentication;
2525

2626
/**
27-
* TriggerInterface.
27+
* DetectorInterface.
2828
*
2929
* @author Konrad Michalik <hej@konradmichalik.dev>
3030
* @license GPL-2.0
3131
*/
32-
interface TriggerInterface
32+
interface DetectorInterface
3333
{
34-
public function isTriggered(AbstractUserAuthentication $user, array $configuration = []): bool;
34+
public function detect(AbstractUserAuthentication $user, array $configuration = []): bool;
3535
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2020
*/
2121

22-
namespace MoveElevator\Typo3LoginWarning\Trigger;
22+
namespace MoveElevator\Typo3LoginWarning\Detector;
2323

2424
use Doctrine\DBAL\Exception;
2525
use MoveElevator\Typo3LoginWarning\Domain\Repository\IpLogRepository;
@@ -28,12 +28,12 @@
2828
use TYPO3\CMS\Core\Utility\GeneralUtility;
2929

3030
/**
31-
* NewIp.
31+
* NewIpDetector.
3232
*
3333
* @author Konrad Michalik <hej@konradmichalik.dev>
3434
* @license GPL-2.0
3535
*/
36-
class NewIp implements TriggerInterface
36+
class NewIpDetector implements DetectorInterface
3737
{
3838
private ?array $locationData = null;
3939

@@ -45,7 +45,7 @@ public function __construct(
4545
/**
4646
* @throws Exception
4747
*/
48-
public function isTriggered(AbstractUserAuthentication $user, array $configuration = []): bool
48+
public function detect(AbstractUserAuthentication $user, array $configuration = []): bool
4949
{
5050
$userArray = $user->user;
5151
if (

Classes/Security/LoginNotification.php

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424
namespace MoveElevator\Typo3LoginWarning\Security;
2525

2626
use MoveElevator\Typo3LoginWarning\Configuration;
27+
use MoveElevator\Typo3LoginWarning\Detector\DetectorInterface;
28+
use MoveElevator\Typo3LoginWarning\Detector\NewIpDetector;
2729
use MoveElevator\Typo3LoginWarning\Notification\NotifierInterface;
28-
use MoveElevator\Typo3LoginWarning\Trigger\NewIp;
29-
use MoveElevator\Typo3LoginWarning\Trigger\TriggerInterface;
3030
use Psr\Log\LoggerAwareInterface;
3131
use Psr\Log\LoggerAwareTrait;
3232
use TYPO3\CMS\Core\Attribute\AsEventListener;
@@ -54,51 +54,51 @@ public function emailAtLogin(AfterUserLoggedInEvent $event): void
5454
}
5555
$currentUser = $event->getUser();
5656

57-
$currentTrigger = null;
58-
$currentTriggerConfiguration = [];
57+
$currentDetector = null;
58+
$currentDetectorConfiguration = [];
5959

60-
// Check configured triggers
61-
foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF'][Configuration::EXT_KEY]['trigger'] as $triggerClass => $triggerConfiguration) {
62-
$triggerHasConfiguration = !is_int($triggerClass);
63-
if ($triggerHasConfiguration) {
64-
$currentTriggerConfiguration = $triggerConfiguration;
60+
// Check configured detectors
61+
foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF'][Configuration::EXT_KEY]['detector'] as $detectorClass => $detectorConfiguration) {
62+
$detectorHasConfiguration = !is_int($detectorClass);
63+
if ($detectorHasConfiguration) {
64+
$currentDetectorConfiguration = $detectorConfiguration;
6565
} else {
66-
$triggerClass = $triggerConfiguration;
67-
$currentTriggerConfiguration = [];
66+
$detectorClass = $detectorConfiguration;
67+
$currentDetectorConfiguration = [];
6868
}
6969

70-
$trigger = GeneralUtility::makeInstance($triggerClass);
70+
$detector = GeneralUtility::makeInstance($detectorClass);
7171

72-
if (!$trigger instanceof TriggerInterface) {
73-
$this->logger->warning('Configured trigger class "{class}" does not implement MoveElevator\Typo3LoginWarning\Security\TriggerInterface', [
74-
'class' => $triggerClass,
72+
if (!$detector instanceof DetectorInterface) {
73+
$this->logger->warning('Configured detector class "{class}" does not implement MoveElevator\Typo3LoginWarning\Detector\DetectorInterface', [
74+
'class' => $detectorClass,
7575
]);
7676
continue;
7777
}
7878

79-
// Merge with trigger default configuration
80-
$defaultConfig = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF'][Configuration::EXT_KEY]['_trigger'][$trigger::class] ?? [];
81-
$currentTriggerConfiguration = array_merge($defaultConfig, $currentTriggerConfiguration);
79+
// Merge with detector default configuration
80+
$defaultConfig = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF'][Configuration::EXT_KEY]['_detector'][$detector::class] ?? [];
81+
$currentDetectorConfiguration = array_merge($defaultConfig, $currentDetectorConfiguration);
8282

83-
if ($trigger->isTriggered($currentUser, $currentTriggerConfiguration)) {
84-
$currentTrigger = $trigger;
83+
if ($detector->detect($currentUser, $currentDetectorConfiguration)) {
84+
$currentDetector = $detector;
8585
break;
8686
}
8787
}
8888

89-
if ($currentTrigger === null) {
89+
if ($currentDetector === null) {
9090
return;
9191
}
9292

9393
// Fallback to global notification configuration
94-
if (!array_key_exists('notification', $currentTriggerConfiguration)) {
95-
$currentTriggerConfiguration = [
94+
if (!array_key_exists('notification', $currentDetectorConfiguration)) {
95+
$currentDetectorConfiguration = [
9696
'notification' => $GLOBALS['TYPO3_CONF_VARS']['EXTCONF'][Configuration::EXT_KEY]['_notification'],
9797
];
9898
}
9999

100100
// Send notifications
101-
foreach ($currentTriggerConfiguration['notification'] as $notificationClass => $notificationConfiguration) {
101+
foreach ($currentDetectorConfiguration['notification'] as $notificationClass => $notificationConfiguration) {
102102
$notifier = GeneralUtility::makeInstance($notificationClass);
103103

104104
if (!$notifier instanceof NotifierInterface) {
@@ -109,14 +109,14 @@ public function emailAtLogin(AfterUserLoggedInEvent $event): void
109109
}
110110

111111
$additionalData = [];
112-
if ($currentTrigger instanceof NewIp) {
113-
$additionalData['locationData'] = $currentTrigger->getLocationData();
112+
if ($currentDetector instanceof NewIpDetector) {
113+
$additionalData['locationData'] = $currentDetector->getLocationData();
114114
}
115115

116116
$notifier->notify(
117117
$currentUser,
118118
$event->getRequest() ?? $GLOBALS['TYPO3_REQUEST'] ?? ServerRequestFactory::fromGlobals()->withAttribute('applicationType', SystemEnvironmentBuilder::REQUESTTYPE_BE),
119-
$currentTrigger::class,
119+
$currentDetector::class,
120120
$notificationConfiguration,
121121
$additionalData
122122
);

README.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,22 +45,22 @@ Download the zip file from [TYPO3 extension repository (TER)](https://extensions
4545

4646
## 🧰 Configuration
4747

48-
Add a warning trigger in your `ext_localconf.php`:
48+
Add a warning detector in your `ext_localconf.php`:
4949

5050
```php
5151
use MoveElevator\Typo3LoginWarning\Configuration;
5252
use MoveElevator\Typo3LoginWarning\Notification\EmailNotification;
53-
use MoveElevator\Typo3LoginWarning\Trigger\NewIp;
53+
use MoveElevator\Typo3LoginWarning\Detector\NewIpDetector;
5454

5555
// Simple configuration
5656
// (EmailNotification will be used with "warning_email_addr" configuration)
57-
$GLOBALS['TYPO3_CONF_VARS']['EXTCONF'][Configuration::EXT_KEY]['trigger'] = [
58-
NewIp::class
57+
$GLOBALS['TYPO3_CONF_VARS']['EXTCONF'][Configuration::EXT_KEY]['detector'] = [
58+
NewIpDetector::class
5959
];
6060

6161
// Extended example configuration
62-
$GLOBALS['TYPO3_CONF_VARS']['EXTCONF'][Configuration::EXT_KEY]['trigger'] = [
63-
NewIp::class => [
62+
$GLOBALS['TYPO3_CONF_VARS']['EXTCONF'][Configuration::EXT_KEY]['detector'] = [
63+
NewIpDetector::class => [
6464
'hashIpAddress' => false,
6565
'fetchGeolocation' => false,
6666
'whitelist' => [
@@ -77,18 +77,18 @@ $GLOBALS['TYPO3_CONF_VARS']['EXTCONF'][Configuration::EXT_KEY]['trigger'] = [
7777

7878
## 💡 Concepts
7979

80-
### Trigger
80+
### Detectors
8181

82-
Triggers are used to detect certain login events. If a trigger matches, a notification will be sent.
82+
Detectors are used to detect certain login events. If a detector matches, a notification will be sent.
8383

84-
The following triggers are available:
84+
The following detectors are available:
8585

86-
- `NewIp`: Triggers a warning email if a backend user logs in from a new IP address. The IP address will be stored and can be hashed for privacy reasons. You can also define a whitelist of IP addresses that will not trigger a warning. An ip geolocation lookup can be enabled to add more information to the notification email.
86+
- `NewIpDetector`: Detects logins from new IP addresses and triggers a warning email. The IP address will be stored and can be hashed for privacy reasons. You can also define a whitelist of IP addresses that will not trigger a warning. An ip geolocation lookup can be enabled to add more information to the notification email.
8787

8888
![email.jpg](Documentation/Images/email.jpg)
8989

9090
> [!TIP]
91-
> You can implement your own trigger by implementing the `MoveElevator\Typo3LoginWarning\Trigger\TriggerInterface`.
91+
> You can implement your own detector by implementing the `MoveElevator\Typo3LoginWarning\Detector\DetectorInterface`.
9292
9393
### Notification
9494

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,23 @@
2121
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2222
*/
2323

24-
namespace MoveElevator\Typo3LoginWarning\Tests\Unit\Trigger;
24+
namespace MoveElevator\Typo3LoginWarning\Tests\Unit\Detector;
2525

26+
use MoveElevator\Typo3LoginWarning\Detector\DetectorInterface;
27+
use MoveElevator\Typo3LoginWarning\Detector\NewIpDetector;
2628
use MoveElevator\Typo3LoginWarning\Domain\Repository\IpLogRepository;
2729
use MoveElevator\Typo3LoginWarning\Service\IpApiGeolocationService;
28-
use MoveElevator\Typo3LoginWarning\Trigger\NewIp;
29-
use MoveElevator\Typo3LoginWarning\Trigger\TriggerInterface;
3030
use PHPUnit\Framework\MockObject\MockObject;
3131
use PHPUnit\Framework\TestCase;
3232
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
3333

3434
/**
35-
* NewIpTest.
35+
* NewIpDetectorTest.
3636
*
3737
* @author Konrad Michalik <hej@konradmichalik.dev>
3838
* @license GPL-2.0
3939
*/
40-
final class NewIpTest extends TestCase
40+
final class NewIpDetectorTest extends TestCase
4141
{
4242
protected function setUp(): void
4343
{
@@ -52,14 +52,14 @@ protected function tearDown(): void
5252
unset($GLOBALS['_SERVER']['REMOTE_ADDR']);
5353
}
5454

55-
public function testImplementsTriggerInterface(): void
55+
public function testImplementsDetectorInterface(): void
5656
{
5757
$ipLogRepository = $this->createMock(IpLogRepository::class);
58-
$subject = new NewIp($ipLogRepository);
59-
self::assertInstanceOf(TriggerInterface::class, $subject);
58+
$subject = new NewIpDetector($ipLogRepository);
59+
self::assertInstanceOf(DetectorInterface::class, $subject);
6060
}
6161

62-
public function testIsTriggeredReturnsFalseWhenIpIsWhitelisted(): void
62+
public function testDetectReturnsFalseWhenIpIsWhitelisted(): void
6363
{
6464
$user = $this->createMockUser(['uid' => 123]);
6565
$configuration = [
@@ -69,13 +69,13 @@ public function testIsTriggeredReturnsFalseWhenIpIsWhitelisted(): void
6969
$GLOBALS['_SERVER']['REMOTE_ADDR'] = '192.168.1.1';
7070

7171
$ipLogRepository = $this->createMock(IpLogRepository::class);
72-
$subject = new NewIp($ipLogRepository);
73-
$result = $subject->isTriggered($user, $configuration);
72+
$subject = new NewIpDetector($ipLogRepository);
73+
$result = $subject->detect($user, $configuration);
7474

7575
self::assertFalse($result);
7676
}
7777

78-
public function testIsTriggeredReturnsTrueWhenIpIsNew(): void
78+
public function testDetectReturnsTrueWhenIpIsNew(): void
7979
{
8080
$user = $this->createMockUser(['uid' => 123]);
8181
$configuration = ['hashIpAddress' => true];
@@ -94,13 +94,13 @@ public function testIsTriggeredReturnsTrueWhenIpIsNew(): void
9494
->method('addUserIp')
9595
->with(123, self::matchesRegularExpression('/.*/'));
9696

97-
$subject = new NewIp($ipLogRepository);
98-
$result = $subject->isTriggered($user, $configuration);
97+
$subject = new NewIpDetector($ipLogRepository);
98+
$result = $subject->detect($user, $configuration);
9999

100100
self::assertTrue($result);
101101
}
102102

103-
public function testIsTriggeredReturnsFalseWhenIpExists(): void
103+
public function testDetectReturnsFalseWhenIpExists(): void
104104
{
105105
$user = $this->createMockUser(['uid' => 123]);
106106
$configuration = ['hashIpAddress' => true];
@@ -118,13 +118,13 @@ public function testIsTriggeredReturnsFalseWhenIpExists(): void
118118
->expects(self::never())
119119
->method('addUserIp');
120120

121-
$subject = new NewIp($ipLogRepository);
122-
$result = $subject->isTriggered($user, $configuration);
121+
$subject = new NewIpDetector($ipLogRepository);
122+
$result = $subject->detect($user, $configuration);
123123

124124
self::assertFalse($result);
125125
}
126126

127-
public function testIsTriggeredWithoutHashingWhenConfigured(): void
127+
public function testDetectWithoutHashingWhenConfigured(): void
128128
{
129129
$user = $this->createMockUser(['uid' => 123]);
130130
$configuration = ['hashIpAddress' => false];
@@ -143,13 +143,13 @@ public function testIsTriggeredWithoutHashingWhenConfigured(): void
143143
->method('addUserIp')
144144
->with(123, self::matchesRegularExpression('/.*/'));
145145

146-
$subject = new NewIp($ipLogRepository);
147-
$result = $subject->isTriggered($user, $configuration);
146+
$subject = new NewIpDetector($ipLogRepository);
147+
$result = $subject->detect($user, $configuration);
148148

149149
self::assertTrue($result);
150150
}
151151

152-
public function testIsTriggeredDefaultsToHashingWhenNotConfigured(): void
152+
public function testDetectDefaultsToHashingWhenNotConfigured(): void
153153
{
154154
$user = $this->createMockUser(['uid' => 123]);
155155
$configuration = [];
@@ -168,13 +168,13 @@ public function testIsTriggeredDefaultsToHashingWhenNotConfigured(): void
168168
->method('addUserIp')
169169
->with(123, self::matchesRegularExpression('/.*/'));
170170

171-
$subject = new NewIp($ipLogRepository);
172-
$result = $subject->isTriggered($user, $configuration);
171+
$subject = new NewIpDetector($ipLogRepository);
172+
$result = $subject->detect($user, $configuration);
173173

174174
self::assertTrue($result);
175175
}
176176

177-
public function testIsTriggeredDoesNotFetchGeolocationWhenDisabled(): void
177+
public function testDetectDoesNotFetchGeolocationWhenDisabled(): void
178178
{
179179
$user = $this->createMockUser(['uid' => 123]);
180180
$configuration = [
@@ -200,8 +200,8 @@ public function testIsTriggeredDoesNotFetchGeolocationWhenDisabled(): void
200200
->expects(self::once())
201201
->method('addUserIp');
202202

203-
$subject = new NewIp($ipLogRepository, $geolocationService);
204-
$result = $subject->isTriggered($user, $configuration);
203+
$subject = new NewIpDetector($ipLogRepository, $geolocationService);
204+
$result = $subject->detect($user, $configuration);
205205

206206
self::assertTrue($result);
207207
self::assertNull($subject->getLocationData());
@@ -210,11 +210,11 @@ public function testIsTriggeredDoesNotFetchGeolocationWhenDisabled(): void
210210
public function testGetLocationDataReturnsNullInitially(): void
211211
{
212212
$ipLogRepository = $this->createMock(IpLogRepository::class);
213-
$subject = new NewIp($ipLogRepository);
213+
$subject = new NewIpDetector($ipLogRepository);
214214
self::assertNull($subject->getLocationData());
215215
}
216216

217-
public function testIsTriggeredDoesNotFetchGeolocationForPrivateIps(): void
217+
public function testDetectDoesNotFetchGeolocationForPrivateIps(): void
218218
{
219219
$user = $this->createMockUser(['uid' => 123]);
220220
$configuration = [
@@ -240,8 +240,8 @@ public function testIsTriggeredDoesNotFetchGeolocationForPrivateIps(): void
240240
->expects(self::once())
241241
->method('addUserIp');
242242

243-
$subject = new NewIp($ipLogRepository, $geolocationService);
244-
$result = $subject->isTriggered($user, $configuration);
243+
$subject = new NewIpDetector($ipLogRepository, $geolocationService);
244+
$result = $subject->detect($user, $configuration);
245245

246246
self::assertTrue($result);
247247
self::assertNull($subject->getLocationData());

0 commit comments

Comments
 (0)