public function deletePolicy($roleId, $policyId, Request $request)
{
try {
// First try to treat $roleId as a role draft ID.
$roleDraft = $this->roleService->loadRoleDraft($roleId);
$policy = null;
foreach ($roleDraft->getPolicies() as $rolePolicy) {
if ($rolePolicy->id == $policyId) {
$policy = $rolePolicy;
break;
}
}
if ($policy !== null) {
$this->roleService->removePolicyByRoleDraft($roleDraft, $policy);
return new Values\NoContent();
}
} catch (NotFoundException $e) {
// Then try to treat $roleId as a role ID.
$role = $this->roleService->loadRole($roleId);
$policy = null;
foreach ($role->getPolicies() as $rolePolicy) {
if ($rolePolicy->id == $policyId) {
$policy = $rolePolicy;
break;
}
}
if ($policy !== null) {
$this->roleService->deletePolicy($policy);
return new Values\NoContent();
}
}
throw new Exceptions\NotFoundException("Policy not found: '{$request->getPathInfo()}'.");
}