56 lines
1.7 KiB
PHP
56 lines
1.7 KiB
PHP
<?php
|
|
class Ldap {
|
|
private $conn;
|
|
private $base_dn = "dc=su,dc=se";
|
|
|
|
public function __construct() {
|
|
$this->conn = ldap_connect('ldaps://ldap.su.se');
|
|
ldap_set_option($this->conn, LDAP_OPT_PROTOCOL_VERSION, 3);
|
|
ldap_bind($this->conn);
|
|
}
|
|
|
|
private function search($term, ...$attributes) {
|
|
$result = ldap_search($this->conn, $this->base_dn, $term, $attributes);
|
|
return ldap_get_entries($this->conn, $result);
|
|
}
|
|
|
|
public function get_attribute($uid, $attribute) {
|
|
$attribute = strtolower($attribute);
|
|
$data = $this->search("uid=$uid", $attribute);
|
|
if($data['count'] !== 1) {
|
|
$err = "LDAP search for '$uid' did not return exactly one result";
|
|
throw new Exception($err);
|
|
}
|
|
$result = $data[0];
|
|
if(!in_array($attribute, $result) || !$result[$attribute][0] ) {
|
|
$err = "LDAP search for '$uid' did not return "
|
|
."a value for '$attribute'";
|
|
throw new Exception($err);
|
|
}
|
|
return $result[$attribute][0];
|
|
}
|
|
|
|
public function get_user($uid) {
|
|
return $this->get_attribute($uid, 'cn');
|
|
}
|
|
|
|
public function get_firstname($uid) {
|
|
return $this->get_attribute($uid, 'givenName');
|
|
}
|
|
|
|
public function get_user_email($uid) {
|
|
return $this->get_attribute($uid, 'mail');
|
|
}
|
|
|
|
public function search_email($email) {
|
|
$data = $this->search("mail=$email", 'mail', 'uid');
|
|
$out = array();
|
|
if($data['count'] !== 1) {
|
|
$err = "LDAP search for '$email' did not return exactly one result.";
|
|
throw new Exception($err);
|
|
}
|
|
return $data[0]['uid'][0];
|
|
}
|
|
}
|
|
?>
|