MDL-54035 accesslib: only modify role_capabilities in accesslib

Whenever the role_capabilities table is changed, we need to remember
to clear the cache for the role(s) being modified. This is far simpler
when all of those changes happen in a single file, i.e. accesslib.php,
allowing other parts of the application to use the public functions
without requiring knowledge of the cache internals.
This commit is contained in:
Jonathan Champ
2018-09-21 09:17:43 -04:00
parent b2f349a433
commit 34dd57053d
4 changed files with 16 additions and 27 deletions
+2 -20
View File
@@ -42,33 +42,15 @@ class enrol_category_plugin_testcase extends advanced_testcase {
}
protected function enable_role_sync($roleid) {
global $DB;
$syscontext = context_system::instance();
if ($rc = $DB->record_exists('role_capabilities', array('capability'=>'enrol/category:synchronised', 'roleid'=>$roleid, 'contextid'=>$syscontext->id))) {
if ($rc->permission != CAP_ALLOW) {
$rc->permission = CAP_ALLOW;
$DB->update_record('role_capabilities', $rc);
}
} else {
$rc = new stdClass();
$rc->capability = 'enrol/category:synchronised';
$rc->roleid = $roleid;
$rc->contextid = $syscontext->id;
$rc->permission = CAP_ALLOW;
$rc->timemodified = time();
$rc->modifierid = 0;
$DB->insert_record('role_capabilities', $rc);
}
assign_capability('enrol/category:synchronised', CAP_ALLOW, $roleid, $syscontext, true);
}
protected function disable_role_sync($roleid) {
global $DB;
$syscontext = context_system::instance();
$DB->delete_records('role_capabilities', array('capability'=>'enrol/category:synchronised', 'roleid'=>$roleid, 'contextid'=>$syscontext->id));
unassign_capability('enrol/category:synchronised', $roleid, $syscontext);
}
/**
+11 -3
View File
@@ -5156,15 +5156,23 @@ abstract class context extends stdClass implements IteratorAggregate {
require_once($CFG->dirroot.'/grade/grading/lib.php');
grading_manager::delete_all_for_context($this->_id);
$ids = $DB->get_fieldset_select('role_capabilities', 'DISTINCT roleid', 'contextid = ?', array($this->_id));
// now delete stuff from role related tables, role_unassign_all
// and unenrol should be called earlier to do proper cleanup
$DB->delete_records('role_assignments', array('contextid'=>$this->_id));
$DB->delete_records('role_capabilities', array('contextid'=>$this->_id));
$DB->delete_records('role_names', array('contextid'=>$this->_id));
$this->delete_capabilities();
}
/**
* Unassign all capabilities from a context.
*/
public function delete_capabilities() {
global $DB;
$ids = $DB->get_fieldset_select('role_capabilities', 'DISTINCT roleid', 'contextid = ?', array($this->_id));
if ($ids) {
$DB->delete_records('role_capabilities', array('contextid' => $this->_id));
// Reset any cache of these roles, including MUC.
accesslib_clear_role_cache($ids);
}
+2 -4
View File
@@ -5349,11 +5349,9 @@ function reset_course_userdata($data) {
if (!empty($data->reset_roles_overrides)) {
$children = $context->get_child_contexts();
foreach ($children as $child) {
$DB->delete_records('role_capabilities', array('contextid' => $child->id));
$child->delete_capabilities();
}
$DB->delete_records('role_capabilities', array('contextid' => $context->id));
// Force refresh for logged in users.
$context->mark_dirty();
$context->delete_capabilities();
$status[] = array('component' => $componentstr, 'item' => get_string('deletecourseoverrides', 'role'), 'error' => false);
}
+1
View File
@@ -121,6 +121,7 @@ information provided here is intended especially for developers.
- phone1
- phone2
- address
* New function \context->delete_capabilities() correctly handles caching when deleting all of a context's capabilities.
=== 3.5 ===