]> git.mjollnir.org Git - moodle.git/commitdiff
manage roles: MDL-8313 face lift allow override/assign, and add securtiy checks!
authortjhunt <tjhunt>
Tue, 18 Nov 2008 10:18:00 +0000 (10:18 +0000)
committertjhunt <tjhunt>
Tue, 18 Nov 2008 10:18:00 +0000 (10:18 +0000)
admin/roles/allowassign.php
admin/roles/allowoverride.php
admin/roles/manage.php

index 3e9cfe6e78748ebf6a8fbe2df06656ff03245e35..bb2e3b16971698393f7f5bdfd3e03c30a6d8034f 100755 (executable)
     require_once('../../config.php');
     require_once($CFG->libdir.'/adminlib.php');
 
-    admin_externalpage_setup('defineroles');
-
-
-    $sitecontext = get_context_instance(CONTEXT_SYSTEM);
-    require_capability('moodle/role:manage', $sitecontext);
-
-/// form processiong here
-
-/// get all roles
+    require_login();
+    $systemcontext = get_context_instance(CONTEXT_SYSTEM);
+    require_capability('moodle/role:manage', $systemcontext);
 
+/// Get all roles
     $roles = get_all_roles();
-
-    if ($grant = data_submitted()) {
-
-        foreach ($grant as $grole => $val) {
-            if ($grole == 'dummy') {
-                continue;
-            }
-
-            $string = explode('_', $grole);
-            $temp[$string[1]][$string[2]] = 1; // if set, means can access
-        }
-
-// if current assignment is in data_submitted, ignore, else, write deny into db
-        foreach ($roles as $srole) {
-            foreach ($roles as $trole) {
-                if (isset($temp[$srole->id][$trole->id])) { // if set, need to write to db
-                    if (!$record = $DB->get_record('role_allow_assign', array('roleid'=>$srole->id, 'allowassign'=>$trole->id))) {
-                        allow_assign($srole->id, $trole->id);
-                    }
-                } else { //if set, means can access, attempt to remove it from db
-                    $DB->delete_records('role_allow_assign', array('roleid'=>$srole->id, 'allowassign'=>$trole->id));
+    role_fix_names($roles, $systemcontext, ROLENAME_ORIGINAL);
+
+/// Process form submission
+    if (optional_param('submit', false, PARAM_BOOL) && data_submitted() && confirm_sesskey()) {
+    /// Delete all records, then add back the ones that should be allowed.
+        $DB->delete_records('role_allow_assign');
+        foreach ($roles as $fromroleid => $notused) {
+            foreach ($roles as $targetroleid => $alsonotused) {
+                if (optional_param('s_' . $fromroleid . '_' . $targetroleid, false, PARAM_BOOL)) {
+                    allow_assign($fromroleid, $targetroleid);
                 }
             }
         }
-        // updated allowassigns sitewide...
-        mark_context_dirty($sitecontext->path);
+
+    /// Updated allowassigns sitewide, so force a premissions refresh, and redirect.
+        mark_context_dirty($systemcontext->path);
+        add_to_log(SITEID, 'role', 'edit allow assign', 'admin/roles/allowassign.php', '', '', $USER->id);
+        redirect($CFG->wwwroot . '/' . $CFG->admin . '/roles/allowassign.php');
     }
-/// displaying form here
 
+/// Load the current settings
+    $allowed = array();
+    foreach ($roles as $role) {
+        // Make an array $role->id => false. This is probalby too clever for its own good.1
+        $allowed[$role->id] = array_combine(array_keys($roles), array_fill(0, count($roles), false));
+    }
+    $raas = $DB->get_recordset('role_allow_assign');
+    foreach ($raas as $raa) {
+        $allowed[$raa->roleid][$raa->allowassign] = true;
+    }
+
+/// Display the editing form.
+    admin_externalpage_setup('defineroles');
     admin_externalpage_print_header();
 
     $currenttab='allowassign';
     $table->cellpadding = 5;
     $table->cellspacing = 0;
     $table->width = '90%';
-    $table->align[] = 'right';
-
-/// get all the roles identifier
-    foreach ($roles as $role) {
-        $rolesname[] = format_string($role->name);
-        $roleids[] = $role->id;
-        $table->align[] = 'center';
-        $table->wrap[] = 'nowrap';
+    $table->align[] = 'left';
+    $table->rotateheaders = true;
+    $table->head = array('&#xa0;');
+
+/// Add role name headers.
+    foreach ($roles as $targetrole) {
+        $table->head[] = $targetrole->localname;
+        $table->align[] = 'left';
     }
 
-    $table->head = array_merge(array(''), $rolesname);
-
-    foreach ($roles as $role) {
-        $beta = get_box_list($role->id, $roleids);
-        $table->data[] = array_merge(array(format_string($role->name)), $beta);
+/// Now the rest of the table.
+    foreach ($roles as $fromrole) {
+        $row = array($fromrole->localname);
+        foreach ($roles as $targetrole) {
+            if ($allowed[$fromrole->id][$targetrole->id]) {
+                $checked = ' checked="checked"';
+            } else {
+                $checked = '';
+            }
+            $row[] = '<input type="checkbox" name="s_' . $fromrole->id . '_' . $targetrole->id . '" value="1"' . $checked . ' />';
+        }
+        $table->data[] = $row;
     }
 
     print_simple_box(get_string('configallowassign', 'admin'), 'center');
 
     echo '<form action="allowassign.php" method="post">';
+    echo '<input type="hidden" name="sesskey" value="' . sesskey() . '" />';
     print_table($table);
-    echo '<div class="buttons"><input type="submit" value="'.get_string('savechanges').'"/>';
-    echo '<input type="hidden" name="dummy" value="1" />'; // this is needed otherwise we do not know a form has been submitted
+    echo '<div class="buttons"><input type="submit" name="submit" value="'.get_string('savechanges').'"/>';
     echo '</div></form>';
 
     admin_externalpage_print_footer();
-
-
-
-function get_box_list($roleid, $arraylist) {
-    global $DB;
-
-    foreach ($arraylist as $targetid) {
-        if ($DB->get_record('role_allow_assign', array('roleid'=>$roleid, 'allowassign'=>$targetid))) {
-            $array[] = '<input type="checkbox" name="s_'.$roleid.'_'.$targetid.'" value="1" checked="checked"/>';
-        } else {
-            $array[] = '<input type="checkbox" name="s_'.$roleid.'_'.$targetid.'" value="1" />';
-        }
-    }
-    return $array;
-}
 ?>
index 04041da2da1e5f19967d88fad6ff2bb752daa5c2..77b9ea5ffd574a4d05a59bb762c9b3c53a743422 100755 (executable)
     require_once('../../config.php');
     require_once($CFG->libdir.'/adminlib.php');
 
-    admin_externalpage_setup('defineroles');
-
-
-    $sitecontext = get_context_instance(CONTEXT_SYSTEM);
-    require_capability('moodle/role:manage', $sitecontext);
-
-/// form processiong here
-
-/// get all roles
+    require_login();
+    $systemcontext = get_context_instance(CONTEXT_SYSTEM);
+    require_capability('moodle/role:manage', $systemcontext);
 
+/// Get all roles
     $roles = get_all_roles();
-
-    if ($grant = data_submitted()) {
-
-        foreach ($grant as $grole => $val) {
-            if ($grole == 'dummy') {
-                continue;
+    role_fix_names($roles, $systemcontext, ROLENAME_ORIGINAL);
+
+/// Process form submission
+    if (optional_param('submit', false, PARAM_BOOL) && data_submitted() && confirm_sesskey()) {
+    /// Delete all records, then add back the ones that should be allowed.
+        $DB->delete_records('role_allow_override');
+        foreach ($roles as $fromroleid => $notused) {
+            foreach ($roles as $targetroleid => $alsonotused) {
+                if (optional_param('s_' . $fromroleid . '_' . $targetroleid, false, PARAM_BOOL)) {
+                    allow_override($fromroleid, $targetroleid);
+                }
             }
-
-            $string = explode('_', $grole);
-            $temp[$string[1]][$string[2]] = 1; // if set, means can access
         }
 
-// if current assignment is in data_submitted, ignore, else, write deny into db
-        foreach ($roles as $srole) {
-            foreach ($roles as $trole) {
-                if (isset($temp[$srole->id][$trole->id])) { // if set, need to write to db
-                    if (!$record = $DB->get_record('role_allow_override', array('roleid'=>$srole->id, 'allowoverride'=>$trole->id))) {
-                        allow_override($srole->id, $trole->id);
-                    }
-                } else { //if set, means can access, attempt to remove it from db
-                    $DB->delete_records('role_allow_override', array('roleid'=>$srole->id, 'allowoverride'=>$trole->id));
-                }
-            }
+    /// Updated allowoverrides sitewide, so force a premissions refresh, and redirect.
+        mark_context_dirty($systemcontext->path);
+        add_to_log(SITEID, 'role', 'edit allow override', 'admin/roles/allowoverride.php', '', '', $USER->id);
+        redirect($CFG->wwwroot . '/' . $CFG->admin . '/roles/allowoverride.php');
+    }
+
+/// Load the current settings
+    $allowed = array();
+    foreach ($roles as $role) {
+        // Make an array $role->id => false. This is probalby too clever for its own good.1
+        $allowed[$role->id] = array_combine(array_keys($roles), array_fill(0, count($roles), false));
         }
-        // updated allowoverride sitewide...
-        mark_context_dirty($sitecontext->path);
+    $raas = $DB->get_recordset('role_allow_override');
+    foreach ($raas as $raa) {
+        $allowed[$raa->roleid][$raa->allowoverride] = true;
     }
-/// displaying form here
 
+/// Display the editing form.
+    admin_externalpage_setup('defineroles');
     admin_externalpage_print_header();
 
     $currenttab='allowoverride';
     $table->cellpadding = 5;
     $table->cellspacing = 0;
     $table->width = '90%';
-    $table->align[] = 'right';
-
-/// get all the roles identifier
-    foreach ($roles as $role) {
-        $rolesname[] = format_string($role->name);
-        $roleids[] = $role->id;
-        $table->align[] = 'center';
-        $table->wrap[] = 'nowrap';
+    $table->align[] = 'left';
+    $table->rotateheaders = true;
+    $table->head = array('&#xa0;');
+
+/// Add role name headers.
+    foreach ($roles as $targetrole) {
+        $table->head[] = $targetrole->localname;
+        $table->align[] = 'left';
     }
 
-    $table->head = array_merge(array(''), $rolesname);
-
-    foreach ($roles as $role) {
-        $beta = get_box_list($role->id, $roleids);
-        $table->data[] = array_merge(array(format_string($role->name)), $beta);
+/// Now the rest of the table.
+    foreach ($roles as $fromrole) {
+        $row = array($fromrole->localname);
+        foreach ($roles as $targetrole) {
+            if ($allowed[$fromrole->id][$targetrole->id]) {
+                $checked = ' checked="checked"';
+            } else {
+                $checked = '';
+            }
+            $row[] = '<input type="checkbox" name="s_' . $fromrole->id . '_' . $targetrole->id . '" value="1"' . $checked . ' />';
+        }
+        $table->data[] = $row;
     }
 
     print_simple_box(get_string('configallowoverride2', 'admin'), 'center');
 
     echo '<form action="allowoverride.php" method="post">';
+    echo '<input type="hidden" name="sesskey" value="' . sesskey() . '" />';
     print_table($table);
-    echo '<div class="buttons"><input type="submit" value="'.get_string('savechanges').'"/>';
-    echo '<input type="hidden" name="dummy" value="1" />'; // this is needed otherwise we do not know a form has been submitted
+    echo '<div class="buttons"><input type="submit" name="submit" value="'.get_string('savechanges').'"/>';
     echo '</div></form>';
 
     admin_externalpage_print_footer();
-
-// returns array
-function get_box_list($roleid, $arraylist) {
-    global $DB;
-
-    foreach ($arraylist as $targetid) {
-        if ($DB->get_record('role_allow_override', array('roleid'=>$roleid, 'allowoverride'=>$targetid))) {
-            $array[] = '<input type="checkbox" name="s_'.$roleid.'_'.$targetid.'" value="1" checked="checked"/>';
-        } else {
-            $array[] = '<input type="checkbox" name="s_'.$roleid.'_'.$targetid.'" value="1" />';
-        }
-    }
-    return $array;
-}
 ?>
index ab46cbf821d81f4de2bd3275fc33a813c77e86e5..e95faa5c165533bec65da04f2e69b9bfc2ac2c54 100755 (executable)
@@ -61,7 +61,6 @@
 /// Get some basic data we are going to need.
     $roles = get_all_roles();
     role_fix_names($roles, $systemcontext, ROLENAME_ORIGINAL);
-    $rolescount = count($roles);
 
     $undeletableroles = array();
     $undeletableroles[$CFG->notloggedinroleid] = 1;