MDL-62134 core_privacy: Allow for a failure handler

This commit is contained in:
Andrew Nicols
2018-05-16 11:52:46 +08:00
parent 691e92efa7
commit 60f6712ce1
6 changed files with 458 additions and 12 deletions
+66 -9
View File
@@ -26,7 +26,6 @@ use core_privacy\local\metadata\collection;
use core_privacy\local\metadata\null_provider;
use core_privacy\local\request\context_aware_provider;
use core_privacy\local\request\contextlist_collection;
use core_privacy\local\request\core_data_provider;
use core_privacy\local\request\core_user_data_provider;
use core_privacy\local\request\data_provider;
use core_privacy\local\request\user_preference_provider;
@@ -110,6 +109,21 @@ defined('MOODLE_INTERNAL') || die();
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class manager {
/**
* @var manager_observer Observer.
*/
protected $observer;
/**
* Set the failure handler.
*
* @param manager_observer $observer
*/
public function set_observer(manager_observer $observer) {
$this->observer = $observer;
}
/**
* Checks whether the given component is compliant with the core_privacy API.
* To be considered compliant, a component must declare whether (and where) it stores personal data.
@@ -152,7 +166,8 @@ class manager {
*/
public function get_null_provider_reason($component) {
if ($this->component_implements($component, null_provider::class)) {
return static::component_class_callback($component, null_provider::class, 'get_reason', []);
$reason = $this->handled_component_class_callback($component, null_provider::class, 'get_reason', []);
return empty($reason) ? 'privacy:reason' : $reason;
} else {
throw new \coding_exception('Call to undefined method', 'Please only call this method on a null provider.');
}
@@ -184,7 +199,7 @@ class manager {
// Get the metadata, and put into an assoc array indexed by component name.
$metadata = [];
foreach ($this->get_component_list() as $component) {
$componentmetadata = static::component_class_callback($component, metadata_provider::class,
$componentmetadata = $this->handled_component_class_callback($component, metadata_provider::class,
'get_metadata', [new collection($component)]);
if ($componentmetadata !== null) {
$metadata[$component] = $componentmetadata;
@@ -196,6 +211,7 @@ class manager {
/**
* Gets a collection of resultset objects for all components.
*
*
* @param int $userid the id of the user we're fetching contexts for.
* @return contextlist_collection the collection of contextlist items for the respective components.
*/
@@ -217,7 +233,7 @@ class manager {
$a->progress++;
$a->datetime = userdate(time());
$progress->output(get_string('trace:processingcomponent', 'core_privacy', $a), 2);
$contextlist = static::component_class_callback($component, core_user_data_provider::class,
$contextlist = $this->handled_component_class_callback($component, core_user_data_provider::class,
'get_contexts_for_userid', [$userid]);
if ($contextlist === null) {
$contextlist = new local\request\contextlist();
@@ -278,7 +294,7 @@ class manager {
if (count($approvedcontextlist)) {
// This plugin has data it knows about. It is responsible for storing basic data about anything it is
// told to export.
static::component_class_callback($component, core_user_data_provider::class,
$this->handled_component_class_callback($component, core_user_data_provider::class,
'export_user_data', [$approvedcontextlist]);
}
} else if (!$this->component_implements($component, context_aware_provider::class)) {
@@ -300,12 +316,12 @@ class manager {
$a->datetime = userdate(time());
$progress->output(get_string('trace:processingcomponent', 'core_privacy', $a), 2);
// Core user preference providers.
static::component_class_callback($component, user_preference_provider::class,
$this->handled_component_class_callback($component, user_preference_provider::class,
'export_user_preferences', [$contextlistcollection->get_userid()]);
// Contextual information providers. Give each component a chance to include context information based on the
// existence of a child context in the contextlist_collection.
static::component_class_callback($component, context_aware_provider::class,
$this->handled_component_class_callback($component, context_aware_provider::class,
'export_context_data', [$contextlistcollection]);
}
$progress->output(get_string('trace:done', 'core_privacy'), 1);
@@ -354,7 +370,7 @@ class manager {
if (count($approvedcontextlist)) {
// The component knows about data that it has.
// Have it delete its own data.
static::component_class_callback($approvedcontextlist->get_component(), core_user_data_provider::class,
$this->handled_component_class_callback($approvedcontextlist->get_component(), core_user_data_provider::class,
'delete_data_for_user', [$approvedcontextlist]);
}
@@ -389,7 +405,7 @@ class manager {
// If this component knows about specific data that it owns,
// have it delete all of that user data for the context.
static::component_class_callback($component, core_user_data_provider::class,
$this->handled_component_class_callback($component, core_user_data_provider::class,
'delete_data_for_all_users_in_context', [$context]);
// Delete any shared user data it doesn't know about.
@@ -496,4 +512,45 @@ class manager {
return new \text_progress_trace();
}
/**
* Call the named method with the specified params on the supplied component if it implements the relevant interface
* on its provider.
*
* @param string $component The component to call
* @param string $interface The interface to implement
* @param string $methodname The method to call
* @param array $params The params to call
* @return mixed
*/
protected function handled_component_class_callback($component, $interface, $methodname, array $params) {
try {
return static::component_class_callback($component, $interface, $methodname, $params);
} catch (\Exception $e) {
debugging($e->getMessage(), DEBUG_DEVELOPER, $e->getTrace());
$this->component_class_callback_failed($e, $component, $interface, $methodname, $params);
return null;
} catch (\Error $e) {
debugging($e->getMessage(), DEBUG_DEVELOPER, $e->getTrace());
$this->component_class_callback_failed($e, $component, $interface, $methodname, $params);
return null;
}
}
/**
* Notifies the observer of any failure.
*
* @param \Throwable $e
* @param string $component
* @param string $interface
* @param string $methodname
* @param array $params
*/
protected function component_class_callback_failed($e, $component, $interface, $methodname, array $params) {
if ($this->observer) {
call_user_func_array([$this->observer, 'handle_component_failure'], func_get_args());
}
}
}
+47
View File
@@ -0,0 +1,47 @@
<?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/>.
/**
* This file contains the interface required to observe failures in the manager.
*
* @package core_privacy
* @copyright 2018 Andrew Nicols <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_privacy;
defined('MOODLE_INTERNAL') || die();
/**
* The interface for a Manager observer.
*
* @package core_privacy
* @copyright 2018 Andrew Nicols <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
interface manager_observer {
/**
* Handle failure of a component.
*
* @param \Throwable $e
* @param string $component
* @param string $interface
* @param string $methodname
* @param array $params
*/
public function handle_component_failure($e, $component, $interface, $methodname, array $params);
}
+91
View File
@@ -0,0 +1,91 @@
<?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/>.
/**
* Test provider which works.
*
* @package core_privacy
* @copyright 2018 Andrew Nicols <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_component_a\privacy;
defined('MOODLE_INTERNAL') || die();
use core_privacy\local\metadata\collection;
use core_privacy\local\request\contextlist;
use core_privacy\local\request\approved_contextlist;
/**
* Mock core_user_data_provider for unit tests.
*
* @package core_privacy
* @copyright 2018 Andrew Nicols <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class provider implements
\core_privacy\local\metadata\provider,
\core_privacy\local\request\plugin\provider {
/**
* Get metadata.
*
* @param collection $collection The initialised collection to add items to.
* @return collection A listing of user data stored through this system.
*/
public static function get_metadata(collection $collection) {
$collection->add_subsystem_link('core_files');
return $collection;
}
/**
* Get the list of contexts that contain user information for the specified user.
*
* @param int $userid The user to search.
* @return contextlist $contextlist The contextlist containing the list of contexts used in this plugin.
*/
public static function get_contexts_for_userid($userid) {
$c = new \core_privacy\local\request\contextlist();
$c->add_system_context();
return $c;
}
/**
* Export all user data for the specified user, in the specified contexts.
*
* @param approved_contextlist $contextlist The approved contexts to export information for.
*/
public static function export_user_data(approved_contextlist $contextlist) {
}
/**
* Delete all data for all users in the specified context.
*
* @param context $context The specific context to delete data for.
*/
public static function delete_data_for_all_users_in_context(\context $context) {
}
/**
* Delete all user data for the specified user, in the specified contexts.
*
* @param approved_contextlist $contextlist The approved contexts and user information to delete information for.
*/
public static function delete_data_for_user(approved_contextlist $contextlist) {
}
}
+88
View File
@@ -0,0 +1,88 @@
<?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/>.
/**
* Test provider which has issues.
*
* @package core_privacy
* @copyright 2018 Andrew Nicols <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_component_broken\privacy;
use core_privacy\local\metadata\collection;
use core_privacy\local\request\contextlist;
use core_privacy\local\request\approved_contextlist;
/**
* Mock core_user_data_provider for unit tests.
*
* @package core_privacy
* @copyright 2018 Andrew Nicols <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class provider implements
\core_privacy\local\metadata\provider,
\core_privacy\local\request\plugin\provider {
/**
* Get metadata.
*
* @param collection $collection The initialised collection to add items to.
* @return collection A listing of user data stored through this system.
*/
public static function get_metadata(collection $collection) {
throw new \coding_exception(__FUNCTION__);
}
/**
* Get the list of contexts that contain user information for the specified user.
*
* @param int $userid The user to search.
* @return contextlist $contextlist The contextlist containing the list of contexts used in this plugin.
*/
public static function get_contexts_for_userid($userid) {
throw new \coding_exception(__FUNCTION__);
}
/**
* Export all user data for the specified user, in the specified contexts.
*
* @param approved_contextlist $contextlist The approved contexts to export information for.
*/
public static function export_user_data(approved_contextlist $contextlist) {
throw new \coding_exception(__FUNCTION__);
}
/**
* Delete all data for all users in the specified context.
*
* @param context $context The specific context to delete data for.
*/
public static function delete_data_for_all_users_in_context(\context $context) {
throw new \coding_exception(__FUNCTION__);
}
/**
* Delete all user data for the specified user, in the specified contexts.
*
* @param approved_contextlist $contextlist The approved contexts and user information to delete information for.
*/
public static function delete_data_for_user(approved_contextlist $contextlist) {
throw new \coding_exception(__FUNCTION__);
}
}
+165 -2
View File
@@ -28,8 +28,11 @@ require_once($CFG->dirroot . '/privacy/tests/fixtures/mock_null_provider.php');
require_once($CFG->dirroot . '/privacy/tests/fixtures/mock_provider.php');
require_once($CFG->dirroot . '/privacy/tests/fixtures/mock_plugin_subplugin_provider.php');
require_once($CFG->dirroot . '/privacy/tests/fixtures/mock_mod_with_user_data_provider.php');
require_once($CFG->dirroot . '/privacy/tests/fixtures/provider_a.php');
require_once($CFG->dirroot . '/privacy/tests/fixtures/provider_throwing_exception.php');
use \core_privacy\local\request\writer;
use \core_privacy\local\request\approved_contextlist;
/**
* Privacy manager unit tests.
@@ -175,7 +178,7 @@ class privacy_manager_testcase extends advanced_testcase {
// Create an approved contextlist.
$approvedcontextlistcollection = new \core_privacy\local\request\contextlist_collection(10);
foreach ($contextlistcollection->get_contextlists() as $contextlist) {
$approvedcontextlist = new \core_privacy\local\request\approved_contextlist(new stdClass(), $contextlist->get_component(),
$approvedcontextlist = new approved_contextlist(new stdClass(), $contextlist->get_component(),
$contextlist->get_contextids());
$approvedcontextlistcollection->add_contextlist($approvedcontextlist);
}
@@ -210,7 +213,7 @@ class privacy_manager_testcase extends advanced_testcase {
// Create an approved contextlist.
$approvedcontextlistcollection = new \core_privacy\local\request\contextlist_collection($user->id);
foreach ($contextlistcollection->get_contextlists() as $contextlist) {
$approvedcontextlist = new \core_privacy\local\request\approved_contextlist($user, $contextlist->get_component(),
$approvedcontextlist = new approved_contextlist($user, $contextlist->get_component(),
$contextlist->get_contextids());
$approvedcontextlistcollection->add_contextlist($approvedcontextlist);
}
@@ -300,4 +303,164 @@ class privacy_manager_testcase extends advanced_testcase {
],
];
}
/**
* Test that get_contexts_for_userid() with a failing item.
*/
public function test_get_contexts_for_userid_with_failing() {
// Get a mock manager, in which the core components list is mocked to include all mock plugins.
// testcomponent is a core provider, testcomponent2 isa null provider, testcomponent3 is subplugin provider (non core).
$mockman = $this->get_mock_manager_with_core_components(['mod_component_broken', 'mod_component_a']);
$observer = $this->getMockBuilder(\core_privacy\manager_observer::class)
->setMethods(['handle_component_failure'])
->getMock();
$mockman->set_observer($observer);
$observer->expects($this->once())
->method('handle_component_failure')
->with(
$this->isInstanceOf(\coding_exception::class),
$this->identicalTo('mod_component_broken'),
$this->identicalTo(\core_privacy\local\request\core_user_data_provider::class),
$this->identicalTo('get_contexts_for_userid'),
$this->anything()
);
// Get the contextlist_collection.
$contextlistcollection = $mockman->get_contexts_for_userid(10);
$this->assertDebuggingCalled();
$this->assertInstanceOf(\core_privacy\local\request\contextlist_collection::class, $contextlistcollection);
$this->assertCount(1, $contextlistcollection);
// The component which completed shoudl have returned a contextlist.
$this->assertInstanceOf(\core_privacy\local\request\contextlist::class,
$contextlistcollection->get_contextlist_for_component('mod_component_a'));
$this->assertEmpty($contextlistcollection->get_contextlist_for_component('mod_component_broken'));
}
/**
* Test that export_user_data() with a failing item.
*/
public function test_export_user_data_with_failing() {
$user = \core_user::get_user_by_username('admin');
$mockman = $this->get_mock_manager_with_core_components(['mod_component_broken', 'mod_component_a']);
$context = \context_system::instance();
$contextid = $context->id;
$observer = $this->getMockBuilder(\core_privacy\manager_observer::class)
->setMethods(['handle_component_failure'])
->getMock();
$mockman->set_observer($observer);
$observer->expects($this->once())
->method('handle_component_failure')
->with(
$this->isInstanceOf(\coding_exception::class),
$this->identicalTo('mod_component_broken'),
$this->identicalTo(\core_privacy\local\request\core_user_data_provider::class),
$this->identicalTo('export_user_data'),
$this->anything()
);
$collection = new \core_privacy\local\request\contextlist_collection(10);
$collection->add_contextlist(new approved_contextlist($user, 'mod_component_broken', [$contextid]));
$collection->add_contextlist(new approved_contextlist($user, 'mod_component_a', [$contextid]));
// Get the contextlist_collection.
$mockman->export_user_data($collection);
$this->assertDebuggingCalled();
}
/**
* Test that delete_data_for_user() with a failing item.
*/
public function test_delete_data_for_user_with_failing() {
$user = \core_user::get_user_by_username('admin');
$mockman = $this->get_mock_manager_with_core_components(['mod_component_broken', 'mod_component_a']);
$context = \context_system::instance();
$contextid = $context->id;
$observer = $this->getMockBuilder(\core_privacy\manager_observer::class)
->setMethods(['handle_component_failure'])
->getMock();
$mockman->set_observer($observer);
$observer->expects($this->once())
->method('handle_component_failure')
->with(
$this->isInstanceOf(\coding_exception::class),
$this->identicalTo('mod_component_broken'),
$this->identicalTo(\core_privacy\local\request\core_user_data_provider::class),
$this->identicalTo('delete_data_for_user'),
$this->anything()
);
$collection = new \core_privacy\local\request\contextlist_collection(10);
$collection->add_contextlist(new approved_contextlist($user, 'mod_component_broken', [$contextid]));
$collection->add_contextlist(new approved_contextlist($user, 'mod_component_a', [$contextid]));
// Get the contextlist_collection.
$mockman->delete_data_for_user($collection);
$this->assertDebuggingCalled();
}
/**
* Test that delete_data_for_all_users_in_context() with a failing item.
*/
public function test_delete_data_for_all_users_in_context_with_failing() {
$user = \core_user::get_user_by_username('admin');
$mockman = $this->get_mock_manager_with_core_components(['mod_component_broken', 'mod_component_a']);
$context = \context_system::instance();
$observer = $this->getMockBuilder(\core_privacy\manager_observer::class)
->setMethods(['handle_component_failure'])
->getMock();
$mockman->set_observer($observer);
$observer->expects($this->once())
->method('handle_component_failure')
->with(
$this->isInstanceOf(\coding_exception::class),
$this->identicalTo('mod_component_broken'),
$this->identicalTo(\core_privacy\local\request\core_user_data_provider::class),
$this->identicalTo('delete_data_for_all_users_in_context'),
$this->anything()
);
// Get the contextlist_collection.
$mockman->delete_data_for_all_users_in_context($context);
$this->assertDebuggingCalled();
}
/**
* Test that get_metadata_for_components() with a failing item.
*/
public function test_get_metadata_for_components_with_failing() {
$user = \core_user::get_user_by_username('admin');
$mockman = $this->get_mock_manager_with_core_components(['mod_component_broken', 'mod_component_a']);
$context = \context_system::instance();
$observer = $this->getMockBuilder(\core_privacy\manager_observer::class)
->setMethods(['handle_component_failure'])
->getMock();
$mockman->set_observer($observer);
$observer->expects($this->once())
->method('handle_component_failure')
->with(
$this->isInstanceOf(\coding_exception::class),
$this->identicalTo('mod_component_broken'),
$this->identicalTo(\core_privacy\local\metadata\provider::class),
$this->identicalTo('get_metadata'),
$this->anything()
);
// Get the contextlist_collection.
$metadata = $mockman->get_metadata_for_components();
$this->assertDebuggingCalled();
$this->assertInternalType('array', $metadata);
$this->assertCount(1, $metadata);
}
}
+1 -1
View File
@@ -29,7 +29,7 @@
defined('MOODLE_INTERNAL') || die();
$version = 2017051505.13; // 20170515 = branching date YYYYMMDD - do not modify!
$version = 2017051505.14; // 20170515 = branching date YYYYMMDD - do not modify!
// RR = release increments - 00 in DEV branches.
// .XX = incremental changes.