From 7b0afca8165cf84e2bd043d812356c3b893db976 Mon Sep 17 00:00:00 2001 From: Simey Lameze Date: Wed, 17 Jul 2019 19:59:01 +0800 Subject: [PATCH] MDL-66034 core_role: implement capability assigned event --- lang/en/role.php | 1 + lib/accesslib.php | 12 +++ lib/classes/event/capability_assigned.php | 99 +++++++++++++++++++++++ 3 files changed, 112 insertions(+) create mode 100644 lib/classes/event/capability_assigned.php diff --git a/lang/en/role.php b/lang/en/role.php index a01ff276355..63996984e5c 100644 --- a/lang/en/role.php +++ b/lang/en/role.php @@ -225,6 +225,7 @@ $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['eventroleallowassignupdated'] = 'Allow role assignment'; $string['eventroleallowoverrideupdated'] = 'Allow role override'; $string['eventroleallowswitchupdated'] = 'Allow role switch'; diff --git a/lib/accesslib.php b/lib/accesslib.php index 856c556d6d2..67f0b0e98cc 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); 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]); + } + } +}