Skip to content

Commit fe8edbf

Browse files
authored
feat: add option to omit the ip address when saving redirections (#20)
Fixes #19
1 parent 7484eca commit fe8edbf

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/Resources/config/config.xml

+10
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<card>
55
<title>Cleanup</title>
66
<title lang="de-DE">Aufräumen</title>
7+
78
<input-field type="int">
89
<name>deleteRequestsAfterDays</name>
910
<label>Delete requests after x days</label>
@@ -12,5 +13,14 @@
1213
<helpText lang="de-DE">Dies wird auch zur Berechnung der Anzahl im Listing verwendet</helpText>
1314
<defaultValue>30</defaultValue>
1415
</input-field>
16+
17+
<input-field type="bool">
18+
<name>saveIpAddresses</name>
19+
<label>Save IP addresses</label>
20+
<label lang="de-DE">IP-Adressen speichern</label>
21+
<helpText>Save IP addresses of requests</helpText>
22+
<helpText lang="de-DE">IP-Adressen von Aufrufen speichern</helpText>
23+
<defaultValue>true</defaultValue>
24+
</input-field>
1525
</card>
1626
</config>

src/Subscriber/ExceptionSubscriber.php

+23-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Shopware\Core\System\SalesChannel\Context\SalesChannelContextFactory;
1818
use Shopware\Core\System\SalesChannel\Context\SalesChannelContextService;
1919
use Shopware\Core\System\SalesChannel\SalesChannelContext;
20+
use Shopware\Core\System\SystemConfig\SystemConfigService;
2021
use Shopware\Storefront\Framework\Routing\RequestTransformer;
2122
use Symfony\Component\DependencyInjection\Attribute\Autowire;
2223
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
@@ -36,6 +37,7 @@ public function __construct(
3637
private readonly EntityRepository $tinectRedirectsRedirectRepository,
3738
private readonly SeoUrlPlaceholderHandlerInterface $seoUrlPlaceholderHandler,
3839
#[Autowire(service: SalesChannelContextFactory::class)] private readonly AbstractSalesChannelContextFactory $salesChannelContextFactory,
40+
private readonly SystemConfigService $systemConfigService,
3941
private readonly MessageBusInterface $messageBus,
4042
) {
4143
}
@@ -90,7 +92,7 @@ private function handleRequest(Request $request): ?Response
9092
$message = new TinectRedirectUpdateMessage(
9193
source: $path,
9294
salesChannelDomainId: $salesChannelDomainId,
93-
ipAddress: $request->getClientIp() ?? '',
95+
ipAddress: $this->getIpAddress($request),
9496
userAgent: $request->headers->get('User-Agent') ?? '',
9597
);
9698

@@ -162,4 +164,24 @@ private function createSalesChannelContext(string $salesChannelId, string $langu
162164
{
163165
return $this->salesChannelContextFactory->create('', $salesChannelId, [SalesChannelContextService::LANGUAGE_ID => $languageId]);
164166
}
167+
168+
private function getIpAddress(Request $request): string
169+
{
170+
$salesChannelId = $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_ID);
171+
172+
if (!\is_string($salesChannelId) || empty($salesChannelId)) {
173+
$salesChannelId = null;
174+
}
175+
176+
if (!$this->canSaveIpAddress($salesChannelId)) {
177+
return '';
178+
}
179+
180+
return $request->getClientIp() ?? '';
181+
}
182+
183+
private function canSaveIpAddress(?string $salesChannelId): bool
184+
{
185+
return $this->systemConfigService->getBool('TinectRedirects.config.saveIpAddresses', $salesChannelId);
186+
}
165187
}

0 commit comments

Comments
 (0)