]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-17767 loginas refactoring and simplification; full SESSION switching implemented
authorskodak <skodak>
Fri, 2 Jan 2009 15:15:26 +0000 (15:15 +0000)
committerskodak <skodak>
Fri, 2 Jan 2009 15:15:26 +0000 (15:15 +0000)
calendar/lib.php
lib/moodlelib.php
lib/sessionlib.php
lib/setup.php
lib/weblib.php

index 5f49117b12d0bef1d2384864f5b0a8f2c7319122..b7841471ebb376c388d05f068935bcc511fcd556 100644 (file)
@@ -558,7 +558,7 @@ function calendar_print_event($event) {
             $editlink   = CALENDAR_URL.'event.php?action=edit&amp;id='.$event->id.$calendarcourseid;
             $deletelink = CALENDAR_URL.'event.php?action=delete&amp;id='.$event->id.$calendarcourseid;
         } else {
-            $editlink   = $CFG->wwwroot.'/course/mod.php?update='.$event->cmid.'&amp;return=true&amp;sesskey='.$USER->sesskey;
+            $editlink   = $CFG->wwwroot.'/course/mod.php?update='.$event->cmid.'&amp;return=true&amp;sesskey='.sesskey();
             $deletelink = ''; // deleting activities directly from calendar is dangerous/confusing - see MDL-11843
         }
         echo ' <a href="'.$editlink.'"><img
@@ -1144,25 +1144,6 @@ function calendar_get_course_cached(&$coursecache, $courseid) {
 function calendar_session_vars($course=null) {
     global $SESSION, $USER;
 
-    if(!empty($USER->id) && isset($USER->realuser) && !isset($SESSION->cal_loggedinas)) {
-        // We just logged in as someone else, update the filtering
-        unset($SESSION->cal_users_shown);
-        unset($SESSION->cal_courses_shown);
-        $SESSION->cal_loggedinas = true;
-        if(intval(get_user_preferences('calendar_persistflt', 0))) {
-            calendar_set_filters_status(get_user_preferences('calendar_savedflt', 0xff));
-        }
-    }
-    else if(!empty($USER->id) && !isset($USER->realuser) && isset($SESSION->cal_loggedinas)) {
-        // We just logged back to our real self, update again
-        unset($SESSION->cal_users_shown);
-        unset($SESSION->cal_courses_shown);
-        unset($SESSION->cal_loggedinas);
-        if(intval(get_user_preferences('calendar_persistflt', 0))) {
-            calendar_set_filters_status(get_user_preferences('calendar_savedflt', 0xff));
-        }
-    }
-
     if(!isset($SESSION->cal_course_referer)) {
         $SESSION->cal_course_referer = 0;
     }
index 8c2f2ec5289a903f7e2d9e51c0109b3d3e680ff7..57fa363597701e812cf155d19f415e362912e6cd 100644 (file)
@@ -2108,7 +2108,8 @@ function require_login($courseorid=0, $autologinguest=true, $cm=null, $setwantsu
 
         } else if (has_capability('moodle/course:view', $COURSE->context)) {
             if (is_loggedinas()) {   // Make sure the REAL person can also access this course
-                if (!has_capability('moodle/course:view', $COURSE->context, $USER->realuser)) {
+                $realuser = get_real_user();
+                if (!has_capability('moodle/course:view', $COURSE->context, $realuser->id)) {
                     print_header_simple();
                     notice(get_string('studentnotallowed', '', fullname($USER, true)), $CFG->wwwroot .'/');
                 }
index 6bb51762392f61cdf9e8100a7a0cf47bf6b01b26..ed73ef086b78ffad2644c28b0652a7ac1907ae34 100644 (file)
@@ -263,6 +263,18 @@ function is_loggedinas() {
     return !empty($USER->realuser);
 }
 
+/**
+ * Returns the $USER object ignoring current login-as session
+ * @return object user object
+ */
+function get_real_user() {
+    if (is_loggedinas()) {
+        return $_SESSION['REALUSER'];
+    } else {
+        return $_SESSION['USER'];
+    }
+}
+
 /**
  * Login as another user - no security checks here.
  * @param int $userid
@@ -276,28 +288,17 @@ function session_loginas($userid, $context) {
         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;
+    // switch to fresh session
+    $_SESSION['REALSESSION'] = $SESSION;
+    $_SESSION['SESSION']     = new object();
 
 /// Create the new USER object with all details and reload needed capabilitites
+    $_SESSION['REALUSER'] = $USER;
     $USER = get_complete_user_data('id', $userid);
-    $USER->realuser = $olduserid;
+    $USER->realuser       = $_SESSION['REALUSER']->id;
     $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);
-    }
 }
 
 /**
@@ -311,21 +312,11 @@ function session_unloginas() {
         return;
     }
 
-    $USER = get_complete_user_data('id', $USER->realuser);
-    load_all_capabilities();   // load all this user's normal capabilities
+    $_SESSION['SESSION'] = $_SESSION['REALSESSION'];
+    unset($_SESSION['REALSESSION']);
 
-    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['USER'] = $_SESSION['REALUSER'];
+    unset($_SESSION['REALUSER']);
 }
 
 /**
index 026aa8ebe075dd5a71d1bf37945da3b7a59e164b..0a2dfe67df24f68d0f5e724aa508267644c5b30f 100644 (file)
@@ -505,11 +505,10 @@ global $HTTPSPAGEREQUIRED;
                                              $USER->lastname);
         }
         if (is_loggedinas()) {
-            if ($realuser = $DB->get_record('user', array('id'=>$USER->realuser))) {
-                $apachelog_username = clean_filename($realuser->username." as ".$apachelog_username);
-                $apachelog_name = clean_filename($realuser->firstname." ".$realuser->lastname ." as ".$apachelog_name);
-                $apachelog_userid = clean_filename($realuser->id." as ".$apachelog_userid);
-            }
+            $realuser = get_real_user();
+            $apachelog_username = clean_filename($realuser->username." as ".$apachelog_username);
+            $apachelog_name = clean_filename($realuser->firstname." ".$realuser->lastname ." as ".$apachelog_name);
+            $apachelog_userid = clean_filename($realuser->id." as ".$apachelog_userid);
         }
         switch ($CFG->apacheloguser) {
             case 3:
index 7d71fb11aa8de67876f29dcee407c6c0bf483c34..408a5f120364fc68b0f33fb2c74bdef0d9707de8 100644 (file)
@@ -3498,11 +3498,10 @@ function user_login_string($course=NULL, $user=NULL) {
     }
 
     if (is_loggedinas()) {
-        if ($realuser = $DB->get_record('user', array('id'=>$user->realuser))) {
-            $fullname = fullname($realuser, true);
-            $realuserinfo = " [<a $CFG->frametarget
-            href=\"$CFG->wwwroot/course/loginas.php?id=$course->id&amp;return=1&amp;sesskey=".sesskey()."\">$fullname</a>] ";
-        }
+        $realuser = get_real_user();
+        $fullname = fullname($realuser, true);
+        $realuserinfo = " [<a $CFG->frametarget
+        href=\"$CFG->wwwroot/course/loginas.php?id=$course->id&amp;return=1&amp;sesskey=".sesskey()."\">$fullname</a>] ";
     } else {
         $realuserinfo = '';
     }