Add exception to LdapServer and LdapDomain construct

This commit is contained in:
Victor LABORIE 2017-12-13 23:51:21 +01:00
parent a99bfd9900
commit 8c91f6ff2a
3 changed files with 41 additions and 32 deletions

View file

@ -7,12 +7,18 @@ if (empty($_SESSION['login'])) {
header("location: auth.php\n\n"); header("location: auth.php\n\n");
exit(0); exit(0);
} else { } else {
if (!$server = new LdapServer($_SESSION['login'])) { try {
print "<div class=\"alert alert-danger\" role=\"alert\">Erreur de connexion LDAP !</div>"; $server = new LdapServer($_SESSION['login']);
exit(1);
} else {
if (!empty($_GET['domain'])) { if (!empty($_GET['domain'])) {
$domain = new LdapDomain($server, Html::clean($_GET['domain'])); try {
$domain = new LdapDomain($server, Html::clean($_GET['domain']));
} catch (Exception $e_d) {
print '<div class="alert alert-danger" role="alert">'.$e_d->getMessage();
exit(1);
}
} }
} catch (Exception $e_s) {
print '<div class="alert alert-danger" role="alert">'.$e_s->getMessage().'</div>';
exit(1);
} }
} }

View file

@ -11,32 +11,32 @@ class LdapDomain extends LdapServer {
$this->dn = $server->dn; $this->dn = $server->dn;
$this->domain = $name; $this->domain = $name;
$sr = ldap_search($this->conn, "cn=".$this->domain.",".LDAP_BASE, "(ObjectClass=*)"); if ($sr = @ldap_search($this->conn, "cn=".$this->domain.",".LDAP_BASE, "(ObjectClass=*)")) {
$objects = ldap_get_entries($this->conn, $sr); $objects = ldap_get_entries($this->conn, $sr);
foreach($objects as $object) { foreach($objects as $object) {
if (!empty($object['objectclass'])) { if (!empty($object['objectclass'])) {
if (in_array("postfixDomain",$object['objectclass'])) { if (in_array("postfixDomain",$object['objectclass'])) {
$this->active = $object['isactive'][0]; $this->active = $object['isactive'][0];
} }
if (in_array("posixAccount",$object['objectclass'])) { if (in_array("posixAccount",$object['objectclass'])) {
array_push($this->posix_accounts,$object['uid'][0]); array_push($this->posix_accounts,$object['uid'][0]);
} }
if (in_array("mailAccount",$object['objectclass'])) { if (in_array("mailAccount",$object['objectclass'])) {
array_push($this->mail_accounts,$object['uid'][0]); array_push($this->mail_accounts,$object['uid'][0]);
} }
if (in_array("mailAlias",$object['objectclass'])) { if (in_array("mailAlias",$object['objectclass'])) {
array_push($this->mail_alias,$object['cn'][0]); array_push($this->mail_alias,$object['cn'][0]);
} }
if (in_array("sambaSamAccount",$object['objectclass'])) { if (in_array("sambaSamAccount",$object['objectclass'])) {
array_push($this->smb_accounts,$object['uid'][0]); array_push($this->smb_accounts,$object['uid'][0]);
}
} }
} }
//$this->quota = getquota($this->domain,'group');
} else {
throw new Exception("Ce domaine n'existe pas !");
} }
// $this->quota = getquota($this->domain,'group');
return $this;
} }
public function getAccounts() { public function getAccounts() {

View file

@ -7,12 +7,15 @@ class LdapServer {
public function __construct($login) { public function __construct($login) {
global $conf; global $conf;
$this->login = $login; $this->login = $login;
$this->conn = ldap_connect(LDAP_URI) or die ("Impossible de se connecter au serveur LDAP ".LDAP_URI); if (!$this->conn = ldap_connect(LDAP_URI)) {
if (!ldap_set_option($this->conn, LDAP_OPT_PROTOCOL_VERSION, 3)) { throw new Exception("Impossible de se connecter au serveur LDPA ".LDAP_URI);
echo 'Impossible de modifier la version du protocole à 3'; }
if (!ldap_set_option($this->conn, LDAP_OPT_PROTOCOL_VERSION, 3)) {
throw new Exception("Impossible de modifier la version du protocole LDAP à 3");
}
if (!ldap_bind($this->conn, LDAP_ADMIN_DN, LDAP_ADMIN_PASS)) {
throw new Exception("Authentification LDAP échoué !");
} }
ldap_bind($this->conn, LDAP_ADMIN_DN, LDAP_ADMIN_PASS) or die ("Authentification LDAP échoué !");
if (in_array($this->login, $conf['admin']['logins'])) { if (in_array($this->login, $conf['admin']['logins'])) {
$this->superadmin = true; $this->superadmin = true;
} }