Skip to content

Commit

Permalink
Rebase on 1.0-dev
Browse files Browse the repository at this point in the history
  • Loading branch information
acornforth committed Mar 9, 2022
1 parent 706f594 commit 413f83c
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 177 deletions.
111 changes: 0 additions & 111 deletions src/DataDog/AuditBundle/Entity/AuditLog.php

This file was deleted.

26 changes: 0 additions & 26 deletions src/DataDog/AuditBundle/Resources/config/doctrine/AuditLog.orm.xml

This file was deleted.

31 changes: 0 additions & 31 deletions src/DataDog/AuditBundle/Resources/config/doctrine/AuditLog.orm.yml

This file was deleted.

12 changes: 11 additions & 1 deletion src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,18 @@ public function getConfigTreeBuilder(): TreeBuilder
->end()
->children()
->booleanNode('blame_impersonator')
->defaultFalse()
->defaultFalse()
->end()
->end()
->children()
->booleanNode('log_user_ip')
->defaultFalse()
->end()
->booleanNode('log_user_agent')
->defaultFalse()
->end()
->end()

;
// @formatter:on

Expand Down
7 changes: 7 additions & 0 deletions src/DependencyInjection/DataDogAuditExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,12 @@ public function load(array $configs, ContainerBuilder $container): void
if (isset($config['blame_impersonator'])) {
$auditSubscriber->addMethodCall('setBlameImpersonator', array($config['blame_impersonator']));
}

if (isset($config['log_user_ip'])) {
$auditSubscriber->addMethodCall('setLogIp', array($config['log_user_ip']));
}
if (isset($config['log_user_agent'])) {
$auditSubscriber->addMethodCall('setLogUserAgent', array($config['log_user_agent']));
}
}
}
14 changes: 14 additions & 0 deletions src/Entity/AuditLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ class AuditLog

private string $action;

private ?string $ip;

private ?string $userAgent;

private string $tbl;

private Association $source;
Expand Down Expand Up @@ -59,4 +63,14 @@ public function getLoggedAt(): \DateTimeInterface
{
return $this->loggedAt;
}

public function getIp(): ?string
{
return $this->ip;
}

public function getUserAgent(): ?string
{
return $this->userAgent;
}
}
35 changes: 27 additions & 8 deletions src/EventSubscriber/AuditSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Doctrine\ORM\Event\OnFlushEventArgs;
use Doctrine\ORM\Events;
use Doctrine\ORM\Mapping\ClassMetadataInfo;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Role\SwitchUserRole;
Expand Down Expand Up @@ -52,9 +53,19 @@ class AuditSubscriber implements EventSubscriber

protected TokenStorageInterface $securityTokenStorage;

public function __construct(TokenStorageInterface $securityTokenStorage)
private RequestStack $requestStack;

private bool $logIp = false;

private bool $logUserAgent = false;

public function __construct(
TokenStorageInterface $securityTokenStorage,
RequestStack $requestStack
)
{
$this->securityTokenStorage = $securityTokenStorage;
$this->requestStack = $requestStack;
}

public function setLabeler(?callable $labeler = null): self
Expand Down Expand Up @@ -346,14 +357,12 @@ protected function audit(EntityManager $em, array $data)
$data[$field] = $meta->idGenerator->generate($em, null);
}
// Log the ip address.
$masterRequest = $this->requestStack->getMasterRequest();
$mainRequest = $this->requestStack->getMasterRequest();
// use this instead when support for symfony <5.3 dropped
// $masterRequest = $this->requestStack->getMainRequest();
if ($masterRequest !== null) {
$data['ip'] = $masterRequest->getClientIp();
} else {
$data['ip'] = null;
}
// $mainRequest = $this->requestStack->getMainRequest();

$data['ip'] = $this->logIp && $mainRequest ? $mainRequest->getClientIp() : null;
$data['userAgent'] = $this->logUserAgent && $mainRequest ? substr($mainRequest->headers->get('User-Agent'), 0, 255) : null;

$meta = $em->getClassMetadata(AuditLog::class);
$data['loggedAt'] = new \DateTime();
Expand Down Expand Up @@ -543,4 +552,14 @@ public function setBlameUser(UserInterface $user)
{
$this->blameUser = $user;
}

public function setLogIp(bool $logIp): void
{
$this->logIp = $logIp;
}

public function setLogUserAgent(bool $logUserAgent): void
{
$this->logUserAgent = $logUserAgent;
}
}
2 changes: 2 additions & 0 deletions src/Resources/config/doctrine/AuditLog.orm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
<id name="id" type="bigint" column="id">
<generator strategy="AUTO" />
</id>
<field name="ip" nullable="true" type="string" length="32"/>
<field name="userAgent" nullable="true" type="string" length="255"/>
<field name="action" length="12" />
<field name="tbl" length="128" />
<one-to-one field="source" target-entity="Association">
Expand Down
2 changes: 2 additions & 0 deletions src/Resources/config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use DataDog\AuditBundle\EventSubscriber\AuditSubscriber;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;

return static function (ContainerConfigurator $container) {
Expand All @@ -12,6 +13,7 @@
$services
->set('datadog.event_subscriber.audit', AuditSubscriber::class)->private()
->arg(0, new Reference(TokenStorageInterface::class))
->arg(1, new Reference(RequestStack::class))
->tag('doctrine.event_subscriber')
;
// @formatter:on
Expand Down

0 comments on commit 413f83c

Please sign in to comment.