From 7fc1276ac77f7195f9de7deb274837930c76ee4b Mon Sep 17 00:00:00 2001 From: "Lorenzo M. Catucci" Date: Mon, 5 Nov 2012 15:38:49 +0100 Subject: [PATCH 1/2] Return true or false from readAttribute if $attr is empty This way, readAttribute can act as an existence checker. --- apps/user_ldap/lib/access.php | 11 ++++++++--- apps/user_ldap/user_ldap.php | 5 ++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/apps/user_ldap/lib/access.php b/apps/user_ldap/lib/access.php index a500e1bf5b4..b09b6e7d719 100644 --- a/apps/user_ldap/lib/access.php +++ b/apps/user_ldap/lib/access.php @@ -38,9 +38,10 @@ abstract class Access { * @brief reads a given attribute for an LDAP record identified by a DN * @param $dn the record in question * @param $attr the attribute that shall be retrieved - * @returns the values in an array on success, false otherwise + * if empty, just check the record's existence + * @returns true or the values in an array on success, false otherwise * - * Reads an attribute from an LDAP entry + * Reads an attribute from an LDAP entry or check if entry exists */ public function readAttribute($dn, $attr) { if(!$this->checkConnection()) { @@ -55,10 +56,14 @@ abstract class Access { } $rr = @ldap_read($cr, $dn, 'objectClass=*', array($attr)); if(!is_resource($rr)) { - \OCP\Util::writeLog('user_ldap', 'readAttribute '.$attr.' failed for DN '.$dn, \OCP\Util::DEBUG); + \OCP\Util::writeLog('user_ldap', 'readAttribute failed for DN '.$dn, \OCP\Util::DEBUG); //in case an error occurs , e.g. object does not exist return false; } + if (empty($attr)) { + \OCP\Util::writeLog('user_ldap', 'readAttribute: '.$dn.' found', \OCP\Util::DEBUG); + return true; + } $er = ldap_first_entry($cr, $rr); //LDAP attributes are not case sensitive $result = \OCP\Util::mb_array_change_key_case(ldap_get_attributes($cr, $er), MB_CASE_LOWER, 'UTF-8'); diff --git a/apps/user_ldap/user_ldap.php b/apps/user_ldap/user_ldap.php index e104c8d1764..5e5af44d55e 100644 --- a/apps/user_ldap/user_ldap.php +++ b/apps/user_ldap/user_ldap.php @@ -141,9 +141,8 @@ class USER_LDAP extends lib\Access implements \OCP\UserInterface { return false; } - //if user really still exists, we will be able to read his objectclass - $objcs = $this->readAttribute($dn, 'objectclass'); - if(!$objcs || empty($objcs)) { + //check if user really still exists by reading its entry + if(!$this->readAttribute($dn, '') ) { $this->connection->writeToCache('userExists'.$uid, false); return false; } From d62f138153a01437709675f44a46027c28aa0281 Mon Sep 17 00:00:00 2001 From: "Lorenzo M. Catucci" Date: Mon, 5 Nov 2012 17:35:09 +0100 Subject: [PATCH 2/2] Return an empty array on succesful existence check --- apps/user_ldap/lib/access.php | 5 +++-- apps/user_ldap/user_ldap.php | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/apps/user_ldap/lib/access.php b/apps/user_ldap/lib/access.php index b09b6e7d719..dd195f28978 100644 --- a/apps/user_ldap/lib/access.php +++ b/apps/user_ldap/lib/access.php @@ -39,7 +39,8 @@ abstract class Access { * @param $dn the record in question * @param $attr the attribute that shall be retrieved * if empty, just check the record's existence - * @returns true or the values in an array on success, false otherwise + * @returns an array of values on success or an empty + * array if $attr is empty, false otherwise * * Reads an attribute from an LDAP entry or check if entry exists */ @@ -62,7 +63,7 @@ abstract class Access { } if (empty($attr)) { \OCP\Util::writeLog('user_ldap', 'readAttribute: '.$dn.' found', \OCP\Util::DEBUG); - return true; + return array(); } $er = ldap_first_entry($cr, $rr); //LDAP attributes are not case sensitive diff --git a/apps/user_ldap/user_ldap.php b/apps/user_ldap/user_ldap.php index 5e5af44d55e..d4f25932066 100644 --- a/apps/user_ldap/user_ldap.php +++ b/apps/user_ldap/user_ldap.php @@ -142,7 +142,7 @@ class USER_LDAP extends lib\Access implements \OCP\UserInterface { } //check if user really still exists by reading its entry - if(!$this->readAttribute($dn, '') ) { + if(!is_array($this->readAttribute($dn, ''))) { $this->connection->writeToCache('userExists'.$uid, false); return false; }