<?php
declare(strict_types=1);
namespace WnsSecurityComplianceSuite\Logging\Subscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use WnsSecurityComplianceSuite\Logging\DTO\Log;
use WnsSecurityComplianceSuite\Logging\DTO\LogSource;
use WnsSecurityComplianceSuite\Logging\Enum\EventTypes;
use WnsSecurityComplianceSuite\Logging\Provider\SourceInfoProvider;
use WnsSecurityComplianceSuite\Logging\Service\ActionLogger;
use WnsSecurityComplianceSuite\Util\Event\UserLoginEvent;
use WnsSecurityComplianceSuite\Util\Event\UserTwoFactorResetEvent;
final class UserActionSubscriber implements EventSubscriberInterface
{
private ActionLogger $actionLogger;
private SourceInfoProvider $sourceInfoProvider;
public function __construct(
ActionLogger $actionLogger,
SourceInfoProvider $sourceInfoProvider
) {
$this->actionLogger = $actionLogger;
$this->sourceInfoProvider = $sourceInfoProvider;
}
public static function getSubscribedEvents()
{
return [
UserLoginEvent::class => 'onUserLogin',
UserTwoFactorResetEvent::class => 'onUserTwoFactorReset',
];
}
public function onUserLogin(UserLoginEvent $event): void
{
$eventType = $event->isSuccessful() ? EventTypes::LOGIN_SUCCESSFUL : EventTypes::LOGIN_FAILED;
$detail = [
'username' => $event->getUsername(),
'success' => $event->isSuccessful(),
'errors' => $event->getErrors(),
];
$log = new Log(
$eventType,
LogSource::createEmpty(),
$detail,
new \DateTimeImmutable(),
);
$this->actionLogger->log($log, null);
}
public function onUserTwoFactorReset(UserTwoFactorResetEvent $event): void
{
$user = $event->getUser();
$detail = [
'userId' => $user->getId(),
'username' => $user->getUsername(),
'email' => $user->getEmail(),
];
$log = new Log(
EventTypes::TWO_FACTOR_RESET,
$this->sourceInfoProvider->getSourceDTO($event->getContext()->getSource()),
$detail,
new \DateTimeImmutable(),
);
$this->actionLogger->log($log, null);
}
}