Merge branch 'MDL-62371-master' of git://github.com/andrewnicols/moodle

This commit is contained in:
Eloy Lafuente (stronk7) 2018-05-14 02:37:38 +02:00
commit 24bd0bc3b3
2 changed files with 109 additions and 4 deletions

View File

@ -23,3 +23,12 @@
*/
$string['privacy:metadata'] = 'The privacy subsystem does not store any data of its own and is designed to act as a channel between components and the interface used to describe, export, and remove their data.';
$string['trace:done'] = 'Complete';
$string['trace:exportcomplete'] = 'Export complete';
$string['trace:exportingapproved'] = 'Performing primary export for {$a->total} components ({$a->datetime})';
$string['trace:exportingrelated'] = 'Performing related export for {$a->total} components ({$a->datetime})';
$string['trace:finalisingexport'] = 'Finalising export';
$string['trace:processingcomponent'] = 'Processing {$a->component} ({$a->progress}/{$a->total}) ({$a->datetime})';
$string['trace:fetchcomponents'] = 'Fetching {$a->total} components ({$a->datetime})';
$string['trace:deletingapproved'] = 'Performing removal of approved {$a->total} contexts ({$a->datetime})';
$string['trace:deletingcontext'] = 'Performing removal of context from {$a->total} components ({$a->datetime})';

View File

@ -168,8 +168,23 @@ class manager {
* @return contextlist_collection the collection of contextlist items for the respective components.
*/
public function get_contexts_for_userid(int $userid) : contextlist_collection {
$progress = static::get_log_tracer();
$components = $this->get_component_list();
$a = (object) [
'total' => count($components),
'progress' => 0,
'component' => '',
'datetime' => userdate(time()),
];
$clcollection = new contextlist_collection($userid);
foreach ($this->get_component_list() as $component) {
$progress->output(get_string('trace:fetchcomponents', 'core_privacy', $a), 1);
foreach ($components as $component) {
$a->component = $component;
$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 {
@ -187,6 +202,7 @@ class manager {
$clcollection->add_contextlist($contextlist);
}
}
$progress->output(get_string('trace:done', 'core_privacy'), 1);
return $clcollection;
}
@ -202,13 +218,29 @@ class manager {
* approved_contextlists' components is not a core_data_provider.
*/
public function export_user_data(contextlist_collection $contextlistcollection) {
$progress = static::get_log_tracer();
$a = (object) [
'total' => count($contextlistcollection),
'progress' => 0,
'component' => '',
'datetime' => userdate(time()),
];
// Export for the various components/contexts.
$progress->output(get_string('trace:exportingapproved', 'core_privacy', $a), 1);
foreach ($contextlistcollection as $approvedcontextlist) {
if (!$approvedcontextlist instanceof \core_privacy\local\request\approved_contextlist) {
throw new \moodle_exception('Contextlist must be an approved_contextlist');
}
$component = $approvedcontextlist->get_component();
$a->component = $component;
$a->progress++;
$a->datetime = userdate(time());
$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 (count($approvedcontextlist)) {
@ -221,9 +253,19 @@ class manager {
local\request\helper::export_data_for_null_provider($approvedcontextlist);
}
}
$progress->output(get_string('trace:done', 'core_privacy'), 1);
// Check each component for non contextlist items too.
foreach ($this->get_component_list() as $component) {
$components = $this->get_component_list();
$a->total = count($components);
$a->progress = 0;
$a->datetime = userdate(time());
$progress->output(get_string('trace:exportingrelated', 'core_privacy', $a), 1);
foreach ($components as $component) {
$a->component = $component;
$a->progress++;
$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());
@ -235,8 +277,13 @@ class manager {
$this->get_provider_classname($component)::export_context_data($contextlistcollection);
}
}
$progress->output(get_string('trace:done', 'core_privacy'), 1);
return local\request\writer::with_context(\context_system::instance())->finalise_content();
$progress->output(get_string('trace:finalisingexport', 'core_privacy'), 1);
$location = local\request\writer::with_context(\context_system::instance())->finalise_content();
$progress->output(get_string('trace:exportcomplete', 'core_privacy'), 1);
return $location;
}
/**
@ -251,13 +298,29 @@ class manager {
* for an approved_contextlist isn't a core provider.
*/
public function delete_data_for_user(contextlist_collection $contextlistcollection) {
$progress = static::get_log_tracer();
$a = (object) [
'total' => count($contextlistcollection),
'progress' => 0,
'component' => '',
'datetime' => userdate(time()),
];
// Delete the data.
$progress->output(get_string('trace:deletingapproved', 'core_privacy', $a), 1);
foreach ($contextlistcollection as $approvedcontextlist) {
if (!$approvedcontextlist instanceof \core_privacy\local\request\approved_contextlist) {
throw new \moodle_exception('Contextlist must be an approved_contextlist');
}
if ($this->component_is_core_provider($approvedcontextlist->get_component())) {
$component = $approvedcontextlist->get_component();
$a->component = $component;
$a->progress++;
$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.
@ -268,6 +331,7 @@ class manager {
// Delete any shared user data it doesn't know about.
local\request\helper::delete_data_for_user($approvedcontextlist);
}
$progress->output(get_string('trace:done', 'core_privacy'), 1);
}
/**
@ -276,7 +340,23 @@ class manager {
* @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();
$components = $this->get_component_list();
$a = (object) [
'total' => count($components),
'progress' => 0,
'component' => '',
'datetime' => userdate(time()),
];
$progress->output(get_string('trace:deletingcontext', 'core_privacy', $a), 1);
foreach ($this->get_component_list() as $component) {
$a->component = $component;
$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)) {
// This component knows about specific data that it owns.
// Have it delete all of that user data for the context.
@ -286,6 +366,7 @@ class manager {
// Delete any shared user data it doesn't know about.
local\request\helper::delete_data_for_all_users_in_context($component, $context);
}
$progress->output(get_string('trace:done', 'core_privacy'), 1);
}
/**
@ -378,4 +459,19 @@ class manager {
return null;
}
/**
* Get the tracer used for logging.
*
* The text tracer is used except for unit tests.
*
* @return \progress_trace
*/
protected static function get_log_tracer() {
if (PHPUNIT_TEST) {
return new \null_progress_trace();
}
return new \text_progress_trace();
}
}