class FloodControlSettingsForm in Flood control 2.0.x
Same name and namespace in other branches
- 1.0.x src/Form/FloodControlSettingsForm.php \Drupal\flood_control\Form\FloodControlSettingsForm
Administration settings form.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, RedirectDestinationTrait, StringTranslationTrait
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
- class \Drupal\flood_control\Form\FloodControlSettingsForm
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
Expanded class hierarchy of FloodControlSettingsForm
1 string reference to 'FloodControlSettingsForm'
File
- src/
Form/ FloodControlSettingsForm.php, line 16
Namespace
Drupal\flood_control\FormView source
class FloodControlSettingsForm extends ConfigFormBase {
/**
* The date formatter interface.
*
* @var \Drupal\Core\Datetime\DateFormatterInterface
*/
protected $dateFormatter;
/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* {@inheritdoc}
*/
public function __construct(ConfigFactoryInterface $config_factory, DateFormatterInterface $dateFormatter, ModuleHandlerInterface $module_handler) {
parent::__construct($config_factory);
$this->dateFormatter = $dateFormatter;
$this->moduleHandler = $module_handler;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('date.formatter'), $container
->get('module_handler'));
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'flood_control_settings_form';
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return [
'user.flood',
];
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$flood_config = $this
->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' => $this
->t('Login'),
];
$form['login']['intro'] = [
'#markup' => $this
->t('The website blocks login attempts when the limit within a particular time window has been reached. The website records both attempts from IP addresses and usernames. When the limit is reached the user login form cannot be used anymore. You can remove blocked usernames and IP address from the <a href=":url">Flood Unblock page</a>.', [
':url' => Url::fromRoute('flood_control.unblock_form')
->toString(),
]),
];
$form['login']['ip_limit'] = [
'#type' => 'select',
'#title' => $this
->t('IP login limit'),
'#options' => array_combine($counterOptions, $counterOptions),
'#default_value' => $flood_settings['ip_limit'],
'#description' => $this
->t('The allowed number of failed login attempts from one IP address within the allowed time window.'),
];
$form['login']['ip_window'] = [
'#type' => 'select',
'#title' => $this
->t('IP time window'),
'#options' => [
0 => $this
->t('None (disabled)'),
] + array_map([
$this->dateFormatter,
'formatInterval',
], array_combine($timeOptions, $timeOptions)),
'#default_value' => $flood_settings['ip_window'],
'#description' => $this
->t('The allowed time window for failed IP logins.'),
];
$form['login']['user_limit'] = [
'#type' => 'select',
'#title' => $this
->t('Username login limit'),
'#options' => array_combine($counterOptions, $counterOptions),
'#default_value' => $flood_settings['user_limit'],
'#description' => $this
->t('The allowed number of failed login attempts with one username within the allowed time window.'),
];
$form['login']['user_window'] = [
'#type' => 'select',
'#title' => $this
->t('Username login time window'),
'#options' => [
0 => $this
->t('None (disabled)'),
] + array_map([
$this->dateFormatter,
'formatInterval',
], array_combine($timeOptions, $timeOptions)),
'#default_value' => $flood_settings['user_window'],
'#description' => $this
->t('The allowed time window for failed username logins.'),
];
// Contact module flood events.
if ($this->moduleHandler
->moduleExists('contact')) {
$contact_config = $this
->config('contact.settings');
$contact_settings = $contact_config
->get();
$form['contact'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Contact forms'),
];
$form['contact']['intro'] = [
'#markup' => $this
->t('The website blocks contact form submissions when the limit within a particular time window has been reached.'),
];
$form['contact']['contact_threshold_limit'] = [
'#type' => 'select',
'#title' => $this
->t('Sending e-mails limit'),
'#options' => array_combine($counterOptions, $counterOptions),
'#default_value' => $contact_settings['flood']['limit'],
'#description' => $this
->t('The allowed number of submissions within the allowed time window.'),
];
$form['contact']['contact_threshold_window'] = [
'#type' => 'select',
'#title' => $this
->t('Sending e-mails window'),
'#options' => [
0 => $this
->t('None (disabled)'),
] + array_map([
$this->dateFormatter,
'formatInterval',
], array_combine($timeOptions, $timeOptions)),
'#default_value' => $contact_settings['flood']['interval'],
'#description' => $this
->t('The allowed time window for contact form submissions.'),
];
}
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$flood_config = $this->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();
if ($this->moduleHandler
->moduleExists('contact')) {
$contact_config = $this->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);
}
/**
* Provides options for the select lists.
*/
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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ConfigFormBaseTrait:: |
protected | function | Retrieves a configuration object. | |
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
public | function | 2 | |
DependencySerializationTrait:: |
public | function | 2 | |
FloodControlSettingsForm:: |
protected | property | The date formatter interface. | |
FloodControlSettingsForm:: |
protected | property | The module handler. | |
FloodControlSettingsForm:: |
public | function |
Form constructor. Overrides ConfigFormBase:: |
|
FloodControlSettingsForm:: |
public static | function |
Instantiates a new instance of this class. Overrides ConfigFormBase:: |
|
FloodControlSettingsForm:: |
protected | function |
Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait:: |
|
FloodControlSettingsForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
FloodControlSettingsForm:: |
protected | function | Provides options for the select lists. | |
FloodControlSettingsForm:: |
public | function |
Form submission handler. Overrides ConfigFormBase:: |
|
FloodControlSettingsForm:: |
public | function |
Constructs a \Drupal\system\ConfigFormBase object. Overrides ConfigFormBase:: |
|
FormBase:: |
protected | property | The config factory. | 3 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 3 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function | Returns a redirect response object for the specified route. | |
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
FormBase:: |
public | function |
Form validation handler. Overrides FormInterface:: |
72 |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 27 |
MessengerTrait:: |
public | function | Gets the messenger. | 27 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 4 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |