From d5a8d310e032a4a928d275ce37627cb714e5680e Mon Sep 17 00:00:00 2001 From: Marina Glancy Date: Mon, 25 Jun 2018 14:54:59 +0800 Subject: [PATCH] MDL-62790 course: cap check in core_course_get_categories --- course/externallib.php | 8 ++------ course/tests/externallib_test.php | 7 ++++--- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/course/externallib.php b/course/externallib.php index b3d1ee17ee5..4ff2083e87f 100644 --- a/course/externallib.php +++ b/course/externallib.php @@ -1592,9 +1592,7 @@ class core_course_external extends external_api { break; case 'visible': - if (has_capability('moodle/category:manage', $context) - or has_capability('moodle/category:viewhiddencategories', - context_system::instance())) { + if (has_capability('moodle/category:viewhiddencategories', $context)) { $value = clean_param($crit['value'], PARAM_INT); $conditions[$key] = $value; $wheres[] = $key . " = :" . $key; @@ -1704,9 +1702,7 @@ class core_course_external extends external_api { if (!isset($excludedcats[$category->id])) { // Final check to see if the category is visible to the user. - if ($category->visible - or has_capability('moodle/category:viewhiddencategories', context_system::instance()) - or has_capability('moodle/category:manage', $context)) { + if ($category->visible or has_capability('moodle/category:viewhiddencategories', $context)) { $categoryinfo = array(); $categoryinfo['id'] = $category->id; diff --git a/course/tests/externallib_test.php b/course/tests/externallib_test.php index 32cd73154a6..462ddb3cd35 100644 --- a/course/tests/externallib_test.php +++ b/course/tests/externallib_test.php @@ -201,6 +201,7 @@ class core_course_externallib_testcase extends externallib_advanced_testcase { // Set the required capabilities by the external function. $context = context_system::instance(); $roleid = $this->assignUserCapability('moodle/category:manage', $context->id); + $this->assignUserCapability('moodle/category:viewhiddencategories', $context->id, $roleid); // Retrieve category1 + sub-categories except not visible ones $categories = core_course_external::get_categories(array( @@ -278,10 +279,10 @@ class core_course_externallib_testcase extends externallib_advanced_testcase { $this->assertEquals($DB->count_records('course_categories'), count($categories)); - $this->unassignUserCapability('moodle/category:manage', $context->id, $roleid); + $this->unassignUserCapability('moodle/category:viewhiddencategories', $context->id, $roleid); - // Ensure maxdepthcategory is 2 and retrieve all categories without category:manage capability. It should retrieve all - // visible categories as well. + // Ensure maxdepthcategory is 2 and retrieve all categories without category:viewhiddencategories capability. + // It should retrieve all visible categories as well. set_config('maxcategorydepth', 2); $categories = core_course_external::get_categories();