You are here

class FloodSettings in Flood settings 8

Class FloodSettings.

@package Drupal\flood_settings\Form

Hierarchy

Expanded class hierarchy of FloodSettings

1 string reference to 'FloodSettings'
flood_settings.routing.yml in ./flood_settings.routing.yml
flood_settings.routing.yml

File

src/Form/FloodSettings.php, line 16

Namespace

Drupal\flood_settings\Form
View source
class FloodSettings extends ConfigFormBase {
  const SETTINGS_KEY = 'user.flood';
  const DEFAULT_IP_LIMIT = 50;
  const DEFAULT_IP_WINDOW = 3600;
  const DEFAULT_USER_LIMIT = 5;
  const DEFAULT_USER_WINDOW = 21600;

  /**
   * Drupal\Core\Datetime\DateFormatterInterface definition.
   *
   * @var \Drupal\Core\Datetime\DateFormatterInterface
   */
  protected $dateFormatter;

  /**
   * FloodSettings constructor.
   *
   * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
   *   DateFormatterInterface Object.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The configuration factory service.
   */
  public function __construct(DateFormatterInterface $date_formatter, ConfigFactoryInterface $config_factory) {
    parent::__construct($config_factory);
    $this->dateFormatter = $date_formatter;
  }

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

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return str_replace('.', '_', self::SETTINGS_KEY);
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      self::SETTINGS_KEY,
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this
      ->config(self::SETTINGS_KEY);
    $occurrenceLimits = [
      1,
      2,
      3,
      4,
      5,
      6,
      7,
      8,
      9,
      10,
      20,
      30,
      40,
      50,
      75,
      100,
      125,
      150,
      200,
      250,
      500,
    ];
    $durationLimits = [
      60,
      180,
      300,
      600,
      900,
      1800,
      2700,
      3600,
      10800,
      21600,
      32400,
      43200,
      86400,
    ];
    $durationLimits_options = $this
      ->buildOptions($durationLimits);
    $durationLimits_options[0] = $this
      ->t('None (disabled)');
    ksort($durationLimits_options);
    $form['login'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Login'),
    ];
    $form['login']['uid_only'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Username only'),
      '#default_value' => $config
        ->get('uid_only') ?? FALSE,
      '#description' => $this
        ->t('Register flood events based on the uid only, so they apply for any
      IP address. This is the most secure option.'),
    ];
    $form['login']['ip_limit'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Failed login (IP) limit'),
      '#default_value' => $config
        ->get('ip_limit') ?? self::DEFAULT_IP_LIMIT,
      '#options' => array_combine($occurrenceLimits, $occurrenceLimits),
    ];
    $form['login']['ip_window'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Failed login (IP) window'),
      '#default_value' => $config
        ->get('ip_window') ?? self::DEFAULT_IP_WINDOW,
      '#options' => $durationLimits_options,
    ];
    $form['login']['user_limit'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Failed login (username) limit'),
      '#default_value' => $config
        ->get('user_limit') ?? self::DEFAULT_USER_LIMIT,
      '#options' => array_combine($occurrenceLimits, $occurrenceLimits),
    ];
    $form['login']['user_window'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Failed login (username) window'),
      '#default_value' => $config
        ->get('user_window') ?? self::DEFAULT_USER_WINDOW,
      '#options' => $durationLimits_options,
    ];
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    foreach ([
      'uid_only',
      'ip_limit',
      'ip_window',
      'user_limit',
      'user_window',
    ] as $configKey) {
      $this->configFactory
        ->getEditable(self::SETTINGS_KEY)
        ->set($configKey, $form_state
        ->getValue($configKey))
        ->save();
    }
    parent::submitForm($form, $form_state);
  }

  /**
   * Provide DateFormatter interval.
   *
   * @param array $time_intervals
   *   Intervals time array.
   * @param int $granularity
   *   Ganularity value.
   * @param string|null $langcode
   *   Langcode value.
   *
   * @return array
   *   Return an array.
   */
  protected function buildOptions(array $time_intervals, $granularity = 2, $langcode = NULL) {
    $callback = function ($value) use ($granularity, $langcode) {
      return $this->dateFormatter
        ->formatInterval($value, $granularity, $langcode);
    };
    return array_combine($time_intervals, array_map($callback, $time_intervals));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FloodSettings::$dateFormatter protected property Drupal\Core\Datetime\DateFormatterInterface definition.
FloodSettings::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
FloodSettings::buildOptions protected function Provide DateFormatter interval.
FloodSettings::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
FloodSettings::DEFAULT_IP_LIMIT constant
FloodSettings::DEFAULT_IP_WINDOW constant
FloodSettings::DEFAULT_USER_LIMIT constant
FloodSettings::DEFAULT_USER_WINDOW constant
FloodSettings::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
FloodSettings::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
FloodSettings::SETTINGS_KEY constant
FloodSettings::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
FloodSettings::__construct public function FloodSettings constructor. Overrides ConfigFormBase::__construct
FormBase::$configFactory protected property The config factory. 1
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. 1
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. Overrides UrlGeneratorTrait::redirect
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 62
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
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. 29
MessengerTrait::messenger public function Gets the messenger. 29
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. 1
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.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.