mirror of
https://github.com/moodle/moodle.git
synced 2025-04-13 12:32:08 +02:00
Merge branch 'MDL-62134-master' of git://github.com/andrewnicols/moodle
This commit is contained in:
commit
0e156326cc
@ -91,6 +91,7 @@ abstract class expired_contexts_manager {
|
||||
}
|
||||
|
||||
$privacymanager = new \core_privacy\manager();
|
||||
$privacymanager->set_observer(new \tool_dataprivacy\manager_observer());
|
||||
|
||||
foreach ($this->get_context_levels() as $level) {
|
||||
|
||||
|
76
admin/tool/dataprivacy/classes/manager_observer.php
Normal file
76
admin/tool/dataprivacy/classes/manager_observer.php
Normal file
@ -0,0 +1,76 @@
|
||||
<?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/>.
|
||||
|
||||
/**
|
||||
* Class \tool_dataprivacy\manager
|
||||
*
|
||||
* @package tool_dataprivacy
|
||||
* @copyright 2018 Marina Glancy
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
namespace tool_dataprivacy;
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* A failure observer for the \core_privacy\manager.
|
||||
*
|
||||
* @package tool_dataprivacy
|
||||
* @copyright 2018 Marina Glancy
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class manager_observer implements \core_privacy\manager_observer {
|
||||
/**
|
||||
* Notifies all DPOs that an exception occurred.
|
||||
*
|
||||
* @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) {
|
||||
// Get the list of the site Data Protection Officers.
|
||||
$dpos = api::get_site_dpos();
|
||||
|
||||
$messagesubject = get_string('exceptionnotificationsubject', 'tool_dataprivacy');
|
||||
$a = (object)[
|
||||
'fullmethodname' => \core_privacy\manager::get_provider_classname_for_component($component) . '::' . $methodname,
|
||||
'component' => $component,
|
||||
'message' => $e->getMessage(),
|
||||
'backtrace' => $e->getTraceAsString()
|
||||
];
|
||||
$messagebody = get_string('exceptionnotificationbody', 'tool_dataprivacy', $a);
|
||||
|
||||
// Email the data request to the Data Protection Officer(s)/Admin(s).
|
||||
foreach ($dpos as $dpo) {
|
||||
$message = new \core\message\message();
|
||||
$message->courseid = SITEID;
|
||||
$message->component = 'tool_dataprivacy';
|
||||
$message->name = 'notifyexceptions';
|
||||
$message->userfrom = \core_user::get_noreply_user();
|
||||
$message->subject = $messagesubject;
|
||||
$message->fullmessageformat = FORMAT_HTML;
|
||||
$message->notification = 1;
|
||||
$message->userto = $dpo;
|
||||
$message->fullmessagehtml = $messagebody;
|
||||
$message->fullmessage = html_to_text($messagebody);
|
||||
|
||||
// Send message.
|
||||
message_send($message);
|
||||
}
|
||||
}
|
||||
}
|
@ -40,6 +40,8 @@ class metadata_registry {
|
||||
*/
|
||||
public function get_registry_metadata() {
|
||||
$manager = new \core_privacy\manager();
|
||||
$manager->set_observer(new \tool_dataprivacy\manager_observer());
|
||||
|
||||
$pluginman = \core_plugin_manager::instance();
|
||||
$contributedplugins = $this->get_contrib_list();
|
||||
$metadata = $manager->get_metadata_for_components();
|
||||
|
@ -30,6 +30,7 @@ use moodle_exception;
|
||||
use tool_dataprivacy\api;
|
||||
use tool_dataprivacy\contextlist_context;
|
||||
use tool_dataprivacy\data_request;
|
||||
use tool_dataprivacy\manager;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
@ -97,6 +98,8 @@ class initiate_data_request_task extends adhoc_task {
|
||||
|
||||
// Add the list of relevant contexts to the request, and mark all as pending approval.
|
||||
$privacymanager = new \core_privacy\manager();
|
||||
$privacymanager->set_observer(new \tool_dataprivacy\manager_observer());
|
||||
|
||||
$contextlistcollection = $privacymanager->get_contexts_for_userid($datarequest->get('userid'));
|
||||
api::add_request_contexts_with_status($contextlistcollection, $requestid, contextlist_context::STATUS_PENDING);
|
||||
|
||||
|
@ -88,6 +88,8 @@ class process_data_request_task extends adhoc_task {
|
||||
|
||||
// Export the data.
|
||||
$manager = new \core_privacy\manager();
|
||||
$manager->set_observer(new \tool_dataprivacy\manager_observer());
|
||||
|
||||
$exportedcontent = $manager->export_user_data($approvedclcollection);
|
||||
|
||||
$fs = get_file_storage();
|
||||
@ -110,6 +112,8 @@ class process_data_request_task extends adhoc_task {
|
||||
|
||||
// Delete the data.
|
||||
$manager = new \core_privacy\manager();
|
||||
$manager->set_observer(new \tool_dataprivacy\manager_observer());
|
||||
|
||||
$manager->delete_data_for_user($approvedclcollection);
|
||||
}
|
||||
|
||||
|
@ -41,4 +41,12 @@ $messageproviders = [
|
||||
'email' => MESSAGE_PERMITTED + MESSAGE_DEFAULT_LOGGEDIN + MESSAGE_DEFAULT_LOGGEDOFF,
|
||||
]
|
||||
],
|
||||
|
||||
// Notify Data Protection Officer about exceptions.
|
||||
'notifyexceptions' => [
|
||||
'defaults' => [
|
||||
'email' => MESSAGE_PERMITTED + MESSAGE_DEFAULT_LOGGEDIN + MESSAGE_DEFAULT_LOGGEDOFF,
|
||||
],
|
||||
'capability' => 'tool/dataprivacy:managedatarequests'
|
||||
],
|
||||
];
|
||||
|
@ -101,6 +101,8 @@ $string['errorrequestalreadyexists'] = 'You already have an ongoing request.';
|
||||
$string['errorrequestnotfound'] = 'Request not found';
|
||||
$string['errorrequestnotwaitingforapproval'] = 'The request is not awaiting approval. Either it is not yet ready or it has already been processed.';
|
||||
$string['errorsendingmessagetodpo'] = 'An error was encountered while trying to send a message to {$a}.';
|
||||
$string['exceptionnotificationsubject'] = "Exception occured while processing privacy data";
|
||||
$string['exceptionnotificationbody'] = "<p>Exception occured while calling <b>{\$a->fullmethodname}</b>.<br>This means that plugin <b>{\$a->component}</b> did not complete processing data. Below you can find exception information that can be passed to the plugin developer.</p><pre>{\$a->message}<br>\n\n{\$a->backtrace}</pre>";
|
||||
$string['expiredretentionperiodtask'] = 'Expired retention period';
|
||||
$string['expiry'] = 'Expiry';
|
||||
$string['expandplugin'] = 'Expand and collapse plugin.';
|
||||
@ -148,6 +150,7 @@ $string['lawfulbases'] = 'Lawful bases';
|
||||
$string['lawfulbases_help'] = 'Select at least one option that will serve as the lawful basis for processing personal data. For details on these lawful bases, please see <a href="https://gdpr-info.eu/art-6-gdpr/" target="_blank">GDPR Art. 6.1</a>';
|
||||
$string['messageprovider:contactdataprotectionofficer'] = 'Data requests';
|
||||
$string['messageprovider:datarequestprocessingresults'] = 'Data request processing results';
|
||||
$string['messageprovider:notifyexceptions'] = 'Data requests exceptions notifications';
|
||||
$string['message'] = 'Message';
|
||||
$string['messagelabel'] = 'Message:';
|
||||
$string['moduleinstancename'] = '{$a->instancename} ({$a->modulename})';
|
||||
|
117
admin/tool/dataprivacy/tests/manager_observer_test.php
Normal file
117
admin/tool/dataprivacy/tests/manager_observer_test.php
Normal file
@ -0,0 +1,117 @@
|
||||
<?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/>.
|
||||
|
||||
/**
|
||||
* Tests for the manager observer.
|
||||
*
|
||||
* @package tool_dataprivacy
|
||||
* @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* API tests.
|
||||
*
|
||||
* @package tool_dataprivacy
|
||||
* @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class tool_dataprivacy_manager_observer_testcase extends advanced_testcase {
|
||||
|
||||
/**
|
||||
* Helper to set andn return two users who are DPOs.
|
||||
*/
|
||||
protected function setup_site_dpos() {
|
||||
global $DB;
|
||||
$this->resetAfterTest();
|
||||
|
||||
$generator = new testing_data_generator();
|
||||
$u1 = $this->getDataGenerator()->create_user();
|
||||
$u2 = $this->getDataGenerator()->create_user();
|
||||
|
||||
$context = context_system::instance();
|
||||
|
||||
// Give the manager role with the capability to manage data requests.
|
||||
$managerroleid = $DB->get_field('role', 'id', array('shortname' => 'manager'));
|
||||
assign_capability('tool/dataprivacy:managedatarequests', CAP_ALLOW, $managerroleid, $context->id, true);
|
||||
|
||||
// Assign both users as manager.
|
||||
role_assign($managerroleid, $u1->id, $context->id);
|
||||
role_assign($managerroleid, $u2->id, $context->id);
|
||||
|
||||
// Only map the manager role to the DPO role.
|
||||
set_config('dporoles', $managerroleid, 'tool_dataprivacy');
|
||||
|
||||
return \tool_dataprivacy\api::get_site_dpos();
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure that when users are configured as DPO, they are sent an message upon failure.
|
||||
*/
|
||||
public function test_handle_component_failure() {
|
||||
$this->resetAfterTest();
|
||||
|
||||
// Create another user who is not a DPO.
|
||||
$this->getDataGenerator()->create_user();
|
||||
|
||||
// Create the DPOs.
|
||||
$dpos = $this->setup_site_dpos();
|
||||
|
||||
$observer = new \tool_dataprivacy\manager_observer();
|
||||
|
||||
// Handle the failure, catching messages.
|
||||
$mailsink = $this->redirectMessages();
|
||||
$mailsink->clear();
|
||||
$observer->handle_component_failure(new \Exception('error'), 'foo', 'bar', 'baz', ['foobarbaz', 'bum']);
|
||||
|
||||
// Messages should be sent to both DPOs only.
|
||||
$this->assertEquals(2, $mailsink->count());
|
||||
|
||||
$messages = $mailsink->get_messages();
|
||||
$messageusers = array_map(function($message) {
|
||||
return $message->useridto;
|
||||
}, $messages);
|
||||
|
||||
$this->assertEquals(array_keys($dpos), $messageusers, '', 0.0, 0, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure that when no user is configured as DPO, the message is sent to admin instead.
|
||||
*/
|
||||
public function test_handle_component_failure_no_dpo() {
|
||||
$this->resetAfterTest();
|
||||
|
||||
// Create another user who is not a DPO or admin.
|
||||
$this->getDataGenerator()->create_user();
|
||||
|
||||
$observer = new \tool_dataprivacy\manager_observer();
|
||||
|
||||
$mailsink = $this->redirectMessages();
|
||||
$mailsink->clear();
|
||||
$observer->handle_component_failure(new \Exception('error'), 'foo', 'bar', 'baz', ['foobarbaz', 'bum']);
|
||||
|
||||
// Messages should have been sent only to the admin.
|
||||
$this->assertEquals(1, $mailsink->count());
|
||||
|
||||
$messages = $mailsink->get_messages();
|
||||
$message = reset($messages);
|
||||
|
||||
$admin = \core_user::get_user_by_username('admin');
|
||||
$this->assertEquals($admin->id, $message->useridto);
|
||||
}
|
||||
}
|
@ -24,6 +24,6 @@
|
||||
|
||||
defined('MOODLE_INTERNAL') || die;
|
||||
|
||||
$plugin->version = 2018051400;
|
||||
$plugin->version = 2018051401;
|
||||
$plugin->requires = 2018050800; // Moodle 3.5dev (Build 2018031600) and upwards.
|
||||
$plugin->component = 'tool_dataprivacy';
|
||||
|
@ -23,7 +23,13 @@
|
||||
*/
|
||||
namespace core_privacy;
|
||||
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_user_data_provider;
|
||||
use core_privacy\local\request\data_provider;
|
||||
use core_privacy\local\request\user_preference_provider;
|
||||
use \core_privacy\local\metadata\provider as metadata_provider;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
@ -103,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.
|
||||
@ -120,7 +141,7 @@ class manager {
|
||||
*/
|
||||
public function component_is_compliant(string $component) : bool {
|
||||
// Components which don't store user data need only implement the null_provider.
|
||||
if ($this->component_implements($component, \core_privacy\local\metadata\null_provider::class)) {
|
||||
if ($this->component_implements($component, null_provider::class)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -129,8 +150,8 @@ class manager {
|
||||
}
|
||||
|
||||
// Components which store user data must implement the local\metadata\provider and the local\request\data_provider.
|
||||
if ($this->component_implements($component, \core_privacy\local\metadata\provider::class) &&
|
||||
$this->component_implements($component, \core_privacy\local\request\data_provider::class)) {
|
||||
if ($this->component_implements($component, metadata_provider::class) &&
|
||||
$this->component_implements($component, data_provider::class)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -144,8 +165,9 @@ class manager {
|
||||
* @return string The key to retrieve the language string for the null provider reason.
|
||||
*/
|
||||
public function get_null_provider_reason(string $component) : string {
|
||||
if ($this->component_implements($component, \core_privacy\local\metadata\null_provider::class)) {
|
||||
return $this->get_provider_classname($component)::get_reason();
|
||||
if ($this->component_implements($component, null_provider::class)) {
|
||||
$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.');
|
||||
}
|
||||
@ -177,8 +199,10 @@ class manager {
|
||||
// Get the metadata, and put into an assoc array indexed by component name.
|
||||
$metadata = [];
|
||||
foreach ($this->get_component_list() as $component) {
|
||||
if ($this->component_implements($component, \core_privacy\local\metadata\provider::class)) {
|
||||
$metadata[$component] = $this->get_provider_classname($component)::get_metadata(new collection($component));
|
||||
$componentmetadata = $this->handled_component_class_callback($component, metadata_provider::class,
|
||||
'get_metadata', [new collection($component)]);
|
||||
if ($componentmetadata !== null) {
|
||||
$metadata[$component] = $componentmetadata;
|
||||
}
|
||||
}
|
||||
return $metadata;
|
||||
@ -187,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.
|
||||
*/
|
||||
@ -208,9 +233,9 @@ class manager {
|
||||
$a->progress++;
|
||||
$a->datetime = userdate(time());
|
||||
$progress->output(get_string('trace:processingcomponent', 'core_privacy', $a), 2);
|
||||
if ($this->component_implements($component, \core_privacy\local\request\core_user_data_provider::class)) {
|
||||
$contextlist = $this->get_provider_classname($component)::get_contexts_for_userid($userid);
|
||||
} else {
|
||||
$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();
|
||||
}
|
||||
|
||||
@ -265,13 +290,14 @@ class manager {
|
||||
$progress->output(get_string('trace:processingcomponent', 'core_privacy', $a), 2);
|
||||
|
||||
// Core user data providers.
|
||||
if ($this->component_implements($component, \core_privacy\local\request\core_user_data_provider::class)) {
|
||||
if ($this->component_implements($component, core_user_data_provider::class)) {
|
||||
if (count($approvedcontextlist)) {
|
||||
// This plugin has data it knows about. It is responsible for storing basic data about anything it is
|
||||
// told to export.
|
||||
$this->get_provider_classname($component)::export_user_data($approvedcontextlist);
|
||||
$this->handled_component_class_callback($component, core_user_data_provider::class,
|
||||
'export_user_data', [$approvedcontextlist]);
|
||||
}
|
||||
} else if (!$this->component_implements($component, \core_privacy\local\request\context_aware_provider::class)) {
|
||||
} else if (!$this->component_implements($component, context_aware_provider::class)) {
|
||||
// This plugin does not know that it has data - export the shared data it doesn't know about.
|
||||
local\request\helper::export_data_for_null_provider($approvedcontextlist);
|
||||
}
|
||||
@ -290,15 +316,13 @@ class manager {
|
||||
$a->datetime = userdate(time());
|
||||
$progress->output(get_string('trace:processingcomponent', 'core_privacy', $a), 2);
|
||||
// Core user preference providers.
|
||||
if ($this->component_implements($component, \core_privacy\local\request\user_preference_provider::class)) {
|
||||
$this->get_provider_classname($component)::export_user_preferences($contextlistcollection->get_userid());
|
||||
}
|
||||
$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.
|
||||
if ($this->component_implements($component, \core_privacy\local\request\context_aware_provider::class)) {
|
||||
$this->get_provider_classname($component)::export_context_data($contextlistcollection);
|
||||
}
|
||||
$this->handled_component_class_callback($component, context_aware_provider::class,
|
||||
'export_context_data', [$contextlistcollection]);
|
||||
}
|
||||
$progress->output(get_string('trace:done', 'core_privacy'), 1);
|
||||
|
||||
@ -343,12 +367,11 @@ class manager {
|
||||
$a->datetime = userdate(time());
|
||||
$progress->output(get_string('trace:processingcomponent', 'core_privacy', $a), 2);
|
||||
|
||||
if ($this->component_is_core_provider($component)) {
|
||||
if (count($approvedcontextlist)) {
|
||||
// The component knows about data that it has.
|
||||
// Have it delete its own data.
|
||||
$this->get_provider_classname($approvedcontextlist->get_component())::delete_data_for_user($approvedcontextlist);
|
||||
}
|
||||
if (count($approvedcontextlist)) {
|
||||
// The component knows about data that it has.
|
||||
// Have it delete its own data.
|
||||
$this->handled_component_class_callback($approvedcontextlist->get_component(), core_user_data_provider::class,
|
||||
'delete_data_for_user', [$approvedcontextlist]);
|
||||
}
|
||||
|
||||
// Delete any shared user data it doesn't know about.
|
||||
@ -360,7 +383,7 @@ class manager {
|
||||
/**
|
||||
* Delete all use data which matches the specified deletion criteria.
|
||||
*
|
||||
* @param context $context The specific context to delete data for.
|
||||
* @param \context $context The specific context to delete data for.
|
||||
*/
|
||||
public function delete_data_for_all_users_in_context(\context $context) {
|
||||
$progress = static::get_log_tracer();
|
||||
@ -380,11 +403,10 @@ class manager {
|
||||
$a->datetime = userdate(time());
|
||||
$progress->output(get_string('trace:processingcomponent', 'core_privacy', $a), 2);
|
||||
|
||||
if ($this->component_implements($component, \core_privacy\local\request\core_user_data_provider::class)) {
|
||||
// This component knows about specific data that it owns.
|
||||
// Have it delete all of that user data for the context.
|
||||
$this->get_provider_classname($component)::delete_data_for_all_users_in_context($context);
|
||||
}
|
||||
// If this component knows about specific data that it owns,
|
||||
// have it delete all of that user data for the context.
|
||||
$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.
|
||||
local\request\helper::delete_data_for_all_users_in_context($component, $context);
|
||||
@ -392,16 +414,6 @@ class manager {
|
||||
$progress->output(get_string('trace:done', 'core_privacy'), 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether the specified component is a core provider.
|
||||
*
|
||||
* @param string $component the frankenstyle component name.
|
||||
* @return bool true if the component is a core provider, false otherwise.
|
||||
*/
|
||||
protected function component_is_core_provider($component) {
|
||||
return $this->component_implements($component, \core_privacy\local\request\core_data_provider::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of frankenstyle names of core components (plugins and subsystems).
|
||||
*
|
||||
@ -433,7 +445,7 @@ class manager {
|
||||
* @return string the fully qualified provider classname.
|
||||
*/
|
||||
public static function get_provider_classname_for_component(string $component) {
|
||||
return "$component\privacy\provider";
|
||||
return "$component\\privacy\\provider";
|
||||
}
|
||||
|
||||
/**
|
||||
@ -500,4 +512,41 @@ 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(string $component, string $interface, string $methodname, array $params) {
|
||||
try {
|
||||
return static::component_class_callback($component, $interface, $methodname, $params);
|
||||
} catch (\Throwable $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(\Throwable $e, string $component, string $interface,
|
||||
string $methodname, array $params) {
|
||||
if ($this->observer) {
|
||||
call_user_func_array([$this->observer, 'handle_component_failure'], func_get_args());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
47
privacy/classes/manager_observer.php
Normal file
47
privacy/classes/manager_observer.php
Normal 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 <andrew@nicols.co.uk>
|
||||
* @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 <andrew@nicols.co.uk>
|
||||
* @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
privacy/tests/fixtures/provider_a.php
vendored
Normal file
91
privacy/tests/fixtures/provider_a.php
vendored
Normal 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 <andrew@nicols.co.uk>
|
||||
* @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 <andrew@nicols.co.uk>
|
||||
* @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 {
|
||||
$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(int $userid) : \core_privacy\local\request\contextlist {
|
||||
$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
privacy/tests/fixtures/provider_throwing_exception.php
vendored
Normal file
88
privacy/tests/fixtures/provider_throwing_exception.php
vendored
Normal 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 <andrew@nicols.co.uk>
|
||||
* @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 <andrew@nicols.co.uk>
|
||||
* @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 {
|
||||
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(int $userid) : \core_privacy\local\request\contextlist {
|
||||
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__);
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user