]> git.mjollnir.org Git - moodle.git/commitdiff
Improved has_capability for admins using switchrole in modules
authormoodler <moodler>
Wed, 11 Oct 2006 05:49:26 +0000 (05:49 +0000)
committermoodler <moodler>
Wed, 11 Oct 2006 05:49:26 +0000 (05:49 +0000)
lib/accesslib.php

index 23260ca4eccb86dd20af3126cf72c177efc05959..f9fe01d336724f781bbe3a818d8f48574bb835d2 100755 (executable)
@@ -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']);