]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-17754 loginas internals refactoring - code moved to sessionlib.php
authorskodak <skodak>
Fri, 2 Jan 2009 13:58:12 +0000 (13:58 +0000)
committerskodak <skodak>
Fri, 2 Jan 2009 13:58:12 +0000 (13:58 +0000)
course/loginas.php
lib/sessionlib.php

index 4b13233085c0b431cca305bb28090ef19d61cfcc..3487c7e1ab7ef908b78f2c7d9ea1fa3676e03962 100644 (file)
             print_error('confirmsesskeybad');
         }
 
-        $USER = get_complete_user_data('id', $USER->realuser);
-        load_all_capabilities();   // load all this user's normal capabilities
-
-        if (isset($SESSION->oldcurrentgroup)) {      // Restore previous "current group" cache.
-            $SESSION->currentgroup = $SESSION->oldcurrentgroup;
-            unset($SESSION->oldcurrentgroup);
-        }
-        if (isset($SESSION->oldtimeaccess)) {        // Restore previous timeaccess settings
-            $USER->timeaccess = $SESSION->oldtimeaccess;
-            unset($SESSION->oldtimeaccess);
-        }
-        if (isset($SESSION->grade_last_report)) {    // Restore grade defaults if any
-            $USER->grade_last_report = $SESSION->grade_last_report;
-            unset($SESSION->grade_last_report);
-        }
+        session_unloginas();
 
         if ($return and isset($_SERVER["HTTP_REFERER"])) { // That's all we wanted to do, so let's go back
             redirect($_SERVER["HTTP_REFERER"]);
         $context = $coursecontext;
     }
 
-/// Remember current timeaccess settings for later
-
-    if (isset($USER->timeaccess)) {
-        $SESSION->oldtimeaccess = $USER->timeaccess;
-    }
-    if (isset($USER->grade_last_report)) {
-        $SESSION->grade_last_report = $USER->grade_last_report;
-    }
-
 /// Login as this user and return to course home page.
-
     $oldfullname = fullname($USER, true);
-    $olduserid   = $USER->id;
-
-/// Create the new USER object with all details and reload needed capabilitites
-    $USER = get_complete_user_data('id', $userid);
-    $USER->realuser = $olduserid;
-    $USER->loginascontext = $context;
-    check_enrolment_plugins($USER);
-    load_all_capabilities();   // reload capabilities
-
-    if (isset($SESSION->currentgroup)) {    // Remember current cache setting for later
-        $SESSION->oldcurrentgroup = $SESSION->currentgroup;
-        unset($SESSION->currentgroup);
-    }
-
+    session_loginas($userid, $context);
     $newfullname = fullname($USER, true);
 
     add_to_log($course->id, "course", "loginas", "../user/view.php?id=$course->id&amp;user=$userid", "$oldfullname -> $newfullname");
index 577608f3810300f3c71e640804b0c32fb126c17f..4ab41c82ffff7d5cb7d7c61c2325e7471f793235 100644 (file)
@@ -249,6 +249,61 @@ function get_moodle_cookie() {
     }
 }
 
+function session_loginas($userid, $context) {
+    global $USER, $SESSION;
+
+    if (!empty($USER->realuser)) {
+        return;
+    }
+
+/// Remember current timeaccess settings for later
+
+    if (isset($USER->timeaccess)) {
+        $SESSION->oldtimeaccess = $USER->timeaccess;
+    }
+    if (isset($USER->grade_last_report)) {
+        $SESSION->grade_last_report = $USER->grade_last_report;
+    }
+
+    $olduserid   = $USER->id;
+
+/// Create the new USER object with all details and reload needed capabilitites
+    $USER = get_complete_user_data('id', $userid);
+    $USER->realuser = $olduserid;
+    $USER->loginascontext = $context;
+    check_enrolment_plugins($USER);
+    load_all_capabilities();   // reload capabilities
+
+    if (isset($SESSION->currentgroup)) {    // Remember current cache setting for later
+        $SESSION->oldcurrentgroup = $SESSION->currentgroup;
+        unset($SESSION->currentgroup);
+    }
+}
+
+function session_unloginas() {
+    global $USER, $SESSION;
+
+    if (empty($USER->realuser)) {
+        return;
+    }
+
+    $USER = get_complete_user_data('id', $USER->realuser);
+    load_all_capabilities();   // load all this user's normal capabilities
+
+    if (isset($SESSION->oldcurrentgroup)) {      // Restore previous "current group" cache.
+        $SESSION->currentgroup = $SESSION->oldcurrentgroup;
+        unset($SESSION->oldcurrentgroup);
+    }
+    if (isset($SESSION->oldtimeaccess)) {        // Restore previous timeaccess settings
+        $USER->timeaccess = $SESSION->oldtimeaccess;
+        unset($SESSION->oldtimeaccess);
+    }
+    if (isset($SESSION->grade_last_report)) {    // Restore grade defaults if any
+        $USER->grade_last_report = $SESSION->grade_last_report;
+        unset($SESSION->grade_last_report);
+    }
+}
+
 /**
 * Enable cookieless sessions by including $CFG->usesid=true;
 * in config.php.