Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Usersync is almost working now. Only unescaped dots etc.. can broke

database updates.
  • Loading branch information...
commit e472709dddca8ca099e099f8af41ccbfecb5cbd2 1 parent 6a1bdf4
authored October 17, 2004

Showing 1 changed file with 20 additions and 16 deletions. Show diff stats Hide diff stats

  1. 36  auth/ldap/lib.php
36  auth/ldap/lib.php
@@ -316,13 +316,13 @@ function auth_sync_users ($firstsync=0, $unsafe_optimizations = false, $bulk_ins
316 316
 ///                         blinding fast inserts -- but test it: you may hit mysqld's 
317 317
 ///                         max_allowed_packet limit.
318 318
 
319  
-    global $CFG ;
  319
+    global $CFG,$db ;
320 320
     auth_ldap_init();
321 321
     $ldapusers     = auth_get_users('*', true); //list all but created users from ldap
322 322
 
323 323
     if (!$unsafe_optimizations) { 
324 324
         //NORMAL SYNRONIZATION ROUTINE STARTS
325  
-        
  325
+        $db->SetFetchMode(ADODB_FETCH_ASSOC) ; 
326 326
         if ($firstsync) {
327 327
             //fill idnumbers 
328 328
             $users = get_records(user, 'auth', 'ldap', '', 'id, username', '');
@@ -358,15 +358,16 @@ function auth_sync_users ($firstsync=0, $unsafe_optimizations = false, $bulk_ins
358 358
         //make information available with idnumber
359 359
         $moodleldapusers = array(); //all users in moodle db
360 360
 
361  
-        foreach ($users as $user){
362  
-            $moodleldapusers[$user->idnumber]= $user;
  361
+        foreach ($users as $id=>$info){
  362
+            $moodleldapusers[$info->idnumber]= $info;
  363
+            $moodleldapusers[$info->idnumber]->id=$id;
363 364
         }    
364 365
 
365 366
         unset($users); //not needed anymore
366 367
         
367 368
         //get attributemappings
368 369
         $attrmap = auth_ldap_attributes();
369  
-        $updatelocals = array();
  370
+        $updatelocals = array('username');
370 371
         $updateremotes = array();
371 372
         foreach ($attrmap as $key=>$value) {
372 373
             if (isset($CFG->{'auth_user_'.$key.'_updatelocal'}) && $CFG->{'auth_user_'.$key.'_updatelocal'}  ) {
@@ -389,8 +390,8 @@ function auth_sync_users ($firstsync=0, $unsafe_optimizations = false, $bulk_ins
389 390
                     $user->idnumber = bin2hex($user->idnumber);
390 391
                 } 
391 392
                 $idnumber = $user->idnumber;
392  
-
393  
-                if (isset($moodleldapusers[$idnumber])) {
  393
+                
  394
+                if (array_key_exists($idnumber, $moodleldapusers)) {
394 395
                     $arraytoupdate = &$updateusers;
395 396
                     $userinfo = $moodleldapusers[$idnumber];
396 397
                     unset($moodleldapusers[$idnumber]); 
@@ -410,10 +411,11 @@ function auth_sync_users ($firstsync=0, $unsafe_optimizations = false, $bulk_ins
410 411
                 $userinfo->timemodified = time();
411 412
                 $userinfo->confirmed = 1;
412 413
                 $userinfo->deleted = 0;
  414
+                $userinfo->auth = 'ldap';
413 415
                 
414 416
                 //store userinfo to selected array
415 417
                 $arraytoupdate[$idnumber] = $userinfo;
416  
-
  418
+               
417 419
             } else {
418 420
                //cannot sync remoteuser  without idumber
419 421
                echo 'Cannot sync remote user to moodle '.$user->dn." without idnumber field\n";
@@ -422,6 +424,7 @@ function auth_sync_users ($firstsync=0, $unsafe_optimizations = false, $bulk_ins
422 424
         
423 425
         //update local users
424 426
         foreach ($updateusers as $user) {
  427
+
425 428
             if (update_record('user', $user)) {
426 429
                 echo 'Updated user record '.$user->username."\n";
427 430
             } else {
@@ -431,31 +434,32 @@ function auth_sync_users ($firstsync=0, $unsafe_optimizations = false, $bulk_ins
431 434
         }    
432 435
 
433 436
         //add new users
434  
-        foreach ($updateusers as $user) {
  437
+        foreach ($newusers as $user) {
435 438
             if (insert_record('user', $user, $false)) {
436 439
                 echo 'Inserted user record'.$user->username."\n";
437 440
             } else {
438 441
                 echo 'Cannot insert user record'.$user->username."\n";
  442
+                print_r($user);
439 443
             }
440 444
         }    
441 445
 
442 446
         //remove old users
443  
-        foreach ($moodleldapusers as $remove) {
  447
+        foreach ($moodleldapusers as $user) {
444 448
             //following is copy pasted from admin/user.php
445 449
             //maybe this should moved to function in lib/datalib.php
446  
-            unset($updateuser);
  450
+            unset($remove);
447 451
             $remove->id = $user->id;
448 452
             $remove->deleted = "1";
449 453
             $remove->username = "$user->email.".time();  // Remember it just in case
450 454
             $remove->email = "";               // Clear this field to free it up
451 455
             $remove->timemodified = time();
452 456
             if (update_record("user", $remove)) {
453  
-                 unenrol_student($remove->id);  // From all courses
454  
-                 remove_teacher($remove->id);   // From all courses
455  
-                 remove_admin($remove->id);
456  
-                 notify(get_string("deletedactivity", "", fullname($remove, true)) );
  457
+                 unenrol_student($user->id);  // From all courses
  458
+                 remove_teacher($user->id);   // From all courses
  459
+                 remove_admin($user->id);
  460
+                 notify(get_string("deletedactivity", "", fullname($user, true)) );
457 461
              } else {
458  
-                 notify(get_string("deletednot", "", fullname($remove, true)));
  462
+                 notify(get_string("deletednot", "", fullname($user, true)));
459 463
              }
460 464
              //copy pasted part ends
461 465
         }    

0 notes on commit e472709

Please sign in to comment.
Something went wrong with that request. Please try again.