public static function search($query, $field = 'givenname') { $ds = LDAP::connect(); $cfg = LDAP::config(); $result = array(); if ($ds) { $resource = ldap_bind($ds); $search = ldap_search($ds, $cfg['base'], "({$field}={$query})"); $entries = ldap_get_entries($ds, $search); $prop_map = array('uid' => 'crsid', 'sn' => 'surname', 'cn' => 'name', 'ou' => 'college', 'instid' => 'collegecode', 'displayname' => 'display'); foreach ($entries as $i => $r) { if (!is_integer($i)) { continue; } array_push($result, array_map(function ($a) { return $a[0]; }, Set::reassignKeys(Set::select($r, array_keys($prop_map)), $prop_map))); } } else { throw new Exception('Unable to connect to LDAP server: ' + ldap_error($ds)); } return $result; }
/** * Looks up a user by crsid * @param string $crsid user crsid * @return array user properties from ldap */ public static function lookup($crsid) { $u = LDAP::search($crsid, 'uid'); return count($u) > 0 ? $u[0] : null; }