Validation of confidential toggle added to accompanyingPeriod validator

This commit is contained in:
2022-02-15 14:22:41 +01:00
parent 385664e2bc
commit 4e83e7905a
5 changed files with 35 additions and 15 deletions

View File

@@ -327,19 +327,19 @@ final class AccompanyingCourseApiController extends ApiController
public function toggleConfidentialApi(AccompanyingPeriod $accompanyingCourse, $id, Request $request)
{
if ($request->getMethod() == 'POST') {
$this->denyAccessUnlessGranted(AccompanyingPeriodVoter::TOGGLE_CONFIDENTIAL, $accompanyingCourse);
if (null != $accompanyingCourse->getUser() && $this->getUser() == $accompanyingCourse->getUser()) {
$accompanyingCourse->setConfidential(!$accompanyingCourse->isConfidential());
$this->getDoctrine()->getManager()->flush();
} else {
if ($accompanyingCourse->getUser() == null) {
throw new ValidationException("The parcours must have a referrer to be set to confidential");
}
throw new ValidationException("Only the referrer can set a parcours to confidential");
}
}
$errors = $this->validator->validate($accompanyingCourse);
if ($errors->count() > 0) {
return $this->json($errors, 422);
} else {
$this->getDoctrine()->getManager()->flush();
}
}
return $this->json($accompanyingCourse->isConfidential(), Response::HTTP_OK, [], ['groups' => ['read']]);
}