You are here

public function SecuresiteManager::plainAuth in Secure Site 8

File

src/SecuresiteManager.php, line 156
Contains \Drupal\securesite\SecuresiteManager.

Class

SecuresiteManager

Namespace

Drupal\securesite

Code

public function plainAuth($edit) {

  // We cant set username to be a required field so we check here if it is empty
  if (empty($edit['name'])) {
    drupal_set_message(t('Unrecognized user name and/or password.'), 'error');
    $this
      ->showDialog($this
      ->getType());
  }
  $accounts = $this->entityManager
    ->getStorage('user')
    ->loadByProperties(array(
    'name' => $edit['name'],
    'status' => 1,
  ));
  $account = reset($accounts);
  if (!$account) {

    // Not a registered user.
    // If we have correct LDAP credentials, register this new user.
    if (\Drupal::moduleHandler()
      ->moduleExists('ldapauth') && _ldapauth_auth($edit['name'], $edit['pass'], TRUE) !== FALSE) {
      $accounts = $this->entityManager
        ->getStorage('user')
        ->loadByProperties(array(
        'name' => $edit['name'],
        'status' => 1,
      ));
      $account = reset($accounts);

      // System should be setup correctly now, perform log-in.
      if ($account != FALSE) {
        $this
          ->userLogin($edit, $account);
      }
    }
    else {

      // See if we have guest user credentials.
      $this
        ->guestLogin($edit);
    }
  }
  else {
    if ($this->userAuth
      ->authenticate($edit['name'], $edit['pass']) || \Drupal::moduleHandler()
      ->moduleExists('ldapauth') && _ldapauth_auth($edit['name'], $edit['pass']) !== FALSE) {

      // Password is correct. Perform log-in.
      $this
        ->userLogin($edit, $account);
    }
    else {

      // Request credentials using most secure authentication method.
      \Drupal::logger('user')
        ->notice('Log-in attempt failed for %user.', array(
        '%user' => $edit['name'],
      ));
      drupal_set_message(t('Unrecognized user name and/or password.'), 'error');
      $this
        ->showDialog($this
        ->getType());
    }
  }
}