diff --git a/composer.json b/composer.json index 1bb4ac25..c1fd4d5c 100644 --- a/composer.json +++ b/composer.json @@ -16,6 +16,7 @@ "ext-json": "*", "ext-pdo": "*", "acelaya/ze-content-based-error-handler": "^2.2", + "akrabat/ip-address-middleware": "^1.0", "cakephp/chronos": "^1.2", "cocur/slugify": "^3.0", "doctrine/cache": "^1.6", diff --git a/module/Common/config/dependencies.config.php b/module/Common/config/dependencies.config.php index e9422d38..6278d63b 100644 --- a/module/Common/config/dependencies.config.php +++ b/module/Common/config/dependencies.config.php @@ -1,16 +1,14 @@ [ 'factories' => [ EntityManager::class => Factory\EntityManagerFactory::class, - GuzzleHttp\Client::class => InvokableFactory::class, + GuzzleClient::class => InvokableFactory::class, Cache::class => Factory\CacheFactory::class, 'Logger_Shlink' => Factory\LoggerFactory::class, Filesystem::class => InvokableFactory::class, Translator::class => Factory\TranslatorFactory::class, - TranslatorExtension::class => ConfigAbstractFactory::class, - LocaleMiddleware::class => ConfigAbstractFactory::class, + Template\Extension\TranslatorExtension::class => ConfigAbstractFactory::class, + + Middleware\LocaleMiddleware::class => ConfigAbstractFactory::class, + IpAddress::class => Middleware\IpAddressMiddlewareFactory::class, Image\ImageBuilder::class => Image\ImageBuilderFactory::class, @@ -37,7 +37,7 @@ return [ ], 'aliases' => [ 'em' => EntityManager::class, - 'httpClient' => GuzzleHttp\Client::class, + 'httpClient' => GuzzleClient::class, 'translator' => Translator::class, 'logger' => LoggerInterface::class, Logger::class => 'Logger_Shlink', @@ -49,11 +49,11 @@ return [ ], ConfigAbstractFactory::class => [ - TranslatorExtension::class => ['translator'], - LocaleMiddleware::class => ['translator'], + Template\Extension\TranslatorExtension::class => ['translator'], + Middleware\LocaleMiddleware::class => ['translator'], Service\IpApiLocationResolver::class => ['httpClient'], Service\PreviewGenerator::class => [ - ImageBuilder::class, + Image\ImageBuilder::class, Filesystem::class, 'config.preview_generation.files_location', ], diff --git a/module/Common/src/Middleware/IpAddressMiddlewareFactory.php b/module/Common/src/Middleware/IpAddressMiddlewareFactory.php new file mode 100644 index 00000000..5d988a2e --- /dev/null +++ b/module/Common/src/Middleware/IpAddressMiddlewareFactory.php @@ -0,0 +1,27 @@ +factory = new IpAddressMiddlewareFactory(); + } + + /** + * @test + */ + public function returnedInstanceIsProperlyConfigured() + { + $instance = $this->factory->__invoke(new ServiceManager(), ''); + + $ref = new ReflectionObject($instance); + $checkProxyHeaders = $ref->getProperty('checkProxyHeaders'); + $checkProxyHeaders->setAccessible(true); + $trustedProxies = $ref->getProperty('trustedProxies'); + $trustedProxies->setAccessible(true); + + $this->assertTrue($checkProxyHeaders->getValue($instance)); + $this->assertEquals([], $trustedProxies->getValue($instance)); + } +}