MDL-81007 tool_policy: Return permissions per policy

This commit is contained in:
Juan Leyva
2024-02-22 17:14:05 +01:00
parent d3ad77e476
commit f8f04b6f63
2 changed files with 26 additions and 0 deletions
@@ -118,6 +118,11 @@ class get_user_acceptances extends external_api {
[$policy['acceptance']['note']] = util::format_text($version->acceptance->note, FORMAT_MOODLE, $systemcontext);
}
}
// Return permission for actions for the current policy and user.
$policy['canaccept'] = api::can_accept_policies([$version->id], $user->id);
$policy['candecline'] = api::can_decline_policies([$version->id], $user->id);
$policy['canrevoke'] = api::can_revoke_policies([$version->id], $user->id);
$policies[] = $policy;
}
}
@@ -157,6 +162,9 @@ class get_user_acceptances extends external_api {
'note' => new external_value(PARAM_TEXT, 'The policy note/remarks.', VALUE_OPTIONAL),
'modfullname' => new external_value(PARAM_NOTAGS, 'The fullname who accepted on behalf.', VALUE_OPTIONAL),
], 'Acceptance status for the given user.', VALUE_OPTIONAL),
'canaccept' => new external_value(PARAM_BOOL, 'Whether the policy can be accepted.'),
'candecline' => new external_value(PARAM_BOOL, 'Whether the policy can be declined.'),
'canrevoke' => new external_value(PARAM_BOOL, 'Whether the policy can be revoked.'),
]), 'Policies and acceptance status for the given user.', VALUE_OPTIONAL
),
'warnings' => new external_warnings(),
@@ -271,9 +271,15 @@ class externallib_test extends externallib_advanced_testcase {
if ($policy['versionid'] == $this->policy2->get('id')) {
$this->assertEquals($this->policy2->get('name'), $policy['name']);
$this->assertEquals(0, $policy['optional']);
$this->assertTrue($policy['canaccept']);
$this->assertFalse($policy['candecline']); // Cannot decline or revoke mandatory for myself.
$this->assertFalse($policy['canrevoke']);
} else {
$this->assertEquals($optionalpolicy->get('name'), $policy['name']);
$this->assertEquals(1, $policy['optional']);
$this->assertTrue($policy['canaccept']);
$this->assertTrue($policy['candecline']); // Can decline or revoke optional for myself.
$this->assertTrue($policy['canrevoke']);
}
$this->assertNotContains('acceptance', $policy); // Nothing accepted yet.
}
@@ -285,6 +291,18 @@ class externallib_test extends externallib_advanced_testcase {
$policies = \core_external\external_api::clean_returnvalue(
\tool_policy\external\get_user_acceptances::execute_returns(), $policies);
$this->assertCount(2, $policies['policies']);
foreach ($policies['policies'] as $policy) {
if ($policy['versionid'] == $this->policy2->get('id')) {
$this->assertTrue($policy['canaccept']);
$this->assertFalse($policy['candecline']); // Cannot decline mandatory in general.
$this->assertTrue($policy['canrevoke']);
} else {
$this->assertTrue($policy['canaccept']);
$this->assertTrue($policy['candecline']);
$this->assertTrue($policy['canrevoke']);
}
$this->assertNotContains('acceptance', $policy); // Nothing accepted yet.
}
// Get other user acceptances without permission.
$this->expectException(\required_capability_exception::class);