You are here

public function AdminForm::submitForm 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::submitForm()

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides FormInterface::submitForm

File

src/Form/AdminForm.php, line 297

Class

AdminForm

Namespace

Drupal\force_password_change\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {

  // First set some variable defaults
  $this->configFactory
    ->getEditable('force_password_change.settings')
    ->set('first_time_login_password_change', (bool) $form_state
    ->getValue('first_time_login_password_change'))
    ->set('check_login_only', (bool) $form_state
    ->getValue('login_only'))
    ->save();
  $selected_roles = [];
  $au = FALSE;
  foreach ($form_state
    ->getValue('roles') as $rid) {

    // The authenticated user role. All users on the site will have a password change forced
    if ($rid === 'authenticated') {

      // Update all user's {user} table
      $this->passwordChangeService
        ->forceUsersPasswordChange();
      $selected_roles[] = $rid;

      // Since all users on the site have had their password change forced,
      // no other queries need to be run on the users table. However, we do want
      // to log the rid for each role. In order to set up this functionality,
      // a flag is set indicating that the authenticated users role was selected.
      $au = TRUE;
    }
    elseif ($rid) {
      $selected_roles[] = $rid;

      // Only execute the following code if the authenticated users role was not selected
      if (!$au) {

        // Get a list of UIDs for the users in that role
        $uids = $this->passwordChangeService
          ->getUsersForRole($rid, TRUE);

        // If the role has any users, force them to change their password
        if (count($uids)) {
          $this->passwordChangeService
            ->forceUsersPasswordChange($uids);
        }
      }
    }
  }

  // If any roles have had a forced password change, enter the following conditional
  if (count($selected_roles)) {

    // Log the time of the force for the role
    $this->passwordChangeService
      ->updateLastChangeForRoles($selected_roles);

    // Build an list of the names of the roles that had their password change forced
    $roles = Role::loadMultiple();
    unset($roles[RoleInterface::ANONYMOUS_ID]);
    $items = [];
    foreach ($selected_roles as $sr) {
      $items[] = $roles[$sr]
        ->label();
    }
    $list = [
      '#theme' => 'item_list',
      '#items' => $items,
    ];
    $item_list = new FormattableMarkup('@item_list', [
      '@item_list' => render($list),
    ]);

    // Set a message informing the user of the roles that had a forced password change
    if ($form_state
      ->getValue('login_only')) {
      drupal_set_message($this
        ->t('Users in the following roles will be required to change their password on their next login: @roles', [
        '@roles' => $item_list,
      ]), 'status');
    }
    else {
      drupal_set_message($this
        ->t('Users in the following roles will be required to immediately change their password: @roles', [
        '@roles' => $item_list,
      ]), 'status');
    }
  }
  $this->configFactory
    ->getEditable('force_password_change.settings')
    ->set('expire_password', (bool) $form_state
    ->getValue('expire_password'))
    ->save();
  $insert_roles = [];

  // Loop through the roles and either update their row in the expiry table, or add to the
  // insert query
  $time_periods = [
    'hour' => 60 * 60,
    'day' => 60 * 60 * 24,
    'week' => 60 * 60 * 24 * 7,
    'year' => 60 * 60 * 24 * 365,
  ];
  foreach ($form_state
    ->getValue('table') as $rid => $expiry) {

    // Convert the selected time period into a UNIX timestamp
    // that will be used to calculate whether or not the password has expired.
    $time_period = $expiry['time']['time_quantity'] * $time_periods[$expiry['time']['time_period']];

    // If the role already exists in the database, and the value has changed,
    if ($form_state
      ->getValue([
      'expiry_data',
      $rid,
    ]) && ($time_period != $form_state
      ->getValue([
      'expiry_data',
      $rid,
      'expiry',
    ]) || $expiry['weight'] != $form_state
      ->getValue([
      'expiry_data',
      $rid,
      'weight',
    ]))) {
      $this->passwordChangeService
        ->updateExpiryForRole($rid, $time_period, $expiry['weight']);
    }
    elseif (!$form_state
      ->getValue([
      'expiry_data',
      $rid,
    ])) {
      $insert_roles[] = [
        'rid' => $rid,
        'expiry' => $time_period,
        'weight' => $expiry['weight'],
      ];
    }
  }

  // Execute the query only if new roles were found
  if (count($insert_roles)) {
    $this->passwordChangeService
      ->insertExpiryForRoles($insert_roles);
  }
}