This commit is contained in:
Sara Arjona
2023-08-17 09:10:51 +02:00
5 changed files with 203 additions and 2 deletions
+6
View File
@@ -2951,6 +2951,12 @@ $functions = array(
'type' => 'read',
'services' => [MOODLE_OFFICIAL_MOBILE_SERVICE],
],
'core_reportbuilder_can_view_system_report' => [
'classname' => 'core_reportbuilder\external\systemreports\can_view',
'description' => 'Determine access to a system report',
'type' => 'read',
'services' => [MOODLE_OFFICIAL_MOBILE_SERVICE],
],
'core_reportbuilder_view_report' => [
'classname' => 'core_reportbuilder\external\reports\view',
'description' => 'Trigger custom report viewed',
@@ -0,0 +1,120 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
declare(strict_types=1);
namespace core_reportbuilder\external\systemreports;
use core_external\external_api;
use core_external\external_multiple_structure;
use core_external\external_single_structure;
use core_external\external_function_parameters;
use core_external\external_value;
use core_reportbuilder\report_access_exception;
use core_reportbuilder\system_report_factory;
/**
* External method for validating access to a system report
*
* @package core_reportbuilder
* @copyright 2023 Paul Holden <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class can_view extends external_api {
/**
* External method parameters
*
* @return external_function_parameters
*/
public static function execute_parameters(): external_function_parameters {
return new external_function_parameters([
'source' => new external_value(PARAM_RAW, 'Report class path'),
'context' => self::get_context_parameters(),
'component' => new external_value(PARAM_COMPONENT, 'Report component', VALUE_DEFAULT, ''),
'area' => new external_value(PARAM_AREA, 'Report area', VALUE_DEFAULT, ''),
'itemid' => new external_value(PARAM_INT, 'Report item ID', VALUE_DEFAULT, 0),
'parameters' => new external_multiple_structure(
new external_single_structure([
'name' => new external_value(PARAM_RAW),
'value' => new external_value(PARAM_RAW),
]),
'Report parameters', VALUE_DEFAULT, []
),
]);
}
/**
* External method execution
*
* @param string $source
* @param array $context
* @param string $component
* @param string $area
* @param int $itemid
* @param array[] $parameters
* @return bool
*/
public static function execute(
string $source,
array $context,
string $component = '',
string $area = '',
int $itemid = 0,
array $parameters = [],
): bool {
[
'source' => $source,
'context' => $context,
'component' => $component,
'area' => $area,
'itemid' => $itemid,
'parameters' => $parameters,
] = self::validate_parameters(self::execute_parameters(), [
'source' => $source,
'context' => $context,
'component' => $component,
'area' => $area,
'itemid' => $itemid,
'parameters' => $parameters,
]);
$context = self::get_context_from_params($context);
self::validate_context($context);
// Flatten the report parameters.
$parameters = array_combine(array_column($parameters, 'name'), array_column($parameters, 'value'));
try {
$report = system_report_factory::create($source, $context, $component, $area, $itemid, $parameters);
$report->require_can_view();
} catch (report_access_exception $exception) {
return false;
}
return true;
}
/**
* External method return value
*
* @return external_value
*/
public static function execute_returns(): external_value {
return new external_value(PARAM_BOOL);
}
}
@@ -0,0 +1,73 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
declare(strict_types=1);
namespace core_reportbuilder\external\systemreports;
use core\context\system;
use core_external\external_api;
use externallib_advanced_testcase;
use core_reportbuilder\local\systemreports\reports_list;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once("{$CFG->dirroot}/webservice/tests/helpers.php");
/**
* Unit tests of external class for validating access to a system report
*
* @package core_reportbuilder
* @covers \core_reportbuilder\external\systemreports\can_view
* @copyright 2023 Paul Holden <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class can_view_test extends externallib_advanced_testcase {
/**
* Text execute method
*/
public function test_execute(): void {
$this->resetAfterTest();
$this->setAdminUser();
$result = can_view::execute(reports_list::class, ['contextid' => system::instance()->id], '', '', 0, []);
$result = external_api::clean_returnvalue(can_view::execute_returns(), $result);
$this->assertTrue($result);
}
/**
* Test execute method for a user without permission to view report
*/
public function test_execute_access_none(): void {
global $DB;
$this->resetAfterTest();
$user = $this->getDataGenerator()->create_user();
$this->setUser($user);
$userrole = $DB->get_field('role', 'id', ['shortname' => 'user']);
unassign_capability('moodle/reportbuilder:view', $userrole, system::instance());
$result = can_view::execute(reports_list::class, ['contextid' => system::instance()->id], '', '', 0, []);
$result = external_api::clean_returnvalue(can_view::execute_returns(), $result);
$this->assertFalse($result);
}
}
+3 -1
View File
@@ -3,7 +3,9 @@ Information provided here is intended especially for developers.
=== 4.3 ===
* New external method `core_reportbuilder_retrieve_system_report` for retrieving system report data
* New external methods for retrieving system report data:
- `core_reportbuilder_can_view_system_report`
- `core_reportbuilder_retrieve_system_report`
* New `get_tag_joins_for_entity` helper in base entity class, for returning SQL joins necessary for retrieving tags
* New `set_is_deprecated` method in base `local\report\[column|filter]` classes to deprecate report entity columns and filters
* The following report entity columns have been deprecated, with replacements as follows:
+1 -1
View File
@@ -29,7 +29,7 @@
defined('MOODLE_INTERNAL') || die();
$version = 2023081400.00; // YYYYMMDD = weekly release date of this DEV branch.
$version = 2023081400.01; // YYYYMMDD = weekly release date of this DEV branch.
// RR = release increments - 00 in DEV branches.
// .XX = incremental changes.
$release = '4.3dev+ (Build: 20230814)'; // Human-friendly version name