MDL-39153 webservices: Provide alternative to passing contextids

As per our policy webservices should never have a required contextid, thuse providing a support for passing contextlevel and instanceid as parameters to the api
This commit is contained in:
Ankit Agarwal 2013-06-18 14:00:08 +08:00
parent c0fb582c58
commit 1e63179285
2 changed files with 20 additions and 4 deletions

View file

@ -655,7 +655,10 @@ class core_role_external extends external_api {
array(
'roleid' => new external_value(PARAM_INT, 'Role to assign to the user'),
'userid' => new external_value(PARAM_INT, 'The user that is going to be assigned'),
'contextid' => new external_value(PARAM_INT, 'The context to unassign the user role from'),
'contextid' => new external_value(PARAM_INT, 'The context to unassign the user role from', VALUE_OPTIONAL),
'contextlevel' => new external_value(PARAM_ALPHA, 'The context level to unassign the user role in
+ (block, course, coursecat, system, user, module)', VALUE_OPTIONAL),
'instanceid' => new external_value(PARAM_INT, 'The Instance id of item where the role needs to be unassigned', VALUE_OPTIONAL),
)
)
)
@ -679,7 +682,7 @@ class core_role_external extends external_api {
foreach ($params['unassignments'] as $unassignment) {
// Ensure the current user is allowed to run this function in the unassignment context
$context = context::instance_by_id($unassignment['contextid'], IGNORE_MISSING);
$context = self::get_context_from_params($unassignment);
self::validate_context($context);
require_capability('moodle/role:assign', $context);
@ -689,7 +692,7 @@ class core_role_external extends external_api {
throw new invalid_parameter_exception('Can not unassign roleid='.$unassignment['roleid'].' in contextid='.$unassignment['contextid']);
}
role_unassign($unassignment['roleid'], $unassignment['userid'], $unassignment['contextid']);
role_unassign($unassignment['roleid'], $unassignment['userid'], $context->id);
}
$transaction->allow_commit();

View file

@ -115,7 +115,7 @@ class core_role_external_testcase extends externallib_advanced_testcase {
$users = get_role_users(3, $context);
$this->assertEquals(count($users), 1);
// Call the external function. Assign teacher role to $USER.
// Call the external function. Unassign teacher role using contextid.
core_role_external::unassign_roles(array(
array('roleid' => 3, 'userid' => $USER->id, 'contextid' => $context->id)));
@ -123,6 +123,19 @@ class core_role_external_testcase extends externallib_advanced_testcase {
$users = get_role_users(3, $context);
$this->assertEquals(count($users), 0);
// Add teacher role to $USER on course context.
role_assign(3, $USER->id, $context->id);
$users = get_role_users(3, $context);
$this->assertEquals(count($users), 1);
// Call the external function. Unassign teacher role using context level and instanceid.
core_role_external::unassign_roles(array(
array('roleid' => 3, 'userid' => $USER->id, 'contextlevel' => "course", 'instanceid' => $course->id)));
// Check the role has been unassigned on course context.
$users = get_role_users(3, $context);
$this->assertEquals(count($users), 0);
// Call without required capability.
$this->unassignUserCapability('moodle/role:assign', $context->id, $roleid);
$this->setExpectedException('moodle_exception');