diff --git a/.upgradenotes/MDL-82567-2024080804140215.yml b/.upgradenotes/MDL-82567-2024080804140215.yml new file mode 100644 index 00000000000..5f35274750a --- /dev/null +++ b/.upgradenotes/MDL-82567-2024080804140215.yml @@ -0,0 +1,9 @@ +issueNumber: MDL-82567 +notes: + core_table: + - message: >- + `core_table\dynamic` declares a new method `::has_capability()` to + allow classes implementing this interface to perform access checks + on the dynamic table. This is a breaking change that all dynamic table + implementations must implement for continued functionality. + type: changed diff --git a/admin/classes/table/plugin_management_table.php b/admin/classes/table/plugin_management_table.php index 0f420d8ec52..6b143ae7d35 100644 --- a/admin/classes/table/plugin_management_table.php +++ b/admin/classes/table/plugin_management_table.php @@ -501,4 +501,15 @@ abstract class plugin_management_table extends flexible_table implements dynamic protected function supports_ordering(): bool { return $this->plugininfoclass::plugintype_supports_ordering(); } + + /** + * Check if the user has the capability to access this table. + * + * Default implementation for plugin management tables is to require 'moodle/site:config' capability + * + * @return bool Return true if capability check passed. + */ + public function has_capability(): bool { + return has_capability('moodle/site:config', $this->get_context()); + } } diff --git a/lib/table/classes/dynamic.php b/lib/table/classes/dynamic.php index 4288756b822..285e0aae4c2 100644 --- a/lib/table/classes/dynamic.php +++ b/lib/table/classes/dynamic.php @@ -41,4 +41,11 @@ namespace core_table; * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ interface dynamic { + + /** + * Check capability for users accessing the dynamic table. + * + * @return bool + */ + public function has_capability(): bool; } diff --git a/lib/table/classes/external/dynamic/get.php b/lib/table/classes/external/dynamic/get.php index 32fbce7e5e0..3d42631a667 100644 --- a/lib/table/classes/external/dynamic/get.php +++ b/lib/table/classes/external/dynamic/get.php @@ -224,9 +224,13 @@ class get extends external_api { ); } + /** @var \core_table\dynamic $instance */ $instance = new $tableclass($uniqueid); $instance->set_filterset($filterset); self::validate_context($instance->get_context()); + if (!$instance->has_capability()) { + throw new \moodle_exception('nopermissiontoaccesspage'); + } $instance->set_sortdata($sortdata); $alphabet = get_string('alphabet', 'langconfig'); diff --git a/reportbuilder/classes/table/custom_report_table.php b/reportbuilder/classes/table/custom_report_table.php index 947e8751301..9ac51c8395f 100644 --- a/reportbuilder/classes/table/custom_report_table.php +++ b/reportbuilder/classes/table/custom_report_table.php @@ -389,4 +389,13 @@ class custom_report_table extends base_report_table { return !empty($CFG->customreportsliveediting); } + + /** + * Check if the user has the capability to access this table. + * + * @return bool Return true if capability check passed. + */ + public function has_capability(): bool { + return \core_reportbuilder\permission::can_edit_report($this->persistent); + } } diff --git a/reportbuilder/classes/table/custom_report_table_view.php b/reportbuilder/classes/table/custom_report_table_view.php index 2ed750d51c8..0533aa4b006 100644 --- a/reportbuilder/classes/table/custom_report_table_view.php +++ b/reportbuilder/classes/table/custom_report_table_view.php @@ -74,4 +74,13 @@ class custom_report_table_view extends custom_report_table { return ''; } + + /** + * Check if the user has the capability to access this table. + * + * @return bool Return true if capability check passed. + */ + public function has_capability(): bool { + return \core_reportbuilder\permission::can_view_report($this->persistent); + } } diff --git a/reportbuilder/classes/table/system_report_table.php b/reportbuilder/classes/table/system_report_table.php index ff601aa9381..a05e4ff1c2e 100644 --- a/reportbuilder/classes/table/system_report_table.php +++ b/reportbuilder/classes/table/system_report_table.php @@ -316,4 +316,18 @@ class system_report_table extends base_report_table { return ''; } + + /** + * Check if the user has the capability to access this table. + * + * @return bool Return true if capability check passed. + */ + public function has_capability(): bool { + try { + $this->report->require_can_view(); + return true; + } catch (\core_reportbuilder\exception\report_access_exception $e) { + return false; + } + } } diff --git a/user/classes/table/participants.php b/user/classes/table/participants.php index 8b61cac294e..ef132ada1fe 100644 --- a/user/classes/table/participants.php +++ b/user/classes/table/participants.php @@ -481,4 +481,17 @@ class participants extends \table_sql implements dynamic_table { public function get_context(): context { return $this->context; } + + /** + * Check if the user has the capability to access this table. + * + * @return bool Return true if capability check passed. + */ + public function has_capability(): bool { + global $CFG; + require_once($CFG->dirroot . '/course/lib.php'); + + $context = $this->course->id == SITEID ? \context_system::instance() : $this->get_context(); + return course_can_view_participants($context); + } }