You are here

public function RoleLoginForm::submitForm in Multiple role login pages 8

Parameters

array $form:

FormStateInterface $form_state:

Return value

boolean

Overrides FormInterface::submitForm

File

src/Form/RoleLoginForm.php, line 103
Contains \Drupal\role_login_page\Form\RoleLoginForm.

Class

RoleLoginForm
Login form.

Namespace

Drupal\role_login_page\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $loginmenu_data = $this->login_settings_data;
  $roles = $loginmenu_data->roles;
  $roles = explode(',', $roles);
  $role_mismatch_error = $loginmenu_data->role_mismatch_error_text ? Html::escape($loginmenu_data->role_mismatch_error_text) : 'You do not have permissions to login through this page.';
  $invalid_credentials_error = $loginmenu_data->invalid_credentials_error_text ? Html::escape($loginmenu_data->invalid_credentials_error_text) : 'Invalid credentials.';
  $username = $form_state
    ->getValue([
    'name',
  ]);
  $password = $form_state
    ->getValue([
    'pass',
  ]);
  $redirect_path = $loginmenu_data->redirect_path ? $loginmenu_data->redirect_path : '';
  if ($uid = \Drupal::service("user.auth")
    ->authenticate($username, $password)) {
    if (_role_login_page_validate_login_roles($uid, $roles)) {
      $user = User::load($uid);
      user_login_finalize($user);
      if ($redirect_path == "/" || $redirect_path == "<front>") {
        $url = "";
        $redirect = new RedirectResponse(Url::fromUserInput('/' . $url)
          ->toString());
      }
      else {
        $redirect = new RedirectResponse(Url::fromUserInput('/' . $redirect_path)
          ->toString());
      }
      $redirect
        ->send();
      return TRUE;
    }
    else {
      $form_state
        ->setErrorByName('name', $this
        ->t($role_mismatch_error));
    }
  }
  else {
    form_set_error('name', $this
      ->t($invalid_credentials_error));
    return FALSE;
  }
}