towards unique role sortorder - fixes for create_role() and new delete_role()

This commit is contained in:
skodak
2006-09-23 10:43:24 +00:00
parent 09cf1c9b55
commit 8420bee94e
2 changed files with 41 additions and 14 deletions
+3 -9
View File
@@ -63,7 +63,7 @@
}
if (empty($errors)) {
$newrole = create_role($name, $shortname, $description, '', $rolescount);
$newrole = create_role($name, $shortname, $description);
} else {
$newrole = new object();
$newrole->name = $name;
@@ -181,14 +181,8 @@
case 'delete':
if ($confirm and data_submitted() and confirm_sesskey()) {
// first unssign all users
if (!role_unassign($roleid)) {
error("Error while unassigning all users from role with ID $roleid!");
}
if (!delete_records('role', 'id', $roleid)) {
error("Could not delete role with ID $roleid!");
if (!delete_role($roleid)) {
error('Could not delete role with ID '.$roleid);
}
} else if (confirm_sesskey()){
+38 -5
View File
@@ -1201,7 +1201,7 @@ function get_local_override($roleid, $contextid, $capability) {
* @param legacy - optional legacy capability
* @return id or false
*/
function create_role($name, $shortname, $description, $legacy='', $sortorder = -1) {
function create_role($name, $shortname, $description, $legacy='') {
// check for duplicate role name
@@ -1218,10 +1218,10 @@ function create_role($name, $shortname, $description, $legacy='', $sortorder = -
$role->shortname = $shortname;
$role->description = $description;
if ($sortorder = -1) {
$role->sortorder = count_records('role');
} else {
$role->sortorder = $sortorder;
//find free sortorder number
$role->sortorder = count_records('role');
while (get_record('role','sortorder', $role->sortorder)) {
$role->sortorder += 1;
}
$context = get_context_instance(CONTEXT_SYSTEM, SITEID);
@@ -1251,6 +1251,39 @@ function create_role($name, $shortname, $description, $legacy='', $sortorder = -
}
/**
* function that deletes a role and cleanups up after it
* @param roleid - id of role to delete
* @return success
*/
function delete_role($roleid) {
$success = true;
// first unssign all users
if (!role_unassign($roleid)) {
debugging("Error while unassigning all users from role with ID $roleid!");
$success = false;
}
// cleanup all references to this role, ignore errors
if ($success) {
delete_records('role_capabilities', 'roleid', $roleid);
delete_records('role_allow_assign', 'roleid', $roleid);
delete_records('role_allow_assign', 'allowassign', $roleid);
delete_records('role_allow_override', 'roleid', $roleid);
delete_records('role_allow_override', 'allowoverride', $roleid);
delete_records('role_names', 'roleid', $roleid);
}
// finally delete the role itself
if ($success and !delete_records('role', 'id', $roleid)) {
debugging("Could not delete role with ID $roleid!");
$success = false;
}
return $success;
}
/**
* Function to write context specific overrides, or default capabilities.
* @param module - string name