From ba19efc104587cf76fd9361f5f50d5dfdcc4569e Mon Sep 17 00:00:00 2001 From: Johann Brocail Date: Mon, 1 Feb 2016 19:32:13 +0100 Subject: [PATCH] implement add and delete method --- src/Symfony/Component/Ldap/LdapClient.php | 61 +++++++++++++++++------ 1 file changed, 46 insertions(+), 15 deletions(-) diff --git a/src/Symfony/Component/Ldap/LdapClient.php b/src/Symfony/Component/Ldap/LdapClient.php index 3fc0cb5454240..b437c784c0e23 100644 --- a/src/Symfony/Component/Ldap/LdapClient.php +++ b/src/Symfony/Component/Ldap/LdapClient.php @@ -54,6 +54,8 @@ public function __construct($host = null, $port = 389, $version = 3, $useSsl = f $this->useSsl = (bool) $useSsl; $this->useStartTls = (bool) $useStartTls; $this->optReferrals = (bool) $optReferrals; + + $this->connect(); } public function __destruct() @@ -66,10 +68,6 @@ public function __destruct() */ public function bind($dn = null, $password = null) { - if (!$this->connection) { - $this->connect(); - } - if (false === @ldap_bind($this->connection, $dn, $password)) { throw new ConnectionException(ldap_error($this->connection)); } @@ -115,23 +113,56 @@ public function escape($subject, $ignore = '', $flags = 0) return $value; } + /** + * Add entry to the ldap. + * + * @param string $dn A LDAP dn + * @param array $entry key:value + * + * @throws \Exception. + */ + public function add($dn, $entry) + { + if(ldap_add($this->connection, $dn, $entry) === false) + { + throw new \Exception('Cannot add to ldap : '.ldap_error($this->connection)); + } + + return true; + } + + /** + * Delete entry to the ldap. + * + * @param string $dn A LDAP dn + * + * @throws \Exception. + */ + public function delete($dn) + { + if(ldap_delete($this->connection, $dn) === false) + { + throw new \Exception('Cannot delete to ldap : '.ldap_error($this->connection)); + } + + return true; + } + private function connect() { - if (!$this->connection) { - $host = $this->host; + $host = $this->host; - if ($this->useSsl) { - $host = 'ldaps://'.$host; - } + if ($this->useSsl) { + $host = 'ldaps://'.$host; + } - $this->connection = ldap_connect($host, $this->port); + $this->connection = ldap_connect($host, $this->port); - ldap_set_option($this->connection, LDAP_OPT_PROTOCOL_VERSION, $this->version); - ldap_set_option($this->connection, LDAP_OPT_REFERRALS, $this->optReferrals); + ldap_set_option($this->connection, LDAP_OPT_PROTOCOL_VERSION, $this->version); + ldap_set_option($this->connection, LDAP_OPT_REFERRALS, $this->optReferrals); - if ($this->useStartTls) { - ldap_start_tls($this->connection); - } + if ($this->useStartTls) { + ldap_start_tls($this->connection); } }