You are here

class FloodUnblockAdminForm in Flood control 2.0.x

Admin form of Flood Unblock.

Hierarchy

Expanded class hierarchy of FloodUnblockAdminForm

1 string reference to 'FloodUnblockAdminForm'
flood_control.routing.yml in ./flood_control.routing.yml
flood_control.routing.yml

File

src/Form/FloodUnblockAdminForm.php, line 16

Namespace

Drupal\flood_control\Form
View source
class FloodUnblockAdminForm extends FormBase {

  /**
   * The Database Connection.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $database;

  /**
   * The FloodUnblockManager service.
   *
   * @var \Drupal\flood_control\FloodUnblockManager
   */
  protected $floodUnblockManager;

  /**
   * The date formatter service.
   *
   * @var \Drupal\Core\Datetime\DateFormatterInterface
   */
  protected $dateFormatter;

  /**
   * User flood config object.
   *
   * @var \Drupal\Core\Config\ImmutableConfig
   */
  protected $userFloodConfig;

  /**
   * FloodUnblockAdminForm constructor.
   *
   * @param \Drupal\flood_control\FloodUnblockManager $floodUnblockManager
   *   The FloodUnblockManager service.
   * @param \Drupal\Core\Database\Connection $database
   *   The database connection.
   * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
   *   The date formatter service.
   */
  public function __construct(FloodUnblockManager $floodUnblockManager, Connection $database, DateFormatterInterface $date_formatter) {
    $this->floodUnblockManager = $floodUnblockManager;
    $this->database = $database;
    $this->dateFormatter = $date_formatter;
    $this->userFloodConfig = $this
      ->configFactory()
      ->get('user.flood');
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('flood_control.flood_unblock_manager'), $container
      ->get('database'), $container
      ->get('date.formatter'));
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'flood_unblock_admin_form';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {

    // Fetches the limit from the form.
    $limit = $form_state
      ->getValue('limit') ?? 33;

    // Fetches the identifier from the form.
    $identifier = $form_state
      ->getValue('identifier');

    // Provides introduction to the table.
    $form['top_markup'] = [
      '#markup' => $this
        ->t("<p>List of IP addresses and user ID's that are blocked after multiple failed login attempts. You can remove separate entries. You can configure the login attempt limits and time windows on the <a href=':url'>Flood Control settings page</a>.</p>", [
        ':url' => Url::fromRoute('flood_control.settings')
          ->toString(),
      ]),
    ];

    // Provides table filters.
    $form['filter'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Filter'),
      '#open' => FALSE,
      'limit' => [
        '#type' => 'number',
        '#title' => $this
          ->t('Amount'),
        '#description' => $this
          ->t("Number of lines shown in table."),
        '#size' => 5,
        '#min' => 1,
        '#steps' => 10,
        '#default_value' => $limit,
      ],
      'identifier' => [
        '#type' => 'textfield',
        '#title' => $this
          ->t('Identifier'),
        '#default_value' => $identifier,
        '#size' => 20,
        '#description' => $this
          ->t('(Part of) identifier: IP address or UID'),
        '#maxlength' => 256,
      ],
      'submit' => [
        '#type' => 'submit',
        '#value' => $this
          ->t('Filter'),
      ],
    ];

    // Provides header for tableselect element.
    $header = [
      'identifier' => [
        'data' => $this
          ->t('Identifier'),
        'field' => 'identifier',
        'sort' => 'asc',
      ],
      'blocked' => $this
        ->t('Status'),
      'event' => [
        'data' => $this
          ->t('Event'),
        'field' => 'event',
        'sort' => 'asc',
      ],
      'timestamp' => [
        'data' => $this
          ->t('Timestamp'),
        'field' => 'timestamp',
        'sort' => 'asc',
      ],
      'expiration' => [
        'data' => $this
          ->t('Expiration'),
        'field' => 'expiration',
        'sort' => 'asc',
      ],
    ];
    $options = [];

    // Fetches items from flood table.
    if ($this->database
      ->schema()
      ->tableExists('flood')) {
      $query = $this->database
        ->select('flood', 'f')
        ->extend('Drupal\\Core\\Database\\Query\\TableSortExtender')
        ->orderByHeader($header);
      $query
        ->fields('f');
      if ($identifier) {
        $query
          ->condition('identifier', "%" . $this->database
          ->escapeLike($identifier) . "%", 'LIKE');
      }
      $pager = $query
        ->extend('Drupal\\Core\\Database\\Query\\PagerSelectExtender')
        ->limit($limit);
      $execute = $pager
        ->execute();
      $results = $execute
        ->fetchAll();
      $results_identifiers = array_column($results, 'identifier', 'fid');

      // Fetches user names or location string for identifiers.
      $identifiers = $this->floodUnblockManager
        ->fetchIdentifiers(array_unique($results_identifiers));
      foreach ($results as $result) {

        // Gets status of identifier.
        $is_blocked = $this->floodUnblockManager
          ->isBlocked($result->identifier, $result->event);

        // Defines list of options for tableselect element.
        $options[$result->fid] = [
          'identifier' => $identifiers[$result->identifier],
          'blocked' => $is_blocked ? $this
            ->t('Blocked') : $this
            ->t('Not blocked'),
          'event' => $this->floodUnblockManager
            ->getEventLabel($result->event),
          'timestamp' => $this->dateFormatter
            ->format($result->timestamp, 'short'),
          'expiration' => $this->dateFormatter
            ->format($result->expiration, 'short'),
        ];
      }
    }

    // Provides the tableselect element.
    $form['table'] = [
      '#type' => 'tableselect',
      '#header' => $header,
      '#options' => $options,
      '#empty' => $this
        ->t('There are no failed logins at this time.'),
    ];

    // Provides the remove submit button.
    $form['remove'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Removed selected items from the flood table'),
      '#validate' => [
        '::validateRemoveItems',
      ],
    ];
    if (count($options) == 0) {
      $form['remove']['#disabled'] = TRUE;
    }

    // Provides the pager element.
    $form['pager'] = [
      '#type' => 'pager',
    ];
    $form['#cache'] = [
      'tags' => $this->userFloodConfig
        ->getCacheTags(),
    ];
    return $form;
  }

