Skip to content

Commit

Permalink
MDL-73598 reportbuilder: feature switch for custom reports.
Browse files Browse the repository at this point in the history
  • Loading branch information
paulholden committed Jan 19, 2022
1 parent a01f1fa commit 3350125
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 8 deletions.
11 changes: 9 additions & 2 deletions admin/settings/reportbuilder.php
Expand Up @@ -30,7 +30,13 @@
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(
Expand All @@ -39,6 +45,7 @@
(new moodle_url('/reportbuilder/index.php'))->out(),
static function(accesscallback $accesscallback): bool {
return permission::can_view_reports_list();
}
},
empty($CFG->enablecustomreports)
)
);
6 changes: 6 additions & 0 deletions admin/settings/subsystems.php
Expand Up @@ -64,6 +64,12 @@
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();
Expand Down
2 changes: 2 additions & 0 deletions lang/en/reportbuilder.php
Expand Up @@ -100,6 +100,8 @@
$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';
Expand Down
19 changes: 14 additions & 5 deletions reportbuilder/classes/permission.php
Expand Up @@ -51,7 +51,9 @@ public static function require_can_view_reports_list(?int $userid = null): void
* @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',
Expand Down Expand Up @@ -96,7 +98,6 @@ public static function can_view_report(report $report, ?int $userid = null): boo
*
* @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 {
Expand All @@ -113,7 +114,11 @@ public static function require_can_edit_report(report $report, ?int $userid = nu
* @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) {
Expand All @@ -135,8 +140,12 @@ public static function can_edit_report(report $report, ?int $userid = null): boo
* @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);
}

/**
Expand Down
7 changes: 6 additions & 1 deletion reportbuilder/classes/task/send_schedule.php
Expand Up @@ -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);
Expand Down
68 changes: 68 additions & 0 deletions reportbuilder/tests/permission_test.php
Expand Up @@ -63,6 +63,20 @@ public function test_require_can_view_reports_list(): void {
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
*/
Expand Down Expand Up @@ -129,6 +143,24 @@ public function test_require_can_view_report_with_audience(): void {
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
*/
Expand Down Expand Up @@ -206,6 +238,24 @@ public function test_require_can_edit_report_all(): void {
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
*/
Expand Down Expand Up @@ -248,4 +298,22 @@ public function test_require_can_create_report(): void {
$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();
}
}

0 comments on commit 3350125

Please sign in to comment.