vendor/wns/security-compliance-suite/src/Logging/Subscriber/UserActionSubscriber.php line 57

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace WnsSecurityComplianceSuite\Logging\Subscriber;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use WnsSecurityComplianceSuite\Logging\DTO\Log;
  6. use WnsSecurityComplianceSuite\Logging\DTO\LogSource;
  7. use WnsSecurityComplianceSuite\Logging\Enum\EventTypes;
  8. use WnsSecurityComplianceSuite\Logging\Provider\SourceInfoProvider;
  9. use WnsSecurityComplianceSuite\Logging\Service\ActionLogger;
  10. use WnsSecurityComplianceSuite\Util\Event\UserLoginEvent;
  11. use WnsSecurityComplianceSuite\Util\Event\UserTwoFactorResetEvent;
  12. final class UserActionSubscriber implements EventSubscriberInterface
  13. {
  14. private ActionLogger $actionLogger;
  15. private SourceInfoProvider $sourceInfoProvider;
  16. public function __construct(
  17. ActionLogger $actionLogger,
  18. SourceInfoProvider $sourceInfoProvider
  19. ) {
  20. $this->actionLogger = $actionLogger;
  21. $this->sourceInfoProvider = $sourceInfoProvider;
  22. }
  23. public static function getSubscribedEvents()
  24. {
  25. return [
  26. UserLoginEvent::class => 'onUserLogin',
  27. UserTwoFactorResetEvent::class => 'onUserTwoFactorReset',
  28. ];
  29. }
  30. public function onUserLogin(UserLoginEvent $event): void
  31. {
  32. $eventType = $event->isSuccessful() ? EventTypes::LOGIN_SUCCESSFUL : EventTypes::LOGIN_FAILED;
  33. $detail = [
  34. 'username' => $event->getUsername(),
  35. 'success' => $event->isSuccessful(),
  36. 'errors' => $event->getErrors(),
  37. ];
  38. $log = new Log(
  39. $eventType,
  40. LogSource::createEmpty(),
  41. $detail,
  42. new \DateTimeImmutable(),
  43. );
  44. $this->actionLogger->log($log, null);
  45. }
  46. public function onUserTwoFactorReset(UserTwoFactorResetEvent $event): void
  47. {
  48. $user = $event->getUser();
  49. $detail = [
  50. 'userId' => $user->getId(),
  51. 'username' => $user->getUsername(),
  52. 'email' => $user->getEmail(),
  53. ];
  54. $log = new Log(
  55. EventTypes::TWO_FACTOR_RESET,
  56. $this->sourceInfoProvider->getSourceDTO($event->getContext()->getSource()),
  57. $detail,
  58. new \DateTimeImmutable(),
  59. );
  60. $this->actionLogger->log($log, null);
  61. }
  62. }