Please please please be defensive about values returned from the database. Always.
In this case moodle.org had some bad data that caused this code to crash. MDLSITE-55
This commit is contained in:
+28
-10
@@ -818,6 +818,14 @@ function check_enrolment_plugins(&$user) {
|
||||
function capability_prohibits($capability, $context, $sum='', $array='') {
|
||||
global $USER;
|
||||
|
||||
if (empty($context->id)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (empty($capability)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($sum < (CAP_PROHIBIT/2)) {
|
||||
// If this capability is set to prohibit.
|
||||
return true;
|
||||
@@ -843,24 +851,26 @@ function capability_prohibits($capability, $context, $sum='', $array='') {
|
||||
break;
|
||||
|
||||
case CONTEXT_PERSONAL:
|
||||
$parent = get_context_instance(CONTEXT_SYSTEM, SITEID);
|
||||
$parent = get_context_instance(CONTEXT_SYSTEM);
|
||||
return capability_prohibits($capability, $parent);
|
||||
break;
|
||||
|
||||
case CONTEXT_USER:
|
||||
$parent = get_context_instance(CONTEXT_SYSTEM, SITEID);
|
||||
$parent = get_context_instance(CONTEXT_SYSTEM);
|
||||
return capability_prohibits($capability, $parent);
|
||||
break;
|
||||
|
||||
case CONTEXT_COURSECAT:
|
||||
// Coursecat -> coursecat or site.
|
||||
$coursecat = get_record('course_categories','id',$context->instanceid);
|
||||
if (!$coursecat = get_record('course_categories','id',$context->instanceid)) {
|
||||
return false;
|
||||
}
|
||||
if (!empty($coursecat->parent)) {
|
||||
// return parent value if exist.
|
||||
$parent = get_context_instance(CONTEXT_COURSECAT, $coursecat->parent);
|
||||
} else {
|
||||
// Return site value.
|
||||
$parent = get_context_instance(CONTEXT_SYSTEM, SITEID);
|
||||
$parent = get_context_instance(CONTEXT_SYSTEM);
|
||||
}
|
||||
return capability_prohibits($capability, $parent);
|
||||
break;
|
||||
@@ -868,35 +878,43 @@ function capability_prohibits($capability, $context, $sum='', $array='') {
|
||||
case CONTEXT_COURSE:
|
||||
// 1 to 1 to course cat.
|
||||
// Find the course cat, and return its value.
|
||||
$course = get_record('course','id',$context->instanceid);
|
||||
if (!$course = get_record('course','id',$context->instanceid)) {
|
||||
return false;
|
||||
}
|
||||
$parent = get_context_instance(CONTEXT_COURSECAT, $course->category);
|
||||
return capability_prohibits($capability, $parent);
|
||||
break;
|
||||
|
||||
case CONTEXT_GROUP:
|
||||
// 1 to 1 to course.
|
||||
$group = get_record('groups','id',$context->instanceid);
|
||||
if (!$group = get_record('groups','id',$context->instanceid)) {
|
||||
return false;
|
||||
}
|
||||
$parent = get_context_instance(CONTEXT_COURSE, $group->courseid);
|
||||
return capability_prohibits($capability, $parent);
|
||||
break;
|
||||
|
||||
case CONTEXT_MODULE:
|
||||
// 1 to 1 to course.
|
||||
$cm = get_record('course_modules','id',$context->instanceid);
|
||||
if (!$cm = get_record('course_modules','id',$context->instanceid)) {
|
||||
return false;
|
||||
}
|
||||
$parent = get_context_instance(CONTEXT_COURSE, $cm->course);
|
||||
return capability_prohibits($capability, $parent);
|
||||
break;
|
||||
|
||||
case CONTEXT_BLOCK:
|
||||
// 1 to 1 to course.
|
||||
$block = get_record('block_instance','id',$context->instanceid);
|
||||
if (!$block = get_record('block_instance','id',$context->instanceid)) {
|
||||
return false;
|
||||
}
|
||||
$parent = get_context_instance(CONTEXT_COURSE, $block->pageid); // needs check
|
||||
return capability_prohibits($capability, $parent);
|
||||
break;
|
||||
|
||||
default:
|
||||
error ('This is an unknown context!');
|
||||
return false;
|
||||
print_error('unknowncontext');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user