MDL-66034 core_role: add more detail to role allow event classes

This commit is contained in:
Simey Lameze 2019-07-24 14:28:18 +08:00
parent de4dc81b24
commit 32306e8390
12 changed files with 97 additions and 27 deletions

View File

@ -46,25 +46,6 @@ $controller = new $classformode[$mode]();
if (optional_param('submit', false, PARAM_BOOL) && data_submitted() && confirm_sesskey()) {
$controller->process_submission();
$event = null;
// Create event depending on mode.
switch ($mode) {
case 'assign':
$event = \core\event\role_allow_assign_updated::create(array('context' => $syscontext));
break;
case 'override':
$event = \core\event\role_allow_override_updated::create(array('context' => $syscontext));
break;
case 'switch':
$event = \core\event\role_allow_switch_updated::create(array('context' => $syscontext));
break;
case 'view':
$event = \core\event\role_allow_view_updated::create(array('context' => $syscontext));
break;
}
if ($event) {
$event->trigger();
}
redirect($baseurl);
}

View File

@ -46,4 +46,8 @@ class core_role_allow_assign_page extends core_role_allow_role_page {
public function get_intro_text() {
return get_string('configallowassign', 'core_admin');
}
protected function get_eventclass() {
return \core\event\role_allow_assign_updated::class;
}
}

View File

@ -46,4 +46,8 @@ class core_role_allow_override_page extends core_role_allow_role_page {
public function get_intro_text() {
return get_string('configallowoverride2', 'core_admin');
}
protected function get_eventclass() {
return \core\event\role_allow_override_updated::class;
}
}

View File

@ -59,12 +59,36 @@ abstract class core_role_allow_role_page {
*/
public function process_submission() {
global $DB;
$context = context_system::instance();
$this->load_current_settings();
// Delete all records, then add back the ones that should be allowed.
$DB->delete_records($this->tablename);
foreach ($this->roles as $fromroleid => $notused) {
foreach ($this->roles as $targetroleid => $alsonotused) {
$isallowed = $this->allowed[$fromroleid][$targetroleid];
if (optional_param('s_' . $fromroleid . '_' . $targetroleid, false, PARAM_BOOL)) {
$this->set_allow($fromroleid, $targetroleid);
// Only trigger events if this role allow relationship did not exist and the checkbox element
// has been submitted.
if (!$isallowed) {
$eventclass = $this->get_eventclass();
$eventclass::create([
'context' => $context,
'objectid' => $fromroleid,
'other' => ['targetroleid' => $targetroleid, 'allow' => true]
])->trigger();
}
} else if ($isallowed) {
// When the user has deselect an existing role allow checkbox but it is in the list of roles
// allowances.
$eventclass = $this->get_eventclass();
$eventclass::create([
'context' => $context,
'objectid' => $fromroleid,
'other' => ['targetroleid' => $targetroleid, 'allow' => false]
])->trigger();
}
}
}
@ -161,4 +185,10 @@ abstract class core_role_allow_role_page {
* @return string
*/
public abstract function get_intro_text();
/**
* Returns the allow class respective event class name.
* @return string
*/
protected abstract function get_eventclass();
}

View File

@ -58,4 +58,8 @@ class core_role_allow_switch_page extends core_role_allow_role_page {
public function get_intro_text() {
return get_string('configallowswitch', 'core_admin');
}
protected function get_eventclass() {
return \core\event\role_allow_switch_updated::class;
}
}

View File

@ -74,4 +74,8 @@ class core_role_allow_view_page extends core_role_allow_role_page {
public function get_intro_text() {
return get_string('configallowview', 'core_admin');
}
protected function get_eventclass() {
return \core\event\role_allow_view_updated::class;
}
}

View File

@ -487,10 +487,17 @@ class core_role_define_role_table_advanced extends core_role_capability_table_wi
$addfunction = "core_role_set_{$type}_allowed";
$deltable = 'role_allow_'.$type;
$field = 'allow'.$type;
$eventclass = "\\core\\event\\role_allow_" . $type . "_updated";
$context = context_system::instance();
foreach ($current as $roleid) {
if (!in_array($roleid, $wanted)) {
$DB->delete_records($deltable, array('roleid'=>$this->roleid, $field=>$roleid));
$eventclass::create([
'context' => $context,
'objectid' => $this->roleid,
'other' => ['targetroleid' => $roleid, 'allow' => false]
])->trigger();
continue;
}
$key = array_search($roleid, $wanted);
@ -502,6 +509,14 @@ class core_role_define_role_table_advanced extends core_role_capability_table_wi
$roleid = $this->roleid;
}
$addfunction($this->roleid, $roleid);
if (in_array($roleid, $wanted)) {
$eventclass::create([
'context' => $context,
'objectid' => $this->roleid,
'other' => ['targetroleid' => $roleid, 'allow' => true]
])->trigger();
}
}
}

View File

