Skip to content

Commit

Permalink
MDL-63533 core_webservice: Add support for removal of context users
Browse files Browse the repository at this point in the history
This issue is part of the MDL-62560 Epic.
  • Loading branch information
Mihail Geshoski authored and David Monllao committed Oct 22, 2018
1 parent 424976e commit 9960e86
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions webservice/classes/privacy/provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
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.
Expand All @@ -43,6 +45,7 @@
*/
class provider implements
\core_privacy\local\metadata\provider,
\core_privacy\local\request\core_userlist_provider,
\core_privacy\local\request\subsystem\provider {

/**
Expand Down Expand Up @@ -106,6 +109,47 @@ public static function get_contexts_for_userid(int $userid) : \core_privacy\loca
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.
*
Expand Down Expand Up @@ -217,6 +261,20 @@ public static function delete_data_for_all_users_in_context(context $context) {
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.
*
Expand Down

0 comments on commit 9960e86

Please sign in to comment.