From: skodak Date: Mon, 19 Mar 2007 18:54:58 +0000 (+0000) Subject: MDL-8840 Loginas cleanup; merged from MOODLE_18_STABLE X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=f6f66b033876182fb501e546d429dfebcd52f624;p=moodle.git MDL-8840 Loginas cleanup; merged from MOODLE_18_STABLE + merging perflib toys from moodlelib.php --- diff --git a/course/loginas.php b/course/loginas.php index 7fd8e2f135..173121c34e 100644 --- a/course/loginas.php +++ b/course/loginas.php @@ -1,13 +1,17 @@ realuser)) { + if (!confirm_sesskey()) { + print_error('confirmsesskeybad'); + } + $USER = get_complete_user_data('id', $USER->realuser); load_all_capabilities(); // load all this user's normal capabilities @@ -27,42 +31,43 @@ } } - ///------------------------------------- /// We are trying to log in as this user in the first place - $id = required_param('id', PARAM_INT); // course id - $userid = required_param('user', PARAM_INT); // login as this user + $id = optional_param('id', SITEID, PARAM_INT); // course id + $userid = required_param('user', PARAM_INT); // login as this user - if (!$site = get_site()) { - error("Site isn't defined!"); + if (!confirm_sesskey()) { + print_error('confirmsesskeybad'); } - - if (! $course = get_record("course", "id", $id)) { + + if (! $course = get_record('course', 'id', $id)) { error("Course ID was incorrect"); } /// User must be logged in - if ($course->id == SITEID) { - require_login(); - $context = get_context_instance(CONTEXT_SYSTEM, SITEID); - } else { - require_login($course->id); - $context = get_context_instance(CONTEXT_COURSE, $course->id); - if (!has_capability('moodle/course:view', $context, $userid, false)) { + $systemcontext = get_context_instance(CONTEXT_SYSTEM); + $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id); + + require_login(); + + if (has_capability('moodle/user:loginas', $systemcontext)) { + if (has_capability('moodle/site:doanything', $systemcontext, $userid, false)) { + print_error('nologinas'); + } + $context = $systemcontext; + } else if (has_capability('moodle/user:loginas', $coursecontext)) { + require_login($course); + if (!has_capability('moodle/course:view', $coursecontext, $userid, false)) { error('This user is not in this course!'); } - if (has_capability('moodle/site:doanything', $context, $userid, false)) { + if (has_capability('moodle/site:doanything', $coursecontext, $userid, false)) { print_error('nologinas'); } + $context = $coursecontext; } -/// User must have permissions - - require_capability('moodle/user:loginas', $context); - - /// Remember current timeaccess settings for later if (isset($USER->timeaccess)) { @@ -74,10 +79,11 @@ $oldfullname = fullname($USER, true); $olduserid = $USER->id; - $USER = get_complete_user_data('id', $userid); // Create the new USER object with all details +/// Create the new USER object with all details and reload needed capabilitites + $USER = get_complete_user_data('id', $userid); $USER->realuser = $olduserid; - - load_user_capability('', $context); // load this user's capabilities for this context only + $USER->loginascontext = $context; + load_all_capabilities(); // reload capabilities if (isset($SESSION->currentgroup)) { // Remember current cache setting for later $SESSION->oldcurrentgroup = $SESSION->currentgroup; diff --git a/lang/en_utf8/error.php b/lang/en_utf8/error.php index 9425c8d511..84f95655bc 100644 --- a/lang/en_utf8/error.php +++ b/lang/en_utf8/error.php @@ -39,6 +39,7 @@ $string['invalidfiletype'] = '\"$a\" is not a valid file type'; $string['invalidmd5'] = 'Invalid md5'; $string['invalidrole'] = 'Invalid role'; $string['invalidxmlfile'] = '\"$a\" is not a valid XML file'; +$string['loginascourseredir'] = 'You can not enter this course.
You have to terminate the \"Login as\" session before entering any other course.'; $string['missingfield'] = 'Field \"$a\" is missing'; $string['missingrequiredfield'] = 'Some required field is missing'; $string['modulemissingcode'] = 'Module $a is missing the code needed to perform this function'; diff --git a/lib/accesslib.php b/lib/accesslib.php index 021458ca5a..c35c5b5475 100755 --- a/lib/accesslib.php +++ b/lib/accesslib.php @@ -1118,7 +1118,16 @@ function load_all_capabilities() { $defcaps = load_defaultuser_role(true); } - load_user_capability(); + if (empty($USER->realuser)) { + load_user_capability(); + } else { + if ($USER->loginascontext->contextlevel != CONTEXT_SYSTEM) { + // load only course caqpabilitites - it may not always work as expected + load_user_capability('', $USER->loginascontext); + } else { + load_user_capability(); + } + } if (!empty($USER->switchrole)) { diff --git a/lib/moodlelib.php b/lib/moodlelib.php index 1e97ed01af..e28728935e 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -1658,6 +1658,16 @@ function require_login($courseorid=0, $autologinguest=true, $cm=null) { exit; } +/// loginas as redirection if needed + if ($COURSE->id != SITEID and !empty($USER->realuser)) { + if ($USER->loginascontext->contextlevel == CONTEXT_COURSE) { + if ($USER->loginascontext->instanceid != $COURSE->id) { + print_error('loginascourseredir', '', $CFG->wwwroot.'/course/view.php?id='.$USER->loginascontext->instanceid); + } + } + } + + /// check whether the user should be changing password (but only if it is REALLY them) $userauth = get_auth_plugin($USER->auth); if (get_user_preferences('auth_forcepasswordchange') && empty($USER->realuser)) { @@ -6548,6 +6558,12 @@ function get_performance_info() { $info['html'] .= 'Log writes '.$info['logwrites'].' '; $info['txt'] .= 'logwrites: '.$info['logwrites'].' '; } + + if (!empty($PERF->profiling)) { + require_once($CFG->dirroot .'/lib/profilerlib.php'); + $profiler = new Profiler(); + $info['html'] .= ''.$profiler->get_profiling().''; + } if (function_exists('posix_times')) { $ptimes = posix_times(); @@ -6591,6 +6607,9 @@ function get_performance_info() { return $info; } +function apd_get_profiling() { + return shell_exec('pprofp -u ' . ini_get('apd.dumpdir') . '/pprof.' . getmypid() . '.*'); +} function remove_dir($dir, $content_only=false) { // if content_only=true then delete all but diff --git a/lib/weblib.php b/lib/weblib.php index 2ffe33b069..4ad2020243 100644 --- a/lib/weblib.php +++ b/lib/weblib.php @@ -2742,11 +2742,11 @@ function user_login_string($course=NULL, $user=NULL) { $course = $SITE; } - if (isset($user->realuser)) { + if (!empty($user->realuser)) { if ($realuser = get_record('user', 'id', $user->realuser)) { $fullname = fullname($realuser, true); $realuserinfo = " [frametarget - href=\"$CFG->wwwroot/course/loginas.php?id=$course->id&return=1\">$fullname] "; + href=\"$CFG->wwwroot/course/loginas.php?id=$course->id&return=1&sesskey=".sesskey()."\">$fullname] "; } } else { $realuserinfo = ''; @@ -2761,7 +2761,7 @@ function user_login_string($course=NULL, $user=NULL) { if (empty($course->id)) { // $course->id is not defined during installation return ''; - } else if (isset($user->id) and $user->id) { + } else if (!empty($user->id)) { $context = get_context_instance(CONTEXT_COURSE, $course->id); $fullname = fullname($user, true); @@ -3354,9 +3354,9 @@ has_capability('moodle/course:viewhiddenuserfields', $context)) { if (has_capability('moodle/role:assign', $context, NULL)) { // Includes admins $output .= ''. $string->unenrol .'
'; } - if ($USER->id != $user->id && has_capability('moodle/user:loginas', $context) && + if ($USER->id != $user->id && empty($USER->realuser) && has_capability('moodle/user:loginas', $context) && ! has_capability('moodle/site:doanything', $context, $user->id, false)) { - $output .= ''. $string->loginas .'
'; + $output .= ''. $string->loginas .'
'; } $output .= ''. $string->fullprofile .'...'; diff --git a/user/view.php b/user/view.php index f3db2b96e2..69a1075a77 100644 --- a/user/view.php +++ b/user/view.php @@ -399,12 +399,13 @@ } } - if ($USER->id != $user->id && has_capability('moodle/user:loginas', $coursecontext) && + if ($USER->id != $user->id && empty($USER->realuser) && has_capability('moodle/user:loginas', $coursecontext) && ! has_capability('moodle/site:doanything', $coursecontext, $user->id, false)) { echo '
'; echo "
"; echo ''; echo ''; + echo ''; echo ''; echo "
"; echo '
';