isClientAllowed example

public function isClientAllowed(Request $request, array $allowedIps): bool
    {
        $isAllowed = IpUtils::checkIp(
            (string) $request->getClientIp(),
            $allowedIps
        );

        $event = new MaintenanceModeRequestEvent($request$allowedIps$isAllowed);

        $this->eventDispatcher->dispatch($event);

        return $event->isClientAllowed();
    }
}

    public function shouldRedirectToShop(Request $request): bool
    {
        return !$this->isXmlHttpRequest($request)
            && !$this->isErrorControllerRequest($request)
            && !$this->isMaintenanceRequest($request);
    }

    public function shouldBeCached(Request $request): bool
    {
        return !$this->isMaintenanceModeActive() || !$this->isClientAllowed($request);
    }

    /** * isMaintenanceRequest returns true, when the maintenance mode is active and the client's IP address * is not listed in the maintenance mode whitelist. */
    public function isMaintenanceRequest(Request $request): bool
    {
        return $this->isMaintenanceModeActive() && !$this->isClientAllowed($request);
    }

    
if ($request->attributes->getBoolean(PlatformRequest::ATTRIBUTE_IS_ALLOWED_IN_MAINTENANCE)) {
            return;
        }

        try {
            /** @var string[] $allowedIps */
            $allowedIps = Json::decodeToList((string) ($salesChannelData['maintenanceIpWhitelist'] ?? ''));
        } catch (UtilException $e) {
            return;
        }

        if ($this->maintenanceModeResolver->isClientAllowed($request$allowedIps)) {
            return;
        }

        throw ApiException::salesChannelInMaintenanceMode();
    }
}
class MaintenanceModeResolverTest extends TestCase
{
    public function testIsClientAllowedTriggersEventAndReturnsFalseForDisallowedClient(): void
    {
        $eventDispatcher = $this->createMock(EventDispatcherInterface::class);

        $eventDispatcher->expects(static::once())
            ->method('dispatch')
            ->with(static::isInstanceOf(MaintenanceModeRequestEvent::class));

        $resolver = new MaintenanceModeResolver($eventDispatcher);
        static::assertFalse($resolver->isClientAllowed(new Request(server: ['REMOTE_ADDR' => '192.168.0.4'])[]));
    }

    public function testIsClientAllowedTriggersEventAndReturnsTrueForAllowedClient(): void
    {
        $eventDispatcher = $this->createMock(EventDispatcherInterface::class);

        $eventDispatcher->expects(static::once())
            ->method('dispatch')
            ->with(static::isInstanceOf(MaintenanceModeRequestEvent::class));

        $resolver = new MaintenanceModeResolver($eventDispatcher);
        

class MaintenanceModeRequestEventTest extends TestCase
{
    public function testIsClientAllowed(): void
    {
        $event = new MaintenanceModeRequestEvent(
            new Request(),
            [],
            true
        );

        static::assertTrue($event->isClientAllowed());

        $event->disallowClient();

        static::assertFalse($event->isClientAllowed());

        $event->allowClient();

        static::assertTrue($event->isClientAllowed());
    }

    public function testGetAllowedIps(): void
    {
Home | Imprint | This part of the site doesn't use cookies.