MDL-37302 allow removal of enrol roles if plugin allows it

This commit is contained in:
Petr Škoda 2012-12-21 13:53:26 +01:00
parent 0dc5a532ec
commit 483e6f5cde

View File

@ -586,16 +586,28 @@ class course_enrolment_manager {
*/
public function unassign_role_from_user($userid, $roleid) {
global $DB;
require_capability('moodle/role:assign', $this->context);
$user = $DB->get_record('user', array('id'=>$userid), '*', MUST_EXIST);
try {
role_unassign($roleid, $user->id, $this->context->id, '', NULL);
} catch (Exception $e) {
if (!array_key_exists($roleid, $this->get_assignable_roles())) {
if (defined('AJAX_SCRIPT')) {
throw $e;
throw new moodle_exception('invalidrole');
}
return false;
}
$user = $DB->get_record('user', array('id'=>$userid), '*', MUST_EXIST);
$ras = $DB->get_records('role_assignments', array('contextid'=>$this->context->id, 'userid'=>$user->id, 'roleid'=>$roleid));
foreach ($ras as $ra) {
if ($ra->component) {
if (strpos($ra->component, 'enrol_') !== 0) {
continue;
}
if (!$plugin = enrol_get_plugin(substr($ra->component, 6))) {
continue;
}
if ($plugin->roles_protected()) {
continue;
}
}
role_unassign($ra->roleid, $ra->userid, $ra->contextid, $ra->component, $ra->itemid);
}
return true;
}
@ -706,6 +718,7 @@ class course_enrolment_manager {
public function get_user_roles($userid) {
$roles = array();
$ras = get_user_roles($this->context, $userid, true, 'c.contextlevel DESC, r.sortorder ASC');
$plugins = $this->get_enrolment_plugins(false);
foreach ($ras as $ra) {
if ($ra->contextid != $this->context->id) {
if (!array_key_exists($ra->roleid, $roles)) {
@ -717,7 +730,18 @@ class course_enrolment_manager {
if (array_key_exists($ra->roleid, $roles) && $roles[$ra->roleid] === false) {
continue;
}
$roles[$ra->roleid] = ($ra->itemid == 0 and $ra->component === '');
$changeable = true;
if ($ra->component) {
$changeable = false;
if (strpos($ra->component, 'enrol_') === 0) {
$plugin = substr($ra->component, 6);
if (isset($plugins[$plugin])) {
$changeable = !$plugins[$plugin]->roles_protected();
}
}
}
$roles[$ra->roleid] = $changeable;
}
return $roles;
}
@ -807,6 +831,7 @@ class course_enrolment_manager {
$userroles = $this->get_other_users($sort, $direction, $page, $perpage);
$roles = $this->get_all_roles();
$plugins = $this->get_enrolment_plugins(false);
$context = $this->get_context();
$now = time();
@ -821,8 +846,17 @@ class course_enrolment_manager {
}
$a = new stdClass;
$a->role = $roles[$userrole->roleid]->localname;
$changeable = ($userrole->component == '');
if ($contextid == $this->context->id) {
$changeable = true;
if ($userrole->component) {
$changeable = false;
if (strpos($userrole->component, 'enrol_') === 0) {
$plugin = substr($userrole->component, 6);
if (isset($plugins[$plugin])) {
$changeable = !$plugin[$plugin]->roles_protected();
}
}
}
$roletext = get_string('rolefromthiscourse', 'enrol', $a);
} else {
$changeable = false;