You are here

public function AutobanController::banIpList in Automatic IP ban (Autoban) 8

Ban addresses.

Parameters

array $ip_list: IP addresses list.

string $rule: Autoban rule ID.

Return value

int IP banned count.

File

src/Controller/AutobanController.php, line 321

Class

AutobanController
Provides an Autoban functional.

Namespace

Drupal\autoban\Controller

Code

public function banIpList(array $ip_list, $rule) {
  $count = 0;
  if (!empty($ip_list) && $rule) {

    // Retrieve Ban manager object for current rule.
    $banManagerData = $this
      ->getBanManagerDataRule($rule);
    if ($banManagerData) {
      foreach ($ip_list as $item) {
        $banStatus = $this
          ->banIp($item->hostname, $banManagerData);
        if ($banStatus) {
          $count++;
          $this
            ->messenger()
            ->addMessage($this
            ->t('IP %ip has been banned (@provider).', [
            '%ip' => $item->hostname,
            '@provider' => $banManagerData['ban_name'],
          ]));
        }
      }
    }
    else {
      $this
        ->messenger()
        ->addMessage($this
        ->t('No ban manager for rule %rule', [
        '%rule' => $rule,
      ]), 'error');
    }
  }
  return $count;
}