MDL-63500 core_group: get_contexts_for_userid should respect component

The get_contexts_for_userid method should not return contexts for all group memberships.
It should only return the contexts in where there is manual group
membership.

This issue is part of the MDL-62560 Epic.
This commit is contained in:
Shamim Rezaie 2018-10-05 06:20:32 +10:00 committed by David Monllao
parent 882c53dc14
commit 89d468eb39
2 changed files with 61 additions and 6 deletions

View File

@ -263,25 +263,33 @@ class provider implements
}
/**
* Get the list of contexts that contain user information for the specified user.
* Get the list of contexts that contain group membership for the specified user.
*
* @param int $userid The user to search.
* @return contextlist The contextlist containing the list of contexts used in this plugin.
* @param int $userid The user to search.
* @param string $component The component to check.
* @param int $itemid Optional itemid associated with component.
* @return contextlist The contextlist containing the list of contexts.
*/
public static function get_contexts_for_userid(int $userid) : contextlist {
public static function get_contexts_for_group_member(int $userid, string $component, int $itemid = 0) {
$contextlist = new contextlist();
$sql = "SELECT ctx.id
FROM {groups_members} gm
JOIN {groups} g ON gm.groupid = g.id
JOIN {context} ctx ON g.courseid = ctx.instanceid AND ctx.contextlevel = :contextcourse
WHERE gm.userid = :userid";
WHERE gm.userid = :userid AND gm.component = :component";
$params = [
'contextcourse' => CONTEXT_COURSE,
'userid' => $userid
'userid' => $userid,
'component' => $component
];
if ($itemid) {
$sql .= ' AND gm.itemid = :itemid';
$params['itemid'] = $itemid;
}
$contextlist->add_from_sql($sql, $params);
return $contextlist;
@ -290,6 +298,16 @@ class provider implements
/**
* Get the list of users who have data within a context.
*
* @param int $userid The user to search.
* @return contextlist The contextlist containing the list of contexts used in this plugin.
*/
public static function get_contexts_for_userid(int $userid) : contextlist {
return static::get_contexts_for_group_member($userid, '');
}
/**
* Get the list of contexts that contain user information for the specified user.
*
* @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) {

View File

@ -581,6 +581,43 @@ class core_group_privacy_provider_testcase extends provider_testcase {
'', 0.0, 10, true);
}
/**
* Test for provider::get_contexts_for_userid() when there are group memberships from other components.
*/
public function test_get_contexts_for_userid_component() {
$this->resetAfterTest();
$course1 = $this->getDataGenerator()->create_course();
$course2 = $this->getDataGenerator()->create_course();
$group1 = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
$group2 = $this->getDataGenerator()->create_group(array('courseid' => $course2->id));
$user = $this->getDataGenerator()->create_user();
$this->getDataGenerator()->enrol_user($user->id, $course1->id);
$this->getDataGenerator()->enrol_user($user->id, $course2->id);
$this->getDataGenerator()->create_group_member(
array(
'userid' => $user->id,
'groupid' => $group1->id
));
$this->getDataGenerator()->create_group_member(
array(
'userid' => $user->id,
'groupid' => $group2->id,
'component' => 'enrol_meta'
));
$coursecontext1 = context_course::instance($course1->id);
// User is member of some groups in course1 and course2,
// but only the membership in course1 is directly managed by core_group.
$contextlist = provider::get_contexts_for_userid($user->id);
$this->assertEquals([$coursecontext1->id], $contextlist->get_contextids());
}
/**
* Test for provider::export_user_data().
*/