This commit is contained in:
Andrew Nicols 2022-01-24 11:29:48 +08:00
commit 6bec1964cc
6 changed files with 105 additions and 8 deletions

View File

@ -30,7 +30,13 @@ use core_reportbuilder\permission;
defined('MOODLE_INTERNAL') || die;
/** @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(
'reportbuilder', new accesscallback(
@ -39,6 +45,7 @@ $ADMIN->add(
(new moodle_url('/reportbuilder/index.php'))->out(),
static function(accesscallback $accesscallback): bool {
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)
);
$optionalsubsystems->add(new admin_setting_configcheckbox('enablecustomreports',
new lang_string('enablecustomreports', 'core_reportbuilder'),
new lang_string('enablecustomreports_desc', 'core_reportbuilder'),
1
));
$fullunicodesupport = true;
if ($DB->get_dbfamily() == 'mysql') {
$collation = $DB->get_dbcollation();

View File

@ -100,6 +100,8 @@ $string['editreportdetails'] = 'Edit report details';
$string['editreportname'] = 'Edit report name';
$string['editscheduledetails'] = 'Edit schedule details';
$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['entityuser'] = 'User';
$string['errorreportcreate'] = 'You cannot create a new report';

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();
}
}