boka3/include/Ldap.php

46 lines
1.4 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_user($uid) {
$data = $this->search("uid=$uid", 'cn', 'uid');
if($data['count'] !== 1) {
$err = "LDAP search for '$uid' did not return exactly one result";
throw new Exception($err);
}
return $data[0]['cn'][0];
}
public function get_user_email($uid) {
$data = $this->search("uid=$uid", 'mail', 'uid');
if($data['count'] !== 1) {
$err = "LDAP search for '$uid' did not return exactly one result";
throw new Exception($err);
}
return $data[0]['mail'][0];
}
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];
}
}
?>