You are here

class SimpleLdapConnection in Simple LDAP 8

Hierarchy

Expanded class hierarchy of SimpleLdapConnection

1 file declares its use of SimpleLdapConnection
SimpleLdap.php in src/SimpleLdap.php
1 string reference to 'SimpleLdapConnection'
simple_ldap.services.yml in ./simple_ldap.services.yml
simple_ldap.services.yml
1 service uses SimpleLdapConnection
simple_ldap.connection in ./simple_ldap.services.yml
Drupal\simple_ldap\SimpleLdapConnection

File

src/SimpleLdapConnection.php, line 9

Namespace

Drupal\simple_ldap
View source
class SimpleLdapConnection implements SimpleLdapConnectionInterface {

  /**
   * Simple LDAP Server Configuration
   *
   * @var \Drupal\Core\Config\ImmutableConfig
   */
  protected $config;

  /**
   * @var string
   */
  protected $connection_string;

  /**
   * The LDAP link identifier
   *
   * @var resource
   */
  private $connection;

  /**
   * Constructs a SimpleLdapConnection.
   *
   * @param ConfigFactoryInterface $config_factory
   */
  public function __construct(ConfigFactoryInterface $config_factory) {
    $this->config = $config_factory
      ->get('simple_ldap.server');
    $connection_prefix = $this->config
      ->get('encryption') === 'ssl' ? 'ldaps://' : 'ldap://';
    $this->connection_string = $connection_prefix . $this->config
      ->get('host') . ':' . $this->config
      ->get('port');
  }
  public function __destruct() {
    $this
      ->disconnect();
  }

  /**
   * Connect to the Simple LDAP server based on configuration settings.
   *
   * @throws \Drupal\simple_ldap\SimpleLdapException
   */
  public function connect() {
    if ($this->connection) {
      return;
    }
    $this->connection = @ldap_connect($this->connection_string);

    // Timeout after 10 seconds if connection is unsuccessful.
    @ldap_set_option($this->connection, LDAP_OPT_NETWORK_TIMEOUT, 10);
    if ($this->connection === FALSE) {
      throw new SimpleLdapException('Could not connect to LDAP server: ', $this->connection);
    }
    if ($this->config
      ->get('encryption') === 'tls' && ldap_start_tls($this->connection) === FALSE) {
      throw new SimpleLdapException('Could not start TLS connection: ', $this->connection);
    }
    @ldap_set_option($this->connection, LDAP_OPT_PROTOCOL_VERSION, 3);
    @ldap_set_option($this->connection, LDAP_OPT_REFERRALS, (int) $this->config
      ->get('opt_referrals'));
  }

  /**
   * Disconnect from LDAP server.
   */
  public function disconnect() {
    if ($this->connection && is_resource($this->connection)) {
      ldap_close($this->connection);
    }
    $this->connection = NULL;
  }

  /**
   * Get the LDAP link identifier for this SimpleLdapConnection
   *
   * @return resource
   */
  public function getResource() {
    return $this->connection;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SimpleLdapConnection::$config protected property Simple LDAP Server Configuration
SimpleLdapConnection::$connection private property The LDAP link identifier
SimpleLdapConnection::$connection_string protected property
SimpleLdapConnection::connect public function Connect to the Simple LDAP server based on configuration settings. Overrides SimpleLdapConnectionInterface::connect
SimpleLdapConnection::disconnect public function Disconnect from LDAP server. Overrides SimpleLdapConnectionInterface::disconnect
SimpleLdapConnection::getResource public function Get the LDAP link identifier for this SimpleLdapConnection Overrides SimpleLdapConnectionInterface::getResource
SimpleLdapConnection::__construct public function Constructs a SimpleLdapConnection.
SimpleLdapConnection::__destruct public function