/** * Gets the Ldap user's distinguished name and optionally authenticate with the password supplied * depending on the parameters specified. * * @param boolean $authenticate True to authenticate with password. * * @return string Distinguished name of user. * * @since 2.0 * @throws Exception * @throws SHLdapException * @throws SHExceptionInvaliduser * @throws SHExceptionStacked User or configuration issues (may not be important) */ public function getId($authenticate) { try { if ($this->_dn instanceof Exception) { // Do not retry. Ldap configuration or user has problems. throw $this->_dn; } elseif (!is_null($this->_dn)) { // Check if this user should be authenticated if ($authenticate && $this->client->bindStatus !== SHLdap::AUTH_USER) { // Bind with the user now $this->_getDn(true); } // Dn has already been discovered so lets return it return $this->_dn; } /* * If the Ldap parameter override has been set then directly instantiate * the Ldap library otherwise use pre-configured platform configurations * through the Ldap library. */ $clients = SHFactory::getLdapClient($this->domain, $this->_config); // Keep a record of any exceptions called $errors = array(); // We have to get the correct LDAP client for this user foreach ($clients as $client) { if (!$client->isConnected()) { // Start the connection procedure (throws an error if it fails) $client->connect(); } try { $this->client = $client; // We need to check that this ldap client config has the required user based parameters $this->_userParams = (array) $this->client->userParams; // If a DN is returned, then this user is successfully authenticated/authorised $this->_dn = $this->_getDn($authenticate); // Emulate dn as an attribute $this->_attributes['dn'] = array($this->_dn); $this->state = self::STATE_EXISTS; return $this->_dn; } catch (Exception $e) { // Add the error to the stack $errors[] = $e; $this->client = null; } } // Failed to find any configs to match if (count($errors) > 1) { // More than one config caused issues, use the stacked exception throw new SHExceptionStacked(JText::_('LIB_SHUSERADAPTERSLDAP_ERR_10915'), 10915, $errors); } elseif (count($errors > 0)) { // Just rethrow the one exception throw $errors[0]; } } catch (Exception $e) { // Save the exception for later if required and re-throw $this->_dn = $e; throw $e; } return $this->_dn; }