You are here

public function AdminForm::buildForm in Force Password Change 8

Same name and namespace in other branches
  1. 2.0.x src/Form/AdminForm.php \Drupal\force_password_change\Form\AdminForm::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 FormInterface::buildForm

File

src/Form/AdminForm.php, line 61

Class

AdminForm

Namespace

Drupal\force_password_change\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $form['#attached']['library'] = [
    'force_password_change/settings_page',
  ];
  $form['first_time_login_password_change'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Force password change on first-time login'),
    '#default_value' => $this->configFactory
      ->get('force_password_change.settings')
      ->get('first_time_login_password_change'),
  ];
  $form['login_only'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Check for pending password change'),
    '#options' => [
      0 => $this
        ->t('On every page load'),
      1 => $this
        ->t('On login only'),
    ],
    '#default_value' => (int) $this->configFactory
      ->get('force_password_change.settings')
      ->get('check_login_only'),
    '#description' => $this
      ->t('Select when to check if a user has a pending password change. Checking on every page load adds a little overhead to every page load, but is the most secure method. Checking on login will only check if a change is pending when the user first logs in, but on sites where users may stay logged in for lengthy periods of time, it may be a long time before they are forced to change their password.'),
  ];
  $roles = [];

  // Get stats for each of the roles on the site
  $all_roles = Role::loadMultiple();
  unset($all_roles[RoleInterface::ANONYMOUS_ID]);
  foreach ($all_roles as $rid => $role) {
    $user_count = $this->passwordChangeService
      ->getUserCountForRole($rid);
    $pending_count = $this->passwordChangeService
      ->getPendingUsersForRole($rid, TRUE);
    $url = Url::fromRoute('force_password_change.admin.role.list', [
      'rid' => $rid,
    ]);
    $roles[$rid] = $this
      ->t('@role (Users in role: @user_count | Users with pending forced password change: @pending_user_count | <a href=":url">Details</a>)', [
      '@role' => $role
        ->label(),
      '@user_count' => $user_count,
      '@pending_user_count' => $pending_count,
      ':url' => $url
        ->toString(),
    ]);
  }
  $form['roles'] = [
    '#type' => 'checkboxes',
    '#options' => $roles,
    '#title' => $this
      ->t('Force users in the following roles to change their password'),
    '#description' => $this
      ->t('Users will be forced to change their password either on their next page load, or on their next login, depending on the setting in "Check for pending password change". If pending password changes are checked on every page load, logged in users will be forced to immediately change their password, and after changing it will be redirected back to the page they were attempting to access.') . '<br />' . $this
      ->t('Note: When you return to this page, no roles will be selected. This is because this setting is a trigger, not a persistant state.'),
  ];
  $expiry_data = $this->passwordChangeService
    ->getRoleExpiryTimePeriods();
  $expiry = [];
  foreach ($expiry_data as $data) {
    $expiry[$data->rid] = [
      'expiry' => $data->expiry,
      'weight' => $data->weight,
    ];
  }
  $form['expiry_data'] = [
    '#type' => 'value',
    '#value' => $expiry,
  ];
  $form['expiry'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Password Expiry'),
    '#collapsible' => TRUE,
  ];
  $form['expiry']['expire_password'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Enable password expiration'),
    '#default_value' => $this->configFactory
      ->get('force_password_change.settings')
      ->get('expire_password'),
    '#description' => $this
      ->t('When this box is checked, passwords will be set to expire according to the rules set out below. If this box is un-checked, password expiry will be disabled, and the password expiry options below will be ignored.'),
  ];
  $form['expiry']['header'] = [
    '#markup' => '<p>' . $this
      ->t('Select the amount of time after which you would like users in a role to be automatically forced to change their password. Any users who do not change their password in this amount of time will be forced to change their password on their next login or page load (depending on the setting in "Check for pending password change"). If you do not wish passwords to expire for a certain role, leave/set the value for that role to zero.') . '</p>',
  ];
  $form['expiry']['table'] = [
    '#tree' => TRUE,
    '#type' => 'table',
    '#header' => [
      $this
        ->t('Role'),
      $this
        ->t('Expire password after:'),
      $this
        ->t('Weight'),
    ],
    '#id' => 'force_password_change_role_expiry_table',
    '#tabledrag' => [
      [
        'action' => 'order',
        'relationship' => 'sibling',
        'group' => 'role-expiry-order-weight',
      ],
    ],
  ];
  $time_periods = [
    'hour' => 60 * 60,
    'day' => 60 * 60 * 24,
    'week' => 60 * 60 * 24 * 7,
    'year' => 60 * 60 * 24 * 365,
  ];
  $time_options = [
    'hour' => $this
      ->t('hours'),
    'day' => $this
      ->t('days'),
    'week' => $this
      ->t('weeks'),
    'year' => $this
      ->t('years'),
  ];
  $heaviest_weight = 0;
  if (count($expiry)) {
    foreach ($expiry as $rid => $data) {
      if (isset($all_roles[$rid])) {
        $form['expiry']['table'][$rid] = [
          '#weight' => $data['weight'],
          '#attributes' => [
            'class' => [
              'draggable',
            ],
          ],
        ];
        $form['expiry']['table'][$rid]['role'] = [
          '#plain_text' => $all_roles[$rid]
            ->label(),
        ];
        $time_period_default = 0;
        $time_quantity_default = 0;
        if ($data['expiry'] != '' && $data['expiry']) {
          $expires = $data['expiry'];
          if ($expires % $time_periods['year'] === 0) {
            $time_period_default = 'year';
            $time_quantity_default = $expires / $time_periods['year'];
          }
          else {
            if ($expires % $time_periods['week'] === 0) {
              $time_period_default = 'week';
              $time_quantity_default = $expires / $time_periods['week'];
            }
            else {
              if ($expires % $time_periods['day'] === 0) {
                $time_period_default = 'day';
                $time_quantity_default = $expires / $time_periods['day'];
              }
              else {
                $time_period_default = 'hour';
                if ($expires % $time_periods['hour'] === 0) {
                  $time_quantity_default = $expires / $time_periods['hour'];
                }
              }
            }
          }
        }
        $form['expiry']['table'][$rid]['time']['time_quantity'] = [
          '#type' => 'textfield',
          '#default_value' => $time_quantity_default,
          '#size' => 4,
        ];
        $form['expiry']['table'][$rid]['time']['time_period'] = [
          '#type' => 'select',
          '#options' => $time_options,
          '#default_value' => $time_period_default,
        ];
        $form['expiry']['table'][$rid]['weight'] = [
          '#type' => 'weight',
          '#title' => $this
            ->t('Weight for expiry roles'),
          '#title_display' => 'invisible',
          '#default_value' => $data['weight'] != '' ? $data['weight'] : 0,
          '#attributes' => [
            'class' => [
              'role-expiry-order-weight',
            ],
          ],
        ];
        $heaviest_weight = $data['weight'] != '' ? $data['weight'] : 0;
      }
    }
  }
  foreach ($roles as $rid => $r) {
    if (!isset($form['expiry']['table'][$rid])) {
      $heaviest_weight++;
      $form['expiry']['table'][$rid] = [
        '#weight' => $heaviest_weight,
        '#attributes' => [
          'class' => [
            'draggable',
          ],
        ],
      ];
      $form['expiry']['table'][$rid]['role'] = [
        '#markup' => $r,
      ];
      $form['expiry']['table'][$rid]['time']['time_quantity'] = [
        '#type' => 'textfield',
        '#default_value' => 0,
        '#size' => 4,
      ];
      $form['expiry']['table'][$rid]['time']['time_period'] = [
        '#type' => 'select',
        '#options' => $time_options,
      ];
      $form['expiry']['table'][$rid]['weight'] = [
        '#type' => 'weight',
        '#title' => $this
          ->t('Weight for expiry roles'),
        '#title_display' => 'invisible',
        '#default_value' => $heaviest_weight,
        '#attributes' => [
          'class' => [
            'role-expiry-order-weight',
          ],
        ],
      ];
    }
  }
  $form['expiry']['footer'] = [
    '#markup' => '<p>' . $this
      ->t('Drag and drop the rows to set the priority for password expiry. The roles with the highest priority should be placed at the top of the list. If a user is a member of more than one role, then the time after which their password expires will be determined by whichever of their roles has the highest priority (highest in the list). Expiry rules for any roles of lower priority (lower in the list) will be ignored. As such, any roles lower in priority (below) the authenticated user role will effectively be ignored, since all users are members of the authenticated users role.') . '</p>',
  ];
  $form['actions'] = [
    '#type' => 'actions',
  ];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Submit'),
  ];
  return $form;
}