diff --git a/flood_control.module b/flood_control.module index 8af30b499497a013d111f2dcc092c82fe42d8d8d..1afdfbf21e5b987c40576fd07ecd09e34732c178 100644 --- a/flood_control.module +++ b/flood_control.module @@ -9,6 +9,7 @@ use Drupal\Core\Database\Query\Condition; use Drupal\migrate\Plugin\MigrateSourceInterface; use Drupal\migrate\Plugin\MigrationInterface; use Drupal\migrate\Row; +use Drupal\Core\Form\FormStateInterface; /** * Implements hook_migration_plugins_alter(). @@ -103,3 +104,53 @@ function flood_control_get_whitelist_ips(string $whitelistIpsValue = '') { return $whitelistIps; } + +/** + * Implements hook_form_alter(). + * + * Adds a custom submit handler to the Flood Control settings form. + */ +function flood_control_form_alter(&$form, FormStateInterface $form_state, $form_id) { + if ($form_id === 'flood_control_settings_form') { + $form['#submit'][] = 'flood_control_settings_form_submit'; + } +} + +/** + * Custom submit handler for the Flood Control settings form. + * + * Saves the updated whitelist IPs configuration and removes flood table entries + * for newly whitelisted IPs. + * + * @param array $form + * An associative array containing the structure of the form. + * @param \Drupal\Core\Form\FormStateInterface $form_state + * The current state of the form. + */ +function flood_control_settings_form_submit($form, FormStateInterface $form_state) { + // Get the new whitelist IPs from the form. + $new_whitelist = $form_state->getValue('ip_white_list'); + + // Save the new whitelist configuration. + $config = \Drupal::configFactory()->getEditable('flood_control.settings'); + $config->set('ip_white_list', $new_whitelist)->save(); + + // Retrieve updated whitelist IPs. + $whitelistIps = flood_control_get_whitelist_ips($new_whitelist); + + if (!empty($whitelistIps['addresses']) || !empty($whitelistIps['ranges'])) { + $query = \Drupal::database()->delete('flood'); + + $orGroup = $query->orConditionGroup(); + foreach ($whitelistIps['addresses'] as $ip) { + $orGroup->condition('identifier', $ip); + } + + foreach ($whitelistIps['ranges'] as $range) { + [$ipLower, $ipUpper] = explode('-', $range, 2); + $orGroup->condition('identifier', [$ipLower, $ipUpper], 'BETWEEN'); + } + + $query->condition($orGroup)->execute(); + } +} diff --git a/src/FloodWhiteList.php b/src/FloodWhiteList.php index 5df61f230cac2746e50d47530b35612c35a2ef9f..2e810130efb81e078d1d4b3b5fca170c5a4b4d97 100644 --- a/src/FloodWhiteList.php +++ b/src/FloodWhiteList.php @@ -52,6 +52,10 @@ class FloodWhiteList implements FloodInterface { * {@inheritdoc} */ public function register($name, $window = 3600, $identifier = NULL) { + if ($this->isIpWhitelisted()) { + return; + } + return $this->flood->register($name, $window, $identifier); }