From 3952ace59fb8fc2d635b0fac9e02251037cea946 Mon Sep 17 00:00:00 2001 From: Mihail Geshoski Date: Wed, 10 Oct 2018 15:54:54 +0800 Subject: [PATCH] MDL-63536 core_repository: Add support for removal of context users This issue is part of the MDL-62560 Epic. --- repository/classes/privacy/provider.php | 47 +++++++++++++++- repository/tests/privacy_test.php | 96 +++++++++++++++++++++++++++++++++ 2 files changed, 142 insertions(+), 1 deletion(-) diff --git a/repository/classes/privacy/provider.php b/repository/classes/privacy/provider.php index 82d5c2a6603..994f97d05e2 100644 --- a/repository/classes/privacy/provider.php +++ b/repository/classes/privacy/provider.php @@ -30,6 +30,8 @@ use core_privacy\local\request\context; use core_privacy\local\request\contextlist; use core_privacy\local\request\transform; use core_privacy\local\request\writer; +use core_privacy\local\request\userlist; +use \core_privacy\local\request\approved_userlist; defined('MOODLE_INTERNAL') || die(); @@ -39,7 +41,10 @@ defined('MOODLE_INTERNAL') || die(); * @copyright 2018 Zig Tan * @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 { +class provider implements + \core_privacy\local\metadata\provider, + \core_privacy\local\request\core_userlist_provider, + \core_privacy\local\request\plugin\provider { /** * Returns meta data about this system. @@ -93,6 +98,31 @@ class provider implements \core_privacy\local\metadata\provider, \core_privacy\l } /** + * Get the list of users within a specific context. + * + * @param userlist $userlist The userlist containing the list of users who have data in this context/plugin combination. + */ + public static function get_users_in_context(userlist $userlist) { + $context = $userlist->get_context(); + + if (!$context instanceof \context_user) { + return; + } + + $params = [ + 'contextid' => $context->id, + 'contextuser' => CONTEXT_USER, + ]; + + $sql = "SELECT c.instanceid as userid + FROM {repository_instances} ri + JOIN {context} c ON c.instanceid = ri.userid AND c.contextlevel = :contextuser + WHERE c.id = :contextid"; + + $userlist->add_from_sql('userid', $sql, $params); + } + + /** * Export all user data for the specified user, in the specified contexts. * * @param approved_contextlist $contextlist The approved contexts to export information for. @@ -166,6 +196,21 @@ class provider implements \core_privacy\local\metadata\provider, \core_privacy\l } /** + * Delete multiple users within a single context. + * + * @param approved_userlist $userlist The approved context and user information to delete information for. + */ + public static function delete_data_for_users(approved_userlist $userlist) { + global $DB; + + $context = $userlist->get_context(); + + if ($context instanceof \context_user) { + $DB->delete_records('repository_instances', ['userid' => $context->instanceid]); + } + } + + /** * 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. diff --git a/repository/tests/privacy_test.php b/repository/tests/privacy_test.php index 75a9adce63c..736d55187f2 100644 --- a/repository/tests/privacy_test.php +++ b/repository/tests/privacy_test.php @@ -21,11 +21,15 @@ * @copyright 2018 Zig Tan * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ + defined('MOODLE_INTERNAL') || die(); + use \core_privacy\local\metadata\collection; use \core_privacy\local\request\writer; use \core_privacy\local\request\approved_contextlist; use \core_repository\privacy\provider; +use \core_privacy\local\request\approved_userlist; + /** * Unit tests for the core_repository implementation of the privacy API. * @@ -162,6 +166,98 @@ class core_repository_privacy_testcase extends \core_privacy\tests\provider_test } /** + * Test that only users with a user context are fetched. + */ + public function test_get_users_in_context() { + $this->resetAfterTest(); + + $component = 'core_repository'; + // Create a user. + $user = $this->getDataGenerator()->create_user(); + $usercontext = \context_user::instance($user->id); + $userlist = new \core_privacy\local\request\userlist($usercontext, $component); + // The list of users should not return anything yet (related data still haven't been created). + provider::get_users_in_context($userlist); + $this->assertCount(0, $userlist); + + // Create 3 repository_instances records for user. + $this->setup_test_scenario_data($user->id, 3); + + // The list of users for user context should return the user. + provider::get_users_in_context($userlist); + $this->assertCount(1, $userlist); + $expected = [$user->id]; + $actual = $userlist->get_userids(); + $this->assertEquals($expected, $actual); + + // The list of users for system context should not return any users. + $systemcontext = context_system::instance(); + $userlist = new \core_privacy\local\request\userlist($systemcontext, $component); + provider::get_users_in_context($userlist); + $this->assertCount(0, $userlist); + } + + /** + * Test that data for users in approved userlist is deleted. + */ + public function test_delete_data_for_users() { + $this->resetAfterTest(); + + $component = 'core_repository'; + // Create user1. + $user1 = $this->getDataGenerator()->create_user(); + $this->setUser($user1); + $usercontext1 = \context_user::instance($user1->id); + // Create list of users with a related user data in usercontext1. + $userlist1 = new \core_privacy\local\request\userlist($usercontext1, $component); + + // Create a user2. + $user2 = $this->getDataGenerator()->create_user(); + $this->setUser($user2); + $usercontext2 = \context_user::instance($user2->id); + // Create list of users with a related user data in usercontext2. + $userlist2 = new \core_privacy\local\request\userlist($usercontext2, $component); + + // Create repository_instances record for user1. + $this->setup_test_scenario_data($user1->id, 1); + // Create repository_instances record for user2. + $this->setup_test_scenario_data($user2->id, 1); + + // Ensure the user list for usercontext1 contains user1. + provider::get_users_in_context($userlist1); + $this->assertCount(1, $userlist1); + // Ensure the user list for usercontext2 contains user2. + provider::get_users_in_context($userlist2); + $this->assertCount(1, $userlist2); + + // Convert $userlist1 into an approved_contextlist. + $approvedlist = new approved_userlist($usercontext1, $component, $userlist1->get_userids()); + + // Delete using delete_data_for_user. + provider::delete_data_for_users($approvedlist); + + // Re-fetch users in the usercontext1 - The user list should now be empty. + $userlist1 = new \core_privacy\local\request\userlist($usercontext1, $component); + provider::get_users_in_context($userlist1); + $this->assertCount(0, $userlist1); + // Re-fetch users in the usercontext2 - The user list should not be empty. + $userlist2 = new \core_privacy\local\request\userlist($usercontext2, $component); + provider::get_users_in_context($userlist2); + $this->assertCount(1, $userlist2); + + // User data should be only removed in the user context. + $systemcontext = context_system::instance(); + // Add userlist2 to the approved user list in the system context. + $approvedlist = new approved_userlist($systemcontext, $component, $userlist2->get_userids()); + // Delete user1 data using delete_data_for_user. + provider::delete_data_for_users($approvedlist); + // Re-fetch users in usercontext2 - The user list should not be empty (user2). + $userlist2 = new \core_privacy\local\request\userlist($usercontext2, $component); + provider::get_users_in_context($userlist2); + $this->assertCount(1, $userlist2); + } + + /** * Helper function to setup repository_instances records for testing a specific user. * * @param int $userid The Id of the User used for testing. -- 2.11.4.GIT