@ -42,6 +42,7 @@ class role_allow_assign_updated extends base {
protected function init() {
$this->data['crud'] = 'u';
$this->data['edulevel'] = self::LEVEL_OTHER;
$this->data['objecttable'] = 'role_allow_assign';
}
/**
@ -59,7 +60,9 @@ class role_allow_assign_updated extends base {
* @return string
*/
public function get_description() {
return "The user with id '$this->userid' updated Allow role assignments.";
$action = ($this->other['allow']) ? 'allow' : 'stop allowing';
return "The user with id '$this->userid' modified the role with id '" . $this->other['targetroleid']
. "' to $action users with that role from assigning the role with id '" . $this->objectid . "' to users";
}
/**

View File

@ -42,6 +42,7 @@ class role_allow_override_updated extends base {
protected function init() {
$this->data['crud'] = 'u';
$this->data['edulevel'] = self::LEVEL_OTHER;
$this->data['objecttable'] = 'role_allow_override';
}
/**
@ -59,7 +60,9 @@ class role_allow_override_updated extends base {
* @return string
*/
public function get_description() {
return "The user with id '$this->userid' updated Allow role overrides.";
$action = ($this->other['allow']) ? 'allow' : 'stop allowing';
return "The user with id '$this->userid' modified the role with id '" . $this->other['targetroleid']
. "' to $action users with that role from overriding the role with id '" . $this->objectid . "' to users";
}
/**

View File

@ -42,6 +42,7 @@ class role_allow_switch_updated extends base {
protected function init() {
$this->data['crud'] = 'u';
$this->data['edulevel'] = self::LEVEL_OTHER;
$this->data['objecttable'] = 'role_allow_switch';
}
/**
@ -59,7 +60,9 @@ class role_allow_switch_updated extends base {
* @return string
*/
public function get_description() {
return "The user with id '$this->userid' updated Allow role switches.";
$action = ($this->other['allow']) ? 'allow' : 'stop allowing';
return "The user with id '$this->userid' modified the role with id '" . $this->other['targetroleid']
. "' to $action users with that role from switching the role with id '" . $this->objectid . "' to users";
}
/**

View File

@ -42,6 +42,7 @@ class role_allow_view_updated extends base {
protected function init() {
$this->data['crud'] = 'u';
$this->data['edulevel'] = self::LEVEL_OTHER;
$this->data['objecttable'] = 'role_allow_view';
}
/**
@ -59,7 +60,9 @@ class role_allow_view_updated extends base {
* @return string
*/
public function get_description() {
return "The user with id '$this->userid' updated Allow role views.";
$action = ($this->other['allow']) ? 'allow' : 'stop allowing';
return "The user with id '$this->userid' modified the role with id '" . $this->other['targetroleid']
. "' to $action users with that role from viewing the role with id '" . $this->objectid . "' to users";
}
/**

View File

@ -977,7 +977,11 @@ class core_accesslib_testcase extends advanced_testcase {
$this->assertTrue($DB->record_exists('role_allow_assign', array('roleid'=>$otherid, 'allowassign'=>$student->id)));
// Test event trigger.
$allowroleassignevent = \core\event\role_allow_assign_updated::create(array('context' => context_system::instance()));
$allowroleassignevent = \core\event\role_allow_assign_updated::create([
'context' => context_system::instance(),
'objectid' => $otherid,
'other' => ['targetroleid' => $student->id]
]);
$sink = $this->redirectEvents();
$allowroleassignevent->trigger();
$events = $sink->get_events();
@ -1006,7 +1010,11 @@ class core_accesslib_testcase extends advanced_testcase {
$this->assertTrue($DB->record_exists('role_allow_override', array('roleid'=>$otherid, 'allowoverride'=>$student->id)));
// Test event trigger.
$allowroleassignevent = \core\event\role_allow_override_updated::create(array('context' => context_system::instance()));
$allowroleassignevent = \core\event\role_allow_override_updated::create([
'context' => context_system::instance(),
'objectid' => $otherid,
'other' => ['targetroleid' => $student->id]
]);
$sink = $this->redirectEvents();
$allowroleassignevent->trigger();
$events = $sink->get_events();
@ -1035,7 +1043,11 @@ class core_accesslib_testcase extends advanced_testcase {
$this->assertTrue($DB->record_exists('role_allow_switch', array('roleid'=>$otherid, 'allowswitch'=>$student->id)));
// Test event trigger.
$allowroleassignevent = \core\event\role_allow_switch_updated::create(array('context' => context_system::instance()));
$allowroleassignevent = \core\event\role_allow_switch_updated::create([
'context' => context_system::instance(),
'objectid' => $otherid,
'other' => ['targetroleid' => $student->id]
]);
$sink = $this->redirectEvents();
$allowroleassignevent->trigger();
$events = $sink->get_events();
@ -1064,7 +1076,11 @@ class core_accesslib_testcase extends advanced_testcase {
$this->assertTrue($DB->record_exists('role_allow_view', array('roleid' => $otherid, 'allowview' => $student->id)));
// Test event trigger.
$allowroleassignevent = \core\event\role_allow_view_updated::create(array('context' => context_system::instance()));
$allowroleassignevent = \core\event\role_allow_view_updated::create([
'context' => context_system::instance(),
'objectid' => $otherid,
'other' => ['targetroleid' => $student->id]
]);
$sink = $this->redirectEvents();
$allowroleassignevent->trigger();
$events = $sink->get_events();