MDL-73598 reportbuilder: feature switch for custom reports.

This commit is contained in:
Paul Holden 2022-01-18 18:04:37 +00:00
parent a01f1fa71c
commit 335012580b
6 changed files with 105 additions and 8 deletions

View file

@ -51,7 +51,9 @@ class permission {
* @return bool
*/
public static function can_view_reports_list(?int $userid = null): bool {
return has_any_capability([
global $CFG;
return !empty($CFG->enablecustomreports) && has_any_capability([
'moodle/reportbuilder:editall',
'moodle/reportbuilder:edit',
'moodle/reportbuilder:view',
@ -96,7 +98,6 @@ class permission {
*
* @param report $report
* @param int|null $userid User ID to check, or the current user if omitted
* @return void
* @throws report_access_exception
*/
public static function require_can_edit_report(report $report, ?int $userid = null): void {
@ -113,7 +114,11 @@ class permission {
* @return bool
*/
public static function can_edit_report(report $report, ?int $userid = null): bool {
global $USER;
global $CFG, $USER;
if (empty($CFG->enablecustomreports)) {
return false;
}
// We can only edit custom reports.
if ($report->get('type') !== base::TYPE_CUSTOM_REPORT) {
@ -135,8 +140,12 @@ class permission {
* @return bool
*/
public static function can_create_report(?int $userid = null): bool {
$capabilities = ['moodle/reportbuilder:edit', 'moodle/reportbuilder:editall'];
return has_any_capability($capabilities, context_system::instance(), $userid);
global $CFG;
return !empty($CFG->enablecustomreports) && has_any_capability([
'moodle/reportbuilder:edit',
'moodle/reportbuilder:editall',
], context_system::instance(), $userid);
}
/**

View file

@ -38,13 +38,18 @@ class send_schedule extends adhoc_task {
* Execute the task
*/
public function execute(): void {
global $USER, $DB;
global $CFG, $USER, $DB;
[
'reportid' => $reportid,
'scheduleid' => $scheduleid,
] = (array) $this->get_custom_data();
// Custom reports are disabled.
if (empty($CFG->enablecustomreports)) {
return;
}
$schedule = schedule::get_record(['id' => $scheduleid, 'reportid' => $reportid]);
if ($schedule === false) {
$this->log('Invalid schedule', 0);

View file

@ -63,6 +63,20 @@ class permission_test extends advanced_testcase {
permission::require_can_view_reports_list();
}
/**
* Test whether user can view reports list when custom reports are disabled
*/
public function test_require_can_view_reports_list_disabled(): void {
$this->resetAfterTest();
$this->setAdminUser();
set_config('enablecustomreports', 0);
$this->expectException(report_access_exception::class);
$this->expectExceptionMessage('You cannot view this report');
permission::require_can_view_reports_list();
}
/**
* Test whether user can view specific report
*/
@ -129,6 +143,24 @@ class permission_test extends advanced_testcase {
permission::require_can_view_report($report);
}
/**
* Test whether user can view report when custom reports are disabled
*/
public function test_require_can_view_report_disabled(): void {
$this->resetAfterTest();
$this->setAdminUser();
set_config('enablecustomreports', 0);
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
$this->expectException(report_access_exception::class);
$this->expectExceptionMessage('You cannot view this report');
permission::require_can_view_report($report);
}
/**
* Test that user cannot edit system reports
*/
@ -206,6 +238,24 @@ class permission_test extends advanced_testcase {
permission::require_can_edit_report($reportadmin);
}
/**
* Test whether user can edit report when custom reports are disabled
*/
public function test_require_can_edit_report_disabled(): void {
$this->resetAfterTest();
$this->setAdminUser();
set_config('enablecustomreports', 0);
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
$this->expectException(report_access_exception::class);
$this->expectExceptionMessage('You cannot edit this report');
permission::require_can_edit_report($report);
}
/**
* Test that user can create a new report
*/
@ -248,4 +298,22 @@ class permission_test extends advanced_testcase {
$this->expectExceptionMessage('You cannot create a new report');
permission::require_can_create_report((int)$user3->id);
}
/**
* Test whether user can create report when custom reports are disabled
*/
public function test_require_can_create_report_disabled(): void {
$this->resetAfterTest();
$this->setAdminUser();
set_config('enablecustomreports', 0);
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
$this->expectException(report_access_exception::class);
$this->expectExceptionMessage('You cannot create a new report');
permission::require_can_create_report();
}
}