accesslib: has_capability() now loads sub-course accessdata for $ACCESS

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 ;-)
This commit is contained in:
martinlanghoff 2007-09-19 07:26:02 +00:00
parent 21e2dcd946
commit 420bfab156

View file

@ -363,7 +363,7 @@ function has_capability($capability, $context=NULL, $userid=NULL, $doanything=tr
return has_cap_fad($capability, $context, return has_cap_fad($capability, $context,
$USER->access, $doanything); $USER->access, $doanything);
} }
// Load it as needed // Load accessdata for below-the-course contexts
if (!path_inaccessdata($context->path,$USER->access)) { if (!path_inaccessdata($context->path,$USER->access)) {
error_log("loading access for context {$context->path} for $capability at {$context->contextlevel} {$context->id}"); error_log("loading access for context {$context->path} for $capability at {$context->contextlevel} {$context->id}");
// $bt = debug_backtrace(); // $bt = debug_backtrace();
@ -382,6 +382,19 @@ function has_capability($capability, $context=NULL, $userid=NULL, $doanything=tr
if (!isset($ACCESS[$userid])) { if (!isset($ACCESS[$userid])) {
load_user_accessdata($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, return has_cap_fad($capability, $context,
$ACCESS[$userid], $doanything); $ACCESS[$userid], $doanything);
} }