summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksander Machniak <machniak@kolabsys.com>2014-11-04 13:49:49 (GMT)
committerAleksander Machniak <machniak@kolabsys.com>2014-11-04 13:49:49 (GMT)
commit6eeff12a1c8d3897704676bee8523c08ca841f8a (patch)
tree44a59e3d8b973e1e1a782b36db4bdb125a44942f
parentee60f50af2c4d27cec8d6c4253981f583236b5ff (diff)
downloadNet_LDAP3-6eeff12a1c8d3897704676bee8523c08ca841f8a.tar.gz
Add possibility to return user attributes from login() (#3858) + small code improvements.
-rw-r--r--lib/Net/LDAP3.php69
1 files changed, 34 insertions, 35 deletions
diff --git a/lib/Net/LDAP3.php b/lib/Net/LDAP3.php
index d370a45..3930a07 100644
--- a/lib/Net/LDAP3.php
+++ b/lib/Net/LDAP3.php
@@ -1068,7 +1068,7 @@ class Net_LDAP3
return $replica_hosts;
}
- public function login($username, $password, $domain = null)
+ public function login($username, $password, $domain = null, &$attributes = null)
{
$this->_debug("Net_LDAP3::login(\$username = '" . $username . "', \$password = '****', \$domain = '" . $domain . "')");
@@ -1102,6 +1102,12 @@ class Net_LDAP3
return null;
}
+ // fetch user attributes if requested
+ if (!empty($attributes)) {
+ $attributes = $this->get_entry($entry_dn, $attributes);
+ $attributes = self::normalize_entry($attributes, true);
+ }
+
return $entry_dn;
}
@@ -1149,7 +1155,7 @@ class Net_LDAP3
$this->_debug("Net::LDAP3::login() actual filter: " . $filter);
- $result = $this->search($base_dn, $filter, 'sub');
+ $result = $this->search($base_dn, $filter, 'sub', $attributes);
if (!$result) {
$this->_debug("Could not search $base_dn with $filter");
@@ -1165,9 +1171,8 @@ class Net_LDAP3
return null;
}
- $entries = $result->entries();
- $entry = self::normalize_result($entries);
- $entry_dn = key($entry);
+ $entries = $result->entries(true);
+ $entry_dn = key($entries);
$bound = $this->bind($entry_dn, $password);
@@ -1176,6 +1181,11 @@ class Net_LDAP3
return null;
}
+ // replace attributes list with key-value data
+ if (!empty($attributes)) {
+ $attributes = $entries[$entry_dn];
+ }
+
return $entry_dn;
}
@@ -1747,10 +1757,11 @@ class Net_LDAP3
* Turn an LDAP entry into a regular PHP array with attributes as keys.
*
* @param array $entry Attributes array as retrieved from ldap_get_attributes() or ldap_get_entries()
+ * @param bool $flat Convert one-element-array values into strings
*
* @return array Hash array with attributes as keys
*/
- public static function normalize_entry($entry)
+ public static function normalize_entry($entry, $flat = false)
{
$rec = array();
for ($i=0; $i < $entry['count']; $i++) {
@@ -1758,6 +1769,10 @@ class Net_LDAP3
for ($j=0; $j < $entry[$attr]['count']; $j++) {
$rec[$attr][$j] = $entry[$attr][$j];
}
+
+ if ($flat && count($rec[$attr]) == 1) {
+ $rec[$attr] = $rec[$attr][0];
+ }
}
return $rec;
@@ -1775,34 +1790,19 @@ class Net_LDAP3
$result = array();
for ($x = 0; $x < $_result['count']; $x++) {
- $dn = $_result[$x]['dn'];
- $result[$dn] = array();
- for ($y = 0; $y < $_result[$x]['count']; $y++) {
- $attr = $_result[$x][$y];
- if ($_result[$x][$attr]['count'] == 1) {
- switch ($attr) {
- case 'objectclass':
- $result[$dn][$attr] = array(strtolower($_result[$x][$attr][0]));
- break;
- default:
- $result[$dn][$attr] = $_result[$x][$attr][0];
- break;
- }
+ $dn = $_result[$x]['dn'];
+ $entry = self::normalize_entry($_result[$x], true);
+
+ if (!empty($entry['objectclass'])) {
+ if (is_array($entry['objectclass'])) {
+ $entry['objectclass'] = array_map('strtolower', $entry['objectclass']);
}
else {
- $result[$dn][$attr] = array();
- for ($z = 0; $z < $_result[$x][$attr]['count']; $z++) {
- switch ($attr) {
- case 'objectclass':
- $result[$dn][$attr][] = strtolower($_result[$x][$attr][$z]);
- break;
- default:
- $result[$dn][$attr][] = $_result[$x][$attr][$z];
- break;
- }
- }
+ $entry['objectclass'] = strtolower($entry['objectclass']);
}
}
+
+ $result[$dn] = $entry;
}
return $result;
@@ -1813,16 +1813,15 @@ class Net_LDAP3
switch ($scope) {
case 2:
return 'sub';
- break;
+
case 1:
return 'one';
- break;
+
case 0:
return 'base';
- break;
+
default:
$this->_debug("Scope $scope is not a valid scope integer");
- break;
}
}
@@ -1837,7 +1836,7 @@ class Net_LDAP3
{
switch ($scope) {
case 'sub':
- $function = $ns_function = 'ldap_search';
+ $function = $ns_function = 'ldap_search';
break;
case 'base':
$function = $ns_function = 'ldap_read';