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

@ -30,7 +30,13 @@ use core_reportbuilder\permission;
defined('MOODLE_INTERNAL') || die; defined('MOODLE_INTERNAL') || die;
/** @var admin_root $ADMIN */ /** @var admin_root $ADMIN */
$ADMIN->add('reports', new admin_category('reportbuilder', new lang_string('reportbuilder', 'core_reportbuilder'))); $ADMIN->add(
'reports', new admin_category(
'reportbuilder',
new lang_string('reportbuilder', 'core_reportbuilder'),
empty($CFG->enablecustomreports)
)
);
$ADMIN->add( $ADMIN->add(
'reportbuilder', new accesscallback( 'reportbuilder', new accesscallback(
@ -39,6 +45,7 @@ $ADMIN->add(
(new moodle_url('/reportbuilder/index.php'))->out(), (new moodle_url('/reportbuilder/index.php'))->out(),
static function(accesscallback $accesscallback): bool { static function(accesscallback $accesscallback): bool {
return permission::can_view_reports_list(); return permission::can_view_reports_list();
} },
empty($CFG->enablecustomreports)
) )
); );

View file

@ -64,6 +64,12 @@ if ($hassiteconfig) { // speedup for non-admins, add all caps used on this page
1) 1)
); );
$optionalsubsystems->add(new admin_setting_configcheckbox('enablecustomreports',
new lang_string('enablecustomreports', 'core_reportbuilder'),
new lang_string('enablecustomreports_desc', 'core_reportbuilder'),
1
));
$fullunicodesupport = true; $fullunicodesupport = true;
if ($DB->get_dbfamily() == 'mysql') { if ($DB->get_dbfamily() == 'mysql') {
$collation = $DB->get_dbcollation(); $collation = $DB->get_dbcollation();

View file

@ -100,6 +100,8 @@ $string['editreportdetails'] = 'Edit report details';
$string['editreportname'] = 'Edit report name'; $string['editreportname'] = 'Edit report name';
$string['editscheduledetails'] = 'Edit schedule details'; $string['editscheduledetails'] = 'Edit schedule details';
$string['editschedulename'] = 'Edit schedule name'; $string['editschedulename'] = 'Edit schedule name';
$string['enablecustomreports'] = 'Enable custom reports';
$string['enablecustomreports_desc'] = 'Allow users to create and view Report builder custom reports';
$string['entitycourse'] = 'Course'; $string['entitycourse'] = 'Course';
$string['entityuser'] = 'User'; $string['entityuser'] = 'User';
$string['errorreportcreate'] = 'You cannot create a new report'; $string['errorreportcreate'] = 'You cannot create a new report';

View file

@ -51,7 +51,9 @@ class permission {
* @return bool * @return bool
*/ */
public static function can_view_reports_list(?int $userid = null): 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:editall',
'moodle/reportbuilder:edit', 'moodle/reportbuilder:edit',
'moodle/reportbuilder:view', 'moodle/reportbuilder:view',
@ -96,7 +98,6 @@ class permission {
* *
* @param report $report * @param report $report
* @param int|null $userid User ID to check, or the current user if omitted * @param int|null $userid User ID to check, or the current user if omitted
* @return void
* @throws report_access_exception * @throws report_access_exception
*/ */
public static function require_can_edit_report(report $report, ?int $userid = null): void { public static function require_can_edit_report(report $report, ?int $userid = null): void {
@ -113,7 +114,11 @@ class permission {
* @return bool * @return bool
*/ */
public static function can_edit_report(report $report, ?int $userid = null): 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. // We can only edit custom reports.
if ($report->get('type') !== base::TYPE_CUSTOM_REPORT) { if ($report->get('type') !== base::TYPE_CUSTOM_REPORT) {
@ -135,8 +140,12 @@ class permission {
* @return bool * @return bool
*/ */
public static function can_create_report(?int $userid = null): bool { public static function can_create_report(?int $userid = null): bool {
$capabilities = ['moodle/reportbuilder:edit', 'moodle/reportbuilder:editall']; global $CFG;
return has_any_capability($capabilities, context_system::instance(), $userid);
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 * Execute the task
*/ */
public function execute(): void { public function execute(): void {
global $USER, $DB; global $CFG, $USER, $DB;
[ [
'reportid' => $reportid, 'reportid' => $reportid,
'scheduleid' => $scheduleid, 'scheduleid' => $scheduleid,
] = (array) $this->get_custom_data(); ] = (array) $this->get_custom_data();
// Custom reports are disabled.
if (empty($CFG->enablecustomreports)) {
return;
}
$schedule = schedule::get_record(['id' => $scheduleid, 'reportid' => $reportid]); $schedule = schedule::get_record(['id' => $scheduleid, 'reportid' => $reportid]);
if ($schedule === false) { if ($schedule === false) {
$this->log('Invalid schedule', 0); $this->log('Invalid schedule', 0);

View file

@ -63,6 +63,20 @@ class permission_test extends advanced_testcase {
permission::require_can_view_reports_list(); 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 * Test whether user can view specific report
*/ */
@ -129,6 +143,24 @@ class permission_test extends advanced_testcase {
permission::require_can_view_report($report); 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 * Test that user cannot edit system reports
*/ */
@ -206,6 +238,24 @@ class permission_test extends advanced_testcase {
permission::require_can_edit_report($reportadmin); 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 * 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'); $this->expectExceptionMessage('You cannot create a new report');
permission::require_can_create_report((int)$user3->id); 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();
}
} }