From 4c63bd26a12572a0b0bce6e4c15099329181ec2c Mon Sep 17 00:00:00 2001 From: stronk7 Date: Sun, 10 May 2009 11:56:28 +0000 Subject: [PATCH] MDL-11144 - restore of mnet users. Another improvement... merged from 19_STABLE --- backup/restorelib.php | 64 +++++++++++++++++++++++++++---------------- 1 file changed, 41 insertions(+), 23 deletions(-) diff --git a/backup/restorelib.php b/backup/restorelib.php index ae5f8b4b2d..34903f0574 100644 --- a/backup/restorelib.php +++ b/backup/restorelib.php @@ -2601,31 +2601,46 @@ define('RESTORE_GROUPS_GROUPINGS', 3); //Has role teacher or student or needed $is_course_user = ($is_teacher or $is_student or $is_needed); - //Calculate mnethostid - if (empty($user->mnethosturl) || $user->mnethosturl===$CFG->wwwroot) { - $user->mnethostid = $CFG->mnet_localhost_id; - } else { - // fast url-to-id lookups - if (isset($mnethosts[$user->mnethosturl])) { - $user->mnethostid = $mnethosts[$user->mnethosturl]->id; - } else { + // in case we are restoring to same server, look for user by id + // it should return record always, but in sites rebuilt from scratch + // and being reconstructed using course backups + $user_data = false; + if (backup_is_same_site($restore)) { + $user_data = $DB->get_record('user', array('id'=>$user->id)); + } + + // Only try to perform mnethost/auth modifications if restoring to another server + // or if, while restoring to same server, the user doesn't exists yet (rebuilt site) + // + // So existing user data in same server *won't be modified by restore anymore*, + // under any circumpstance. If somehting is wrong with existing data, it's server fault. + if (!backup_is_same_site($restore) || (backup_is_same_site($restore) && !$user_data)) { + //Calculate mnethostid + if (empty($user->mnethosturl) || $user->mnethosturl===$CFG->wwwroot) { $user->mnethostid = $CFG->mnet_localhost_id; - } - } - //Arriving here, any user with mnet auth and using $CFG->mnet_localhost_id is wrong - //as own server cannot be accesed over mnet. Change auth to manual and inform about the switch - if ($user->auth == 'mnet' && $user->mnethostid == $CFG->mnet_localhost_id) { - // Respect registerauth - if ($CFG->registerauth == 'email') { - $user->auth = 'email'; } else { - $user->auth = 'manual'; + // fast url-to-id lookups + if (isset($mnethosts[$user->mnethosturl])) { + $user->mnethostid = $mnethosts[$user->mnethosturl]->id; + } else { + $user->mnethostid = $CFG->mnet_localhost_id; + } } - // inform about the automatic switch of authentication/host - if(empty($user->mnethosturl)) { - $user->mnethosturl = '----'; + //Arriving here, any user with mnet auth and using $CFG->mnet_localhost_id is wrong + //as own server cannot be accesed over mnet. Change auth to manual and inform about the switch + if ($user->auth == 'mnet' && $user->mnethostid == $CFG->mnet_localhost_id) { + // Respect registerauth + if ($CFG->registerauth == 'email') { + $user->auth = 'email'; + } else { + $user->auth = 'manual'; + } + // inform about the automatic switch of authentication/host + if(empty($user->mnethosturl)) { + $user->mnethosturl = '----'; + } + $messages[] = get_string('mnetrestore_extusers_switchuserauth', 'admin', $user); } - $messages[] = get_string('mnetrestore_extusers_switchuserauth', 'admin', $user); } unset($user->mnethosturl); @@ -2633,8 +2648,11 @@ define('RESTORE_GROUPS_GROUPINGS', 3); $newid=null; //check if it exists (by username) and get its id $user_exists = true; - $user_data = $DB->get_record("user", array("username"=>$user->username, - 'mnethostid'=>$user->mnethostid)); + if (!backup_is_same_site($restore)) { /// Restoring to another server, look for existing user based on fields + /// If restoring to same server, look has been performed some lines above (by id) + $user_data = $DB->get_record('user', array('username'=>$user->username, 'mnethostid'=>$user->mnethostid)); + } + if (!$user_data) { $user_exists = false; } else { -- 2.39.5