From 8d1964c4f840f1b923b3991891a1de433f68f4aa Mon Sep 17 00:00:00 2001 From: skodak Date: Fri, 2 Jan 2009 13:58:12 +0000 Subject: [PATCH] MDL-17754 loginas internals refactoring - code moved to sessionlib.php --- course/loginas.php | 41 ++-------------------------------- lib/sessionlib.php | 55 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 39 deletions(-) diff --git a/course/loginas.php b/course/loginas.php index 4b13233085..3487c7e1ab 100644 --- a/course/loginas.php +++ b/course/loginas.php @@ -12,21 +12,7 @@ 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"]); @@ -73,32 +59,9 @@ $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&user=$userid", "$oldfullname -> $newfullname"); diff --git a/lib/sessionlib.php b/lib/sessionlib.php index 577608f381..4ab41c82ff 100644 --- a/lib/sessionlib.php +++ b/lib/sessionlib.php @@ -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. -- 2.39.5