]> git.mjollnir.org Git - moodle.git/commitdiff
accesslib: has_capability() now loads sub-course accessdata for $ACCESS
authormartinlanghoff <martinlanghoff>
Wed, 19 Sep 2007 07:26:02 +0000 (07:26 +0000)
committermartinlanghoff <martinlanghoff>
Wed, 19 Sep 2007 07:26:02 +0000 (07:26 +0000)
When querying capabilities of non-logged-in users, has_capability()
will now load accessdata for the subcontexts as needed.

Without this patch, below-the-course RAs and rdefs were ignored when
checking caps for a user different from $USER. I don't think it is
ever done in current moodle code, so the problem wasn't visible.

In any case - it's fixed ;-)

lib/accesslib.php

index f38e580027200d7a01ccadb0c84135c5dcb63d88..789c51b7eb8567eaf5087bfe8133c99a7f347b8e 100755 (executable)
@@ -363,7 +363,7 @@ function has_capability($capability, $context=NULL, $userid=NULL, $doanything=tr
             return has_cap_fad($capability, $context,
                                $USER->access, $doanything);
         }
-        // Load it as needed
+        // Load accessdata for below-the-course contexts
         if (!path_inaccessdata($context->path,$USER->access)) {
             error_log("loading access for context {$context->path} for $capability at {$context->contextlevel} {$context->id}");
             // $bt = debug_backtrace();
@@ -382,6 +382,19 @@ function has_capability($capability, $context=NULL, $userid=NULL, $doanything=tr
     if (!isset($ACCESS[$userid])) {
         load_user_accessdata($userid);
     }
+    if ($context->contextlevel <= CONTEXT_COURSE) {
+        // Course and above are always preloaded
+        return has_cap_fad($capability, $context,
+                           $ACCESS[$userid], $doanything);
+    }
+    // Load accessdata for below-the-course contexts as needed
+    if (!path_inaccessdata($context->path,$ACCESS[$userid])) {
+        error_log("loading access for context {$context->path} for $capability at {$context->contextlevel} {$context->id}");
+        // $bt = debug_backtrace();
+        // error_log("bt {$bt[0]['file']} {$bt[0]['line']}");
+        $ACCESS[$userid] = get_user_access_bycontext($userid, $context,
+                                                         $ACCESS[$userid]);
+    }
     return has_cap_fad($capability, $context,
                        $ACCESS[$userid], $doanything);
 }