public function ConfigForm::buildForm in Restrict IP 8
Same name and namespace in other branches
- 8.2 src/Form/ConfigForm.php \Drupal\restrict_ip\Form\ConfigForm::buildForm()
- 3.x src/Form/ConfigForm.php \Drupal\restrict_ip\Form\ConfigForm::buildForm()
Form constructor.
Parameters
array $form: An associative array containing the structure of the form.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
Return value
array The form structure.
Overrides ConfigFormBase::buildForm
File
- src/
Form/ ConfigForm.php, line 109
Class
Namespace
Drupal\restrict_ip\FormCode
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this
->config('restrict_ip.settings');
if ($this->currentUser
->hasPermission('administer permissions')) {
$url = Url::fromRoute('user.admin_permissions');
$permission_page_link = Link::fromTextAndUrl($this
->t('permissions'), $url)
->toString();
}
else {
$permission_page_link = $this
->t('permissions');
}
$form['address_description'] = [
'#markup' => $this
->t('Enter the list of allowed IP addresses below'),
'#prefix' => '<h2>',
'#suffix' => '</h2>',
];
$form['warning'] = [
'#prefix' => '<p>',
'#suffix' => '</p>',
'#markup' => $this
->t("Warning: If you enable IP restriction, and don't enter your current IP address into the list, you will immediately be locked out of the system upon save, and will not be able to access the system until you are in a location with an allowed IP address. Alternatively you can allow Restrict IP to be bypassed by role, and set at least one of your roles to be bypassed on the @permissions page.", [
'@permissions' => $permission_page_link,
]),
];
$form['current_ip'] = [
'#prefix' => '<p><strong>',
'#suffix' => '</strong></p>',
'#markup' => $this
->t('Your current IP address is: %ip_address', [
'%ip_address' => $this->restrictIpService
->getCurrentUserIp(),
]),
];
$form['enable'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Enable Restricted IPs'),
'#description' => $this
->t('IP addresses will only be enabled when this option is selected'),
'#default_value' => $config
->get('enable'),
];
$form['address_list'] = [
'#title' => $this
->t('Allowed IP Address List'),
'#description' => $this
->t('Enter the list of IP Addresses that are allowed to access the site. Enter one IP address per line, in IPv4 or IPv6 format. You may also enter a range of IPv4 addresses in the format AAA.BBB.CCC.XXX - AAA.BBB.CCC.YYY'),
'#type' => 'textarea',
'#default_value' => implode(PHP_EOL, $this->whitelistedIpAddresses),
];
$form['mail_address'] = [
'#title' => $this
->t('Email Address'),
'#type' => 'textfield',
'#description' => $this
->t('If you would like to include a contact email address in the error message that is shown to users that do not have an allowed IP address, enter the email address here.'),
'#default_value' => $config
->get('mail_address'),
];
if ($this->moduleHandler
->moduleExists('dblog')) {
$form['dblog'] = [
'#title' => $this
->t('Log access attempts to watchdog'),
'#type' => 'checkbox',
'#default_value' => $config
->get('dblog'),
'#description' => $this
->t('When this box is checked, attempts to access the site will be logged to the Drupal log (Recent log entries)'),
];
}
else {
$form['dblog'] = [
'#type' => 'value',
'#value' => $config
->get('dblog'),
];
}
$form['allow_role_bypass'] = [
'#title' => $this
->t('Allow restrict IP to be bypassed by role'),
'#type' => 'checkbox',
'#default_value' => $config
->get('allow_role_bypass'),
'#description' => $this
->t('When this box is checked, the permission "Bypass IP Restriction" will become available on the site @permissions page', [
'@permissions' => $permission_page_link,
]),
];
$form['bypass_action'] = [
'#title' => $this
->t('Access denied action'),
'#type' => 'radios',
'#default_value' => $config
->get('bypass_action'),
'#description' => $this
->t('Action to be performed when access is permitted by role, but the user is not logged in.'),
'#options' => [
'provide_link_login_page' => $this
->t('Provide a link to the login page'),
'redirect_login_page' => $this
->t('Redirect to the login page'),
],
'#states' => [
'visible' => [
'#edit-allow-role-bypass' => [
'checked' => TRUE,
],
],
],
];
$form['white_black_list'] = [
'#type' => 'radios',
'#options' => [
$this
->t('Check IP addresses on all paths'),
$this
->t('Check IP addresses on all paths except the following'),
$this
->t('Check IP addresses only on the following paths'),
],
'#default_value' => $config
->get('white_black_list'),
];
$form['page_whitelist'] = [
'#title' => $this
->t('Whitelisted pages'),
'#description' => $this
->t("Enter a list of paths that will be allowed regardless of IP address. For example, to not check IP addresses on this page, you would enter <em>admin/config/people/restrict_ip</em>. All paths not included here will be checked. Do not include domain names. The '*' character is a wildcard. An example path is /user/* for every user page."),
'#type' => 'textarea',
'#default_value' => implode(PHP_EOL, $this->whitelistedPagePaths),
'#states' => [
'visible' => [
':input[name="white_black_list"]' => [
'value' => 1,
],
],
],
];
$form['page_blacklist'] = [
'#title' => $this
->t('Blacklisted pages'),
'#description' => $this
->t("Enter a list of paths on which IP addresses will be checked. For example, to check IP addresses on this page, you would enter <em>admin/config/people/restrict_ip</em>. All paths not included here will not be checked. Do not include domain names. The '*' character is a wildcard. An example path is /user/* for every user page."),
'#type' => 'textarea',
'#default_value' => implode(PHP_EOL, $this->blacklistedPagePaths),
'#states' => [
'visible' => [
':input[name="white_black_list"]' => [
'value' => 2,
],
],
],
];
return parent::buildForm($form, $form_state);
}