Skip to content

Commit

Permalink
Merge branch 'MDL-62371-master' of git://github.com/andrewnicols/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
stronk7 committed May 14, 2018
2 parents 28ec28e + 5638527 commit 24bd0bc
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 4 deletions.
9 changes: 9 additions & 0 deletions lang/en/privacy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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})';
104 changes: 100 additions & 4 deletions privacy/classes/manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,23 @@ public function get_metadata_for_components() : array {
* @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 {
Expand All @@ -187,6 +202,7 @@ public function get_contexts_for_userid(int $userid) : contextlist_collection {
$clcollection->add_contextlist($contextlist);
}
}
$progress->output(get_string('trace:done', 'core_privacy'), 1);

return $clcollection;
}
Expand All @@ -202,13 +218,29 @@ public function get_contexts_for_userid(int $userid) : contextlist_collection {
* 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)) {
Expand All @@ -221,9 +253,19 @@ public function export_user_data(contextlist_collection $contextlistcollection)
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());
Expand All @@ -235,8 +277,13 @@ public function export_user_data(contextlist_collection $contextlistcollection)
$this->get_provider_classname($component)::export_context_data($contextlistcollection);
}
}
$progress->output(get_string('trace:done', 'core_privacy'), 1);

$progress->output(get_string('trace:finalisingexport', 'core_privacy'), 1);
$location = local\request\writer::with_context(\context_system::instance())->finalise_content();

return local\request\writer::with_context(\context_system::instance())->finalise_content();
$progress->output(get_string('trace:exportcomplete', 'core_privacy'), 1);
return $location;
}

/**
Expand All @@ -251,13 +298,29 @@ public function export_user_data(contextlist_collection $contextlistcollection)
* 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.
Expand All @@ -268,6 +331,7 @@ public function delete_data_for_user(contextlist_collection $contextlistcollecti
// 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);
}

/**
Expand All @@ -276,7 +340,23 @@ public function delete_data_for_user(contextlist_collection $contextlistcollecti
* @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.
Expand All @@ -286,6 +366,7 @@ public function delete_data_for_all_users_in_context(\context $context) {
// 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);
}

/**
Expand Down Expand Up @@ -378,4 +459,19 @@ public static function component_class_callback(string $component, string $inter

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();
}
}

0 comments on commit 24bd0bc

Please sign in to comment.