/** * Constructor. * * Tries to bind to the AD domain over LDAP or LDAPs * * @param array|Configuration $configuration The Adldap configuration options array * @param ConnectionInterface $connection The connection you'd like to use * @param bool $autoConnect Whether or not you want to connect on construct * * @throws AdldapException * @throws InvalidArgumentException */ public function __construct($configuration, $connection = null, $autoConnect = true) { if (is_array($configuration)) { // If we've been given an array, we'll create // a new Configuration instance. $configuration = new Configuration($configuration); } else { if (!$configuration instanceof Configuration) { // Otherwise, if the Configuration isn't a Configuration // object, we'll throw an exception. $message = 'Configuration must either be an array or an instance of Adldap\\Connections\\Configuration'; throw new InvalidArgumentException($message); } } // Set the configuration $this->setConfiguration($configuration); // Create a new LDAP Connection if one isn't set if (!$connection) { $connection = new Connections\Ldap(); } // Set the connection $this->setConnection($connection); // If we dev wants to connect automatically, we'll construct // a new Connection and try to connect using the // supplied configuration object if ($autoConnect) { // Set the beginning protocol options on the connection // if they're set in the configuration if ($this->configuration->getUseSSL()) { $this->connection->useSSL(); } else { if ($this->configuration->getUseTLS()) { $this->connection->useTLS(); } } // If we've set SSO to true, we'll make sure we check if // SSO is supported, and if so we'll bind it to // the current LDAP connection. if ($this->configuration->getUseSSO()) { if ($this->connection->isSaslSupported()) { $this->connection->useSSO(); } } // Looks like we're all set. Let's try and connect $this->connect(); } }
/** * Prepares the connection by setting configured parameters. * * @return void */ protected function prepareConnection() { // Set the beginning protocol options on the connection // if they're set in the configuration. if ($this->configuration->getUseSSL()) { $this->connection->useSSL(); } elseif ($this->configuration->getUseTLS()) { $this->connection->useTLS(); } // If we've set SSO to true, we'll make sure we check if // SSO is supported, and if so we'll bind it to // the current LDAP connection. if ($this->configuration->getUseSSO() && $this->connection->isSaslSupported()) { $this->connection->useSSO(); } }
/** * {@inheritdoc} */ public function connect($username = null, $password = null) { // Set the beginning protocol options on the connection // if they're set in the configuration. if ($this->configuration->getUseSSL()) { $this->connection->useSSL(); } elseif ($this->configuration->getUseTLS()) { $this->connection->useTLS(); } // If we've set SSO to true, we'll make sure we check if // SSO is supported, and if so we'll bind it to // the current LDAP connection. if ($this->configuration->getUseSSO() && $this->connection->isSaslSupported()) { $this->connection->useSSO(); } // Retrieve the controllers from the configuration. $controllers = $this->configuration->getDomainControllers(); if (count($controllers) === 0) { // Make sure we have at least one domain controller. throw new AdldapException('You must specify at least one domain controller in your configuration.'); } // Select a random domain controller. $controller = $controllers[array_rand($controllers)]; // Set the controller selected in the configuration so devs // can retrieve the domain controller in use if needed. $this->configuration->setDomainControllerSelected($controller); // Get the LDAP port. $port = $this->configuration->getPort(); // Create the LDAP connection. $this->connection->connect($controller, $port); // Set the LDAP options. $this->connection->setOption(LDAP_OPT_PROTOCOL_VERSION, 3); $this->connection->setOption(LDAP_OPT_REFERRALS, $this->configuration->getFollowReferrals()); // If both the username and password are null, we'll connect to the server // using the configured administrator username and password. if (is_null($username) && is_null($password)) { return $this->bindAsAdministrator(); } // Bind as the specified user. return $this->bindUsingCredentials($username, $password); }