You are here

class FloodControlSettingsForm in Flood control 1.0.x

Same name and namespace in other branches
  1. 2.0.x src/Form/FloodControlSettingsForm.php \Drupal\flood_control\Form\FloodControlSettingsForm

Administration settings form.

Hierarchy

Expanded class hierarchy of FloodControlSettingsForm

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

File

src/Form/FloodControlSettingsForm.php, line 19
Contains \Drupal\flood_control\Form\FloodControlSettingsForm.

Namespace

Drupal\flood_control\Form
View source
class FloodControlSettingsForm extends ConfigFormBase {

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

  /**
   * {@inheritdoc}
   */
  public function getFormID() {
    return 'flood_control_settings_form';
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      'user.flood',
    ];
  }
  public function __construct(ConfigFactoryInterface $config_factory, DateFormatterInterface $dateFormatter) {
    parent::__construct($config_factory);
    $this->dateFormatter = $dateFormatter;
  }

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $flood_config = \Drupal::config('user.flood');
    $flood_settings = $flood_config
      ->get();
    $options = $this
      ->getOptions();
    $counterOptions = $options['counter'];
    $timeOptions = $options['time'];

    // User module flood events.
    $form['login'] = [
      '#type' => 'fieldset',
      '#title' => t('Login'),
    ];
    $form['login']['ip_limit'] = [
      '#type' => 'select',
      '#title' => t('Failed login (IP) limit'),
      '#options' => array_combine($counterOptions, $counterOptions),
      '#default_value' => $flood_settings['ip_limit'],
    ];
    $form['login']['ip_window'] = [
      '#type' => 'select',
      '#title' => t('Failed login (IP) window'),
      '#options' => [
        0 => t('None (disabled)'),
      ] + array_map([
        $this->dateFormatter,
        'formatInterval',
      ], array_combine($timeOptions, $timeOptions)),
      '#default_value' => $flood_settings['ip_window'],
    ];
    $form['login']['user_limit'] = [
      '#type' => 'select',
      '#title' => t('Failed login (username) limit'),
      '#options' => array_combine($counterOptions, $counterOptions),
      '#default_value' => $flood_settings['user_limit'],
    ];
    $form['login']['user_window'] = [
      '#type' => 'select',
      '#title' => t('Failed login (username) window'),
      '#options' => [
        0 => t('None (disabled)'),
      ] + array_map([
        $this->dateFormatter,
        'formatInterval',
      ], array_combine($timeOptions, $timeOptions)),
      '#default_value' => $flood_settings['user_window'],
    ];

    // Contact module flood events.
    $contact_config = \Drupal::config('contact.settings');
    $contact_settings = $contact_config
      ->get();
    $form['contact'] = [
      '#type' => 'fieldset',
      '#title' => t('Contact forms'),
    ];
    $form['contact']['contact_threshold_limit'] = [
      '#type' => 'select',
      '#title' => t('Sending e-mails limit'),
      '#options' => array_combine($counterOptions, $counterOptions),
      '#default_value' => $contact_settings['flood']['limit'],
    ];
    $form['contact']['contact_threshold_window'] = [
      '#type' => 'select',
      '#title' => t('Sending e-mails window'),
      '#options' => [
        0 => t('None (disabled)'),
      ] + array_map([
        $this->dateFormatter,
        'formatInterval',
      ], array_combine($timeOptions, $timeOptions)),
      '#default_value' => $contact_settings['flood']['interval'],
    ];
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    parent::validateForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $flood_config = \Drupal::configFactory()
      ->getEditable('user.flood');
    $flood_config
      ->set('ip_limit', $form_state
      ->getValue('ip_limit'))
      ->set('ip_window', $form_state
      ->getValue('ip_window'))
      ->set('user_limit', $form_state
      ->getValue('user_limit'))
      ->set('user_window', $form_state
      ->getValue('user_window'))
      ->save();
    $contact_config = \Drupal::configFactory()
      ->getEditable('contact.settings');
    $contact_config
      ->set('flood.limit', $form_state
      ->getValue('contact_threshold_limit'))
      ->set('flood.interval', $form_state
      ->getValue('contact_threshold_window'))
      ->save();
    parent::submitForm($form, $form_state);
  }
  protected function getOptions() {
    return [
      'counter' => [
        1,
        2,
        3,
        4,
        5,
        6,
        7,
        8,
        9,
        10,
        20,
        30,
        40,
        50,
        75,
        100,
        125,
        150,
        200,
        250,
        500,
      ],
      'time' => [
        60,
        180,
        300,
        600,
        900,
        1800,
        2700,
        3600,
        10800,
        21600,
        32400,
        43200,
        86400,
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
FloodControlSettingsForm::$dateFormatter protected property The date formatter interface.
FloodControlSettingsForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
FloodControlSettingsForm::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
FloodControlSettingsForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
FloodControlSettingsForm::getFormID public function
FloodControlSettingsForm::getOptions protected function
FloodControlSettingsForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
FloodControlSettingsForm::validateForm public function Form validation handler. Overrides FormBase::validateForm
FloodControlSettingsForm::__construct public function Constructs a \Drupal\system\ConfigFormBase object. Overrides ConfigFormBase::__construct
FormBase::$configFactory protected property The config factory. 3
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
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.
FormInterface::getFormId public function Returns a unique string identifying the form. 264
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.