You are here

class SimpleLdapUserAuthenticator in Simple LDAP 8

Hierarchy

Expanded class hierarchy of SimpleLdapUserAuthenticator

1 string reference to 'SimpleLdapUserAuthenticator'
simple_ldap_user.services.yml in modules/simple_ldap_user/simple_ldap_user.services.yml
modules/simple_ldap_user/simple_ldap_user.services.yml
1 service uses SimpleLdapUserAuthenticator
simple_ldap_user.auth in modules/simple_ldap_user/simple_ldap_user.services.yml
Drupal\simple_ldap_user\SimpleLdapUserAuthenticator

File

modules/simple_ldap_user/src/SimpleLdapUserAuthenticator.php, line 11

Namespace

Drupal\simple_ldap_user
View source
class SimpleLdapUserAuthenticator {

  /**
   * @var SimpleLdapServer
   */
  protected $server;

  /**
   * @var ImmutableConfig
   */
  protected $config;
  public function __construct(SimpleLdapServer $server, ConfigFactoryInterface $config_factory) {
    $this->server = $server;
    $this->server
      ->connect();
    $this->config = $config_factory
      ->get('simple_ldap.user');
  }

  /**
   * Attempt to authenticate against the LDAP server.
   *
   * @param $dn
   *  DN (Distinguished Name) of the user.
   * @param $password
   *
   * @return boolean
   *  TRUE on success, FALSE on failure.
   */
  public function authenticate($dn, $password) {
    return $this->server
      ->bind($dn, $password, TRUE);
  }

  /**
   * Whether the given name should be checked against the LDAP server.
   *
   * @param string $name
   *  A username that might or not be an existing user.
   *
   * @return boolean
   *  TRUE if the user should be authenticated against the LDAP server. FALSE if it is the username for Drupal user 1.
   */
  public function canAuthenticate($name) {
    $db = \Drupal::database();
    $result = $db
      ->select('users_field_data', 'ud')
      ->fields('ud', array(
      'name',
    ))
      ->condition('uid', 1)
      ->execute()
      ->fetchField();
    if ($result == $name) {
      return FALSE;
    }
    return TRUE;
  }

  /**
   * Whether we should skip any LDAP checks on a User object.
   *
   * @param $user
   *  A User object.
   *
   * @return boolean
   *  TRUE if this User should be skipped and LDAP ignored.
   */
  public function skipCheck(UserInterface $user) {
    $uid = $user
      ->get('uid')->value;

    // If User 1 or anonymous or blocked...
    if ($uid == 1 || $user
      ->isAnonymous() || $user
      ->isBlocked()) {
      return TRUE;
    }
    return FALSE;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SimpleLdapUserAuthenticator::$config protected property
SimpleLdapUserAuthenticator::$server protected property
SimpleLdapUserAuthenticator::authenticate public function Attempt to authenticate against the LDAP server.
SimpleLdapUserAuthenticator::canAuthenticate public function Whether the given name should be checked against the LDAP server.
SimpleLdapUserAuthenticator::skipCheck public function Whether we should skip any LDAP checks on a User object.
SimpleLdapUserAuthenticator::__construct public function