From 815002f07fd103d89d7d45f648119b732eacb19c Mon Sep 17 00:00:00 2001 From: paca70 Date: Sun, 15 Aug 2004 17:46:30 +0000 Subject: [PATCH] while waiting that guid-field, I added two functions: auth_get_users () Returns all userobjects from external database, with userinformation like address, phone ... auth_sync_users () 1. Adds users from externaldatabase to moodle. 2. renamed user in moodles db if renamed in externaldb 3. "Delete" user in moodles db if removed from externaldb and configuration interface for guid. ;) --- auth/ldap/config.html | 7 ++ auth/ldap/lib.php | 193 ++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 195 insertions(+), 5 deletions(-) diff --git a/auth/ldap/config.html b/auth/ldap/config.html index bfaf88cf28..018072bed5 100644 --- a/auth/ldap/config.html +++ b/auth/ldap/config.html @@ -228,6 +228,13 @@ + +

: + + + + +

: diff --git a/auth/ldap/lib.php b/auth/ldap/lib.php index 42c519dde1..1a17227862 100644 --- a/auth/ldap/lib.php +++ b/auth/ldap/lib.php @@ -1,29 +1,65 @@ + +Usersync is quite heavy process, it could be good idea to place that script outside of webroot and run it with cron. + + Any feedback is wellcome, Petri Asikainen paca@sci.fi @@ -149,6 +185,130 @@ function auth_user_create ($userobject,$plainpass) { } +function auth_get_users($filter='*') { +//returns all userobjects from external database + global $CFG; + + $fresult = array(); + $ldap_connection = auth_ldap_connect(); + + auth_ldap_bind($ldap_connection); + + if (! isset($CFG->ldap_objectclass)) { + $CFG->ldap_objectclass="objectClass=*"; + } + + if ($filter=="*") { + $filter = "(&(".$CFG->ldap_user_attribute."=*)(".$CFG->ldap_objectclass."))"; + } + + $contexts = explode(";",$CFG->ldap_contexts); + + if (!empty($CFG->ldap_create_context)){ + array_push($contexts, $CFG->ldap_create_context); + } + + $attrmap = auth_ldap_attributes(); + + $search_attribs = array(); + + foreach ($attrmap as $key=>$value) { + if (!in_array($value, $search_attribs)) { + array_push($search_attribs, $value); + } + } + + + foreach ($contexts as $context) { + + if ($CFG->ldap_search_sub) { + //use ldap_search to find first user from subtree + $ldap_result = ldap_search($ldap_connection, $context, + $filter, + $search_attribs); + } else { + //search only in this context + $ldap_result = ldap_list($ldap_connection, $context, + $filter, + $search_attribs); + } + + $users = auth_ldap_get_entries($ldap_connection, $ldap_result); + + //add found users to list + foreach ($users as $ldapuser=>$attribs) { + $user = new object(); + foreach ($attrmap as $key=>$value){ + if(isset($users[$ldapuser][$value][0])){ + $user->$key=$users[$ldapuser][$value][0]; + } + } + //quick way to get around binarystrings + $user->guid=bin2hex($user->guid); + //add authentication source stamp + $user->auth='ldap'; + $fresult[$user->username]=$user; + + } + } + + return $fresult; +} + +function auth_sync_users () { +//Syncronizes userdb with ldap +//This will add, rename + global $CFG ; + $users = auth_get_users(); + $usedguids = Array(); + + foreach ($users as $user) { + $usedguids[] = $user->guid; //we will need all used guids later + //update modified time + $user->modified = time(); + //All users are confirmed + $user->confirmed = 1; + // if user does not exist create it + if (!record_exists('user','auth', 'ldap', 'guid', $user->guid)) { + if (insert_record ('user',$user)) { + echo "inserted user $user->username with guid $user->guid \n"; + } else { + echo "error inserting user $user->username with guid $user->guid \n"; + } + continue ; + } else { + //update username + set_field('user', 'username', $user->username , 'auth', 'ldap', 'guid', $user->guid); + } + } + + //find nonexisting users from moodles userdb + $sql = "SELECT * FROM ".$CFG->prefix."user WHERE auth='ldap' AND guid NOT IN ('".implode('\' , \'',$usedguids)."');" ; + $result = get_records_sql($sql); + + if (!empty($result)){ + foreach ($result as $user) { + //following is copy pasted from admin/user.php + //maybe this should moved to function in lib/datalib.php + unset($updateuser); + $updateuser->id = $user->id; + $updateuser->deleted = "1"; + $updateuser->username = "$user->email.".time(); // Remember it just in case + $updateuser->email = ""; // Clear this field to free it up + $updateuser->timemodified = time(); + if (update_record("user", $updateuser)) { + unenrol_student($user->id); // From all courses + remove_teacher($user->id); // From all courses + remove_admin($user->id); + notify(get_string("deletedactivity", "", fullname($user, true)) ); + } else { + notify(get_string("deletednot", "", fullname($user, true))); + } + //copy pasted part ends + } + } +} + function auth_user_activate ($username) { //activate new ldap-user after email-address is confirmed global $CFG; @@ -311,7 +471,7 @@ function auth_ldap_attributes (){ $config = (array)$CFG; $fields = array("firstname", "lastname", "email", "phone1", "phone2", "department", "address", "city", "country", "description", - "idnumber", "lang"); + "idnumber", "lang", "guid"); $moodleattributes = array(); foreach ($fields as $field) { @@ -319,6 +479,7 @@ function auth_ldap_attributes (){ $moodleattributes[$field] = $config["auth_user_$field"]; } } + $moodleattributes['username']=$config["ldap_user_attribute"]; return $moodleattributes; } @@ -370,4 +531,26 @@ function auth_ldap_get_userlist($filter="*") { return $fresult; } +function auth_ldap_get_entries($conn, $searchresult){ +//Returns values like ldap_get_entries but is +//binary compatible + $i=0; + $fresult=array(); + $entry = ldap_first_entry($conn, $searchresult); + do { + $attributes = ldap_get_attributes($conn, $entry); + for($j=0; $j<$attributes['count']; $j++) { + $values = ldap_get_values_len($conn, $entry,$attributes[$j]); + $fresult[$i][$attributes[$j]] = $values; + } + $i++; + } + while ($entry = ldap_next_entry($conn, $entry)); + //we're done + return ($fresult); +} + + + + ?> -- 2.39.5