Skip to content

Commit

Permalink
MDL-66257 tool_cohortroles: Unassign the role on removal cohortroles.
Browse files Browse the repository at this point in the history
  • Loading branch information
cescobedo authored and junpataleta committed Nov 7, 2019
1 parent d084e41 commit b7ee5cb
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 8 deletions.
42 changes: 42 additions & 0 deletions admin/tool/cohortroles/classes/api.php
Expand Up @@ -222,6 +222,48 @@ public static function sync_all_cohort_roles() {
}
}

// Clean the legacy role assignments which are stale.
$paramsclean['usercontext'] = CONTEXT_USER;
$paramsclean['component'] = 'tool_cohortroles';
$sql = 'SELECT DISTINCT(ra.id), ra.roleid, ra.userid, ra.contextid, ctx.instanceid
FROM {role_assignments} ra
JOIN {context} ctx ON ctx.id = ra.contextid AND ctx.contextlevel = :usercontext
JOIN {cohort_members} cm ON cm.userid = ctx.instanceid
LEFT JOIN {tool_cohortroles} tc ON tc.cohortid = cm.cohortid
AND tc.userid = ra.userid
AND tc.roleid = ra.roleid
WHERE ra.component = :component
AND tc.id is null';
if ($candidatelegacyassignments = $DB->get_records_sql($sql, $paramsclean)) {
$sql = 'SELECT DISTINCT(ra.id)
FROM {role_assignments} ra
JOIN {context} ctx ON ctx.id = ra.contextid AND ctx.contextlevel = :usercontext
JOIN {cohort_members} cm ON cm.userid = ctx.instanceid
JOIN {tool_cohortroles} tc ON tc.cohortid = cm.cohortid AND tc.userid = ra.userid
WHERE ra.component = :component';
if ($currentvalidroleassignments = $DB->get_records_sql($sql, $paramsclean)) {
foreach ($candidatelegacyassignments as $candidate) {
if (!array_key_exists($candidate->id, $currentvalidroleassignments)) {
role_unassign($candidate->roleid, $candidate->userid, $candidate->contextid, 'tool_cohortroles');
$rolesremoved[] = array(
'useridassignedto' => $candidate->userid,
'useridassignedover' => $candidate->instanceid,
'roleid' => $candidate->roleid
);
}
}
} else {
foreach ($candidatelegacyassignments as $candidate) {
role_unassign($candidate->roleid, $candidate->userid, $candidate->contextid, 'tool_cohortroles');
$rolesremoved[] = array(
'useridassignedto' => $candidate->userid,
'useridassignedover' => $candidate->instanceid,
'roleid' => $candidate->roleid
);
}
}
}

return array('rolesadded' => $rolesadded, 'rolesremoved' => $rolesremoved);
}

Expand Down
1 change: 0 additions & 1 deletion admin/tool/cohortroles/classes/cohort_role_assignment.php
Expand Up @@ -103,5 +103,4 @@ protected function validate_cohortid($value) {

return true;
}

}
34 changes: 27 additions & 7 deletions admin/tool/cohortroles/tests/api_test.php
Expand Up @@ -50,8 +50,6 @@ class tool_cohortroles_api_testcase extends advanced_testcase {
* Setup function- we will create a course and add an assign instance to it.
*/
protected function setUp() {
global $DB;

$this->resetAfterTest(true);

// Create some users.
Expand Down Expand Up @@ -133,14 +131,36 @@ public function test_delete_cohort_role_assignment_with_invalid_data() {

public function test_delete_cohort_role_assignment() {
$this->setAdminUser();
$params = (object) array(
// Create a cohort role assigment.
$params = (object) [
'userid' => $this->userassignto->id,
'roleid' => $this->roleid,
'cohortid' => $this->cohort->id
);
$result = api::create_cohort_role_assignment($params);
$worked = api::delete_cohort_role_assignment($result->get('id'));
$this->assertTrue($worked);
];
$cohortroleassignment = api::create_cohort_role_assignment($params);
$sync = api::sync_all_cohort_roles();
$rolesadded = [
[
'useridassignedto' => $this->userassignto->id,
'useridassignedover' => $this->userassignover->id,
'roleid' => $this->roleid
]
];
$expected = [
'rolesadded' => $rolesadded,
'rolesremoved' => []
];
$this->assertEquals($sync, $expected);

// Delete the cohort role assigment and confirm the roles are removed.
$result = api::delete_cohort_role_assignment($cohortroleassignment->get('id'));
$this->assertTrue($result);
$sync = api::sync_all_cohort_roles();
$expected = [
'rolesadded' => [],
'rolesremoved' => $rolesadded
];
$this->assertEquals($expected, $sync);
}

public function test_list_cohort_role_assignments() {
Expand Down

0 comments on commit b7ee5cb

Please sign in to comment.