You are here

function autoban_cron in Automatic IP ban (Autoban) 8

Same name and namespace in other branches
  1. 7 autoban.module \autoban_cron()

Implements hook_cron().

1 string reference to 'autoban_cron'
AutobanSettingsForm::buildForm in src/Form/AutobanSettingsForm.php
Form constructor.

File

./autoban.module, line 35
Autoban entity type configuration.

Code

function autoban_cron() {
  $config = \Drupal::config('autoban.settings');
  $autoban_cron = $config
    ->get('autoban_cron') ?: TRUE;
  if (!$autoban_cron) {
    return;
  }
  $rules = \Drupal::entityTypeManager()
    ->getStorage('autoban')
    ->loadMultiple();
  if (empty($rules)) {
    return;
  }
  $total_banned = 0;
  foreach (array_keys($rules) as $rule_id) {
    $context = [];
    AutobanBatch::ipBan($rule_id, $context);
    $total_banned += (int) $context['results'];
  }
  if ($total_banned > 0) {
    \Drupal::logger('Autoban')
      ->notice(t('Total IP banned: %count', [
      '%count' => $total_banned,
    ]));
  }
}