MDL-66730 core_course: Improve permission check for category moving
Co-authored-by: Erica Bithell <[email protected]>
This commit is contained in:
committed by
Jenkins
co-authored by
Erica Bithell
parent
c22b94aea7
commit
1c2bdf454a
@@ -2131,6 +2131,19 @@ class core_course_external extends external_api {
|
||||
self::validate_context($categorycontext);
|
||||
require_capability('moodle/category:manage', $categorycontext);
|
||||
|
||||
// If the category parent is being changed, check for capability in the new parent category
|
||||
if (isset($cat['parent']) && ($cat['parent'] !== $category->parent)) {
|
||||
if ($cat['parent'] == 0) {
|
||||
// Creating a top level category requires capability in the system context
|
||||
$parentcontext = context_system::instance();
|
||||
} else {
|
||||
// Category context
|
||||
$parentcontext = context_coursecat::instance($cat['parent']);
|
||||
}
|
||||
self::validate_context($parentcontext);
|
||||
require_capability('moodle/category:manage', $parentcontext);
|
||||
}
|
||||
|
||||
// this will throw an exception if descriptionformat is not valid
|
||||
external_validate_format($cat['descriptionformat']);
|
||||
|
||||
|
||||
@@ -362,6 +362,44 @@ class core_course_externallib_testcase extends externallib_advanced_testcase {
|
||||
core_course_external::update_categories($categories);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test update_categories method for moving categories
|
||||
*/
|
||||
public function test_update_categories_moving() {
|
||||
$this->resetAfterTest();
|
||||
|
||||
// Create data.
|
||||
$categorya = self::getDataGenerator()->create_category([
|
||||
'name' => 'CAT_A',
|
||||
]);
|
||||
$categoryasub = self::getDataGenerator()->create_category([
|
||||
'name' => 'SUBCAT_A',
|
||||
'parent' => $categorya->id
|
||||
]);
|
||||
$categoryb = self::getDataGenerator()->create_category([
|
||||
'name' => 'CAT_B',
|
||||
]);
|
||||
|
||||
// Create a new test user.
|
||||
$testuser = self::getDataGenerator()->create_user();
|
||||
$this->setUser($testuser);
|
||||
|
||||
// Set the capability for CAT_A only.
|
||||
$contextcata = context_coursecat::instance($categorya->id);
|
||||
$roleid = $this->assignUserCapability('moodle/category:manage', $contextcata->id);
|
||||
|
||||
// Then we move SUBCAT_A parent: CAT_A => CAT_B.
|
||||
$categories = [
|
||||
[
|
||||
'id' => $categoryasub->id,
|
||||
'parent' => $categoryb->id
|
||||
]
|
||||
];
|
||||
|
||||
$this->expectException('required_capability_exception');
|
||||
core_course_external::update_categories($categories);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test create_courses numsections
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user