You are here

public function AutobanBanForm::submitForm in Automatic IP ban (Autoban) 8

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides FormInterface::submitForm

File

src/Form/AutobanBanForm.php, line 105

Class

AutobanBanForm
Displays banned IP addresses.

Namespace

Drupal\autoban\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $rule = trim($form_state
    ->getValue('rule'));
  if (empty($rule)) {
    $rules = $this->entityTypeManager
      ->getStorage('autoban')
      ->loadMultiple();
    if (empty($rules)) {
      return;
    }
    foreach (array_keys($rules) as $rule_id) {
      $operations[] = [
        '\\Drupal\\autoban\\AutobanBatch::ipBan',
        [
          $rule_id,
        ],
      ];
    }
    $batch = [
      'title' => $this
        ->t('IP ban'),
      'operations' => $operations,
      'finished' => '\\Drupal\\autoban\\AutobanBatch::ipBanFinished',
      'file' => drupal_get_path('module', 'autoban') . '/AutobanBatch.php',
    ];
    batch_set($batch);
    $form_state
      ->setRedirect('entity.autoban.list');
  }
  else {
    $controller = $this->autoban;
    $banned_ip = $controller
      ->getBannedIp($rule);
    if (empty($banned_ip)) {
      $this
        ->messenger()
        ->addMessage($this
        ->t('No banned IP addresses for rule %rule.', [
        '%rule' => $rule,
      ]), 'warning');
      return;
    }
    $banned = $controller
      ->banIpList($banned_ip, $rule);
    if ($banned > 0) {
      $message = $this
        ->t('The IP addresses for rule %rule has been banned. Count: %count', [
        '%rule' => $rule,
        '%count' => $banned,
      ]);
    }
    else {
      $message = $this
        ->t('No banned IP addresses for rule %rule', [
        '%rule' => $rule,
      ]);
    }
    $this
      ->messenger()
      ->addMessage($message);
    $this
      ->getLogger('autoban')
      ->notice($message);
  }
}