  /**
   * Validates that items have been selected for removal.
   */
  public function validateRemoveItems(array &$form, FormStateInterface $form_state) {
    parent::validateForm($form, $form_state);
    $entries = $form_state
      ->getValue('table');
    $selected_entries = array_filter($entries, function ($selected) {
      return $selected !== 0;
    });
    if (empty($selected_entries)) {
      $form_state
        ->setErrorByName('table', $this
        ->t('Please make a selection.'));
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    foreach ($form_state
      ->getValue('table') as $fid) {
      if ($fid !== 0) {
        $this->floodUnblockManager
          ->floodUnblockClearEvent($fid);
      }
    }
    $form_state
      ->setRebuild();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
FloodUnblockAdminForm::$database protected property The Database Connection.
FloodUnblockAdminForm::$dateFormatter protected property The date formatter service.
FloodUnblockAdminForm::$floodUnblockManager protected property The FloodUnblockManager service.
FloodUnblockAdminForm::$userFloodConfig protected property User flood config object.
FloodUnblockAdminForm::buildForm public function Form constructor. Overrides FormInterface::buildForm
FloodUnblockAdminForm::create public static function Instantiates a new instance of this class. Overrides FormBase::create
FloodUnblockAdminForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
FloodUnblockAdminForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
FloodUnblockAdminForm::validateRemoveItems public function Validates that items have been selected for removal.
FloodUnblockAdminForm::__construct public function FloodUnblockAdminForm constructor.
FormBase::$configFactory protected property The config factory. 3
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 3
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route.
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 72
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.