public function getUser()
{
if ($this->_user === false) {
self::getConn();
self::bind();
$filter = str_replace('${username}', $this->username, self::$_configs['accountPattern']);
$sr = ldap_search(self::$_conn, self::$_configs['accountBase'], $filter);
unset($filter);
if (ldap_count_entries(self::$_conn, $sr) == 0) {
ldap_close(self::$_conn);
return array();
}
$entry = ldap_first_entry(self::$_conn, $sr);
$attributes = ldap_get_attributes(self::$_conn, $entry);
$this->_user = new LdapUser();
foreach ($attributes as $key => $value) {
if ($key == 'userPassword') {
$this->_user->setPassword($value[0]);
}
if (isset(self::$_configs['attributesMap']) == true && is_array(self::$_configs['attributesMap']) == true) {
if (isset(self::$_configs['attributesMap'][$key]) == true && is_string(self::$_configs['attributesMap'][$key]) == true) {
$field = self::$_configs['attributesMap'][$key];
$this->_user->setAttribute($field, $value[0]);
unset($field);
}
} else {
if (is_string($key) == true) {
$this->_user->{$key} = $value[0];
}
}
}
ldap_close(self::$_conn);
$user = User::findByUsername(array('username' => $this->username));
if ($user == null) {
$attributes['username'] = $this->username;
$user = $this->register($attributes);
}
$this->_user->setId($user->getId());
unset($attributes, $user);
}
return $this->_user;
}