Skip to content

Commit

Permalink
MDL-56285 competency: fix move competencies capabilities
Browse files Browse the repository at this point in the history
In learning plan templates, cannot move competencies
around even if capability templatemanage is given
to the role because of bad context check
  • Loading branch information
taboubi committed Oct 6, 2016
1 parent f73f938 commit fd8043b
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 3 deletions.
9 changes: 6 additions & 3 deletions competency/classes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -2196,10 +2196,13 @@ public static function remove_competency_from_template($templateid, $competencyi
*/
public static function reorder_template_competency($templateid, $competencyidfrom, $competencyidto) {
static::require_enabled();
// First we do a permissions check.
$context = context_system::instance();
$template = new template($templateid);

require_capability('moodle/competency:templatemanage', $context);
// First we do a permissions check.
if (!$template->can_manage()) {
throw new required_capability_exception($template->get_context(), 'moodle/competency:templatemanage',
'nopermissions', '');
}

$down = true;
$matches = template_competency::get_records(array('templateid' => $templateid, 'competencyid' => $competencyidfrom));
Expand Down
65 changes: 65 additions & 0 deletions competency/tests/api_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -1883,6 +1883,71 @@ public function test_create_template_cohort_permissions() {
$this->assertInstanceOf('core_competency\\template_cohort', $result);
}

public function test_reorder_template_competencies_permissions() {
$this->resetAfterTest(true);

$dg = $this->getDataGenerator();
$lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
$cat = $dg->create_category();
$catcontext = context_coursecat::instance($cat->id);
$syscontext = context_system::instance();

$user = $dg->create_user();
$role = $dg->create_role();
assign_capability('moodle/competency:templatemanage', CAP_ALLOW, $role, $syscontext->id, true);
$dg->role_assign($role, $user->id, $syscontext->id);

// Create a template.
$template = $lpg->create_template(array('contextid' => $catcontext->id));

// Create a competency framework.
$framework = $lpg->create_framework(array('contextid' => $catcontext->id));

// Create competencies.
$competency1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
$competency2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));

// Add the competencies.
$lpg->create_template_competency(array(
'templateid' => $template->get_id(),
'competencyid' => $competency1->get_id()
));
$lpg->create_template_competency(array(
'templateid' => $template->get_id(),
'competencyid' => $competency2->get_id()
));
$this->setUser($user);
// Can reorder competencies with system context permissions in category context.
$result = api::reorder_template_competency($template->get_id(), $competency2->get_id(), $competency1->get_id());
$this->assertTrue($result);
unassign_capability('moodle/competency:templatemanage', $role, $syscontext->id);
accesslib_clear_all_caches_for_unit_testing();

try {
api::reorder_template_competency($template->get_id(), $competency2->get_id(), $competency1->get_id());
$this->fail('Exception expected due to not permissions to manage template competencies');
} catch (required_capability_exception $e) {
$this->assertEquals('nopermissions', $e->errorcode);
}

// Giving permissions in category context.
assign_capability('moodle/competency:templatemanage', CAP_ALLOW, $role, $catcontext->id, true);
$dg->role_assign($role, $user->id, $catcontext->id);
// User with templatemanage capability in category context can reorder competencies in temple.
$result = api::reorder_template_competency($template->get_id(), $competency1->get_id(), $competency2->get_id());
$this->assertTrue($result);
// Removing templatemanage capability in category context.
unassign_capability('moodle/competency:templatemanage', $role, $catcontext->id);
accesslib_clear_all_caches_for_unit_testing();

try {
api::reorder_template_competency($template->get_id(), $competency2->get_id(), $competency1->get_id());
$this->fail('Exception expected due to not permissions to manage template competencies');
} catch (required_capability_exception $e) {
$this->assertEquals('nopermissions', $e->errorcode);
}
}

public function test_delete_template() {
$this->resetAfterTest(true);
$this->setAdminUser();
Expand Down

0 comments on commit fd8043b

Please sign in to comment.