From: moodler Date: Wed, 11 Oct 2006 05:49:26 +0000 (+0000) Subject: Improved has_capability for admins using switchrole in modules X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=f4e2d38aaf8ea81ae6af4850891dfa4ad4d228a5;p=moodle.git Improved has_capability for admins using switchrole in modules --- diff --git a/lib/accesslib.php b/lib/accesslib.php index 23260ca4ec..f9fe01d336 100755 --- a/lib/accesslib.php +++ b/lib/accesslib.php @@ -312,8 +312,28 @@ function has_capability($capability, $context=NULL, $userid=NULL, $doanything=tr if ($doanything) { - // Check site - if (empty($USER->switchrole[$context->id])) { // Ignore site setting if switchrole is active + /// First make sure that we aren't in a "switched role" + + $switchroleactive = false; // Assume it isn't active in this context + + if (!empty($USER->switchrole)) { // Switchrole is active somewhere! + if (!empty($USER->switchrole[$context->id])) { // Because of current context + $switchroleactive = true; + } else { // Check parent contexts + if ($parentcontextids = get_parent_contexts($context)) { + foreach ($parentcontextids as $parentcontextid) { + if (!empty($USER->switchrole[$parentcontextid])) { // Yep, switchroles active here + $switchroleactive = true; + break; + } + } + } + } + } + + /// Check the site context for doanything (most common) first + + if (empty($switchroleactive)) { // Ignore site setting if switchrole is active $sitecontext = get_context_instance(CONTEXT_SYSTEM, SITEID); if (isset($capabilities[$sitecontext->id]['moodle/site:doanything'])) { return (0 < $capabilities[$sitecontext->id]['moodle/site:doanything']);