You are here

function restrict_by_ip_form_alter in Restrict Login or Role Access by IP Address 8.4

Same name and namespace in other branches
  1. 6.3 restrict_by_ip.module \restrict_by_ip_form_alter()
  2. 7.3 restrict_by_ip.module \restrict_by_ip_form_alter()

Implements hook_form_alter().

File

./restrict_by_ip.module, line 67
Restrict logins or roles to whitelisted IP addresses.

Code

function restrict_by_ip_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'user_form' || $form_id == 'user_register_form') {

    // Add restrict by ip form fields to user add/edit form.
    if (\Drupal::currentUser()
      ->hasPermission('administer restrict by ip')) {
      $address_entry = '';
      if ($form_id == 'user_form') {
        $user = $form_state
          ->getFormObject()
          ->getEntity();

        // Grab the current restrict by ip data if it exists.
        $config = \Drupal::config('restrict_by_ip.settings');
        $address_entry = $config
          ->get('user.' . $user
          ->id());
      }
      $form['#validate'][] = 'restrict_by_ip_user_profile_validate';
      $form['actions']['submit']['#submit'][] = 'restrict_by_ip_user_profile_submit';
      $form['rip'] = [
        '#type' => 'details',
        '#attributes' => [
          'class' => [
            'restrict-by-ip',
          ],
        ],
        '#title' => t('Restrict by IP settings'),
        '#weight' => 5,
        '#open' => TRUE,
      ];
      $form['rip']['restrict_by_ip_address'] = [
        '#type' => 'textfield',
        '#default_value' => $address_entry,
        '#maxlength' => NULL,
        '#description' => t('Enter IP Address Ranges in CIDR Notation separated with semi-colons, with no trailing semi-colon. E.G. 10.20.30.0/24;192.168.199.1/32;1.0.0.0/8<br />For more information on CIDR notation <a href="http://www.brassy.net/2007/mar/cidr_basic_subnetting" target="_blank">click here</a>.<br /><strong>Leave field empty to disable IP restrictions for this user.</strong>'),
      ];
    }
  }
}