Loading config/install/geoblock.settings.yml +1 −0 Original line number Diff line number Diff line applicable_methods: ['CONNECT', 'DELETE', 'PATCH', 'POST', 'PUT'] data_source: '' enable_logging: false require_domestic_use: false restriction_country_codes: [] restriction_type: '' config/schema/geoblock.schema.yml +3 −0 Original line number Diff line number Diff line Loading @@ -11,6 +11,9 @@ geoblock.settings: data_source: type: string label: 'Data source' enable_logging: type: boolean label: 'Enable logging' require_domestic_use: type: boolean label: 'Require domestic use' Loading geoblock.services.yml +1 −1 Original line number Diff line number Diff line services: geoblock.request_handler: class: '\Drupal\geoblock\EventSubscriber\RequestHandler' arguments: ['@config.factory', '@plugin.manager.geoblock_data_source'] arguments: ['@config.factory', '@plugin.manager.geoblock_data_source', '@logger.factory'] tags: - { name: event_subscriber } - { name: service_collector, tag: geoblock_restriction, call: addRestriction } Loading src/EventSubscriber/RequestHandler.php +50 −1 Original line number Diff line number Diff line Loading @@ -6,12 +6,15 @@ namespace Drupal\geoblock\EventSubscriber; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Config\ImmutableConfig; use Drupal\Core\Logger\LoggerChannelFactoryInterface; use Drupal\geoblock\Plugin\GeoblockDataSourcePluginInterface; use Drupal\geoblock\Plugin\GeoblockDataSourcePluginManagerInterface; use Drupal\geoblock\Restriction\RestrictionInterface; use Drupal\geoblock\IPAddress; use Psr\Log\LoggerInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; Loading Loading @@ -44,6 +47,13 @@ class RequestHandler implements EventSubscriberInterface { */ protected $geoblockDataSourcePluginManager; /** * The logger factory service. * * @var \Drupal\Core\Logger\LoggerChannelFactoryInterface */ protected $loggerFactory; /** * A collection of restrictions to enforce. * Loading @@ -54,9 +64,10 @@ class RequestHandler implements EventSubscriberInterface { /** * Constructs a RequestHandler object. */ public function __construct(ConfigFactoryInterface $config_factory, GeoblockDataSourcePluginManagerInterface $geoblock_data_source_plugin_manager) { public function __construct(ConfigFactoryInterface $config_factory, GeoblockDataSourcePluginManagerInterface $geoblock_data_source_plugin_manager, LoggerChannelFactoryInterface $logger_factory) { $this->configFactory = $config_factory; $this->geoblockDataSourcePluginManager = $geoblock_data_source_plugin_manager; $this->loggerFactory = $logger_factory; } /** Loading Loading @@ -115,6 +126,8 @@ class RequestHandler implements EventSubscriberInterface { 'Content-Type' => 'text/plain', ])); $this->logRestrictionEnforcement($restriction, $event->getRequest(), $address); return; } } Loading Loading @@ -202,6 +215,42 @@ class RequestHandler implements EventSubscriberInterface { return $address; } /** * Conditionally log when a restriction was enforced. * * This method will log when a restriction was enforced for a given request * and address, but only if logging is enabled in configuration. * * @param \Drupal\geoblock\Restriction\RestrictionInterface $restriction * The restriction that was enforced. * @param \Symfony\Component\HttpFoundation\Request $request * The request object. * @param \Drupal\geoblock\IPAddress $address * The IP address. */ protected function logRestrictionEnforcement(RestrictionInterface $restriction, Request $request, IPAddress $address): void { if (!empty($this->config()->get('enable_logging'))) { $this->logger()->info('<code>@type</code> was enforced on a @request_method request for <code>@request_uri</code> by @address (country code: @country_code, registered country code: @registered_country_code).', [ '@type' => \get_class($restriction), '@request_method' => $request->getRealMethod(), '@request_uri' => $request->getRequestUri(), '@address' => $address->getAddress(), '@country_code' => $address->getCountryCode() ?? 'unknown', '@registered_country_code' => $address->getRegisteredCountryCode() ?? 'unknown', ]); } } /** * Get the logger channel for this module. * * @return \Psr\Log\LoggerInterface * The logger channel for this module. */ protected function logger(): LoggerInterface { return $this->loggerFactory->get('geoblock'); } /** * Attempt to enforce geographical restrictions where applicable. * Loading src/Form/SettingsForm.php +7 −0 Original line number Diff line number Diff line Loading @@ -85,6 +85,13 @@ class SettingsForm extends ConfigFormBase { '#multiple' => TRUE, ]; $form['enable_logging'] = [ '#type' => 'checkbox', '#title' => $this->t('Enable logging'), '#description' => $this->t('If enabled, log messages will be recorded when a restriction has been enforced.'), '#default_value' => $config->get('enable_logging'), ]; $form['require_domestic_use'] = [ '#type' => 'checkbox', '#title' => $this->t('Require domestic use'), Loading Loading
config/install/geoblock.settings.yml +1 −0 Original line number Diff line number Diff line applicable_methods: ['CONNECT', 'DELETE', 'PATCH', 'POST', 'PUT'] data_source: '' enable_logging: false require_domestic_use: false restriction_country_codes: [] restriction_type: ''
config/schema/geoblock.schema.yml +3 −0 Original line number Diff line number Diff line Loading @@ -11,6 +11,9 @@ geoblock.settings: data_source: type: string label: 'Data source' enable_logging: type: boolean label: 'Enable logging' require_domestic_use: type: boolean label: 'Require domestic use' Loading
geoblock.services.yml +1 −1 Original line number Diff line number Diff line services: geoblock.request_handler: class: '\Drupal\geoblock\EventSubscriber\RequestHandler' arguments: ['@config.factory', '@plugin.manager.geoblock_data_source'] arguments: ['@config.factory', '@plugin.manager.geoblock_data_source', '@logger.factory'] tags: - { name: event_subscriber } - { name: service_collector, tag: geoblock_restriction, call: addRestriction } Loading
src/EventSubscriber/RequestHandler.php +50 −1 Original line number Diff line number Diff line Loading @@ -6,12 +6,15 @@ namespace Drupal\geoblock\EventSubscriber; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Config\ImmutableConfig; use Drupal\Core\Logger\LoggerChannelFactoryInterface; use Drupal\geoblock\Plugin\GeoblockDataSourcePluginInterface; use Drupal\geoblock\Plugin\GeoblockDataSourcePluginManagerInterface; use Drupal\geoblock\Restriction\RestrictionInterface; use Drupal\geoblock\IPAddress; use Psr\Log\LoggerInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; Loading Loading @@ -44,6 +47,13 @@ class RequestHandler implements EventSubscriberInterface { */ protected $geoblockDataSourcePluginManager; /** * The logger factory service. * * @var \Drupal\Core\Logger\LoggerChannelFactoryInterface */ protected $loggerFactory; /** * A collection of restrictions to enforce. * Loading @@ -54,9 +64,10 @@ class RequestHandler implements EventSubscriberInterface { /** * Constructs a RequestHandler object. */ public function __construct(ConfigFactoryInterface $config_factory, GeoblockDataSourcePluginManagerInterface $geoblock_data_source_plugin_manager) { public function __construct(ConfigFactoryInterface $config_factory, GeoblockDataSourcePluginManagerInterface $geoblock_data_source_plugin_manager, LoggerChannelFactoryInterface $logger_factory) { $this->configFactory = $config_factory; $this->geoblockDataSourcePluginManager = $geoblock_data_source_plugin_manager; $this->loggerFactory = $logger_factory; } /** Loading Loading @@ -115,6 +126,8 @@ class RequestHandler implements EventSubscriberInterface { 'Content-Type' => 'text/plain', ])); $this->logRestrictionEnforcement($restriction, $event->getRequest(), $address); return; } } Loading Loading @@ -202,6 +215,42 @@ class RequestHandler implements EventSubscriberInterface { return $address; } /** * Conditionally log when a restriction was enforced. * * This method will log when a restriction was enforced for a given request * and address, but only if logging is enabled in configuration. * * @param \Drupal\geoblock\Restriction\RestrictionInterface $restriction * The restriction that was enforced. * @param \Symfony\Component\HttpFoundation\Request $request * The request object. * @param \Drupal\geoblock\IPAddress $address * The IP address. */ protected function logRestrictionEnforcement(RestrictionInterface $restriction, Request $request, IPAddress $address): void { if (!empty($this->config()->get('enable_logging'))) { $this->logger()->info('<code>@type</code> was enforced on a @request_method request for <code>@request_uri</code> by @address (country code: @country_code, registered country code: @registered_country_code).', [ '@type' => \get_class($restriction), '@request_method' => $request->getRealMethod(), '@request_uri' => $request->getRequestUri(), '@address' => $address->getAddress(), '@country_code' => $address->getCountryCode() ?? 'unknown', '@registered_country_code' => $address->getRegisteredCountryCode() ?? 'unknown', ]); } } /** * Get the logger channel for this module. * * @return \Psr\Log\LoggerInterface * The logger channel for this module. */ protected function logger(): LoggerInterface { return $this->loggerFactory->get('geoblock'); } /** * Attempt to enforce geographical restrictions where applicable. * Loading
src/Form/SettingsForm.php +7 −0 Original line number Diff line number Diff line Loading @@ -85,6 +85,13 @@ class SettingsForm extends ConfigFormBase { '#multiple' => TRUE, ]; $form['enable_logging'] = [ '#type' => 'checkbox', '#title' => $this->t('Enable logging'), '#description' => $this->t('If enabled, log messages will be recorded when a restriction has been enforced.'), '#default_value' => $config->get('enable_logging'), ]; $form['require_domestic_use'] = [ '#type' => 'checkbox', '#title' => $this->t('Require domestic use'), Loading