From 9960e8694dc65d40fd244f058c72f3418f94ec55 Mon Sep 17 00:00:00 2001 From: Mihail Geshoski Date: Thu, 4 Oct 2018 14:48:53 +0800 Subject: [PATCH] MDL-63533 core_webservice: Add support for removal of context users This issue is part of the MDL-62560 Epic. --- webservice/classes/privacy/provider.php | 58 +++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/webservice/classes/privacy/provider.php b/webservice/classes/privacy/provider.php index 681ec63ce59..dc9ad3a2c6b 100644 --- a/webservice/classes/privacy/provider.php +++ b/webservice/classes/privacy/provider.php @@ -32,6 +32,8 @@ use core_privacy\local\metadata\collection; use core_privacy\local\request\approved_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; /** * Data provider class. @@ -43,6 +45,7 @@ use core_privacy\local\request\writer; */ class provider implements \core_privacy\local\metadata\provider, + \core_privacy\local\request\core_userlist_provider, \core_privacy\local\request\subsystem\provider { /** @@ -106,6 +109,47 @@ class provider implements return $contextlist; } + /** + * 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(); + + $params = [ + 'contextid' => $context->id, + 'contextuser' => CONTEXT_USER, + ]; + + $sql = "SELECT ctx.instanceid as userid + FROM {external_tokens} t + JOIN {context} ctx + ON ctx.instanceid = t.userid + AND ctx.contextlevel = :contextuser + WHERE ctx.id = :contextid"; + + $userlist->add_from_sql('userid', $sql, $params); + + $sql = "SELECT ctx.instanceid as userid + FROM {external_tokens} t + JOIN {context} ctx + ON ctx.instanceid = t.creatorid + AND ctx.contextlevel = :contextuser + WHERE ctx.id = :contextid"; + + $userlist->add_from_sql('userid', $sql, $params); + + $sql = "SELECT ctx.instanceid as userid + FROM {external_services_users} su + JOIN {context} ctx + ON ctx.instanceid = su.userid + AND ctx.contextlevel = :contextuser + WHERE ctx.id = :contextid"; + + $userlist->add_from_sql('userid', $sql, $params); + } + /** * Export all user data for the specified user, in the specified contexts. * @@ -217,6 +261,20 @@ class provider implements static::delete_user_data($context->instanceid); } + /** + * 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) { + + $context = $userlist->get_context(); + + if ($context instanceof \context_user) { + static::delete_user_data($context->instanceid); + } + } + /** * Delete all user data for the specified user, in the specified contexts. *