From 9566e4ce84fd52b32f5d0daea09ff867206e9515 Mon Sep 17 00:00:00 2001
From: paca70 <paca70>
Date: Mon, 16 Aug 2004 04:41:51 +0000
Subject: [PATCH] Moved groupmembership code from function auth_iscreator() to
 generic private function auth_ldap_isgroupmember().

---
 auth/ldap/lib.php | 61 ++++++++++++++++++++++++++++-------------------
 1 file changed, 36 insertions(+), 25 deletions(-)

diff --git a/auth/ldap/lib.php b/auth/ldap/lib.php
index 1a17227862..92b7fd80c7 100644
--- a/auth/ldap/lib.php
+++ b/auth/ldap/lib.php
@@ -342,38 +342,49 @@ function auth_user_disable ($username) {
 
 function auth_iscreator($username=0) {
 ///if user is member of creator group return true
-   global $CFG, $USER;
-
-   $ldapconnect = auth_ldap_connect();
-   $ldapbind = auth_ldap_bind($ldapconnect);
-
-   if (! $username) {
-       $username=$USER->username;
-   }
-   
-   if ((! $CFG->ldap_creators) OR (! $CFG->ldap_memberattribute)) {
-      return false;
-   } else {
-      $groups = explode(";",$CFG->ldap_creators);
-   }
-
-
-   //build filter
-   $filter = "(& ($CFG->ldap_user_attribute=$username)(|";
-   foreach ($groups as $group){
-       $filter .= "($CFG->ldap_memberattribute=$group)";
-   }
-   $filter .= "))";
-   //search
-   $result = auth_ldap_get_userlist($filter);
+    global $USER , $CFG; 
+    if (! $username) {
+        $username=$USER->username;
+    }
    
-   return count($result);
+    if ((! $CFG->ldap_creators) OR (! $CFG->ldap_memberattribute)) {
+        return false;
+    } 
 
+    return auth_ldap_isgroupmember($username, $CFG->ldap_creators);
+ 
 }
 
 //PRIVATE FUNCTIONS starts
 //private functions are named as auth_ldap*
 
+function auth_ldap_isgroupmember ($username='', $groupdns='') {
+// Takes username and groupdn(s) , separated by ;
+// Returns true if user is member of any given groups
+
+    global $CFG, $USER;
+
+    $ldapconnect = auth_ldap_connect();
+    $ldapbind = auth_ldap_bind($ldapconnect);
+   
+    if (empty($username) OR empty($groupdns)) {
+        return false;
+    }
+    
+    $groups = explode(";",$groupdns);
+
+    //build filter
+    $filter = "(& ($CFG->ldap_user_attribute=$username)(|";
+    foreach ($groups as $group){
+        $filter .= "($CFG->ldap_memberattribute=$group)";
+    }
+    $filter .= "))";
+    //search
+    $result = auth_ldap_get_userlist($filter);
+   
+    return count($result);
+
+}
 function auth_ldap_connect(){
 /// connects to ldap-server
     global $CFG;
-- 
2.39.5