diff --git a/admin/roles/allow.php b/admin/roles/allow.php index 88609cfe359..643e6cee561 100644 --- a/admin/roles/allow.php +++ b/admin/roles/allow.php @@ -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); } diff --git a/admin/roles/classes/allow_assign_page.php b/admin/roles/classes/allow_assign_page.php index a89228fdd1f..002d944657c 100644 --- a/admin/roles/classes/allow_assign_page.php +++ b/admin/roles/classes/allow_assign_page.php @@ -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; + } } diff --git a/admin/roles/classes/allow_override_page.php b/admin/roles/classes/allow_override_page.php index 4b160ee015e..16226a606cf 100644 --- a/admin/roles/classes/allow_override_page.php +++ b/admin/roles/classes/allow_override_page.php @@ -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; + } } diff --git a/admin/roles/classes/allow_role_page.php b/admin/roles/classes/allow_role_page.php index c84d01a44ab..376489fa239 100644 --- a/admin/roles/classes/allow_role_page.php +++ b/admin/roles/classes/allow_role_page.php @@ -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(); } diff --git a/admin/roles/classes/allow_switch_page.php b/admin/roles/classes/allow_switch_page.php index 5b22e1e3711..195aab76a83 100644 --- a/admin/roles/classes/allow_switch_page.php +++ b/admin/roles/classes/allow_switch_page.php @@ -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; + } } diff --git a/admin/roles/classes/allow_view_page.php b/admin/roles/classes/allow_view_page.php index f1a1031e5bb..d3313220d0c 100644 --- a/admin/roles/classes/allow_view_page.php +++ b/admin/roles/classes/allow_view_page.php @@ -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; + } } diff --git a/admin/roles/classes/define_role_table_advanced.php b/admin/roles/classes/define_role_table_advanced.php index d5f6c183508..265119fafd9 100644 --- a/admin/roles/classes/define_role_table_advanced.php +++ b/admin/roles/classes/define_role_table_advanced.php @@ -434,7 +434,7 @@ class core_role_define_role_table_advanced extends core_role_capability_table_wi } public function save_changes() { - global $DB; + global $DB, $USER; if (!$this->roleid) { // Creating role. @@ -444,6 +444,20 @@ class core_role_define_role_table_advanced extends core_role_capability_table_wi // Updating role. $DB->update_record('role', $this->role); + // Trigger role updated event. + \core\event\role_updated::create([ + 'userid' => $USER->id, + 'objectid' => $this->role->id, + 'context' => $this->context, + 'other' => [ + 'name' => $this->role->name, + 'shortname' => $this->role->shortname, + 'description' => $this->role->description, + 'archetype' => $this->role->archetype, + 'contextlevels' => $this->contextlevels + ] + ])->trigger(); + // This will ensure the course contacts cache is purged so name changes get updated in // the UI. It would be better to do this only when we know that fields affected are // updated. But thats getting into the weeds of the coursecat cache and role edits @@ -473,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); @@ -488,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(); + } } } diff --git a/admin/roles/define.php b/admin/roles/define.php index 1a90cddd39a..fdf144de6ca 100644 --- a/admin/roles/define.php +++ b/admin/roles/define.php @@ -200,19 +200,6 @@ if (optional_param('cancel', false, PARAM_BOOL)) { if (optional_param('savechanges', false, PARAM_BOOL) && confirm_sesskey() && $definitiontable->is_submission_valid()) { $definitiontable->save_changes(); $tableroleid = $definitiontable->get_role_id(); - // Trigger event. - $event = \core\event\role_capabilities_updated::create( - array( - 'context' => $systemcontext, - 'objectid' => $tableroleid - ) - ); - $event->set_legacy_logdata(array(SITEID, 'role', $action, 'admin/roles/define.php?action=view&roleid=' . $tableroleid, - $definitiontable->get_role_name(), '', $USER->id)); - if (!empty($role)) { - $event->add_record_snapshot('role', $role); - } - $event->trigger(); if ($action === 'add') { redirect(new moodle_url('/admin/roles/define.php', array('action'=>'view', 'roleid'=>$definitiontable->get_role_id()))); diff --git a/admin/roles/override.php b/admin/roles/override.php index 878c820384b..f3d393b269e 100644 --- a/admin/roles/override.php +++ b/admin/roles/override.php @@ -134,22 +134,6 @@ $overridestable->read_submitted_permissions(); if (optional_param('savechanges', false, PARAM_BOOL) && confirm_sesskey()) { $overridestable->save_changes(); $rolename = $overridableroles[$roleid]; - // Trigger event. - $event = \core\event\role_capabilities_updated::create( - array( - 'context' => $context, - 'objectid' => $roleid, - ) - ); - - $event->set_legacy_logdata( - array( - $course->id, 'role', 'override', 'admin/roles/override.php?contextid=' . $context->id . '&roleid=' . $roleid, - $rolename, '', $USER->id - ) - ); - $event->add_record_snapshot('role', $role); - $event->trigger(); redirect($returnurl); } diff --git a/lang/en/deprecated.txt b/lang/en/deprecated.txt index 14eafe9dc3b..8158d35d857 100644 --- a/lang/en/deprecated.txt +++ b/lang/en/deprecated.txt @@ -160,3 +160,4 @@ nobackpackcollections,core_badges error:nogroups,core_badges purgedefinitionsuccess,core_cache purgestoresuccess,core_cache +eventrolecapabilitiesupdated,core_role \ No newline at end of file diff --git a/lang/en/role.php b/lang/en/role.php index a01ff276355..ead0c9e6f29 100644 --- a/lang/en/role.php +++ b/lang/en/role.php @@ -225,14 +225,16 @@ $string['errorbadroleshortname'] = 'Incorrect role short name'; $string['errorexistsrolename'] = 'Role name already exists'; $string['errorexistsroleshortname'] = 'Role name already exists'; $string['errorroleshortnametoolong'] = 'The short name must not exceed 100 characters'; +$string['eventcapabilityassigned'] = 'Capability assigned'; +$string['eventcapabilityunassigned'] = 'Capability unassigned'; $string['eventroleallowassignupdated'] = 'Allow role assignment'; $string['eventroleallowoverrideupdated'] = 'Allow role override'; $string['eventroleallowswitchupdated'] = 'Allow role switch'; $string['eventroleallowviewupdated'] = 'Allow role view'; $string['eventroleassigned'] = 'Role assigned'; -$string['eventrolecapabilitiesupdated'] = 'Role capabilities updated'; $string['eventroledeleted'] = 'Role deleted'; $string['eventroleunassigned'] = 'Role unassigned'; +$string['eventroleupdated'] = 'Role updated'; $string['existingadmins'] = 'Current site administrators'; $string['existingusers'] = '{$a} existing users'; $string['explanation'] = 'Explanation'; @@ -495,3 +497,6 @@ $string['privacy:metadata:role_capabilities:tableexplanation'] = 'The capabiliti $string['privacy:metadata:role_capabilities:timemodified'] = 'The date when the capability was created or modified.'; $string['privacy:metadata:role_cohortroles'] = 'Roles to cohort'; $string['course:togglecompletion'] = 'Manually mark activities as complete'; + +// Deprecated since Moodle 3.8. +$string['eventrolecapabilitiesupdated'] = 'Role capabilities updated'; \ No newline at end of file diff --git a/lib/accesslib.php b/lib/accesslib.php index 856c556d6d2..2383769529a 100644 --- a/lib/accesslib.php +++ b/lib/accesslib.php @@ -1379,6 +1379,18 @@ function assign_capability($capability, $permission, $roleid, $contextid, $overw } } + // Trigger capability_assigned event. + \core\event\capability_assigned::create([ + 'userid' => $cap->modifierid, + 'context' => $context, + 'objectid' => $roleid, + 'other' => [ + 'capability' => $capability, + 'oldpermission' => $existing->permission ?? CAP_INHERIT, + 'permission' => $permission + ] + ])->trigger(); + // Reset any cache of this role, including MUC. accesslib_clear_role_cache($roleid); @@ -1394,7 +1406,7 @@ function assign_capability($capability, $permission, $roleid, $contextid, $overw * @return boolean true or exception */ function unassign_capability($capability, $roleid, $contextid = null) { - global $DB; + global $DB, $USER; // Capability must exist. if (!$capinfo = get_capability_info($capability)) { @@ -1413,6 +1425,16 @@ function unassign_capability($capability, $roleid, $contextid = null) { $DB->delete_records('role_capabilities', array('capability'=>$capability, 'roleid'=>$roleid)); } + // Trigger capability_assigned event. + \core\event\capability_unassigned::create([ + 'userid' => $USER->id, + 'context' => $context ?? context_system::instance(), + 'objectid' => $roleid, + 'other' => [ + 'capability' => $capability, + ] + ])->trigger(); + // Reset any cache of this role, including MUC. accesslib_clear_role_cache($roleid); diff --git a/lib/classes/event/capability_assigned.php b/lib/classes/event/capability_assigned.php new file mode 100644 index 00000000000..a5b4d4ab45b --- /dev/null +++ b/lib/classes/event/capability_assigned.php @@ -0,0 +1,99 @@ +. + +/** + * Capability assigned event. + * + * @package core + * @since Moodle 3.8 + * @copyright 2019 Simey Lameze + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace core\event; + +defined('MOODLE_INTERNAL') || die(); + +/** + * Capability assigned event class. + * + * @package core + * @since Moodle 3.8 + * @copyright 2019 Simey Lameze + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class capability_assigned extends base { + /** + * Initialise event parameters. + */ + protected function init() { + $this->data['objecttable'] = 'role_capabilities'; + $this->data['crud'] = 'u'; + $this->data['edulevel'] = self::LEVEL_OTHER; + } + + /** + * Returns localised event name. + * + * @return string + */ + public static function get_name() { + return get_string('eventcapabilityassigned', 'role'); + } + + /** + * Returns non-localised event description with id's for admin use only. + * + * @return string + */ + public function get_description() { + + $strpermissions = [ + CAP_INHERIT => get_string('notset', 'role'), + CAP_ALLOW => get_string('allow', 'role'), + CAP_PREVENT => get_string('prevent', 'role'), + CAP_PROHIBIT => get_string('prohibit', 'role') + ]; + + $capability = $this->other['capability']; + $oldpermission = $this->other['oldpermission']; + $permission = $this->other['permission']; + + if ($oldpermission == CAP_INHERIT && $permission == CAP_ALLOW) { + $description = "The user id '$this->userid' assigned the '$capability' capability for " . + "role '$this->objectid' with '$strpermissions[$permission]' permission"; + } else { + $description = "The user id '$this->userid' changed the '$capability' capability permission for " . + "role '$this->objectid' from '$strpermissions[$oldpermission]' to '$strpermissions[$permission]'"; + } + + return $description; + } + + /** + * Returns relevant URL. + * + * @return \moodle_url + */ + public function get_url() { + if ($this->contextlevel == CONTEXT_SYSTEM) { + return new \moodle_url('/admin/roles/define.php', ['action' => 'edit', 'roleid' => $this->objectid]); + } else { + return new \moodle_url('/admin/roles/override.php', ['contextid' => $this->contextid, + 'roleid' => $this->objectid]); + } + } +} diff --git a/lib/classes/event/capability_unassigned.php b/lib/classes/event/capability_unassigned.php new file mode 100644 index 00000000000..928795fb6d9 --- /dev/null +++ b/lib/classes/event/capability_unassigned.php @@ -0,0 +1,81 @@ +. + +/** + * Capability unassigned event. + * + * @package core + * @since Moodle 3.8 + * @copyright 2019 Simey Lameze + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace core\event; + +defined('MOODLE_INTERNAL') || die(); + +/** + * Capability unassigned event class. + * + * @package core + * @since Moodle 3.8 + * @copyright 2019 Simey Lameze + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class capability_unassigned extends base { + /** + * Initialise event parameters. + */ + protected function init() { + $this->data['objecttable'] = 'role_capabilities'; + $this->data['crud'] = 'u'; + $this->data['edulevel'] = self::LEVEL_OTHER; + } + + /** + * Returns localised event name. + * + * @return string + */ + public static function get_name() { + return get_string('eventcapabilityunassigned', 'role'); + } + + /** + * Returns non-localised event description with id's for admin use only. + * + * @return string + */ + public function get_description() { + $capability = $this->other['capability']; + + return "The user id id '$this->userid' has unassigned the '$capability' capability for role '$this->objectid'"; + } + + /** + * Returns relevant URL. + * + * @return \moodle_url + */ + public function get_url() { + if ($this->contextlevel == CONTEXT_SYSTEM) { + return new \moodle_url('/admin/roles/define.php', ['action' => 'view', 'roleid' => $this->objectid]); + } else { + return new \moodle_url('/admin/roles/override.php', ['contextid' => $this->contextid, + 'roleid' => $this->objectid]); + } + } +} diff --git a/lib/classes/event/role_allow_assign_updated.php b/lib/classes/event/role_allow_assign_updated.php index 5cdb7e2cca5..850f89414b0 100644 --- a/lib/classes/event/role_allow_assign_updated.php +++ b/lib/classes/event/role_allow_assign_updated.php @@ -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"; } /** diff --git a/lib/classes/event/role_allow_override_updated.php b/lib/classes/event/role_allow_override_updated.php index 84960fbc6ef..a405116fade 100644 --- a/lib/classes/event/role_allow_override_updated.php +++ b/lib/classes/event/role_allow_override_updated.php @@ -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"; } /** diff --git a/lib/classes/event/role_allow_switch_updated.php b/lib/classes/event/role_allow_switch_updated.php index 7115efcb40c..70b8c36374e 100644 --- a/lib/classes/event/role_allow_switch_updated.php +++ b/lib/classes/event/role_allow_switch_updated.php @@ -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"; } /** diff --git a/lib/classes/event/role_allow_view_updated.php b/lib/classes/event/role_allow_view_updated.php index 1d08df97eb8..7603988570e 100644 --- a/lib/classes/event/role_allow_view_updated.php +++ b/lib/classes/event/role_allow_view_updated.php @@ -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"; } /** diff --git a/lib/classes/event/role_capabilities_updated.php b/lib/classes/event/role_capabilities_updated.php index e94e768cb4a..28f14d87d27 100644 --- a/lib/classes/event/role_capabilities_updated.php +++ b/lib/classes/event/role_capabilities_updated.php @@ -27,6 +27,9 @@ namespace core\event; defined('MOODLE_INTERNAL') || die(); +debugging('core\\event\\role_capabilities_updated has been deprecated. Please use + core\\event\\capability_assigned instead', DEBUG_DEVELOPER); + /** * Role updated event class. * @@ -102,4 +105,14 @@ class role_capabilities_updated extends base { public static function get_objectid_mapping() { return array('db' => 'role', 'restore' => 'role'); } + + + /** + * This event has been deprecated. + * + * @return boolean + */ + public static function is_deprecated() { + return true; + } } diff --git a/lib/classes/event/role_updated.php b/lib/classes/event/role_updated.php new file mode 100644 index 00000000000..dd6e948d51e --- /dev/null +++ b/lib/classes/event/role_updated.php @@ -0,0 +1,97 @@ +. + +/** + * Role updated event. + * + * @package core + * @copyright 2019 Simey Lameze + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace core\event; + +defined('MOODLE_INTERNAL') || die(); + +/** + * Role updated event class. + * + * @package core + * @since Moodle 3.8 + * @copyright 2019 Simey Lameze + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class role_updated extends base { + /** + * Initialise event parameters. + */ + protected function init() { + $this->data['objecttable'] = 'role'; + $this->data['crud'] = 'u'; + $this->data['edulevel'] = self::LEVEL_OTHER; + } + + /** + * Returns localised event name. + * + * @return string + */ + public static function get_name() { + return get_string('eventroleupdated', 'role'); + } + + /** + * Returns non-localised event description with id's for admin use only. + * + * @return string + */ + public function get_description() { + return "The user with id '$this->userid' updated the role with id '$this->objectid'."; + } + + /** + * Returns relevant URL. + * + * @return \moodle_url + */ + public function get_url() { + return new \moodle_url('/admin/roles/define.php', ['action' => 'edit', 'roleid' => $this->objectid]); + } + + /** + * Returns array of parameters to be passed to legacy add_to_log() function. + * + * @return array + */ + protected function get_legacy_logdata() { + return [SITEID, 'role', 'update', 'admin/roles/manage.php?action=edit&roleid=' . $this->objectid, + $this->other['shortname'], '']; + } + + /** + * Custom validation. + * + * @throws \coding_exception + * @return void + */ + protected function validate_data() { + parent::validate_data(); + + if (!isset($this->other['shortname'])) { + throw new \coding_exception('The \'shortname\' value must be set in other.'); + } + } +} diff --git a/lib/tests/accesslib_test.php b/lib/tests/accesslib_test.php index e6834a491b6..8e6ce40f250 100644 --- a/lib/tests/accesslib_test.php +++ b/lib/tests/accesslib_test.php @@ -368,7 +368,7 @@ class core_accesslib_testcase extends advanced_testcase { * Test adding of capabilities to roles. */ public function test_assign_capability() { - global $DB; + global $DB, $USER; $this->resetAfterTest(); @@ -408,34 +408,40 @@ class core_accesslib_testcase extends advanced_testcase { $permission = $DB->get_record('role_capabilities', array('contextid'=>$frontcontext->id, 'roleid'=>$student->id, 'capability'=>'moodle/backup:backupcourse')); $this->assertEmpty($permission); - // Test event trigger. - $rolecapabilityevent = \core\event\role_capabilities_updated::create(array('context' => $syscontext, - 'objectid' => $student->id, - 'other' => array('name' => $student->shortname) - )); - $expectedlegacylog = array(SITEID, 'role', 'view', 'admin/roles/define.php?action=view&roleid=' . $student->id, - $student->shortname, '', $user->id); - $rolecapabilityevent->set_legacy_logdata($expectedlegacylog); - $rolecapabilityevent->add_record_snapshot('role', $student); - + // Test event triggered. $sink = $this->redirectEvents(); - $rolecapabilityevent->trigger(); + $capability = 'moodle/backup:backupcourse'; + assign_capability($capability, CAP_ALLOW, $student->id, $syscontext); $events = $sink->get_events(); $sink->close(); - $event = array_pop($events); + $this->assertCount(1, $events); + $event = $events[0]; + $this->assertInstanceOf('\core\event\capability_assigned', $event); + $this->assertSame('role_capabilities', $event->objecttable); + $this->assertEquals($student->id, $event->objectid); + $this->assertEquals($syscontext->id, $event->contextid); + $other = ['capability' => $capability, 'oldpermission' => CAP_INHERIT, 'permission' => CAP_ALLOW]; + $this->assertEquals($other, $event->other); + $description = "The user id '$USER->id' assigned the '$capability' capability for " . + "role '$student->id' with 'Allow' permission"; + $this->assertEquals($description, $event->get_description()); - $this->assertInstanceOf('\core\event\role_capabilities_updated', $event); - $expectedurl = new moodle_url('/admin/roles/define.php', array('action' => 'view', 'roleid' => $student->id)); - $this->assertEquals($expectedurl, $event->get_url()); - $this->assertEventLegacyLogData($expectedlegacylog, $event); - $this->assertEventContextNotUsed($event); + // Test if the event has different description when updating the capability permission. + $sink = $this->redirectEvents(); + assign_capability($capability, CAP_PROHIBIT, $student->id, $syscontext, true); + $events = $sink->get_events(); + $sink->close(); + $event = $events[0]; + $description = "The user id '$USER->id' changed the '$capability' capability permission for " . + "role '$student->id' from 'Allow' to 'Prohibit'"; + $this->assertEquals($description, $event->get_description()); } /** * Test removing of capabilities from roles. */ public function test_unassign_capability() { - global $DB; + global $DB, $USER; $this->resetAfterTest(); @@ -463,6 +469,22 @@ class core_accesslib_testcase extends advanced_testcase { $this->assertTrue($result); $this->assertFalse($DB->record_exists('role_capabilities', array('contextid'=>$syscontext->id, 'roleid'=>$manager->id, 'capability'=>'moodle/backup:backupcourse'))); $this->assertFalse($DB->record_exists('role_capabilities', array('contextid'=>$frontcontext->id, 'roleid'=>$manager->id, 'capability'=>'moodle/backup:backupcourse'))); + + // Test event triggered. + $sink = $this->redirectEvents(); + $capability = 'moodle/backup:backupcourse'; + unassign_capability($capability, CAP_ALLOW, $manager->id); + $events = $sink->get_events(); + $sink->close(); + $this->assertCount(1, $events); + $event = $events[0]; + $this->assertInstanceOf('\core\event\capability_unassigned', $event); + $this->assertSame('role_capabilities', $event->objecttable); + $this->assertEquals($manager->id, $event->objectid); + $this->assertEquals($syscontext->id, $event->contextid); + $this->assertEquals($capability, $event->other['capability']); + $description = "The user id id '$USER->id' has unassigned the '$capability' capability for role '$manager->id'"; + $this->assertEquals($description, $event->get_description()); } /** @@ -977,7 +999,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 +1032,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 +1065,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 +1098,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();