public function isValidUsername()
{
$this->log('%s is called.', __FUNCTION__);
if (empty($this->config->user) || !isset($this->config->password)) {
throw new LdapException("Both LDAP user and password must be provided in the config " . "for the scalr.connections.ldap parameter's bag.");
}
$this->getConnection();
if (($ret = $this->bindRdn()) == false) {
throw new LdapException(sprintf("Cannot bind to ldap server with username '{$this->config->user}' and password in the scalr.connections.ldap section of config. %s", $this->getLdapError()));
} else {
if (stristr($this->username, "{$this->getConfig()->usernameAttribute}=")) {
$filter = sprintf('(&%s(' . $this->getConfig()->usernameAttribute . '=%s))', $this->config->userFilter, self::realEscape($this->uid));
} else {
$filter = sprintf('(&%s(' . $this->getConfig()->usernameAttribute . '=%s))', $this->config->userFilter, self::realEscape(strtok($this->username, '@')));
}
$attrs = array('dn', 'memberof');
if ($this->config->mailAttribute) {
$mailAttribute = strtolower($this->config->mailAttribute);
$attrs[] = $mailAttribute;
}
$query = @ldap_search($this->conn, $this->config->baseDn, $filter, $attrs, 0, 1);
$this->log("Query baseDn (3):%s filter:%s, attributes: %s - %s", $this->config->baseDn, $filter, join(', ', $attrs), $query !== false ? 'OK' : 'Failed');
if ($query !== false) {
$results = ldap_get_entries($this->conn, $query);
if ($results['count'] == 1) {
//Caches base DN to increase performance
$this->dn = $results[0]['dn'];
$this->memberofDn = $results[0]['memberof'];
if (isset($mailAttribute) && isset($results[0][$mailAttribute])) {
$this->email = (is_array($results[0][$mailAttribute]) ? $results[0][$mailAttribute][0] : $results[0][$mailAttribute]) . '';
$this->log('Email has been retrieved: %s', $this->email);
}
if (isset($this->memberofDn['count'])) {
unset($this->memberofDn['count']);
}
$ret = true;
} else {
$ret = false;
}
} else {
$ret = false;
}
}
return $ret;
}