]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-8841 Switch role cleanup; merged from MOODLE_18_STABLE
authorskodak <skodak>
Fri, 16 Mar 2007 20:21:27 +0000 (20:21 +0000)
committerskodak <skodak>
Fri, 16 Mar 2007 20:21:27 +0000 (20:21 +0000)
lib/accesslib.php
lib/weblib.php

index ab9871451694d975e144f5c3ae0034d00a1c493a..d46d91e8316e4a917a9b66493cd55ab27b70d0b5 100755 (executable)
@@ -1120,13 +1120,26 @@ function load_all_capabilities() {
 
         load_user_capability();
 
-        if (!empty($USER->capabilities)) {
-            $USER->capabilities = merge_role_caps($USER->capabilities, $defcaps);
+        if (!empty($USER->switchrole)) {
 
-        } else {
-            $USER->capabilities = $defcaps;
+            foreach ($USER->switchrole as $contextid => $roleid) {
+                $context = get_context_instance_by_id($contextid);
+
+                // first prune context and any child contexts
+                $children = get_child_contexts($context);
+                foreach ($children as $childid) {
+                    unset($USER->capabilities[$childid]);
+                }
+                unset($USER->capabilities[$contextid]);
+
+                // now merge all switched role caps in context and bellow
+                $swithccaps = get_role_context_caps($roleid, $context);
+                $USER->capabilities = merge_role_caps($USER->capabilities, $swithccaps);
+            }
         }
 
+        $USER->capabilities = merge_role_caps($USER->capabilities, $defcaps);
+
     } else {
         load_notloggedin_role();
     }
@@ -3694,16 +3707,14 @@ function get_roles_on_exact_context($context) {
  * @return bool
  */
 function role_switch($roleid, $context) {
-    global $USER;
-
-    global $db;
+    global $USER, $CFG;
 
 /// If we can't use this or are already using it or no role was specified then bail completely and reset
     if (empty($roleid) || !has_capability('moodle/role:switchroles', $context)
         || !empty($USER->switchrole[$context->id])  || !confirm_sesskey()) {
-        load_user_capability('', $context);   // Reset all permissions for this context to normal
+
         unset($USER->switchrole[$context->id]);  // Delete old capabilities
-        has_capability('clearcache');  
+        load_all_capabilities();   //reload user caps
         return true;
     }
 
@@ -3712,11 +3723,10 @@ function role_switch($roleid, $context) {
         return false;
     }
 
-    if (empty($roles[$roleid])) {   /// We can't switch to this particular role
-        return false;
-    }
+/// unset default user role - it would not work anyway
+    unset($roles[$CFG->defaultuserroleid]);
 
-    if (!$sitecontext = get_context_instance(CONTEXT_SYSTEM)) {
+    if (empty($roles[$roleid])) {   /// We can't switch to this particular role
         return false;
     }
 
@@ -3724,22 +3734,12 @@ function role_switch($roleid, $context) {
 
     $USER->switchrole[$context->id] = $roleid;     // So we know later what state we are in
 
-    unset($USER->capabilities[$context->id]);  // Delete old capabilities
-
-    if ($capabilities = get_records_select('role_capabilities', "roleid = $roleid AND contextid = $sitecontext->id")) {
-        foreach ($capabilities as $capability) {
-            $USER->capabilities[$context->id][$capability->capability] = $capability->permission;
-        }
-    }
+    load_all_capabilities();   //reload switched role caps
 
 /// Add some permissions we are really going to always need, even if the role doesn't have them!
 
     $USER->capabilities[$context->id]['moodle/course:view'] = CAP_ALLOW;
 
-/// Clear the entire capability cache to avoid mixups
-
-    has_capability('clearcache');  
-
     return true;
 
 }
index fa6e656a132200b59a30026eca7e943dc817f9e7..cef75867dc3e4d786674bd2d143510b0c259df35 100644 (file)
@@ -3979,17 +3979,7 @@ function switchroles_form($courseid) {
         return '';
     }
 
-    if (empty($USER->switchrole)) {   // Try to print a menu
-        if (has_capability('moodle/role:switchroles', $context)) {
-            if (!$roles = get_assignable_roles($context)) {
-                return '';   // Nothing to show!
-            }
-            return popup_form($CFG->wwwroot.'/course/view.php?id='.$courseid.'&amp;sesskey='.sesskey().'&amp;switchrole=',
-                              $roles, 'switchrole', '', get_string('switchroleto'), 'switchrole', get_string('switchroleto'), true);
-        } else {
-            return '';
-        }
-    } else {  // Just a button to return to normal
+    if (!empty($USER->switchrole[$context->id])){  // Just a button to return to normal
         $options = array();
         $options['id'] = $courseid;
         $options['sesskey'] = sesskey();
@@ -3998,6 +3988,18 @@ function switchroles_form($courseid) {
         return print_single_button($CFG->wwwroot.'/course/view.php', $options,
                                    get_string('switchrolereturn'), 'post', '_self', true);
     }
+
+    if (has_capability('moodle/role:switchroles', $context)) {
+        if (!$roles = get_assignable_roles($context)) {
+            return '';   // Nothing to show!
+        }
+        // unset default user role - it would not work
+        unset($roles[$CFG->guestroleid]);
+        return popup_form($CFG->wwwroot.'/course/view.php?id='.$courseid.'&amp;sesskey='.sesskey().'&amp;switchrole=',
+                          $roles, 'switchrole', '', get_string('switchroleto'), 'switchrole', get_string('switchroleto'), true);
+    }
+
+    return '';
 }