From 8a9febcdb05cda8424b1b18aa0373be1c30da923 Mon Sep 17 00:00:00 2001 From: skodak Date: Wed, 12 Sep 2007 10:45:32 +0000 Subject: [PATCH] MDL-10635 adding caching workaround for performance problems with update_course_icon() function - 1.9 will have a different solution --- lib/accesslib.php | 33 +++++++++++++++++++++++++++++++++ lib/moodlelib.php | 37 +++++++++++++++++++++++++++++-------- lib/pagelib.php | 2 +- lib/weblib.php | 25 +------------------------ 4 files changed, 64 insertions(+), 33 deletions(-) diff --git a/lib/accesslib.php b/lib/accesslib.php index 7cf11d3267d..fb93178640e 100755 --- a/lib/accesslib.php +++ b/lib/accesslib.php @@ -377,6 +377,37 @@ function require_capability($capability, $context=NULL, $userid=NULL, $doanythin } } +/** + * Cheks if current user has allowed permission for any of submitted capabilities + * in given or child contexts. + * @param object $context - a context object (record from context table) + * @param array $capabilitynames array of strings, capability names + * @return boolean + */ +function has_capability_including_child_contexts($context, $capabilitynames) { + global $USER; + + foreach ($capabilitynames as $capname) { + if (has_capability($capname, $context)) { + return true; + } + } + + if ($children = get_child_contexts($context)) { + foreach ($capabilitynames as $capname) { + foreach ($children as $child) { + if (isset($USER->capabilities[$child][$capname]) and $USER->capabilities[$child][$capname] > 0) { + // extra check for inherited prevent and prohibit + if (has_capability($capname, get_context_instance_by_id($child), $USER->id, false)) { + return true; + } + } + } + } + } + + return false; +} /** * This function returns whether the current user has the capability of performing a function @@ -3827,6 +3858,7 @@ function role_switch($roleid, $context) { || !empty($USER->switchrole[$context->id]) || !confirm_sesskey()) { unset($USER->switchrole[$context->id]); // Delete old capabilities + unset($USER->courseeditallowed); // drop cache for course edit button load_all_capabilities(); //reload user caps return true; } @@ -3846,6 +3878,7 @@ function role_switch($roleid, $context) { /// We have a valid roleid that this user can switch to, so let's set up the session $USER->switchrole[$context->id] = $roleid; // So we know later what state we are in + unset($USER->courseeditallowed); // drop cache for course edit button load_all_capabilities(); //reload switched role caps diff --git a/lib/moodlelib.php b/lib/moodlelib.php index 780a7c29fbe..bac5fed112a 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -2211,22 +2211,43 @@ function isguestuser($user=NULL) { } /** - * Determines if the currently logged in user is in editing mode + * Determines if the currently logged in user is in editing mode. + * Note: originally this function had $userid parameter - it was not usable anyway * * @uses $USER * @param int $courseid The id of the course being tested - * @param user $user A {@link $USER} object. If null then the currently logged in user is used. * @return bool */ -function isediting($courseid, $user=NULL) { +function isediting($courseid) { global $USER; - if (!$user) { - $user = $USER; - } - if (empty($user->editing)) { + + if (empty($USER->editing)) { return false; + + } else { + return editcourseallowed($courseid); } - return ($user->editing and has_capability('moodle/course:manageactivities', get_context_instance(CONTEXT_COURSE, $courseid))); +} + +/** + * Verifies if user allowed to edit something in the course page. + * @param int $courseid The id of the course being tested + * @return bool + */ +function editcourseallowed($courseid) { + global $USER; + + // cache the result per course, it is automatically reset when using switchrole or loginas + if (!array_key_exists('courseeditallowed', $USER)) { + $USER->courseeditallowed = array(); + } + + if (!array_key_exists($courseid, $USER->courseeditallowed)) { + $USER->courseeditallowed[$courseid] = has_capability_including_child_contexts(get_context_instance(CONTEXT_COURSE, $courseid), + array('moodle/site:manageblocks', 'moodle/course:manageactivities')); + } + + return $USER->courseeditallowed[$courseid]; } /** diff --git a/lib/pagelib.php b/lib/pagelib.php index b159eca00e9..3447a494b1e 100644 --- a/lib/pagelib.php +++ b/lib/pagelib.php @@ -350,7 +350,7 @@ class page_course extends page_base { if (has_capability('moodle/site:manageblocks', get_context_instance(CONTEXT_SYSTEM)) && defined('ADMIN_STICKYBLOCKS')) { return true; } - return has_capability('moodle/course:manageactivities', get_context_instance(CONTEXT_COURSE, $this->id)); + return editcourseallowed($this->id); } // Is the user actually editing this course page or "sticky page" right now? diff --git a/lib/weblib.php b/lib/weblib.php index 83dccd6fae5..47927cd1963 100644 --- a/lib/weblib.php +++ b/lib/weblib.php @@ -4049,32 +4049,9 @@ function print_editor_config($editorhidebuttons='', $return=false) { * @return string */ function update_course_icon($courseid) { - global $CFG, $USER; - $coursecontext = get_context_instance(CONTEXT_COURSE, $courseid); - - $capcheck = false; - - if (has_capability('moodle/course:manageactivities', $coursecontext) || - has_capability('moodle/site:manageblocks', $coursecontext)) { - $capcheck = true; - } else { - // loop through all child context, see if user has moodle/course:manageactivities or moodle/site:manageblocks - if ($children = get_child_contexts($coursecontext)) { - foreach ($children as $child) { - $childcontext = get_record('context', 'id', $child); - if (has_capability('moodle/course:manageactivities', $childcontext) || - has_capability('moodle/site:manageblocks', $childcontext)) { - $capcheck = true; - break; - } - } - } - } - - - if ($capcheck) { + if (editcourseallowed($courseid)) { if (!empty($USER->editing)) { $string = get_string('turneditingoff'); $edit = '0';