This new function called load_all_capabilities() should be used whenever

you want to completely reload the capabilities for the current user.

Basically it will deal nicely with the default site-wide permissions
depending on the current user.

More background in the comments on MDL-6963

Merged from stable
This commit is contained in:
moodler 2006-10-23 15:17:31 +00:00
parent 18894ee462
commit 2f1a42481d
6 changed files with 35 additions and 12 deletions

View File

@ -65,7 +65,7 @@
sesskey(); // For added security, used to check script parameters
load_user_capability();
load_all_capabilities();
redirect("$CFG->wwwroot/user/edit.php?id=$user->id&course=$site->id"); // Edit thyself
exit;

View File

@ -26,7 +26,7 @@
/// Refreshing all current role assignments for the current user
load_user_capability();
load_all_capabilities();
/// Double check just in case they are actually enrolled already and
/// thus got to this script by mistake. This might occur if enrolments

View File

@ -9,7 +9,7 @@
if (!empty($USER->realuser)) {
$USER = get_complete_user_data('id', $USER->realuser);
load_user_capability(); // load all this user's normal capabilities
load_all_capabilities(); // load all this user's normal capabilities
if (isset($SESSION->oldcurrentgroup)) { // Restore previous "current group" cache.
$SESSION->currentgroup = $SESSION->oldcurrentgroup;

View File

@ -16,7 +16,7 @@
require_login();
/// Refreshing enrolment data in the USER session
load_user_capability();
load_all_capabilities();
if ($SESSION->wantsurl) {
$destination = $SESSION->wantsurl;

View File

@ -538,6 +538,10 @@ function load_user_capability($capability='', $context ='', $userid='') {
global $USER, $CFG;
if (empty($CFG->rolesactive)) {
return false;
}
if (empty($userid)) {
if (empty($USER->id)) { // We have no user to get capabilities for
debugging('User not logged in for load_user_capability!');
@ -754,10 +758,30 @@ function load_user_capability($capability='', $context ='', $userid='') {
if (!empty($otheruserid)) {
return $usercap; // return the array
}
// see array in session to see what it looks like
}
/*
* A convenience function to completely load all the capabilities
* for the current user. This is what gets called from login, for example.
*/
function load_all_capabilities() {
global $USER;
if (empty($USER->username)) {
return;
}
load_user_capability(); // Load basic capabilities assigned to this user
if ($USER->username == 'guest') {
load_guest_role(); // All non-guest users get this by default
} else {
load_defaultuser_role(); // All non-guest users get this by default
}
}
/*
* Check all the login enrolment information for the given user object
* by querying the enrolment plugins
@ -1646,7 +1670,7 @@ function role_assign($roleid, $userid, $groupid, $contextid, $timestart=0, $time
/// If the user is the current user, then reload the capabilities too.
if (!empty($USER->id) && $USER->id == $userid) {
load_user_capability();
load_all_capabilities();
}
/// Ask all the modules if anything needs to be done for this user
@ -1711,7 +1735,7 @@ function role_unassign($roleid=0, $userid=0, $groupid=0, $contextid=0) {
/// If the user is the current user, then reload the capabilities too.
if (!empty($USER->id) && $USER->id == $ra->userid) {
load_user_capability();
load_all_capabilities();
}
$context = get_record('context', 'id', $ra->contextid);

View File

@ -207,10 +207,9 @@
}
reset_login_count();
if (!empty($CFG->rolesactive)) {
load_user_capability(); // load user's capabilities
load_defaultuser_role(); // All users get this by default
}
load_all_capabilities(); /// This is what lets the user do anything on the site :-)
redirect($urltogo);
exit;