Skip to content

Commit

Permalink
MDL-10149, preventing the deletion of the last admin role
Browse files Browse the repository at this point in the history
  • Loading branch information
toyomoyo committed Jul 19, 2007
1 parent 29b3093 commit b8f8058
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions lib/accesslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1962,6 +1962,33 @@ function create_role($name, $shortname, $description, $legacy='') {
function delete_role($roleid) {
$success = true;

// mdl 10149, check if this is the last active admin role
// if we make the admin role not deletable then this part can go

$systemcontext = get_context_instance(CONTEXT_SYSTEM);

if ($role = get_record('role', 'id', $roleid)) {
if (record_exists('role_capabilities', 'contextid', $systemcontext->id, 'roleid', $roleid, 'capability', 'moodle/site:doanything')) {
// deleting an admin role
$status = false;
if ($adminroles = get_roles_with_capability('moodle/site:doanything', CAP_ALLOW, $systemcontext)) {
foreach ($adminroles as $adminrole) {
if ($adminrole->id != $roleid) {
// some other admin role
if (record_exists('role_assignments', 'roleid', $adminrole->id, 'contextid', $systemcontext->id)) {
// found another admin role with at least 1 user assigned
$status = true;
break;
}
}
}
}
if ($status !== true) {
error ('You can not delete this role because there is no other admin roles with users assigned');
}
}
}

// first unssign all users
if (!role_unassign($roleid)) {
debugging("Error while unassigning all users from role with ID $roleid!");
Expand Down

0 comments on commit b8f8058

Please sign in to comment.