|
|
|
@ -1,9 +1,9 @@ |
|
|
|
|
<?php |
|
|
|
|
|
|
|
|
|
declare(strict_types=1); |
|
|
|
|
|
|
|
|
|
/* For licensing terms, see /license.txt */ |
|
|
|
|
|
|
|
|
|
declare(strict_types=1); |
|
|
|
|
|
|
|
|
|
namespace Chamilo\CoreBundle\EventListener; |
|
|
|
|
|
|
|
|
|
use Chamilo\CoreBundle\Controller\EditorController; |
|
|
|
@ -15,7 +15,7 @@ use Chamilo\CoreBundle\Security\Authorization\Voter\GroupVoter; |
|
|
|
|
use Chamilo\CoreBundle\Security\Authorization\Voter\SessionVoter; |
|
|
|
|
use Chamilo\CourseBundle\Controller\CourseControllerInterface; |
|
|
|
|
use Chamilo\CourseBundle\Entity\CGroup; |
|
|
|
|
use Doctrine\ORM\EntityManager; |
|
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareTrait; |
|
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
|
|
|
use Symfony\Component\HttpKernel\Event\ControllerEvent; |
|
|
|
@ -25,6 +25,7 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
|
|
|
|
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; |
|
|
|
|
use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
|
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
|
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
|
|
|
|
use Twig\Environment; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -37,7 +38,9 @@ class CourseListener |
|
|
|
|
|
|
|
|
|
public function __construct( |
|
|
|
|
private readonly Environment $twig, |
|
|
|
|
private readonly AuthorizationCheckerInterface $authorizationChecker |
|
|
|
|
private readonly AuthorizationCheckerInterface $authorizationChecker, |
|
|
|
|
private readonly TranslatorInterface $translator, |
|
|
|
|
private readonly EntityManagerInterface $entityManager, |
|
|
|
|
) { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -46,6 +49,8 @@ class CourseListener |
|
|
|
|
*/ |
|
|
|
|
public function onKernelRequest(RequestEvent $event): void |
|
|
|
|
{ |
|
|
|
|
global $cidReset; |
|
|
|
|
|
|
|
|
|
if (!$event->isMainRequest()) { |
|
|
|
|
// don't do anything if it's not the master request |
|
|
|
|
return; |
|
|
|
@ -67,9 +72,13 @@ class CourseListener |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (true === $cidReset) { |
|
|
|
|
$this->removeCourseFromSession($request); |
|
|
|
|
|
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
$sessionHandler = $request->getSession(); |
|
|
|
|
$container = $this->container; |
|
|
|
|
$translator = $container->get('translator'); |
|
|
|
|
$twig = $this->twig; |
|
|
|
|
|
|
|
|
|
$course = null; |
|
|
|
@ -79,46 +88,25 @@ class CourseListener |
|
|
|
|
$courseId = (int) $request->get('cid'); |
|
|
|
|
$checker = $this->authorizationChecker; |
|
|
|
|
|
|
|
|
|
/** @var EntityManager $em */ |
|
|
|
|
$em = $container->get('doctrine')->getManager(); |
|
|
|
|
|
|
|
|
|
//dump("cid value in request: $courseId"); |
|
|
|
|
if (!empty($courseId)) { |
|
|
|
|
$course = null; |
|
|
|
|
if ($sessionHandler->has('course')) { |
|
|
|
|
/** @var Course $courseFromSession */ |
|
|
|
|
$courseFromSession = $sessionHandler->get('course'); |
|
|
|
|
if ($courseId === $courseFromSession->getId()) { |
|
|
|
|
$course = $courseFromSession; |
|
|
|
|
$courseInfo = $sessionHandler->get('_course'); |
|
|
|
|
//dump("Course #$courseId loaded from Session "); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//$course = null; //force loading from database |
|
|
|
|
//if (null === $course) { |
|
|
|
|
$course = $em->getRepository(Course::class)->find($courseId); |
|
|
|
|
if (null === $course) { |
|
|
|
|
throw new NotFoundHttpException($translator->trans('Course does not exist')); |
|
|
|
|
$course = $this->entityManager->find(Course::class, $courseId); |
|
|
|
|
$courseInfo = api_get_course_info($course->getCode()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//dump("Course loaded from DB #$courseId"); |
|
|
|
|
$courseInfo = api_get_course_info($course->getCode()); |
|
|
|
|
//} |
|
|
|
|
|
|
|
|
|
/*if (null === $course) { |
|
|
|
|
throw new NotFoundHttpException($translator->trans('Course does not exist')); |
|
|
|
|
}*/ |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
global $cidReset; |
|
|
|
|
if (true === $cidReset) { |
|
|
|
|
$this->removeCourseFromSession($request); |
|
|
|
|
|
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
if (null === $course) { |
|
|
|
|
throw new NotFoundHttpException($this->translator->trans('Course does not exist')); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (null !== $course) { |
|
|
|
|
// Setting variables in the session. |
|
|
|
|
$sessionHandler->set('course', $course); |
|
|
|
|
$sessionHandler->set('_real_cid', $course->getId()); |
|
|
|
@ -129,32 +117,27 @@ class CourseListener |
|
|
|
|
// Setting variables for the twig templates. |
|
|
|
|
$twig->addGlobal('course', $course); |
|
|
|
|
|
|
|
|
|
if (false === $checker->isGranted(CourseVoter::VIEW, $course)) { |
|
|
|
|
throw new AccessDeniedException($this->translator->trans('You\'re not allowed in this course')); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Checking if sid is used. |
|
|
|
|
$sessionId = (int) $request->get('sid'); |
|
|
|
|
$session = null; |
|
|
|
|
|
|
|
|
|
if (empty($sessionId)) { |
|
|
|
|
$sessionHandler->remove('session_name'); |
|
|
|
|
$sessionHandler->remove('sid'); |
|
|
|
|
$sessionHandler->remove('session'); |
|
|
|
|
// Check if user is allowed to this course |
|
|
|
|
// See CourseVoter.php |
|
|
|
|
//dump("Checkisgranted"); |
|
|
|
|
if (false === $checker->isGranted(CourseVoter::VIEW, $course)) { |
|
|
|
|
throw new AccessDeniedException($translator->trans('You\'re not allowed in this course')); |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
//dump("Load chamilo session from DB"); |
|
|
|
|
$session = $em->getRepository(Session::class)->find($sessionId); |
|
|
|
|
$session = $this->entityManager->find(Session::class, $sessionId); |
|
|
|
|
if (null !== $session) { |
|
|
|
|
if (!$session->hasCourse($course)) { |
|
|
|
|
throw new AccessDeniedException($translator->trans('Course is not registered in the Session')); |
|
|
|
|
} |
|
|
|
|
//$course->setCurrentSession($session); |
|
|
|
|
$session->setCurrentCourse($course); |
|
|
|
|
// Check if user is allowed to this course-session |
|
|
|
|
// See SessionVoter.php |
|
|
|
|
if (false === $checker->isGranted(SessionVoter::VIEW, $session)) { |
|
|
|
|
throw new AccessDeniedException($translator->trans('You\'re not allowed in this session')); |
|
|
|
|
throw new AccessDeniedException($this->translator->trans('You\'re not allowed in this session')); |
|
|
|
|
} |
|
|
|
|
$sessionHandler->set('session_name', $session->getName()); |
|
|
|
|
$sessionHandler->set('sid', $session->getId()); |
|
|
|
@ -162,7 +145,7 @@ class CourseListener |
|
|
|
|
|
|
|
|
|
$twig->addGlobal('session', $session); |
|
|
|
|
} else { |
|
|
|
|
throw new NotFoundHttpException($translator->trans('Session not found')); |
|
|
|
|
throw new NotFoundHttpException($this->translator->trans('Session not found')); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -173,16 +156,16 @@ class CourseListener |
|
|
|
|
$sessionHandler->remove('gid'); |
|
|
|
|
} else { |
|
|
|
|
//dump('Load chamilo group from DB'); |
|
|
|
|
$group = $em->getRepository(CGroup::class)->find($groupId); |
|
|
|
|
$group = $this->entityManager->getRepository(CGroup::class)->find($groupId); |
|
|
|
|
|
|
|
|
|
if (null === $group) { |
|
|
|
|
throw new NotFoundHttpException($translator->trans('Group not found')); |
|
|
|
|
throw new NotFoundHttpException($this->translator->trans('Group not found')); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
$group->setParent($course); |
|
|
|
|
|
|
|
|
|
if (false === $checker->isGranted(GroupVoter::VIEW, $group)) { |
|
|
|
|
throw new AccessDeniedException($translator->trans('You\'re not allowed in this group')); |
|
|
|
|
throw new AccessDeniedException($this->translator->trans('You\'re not allowed in this group')); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
$sessionHandler->set('gid', $groupId); |
|
|
|
@ -191,11 +174,11 @@ class CourseListener |
|
|
|
|
// Check if user is allowed to this course-group |
|
|
|
|
// See GroupVoter.php |
|
|
|
|
if (false === $checker->isGranted(GroupVoter::VIEW, $group)) { |
|
|
|
|
throw new AccessDeniedException($translator->trans('Unauthorised access to group')); |
|
|
|
|
throw new AccessDeniedException($this->translator->trans('Unauthorised access to group')); |
|
|
|
|
} |
|
|
|
|
$sessionHandler->set('gid', $groupId); |
|
|
|
|
} else { |
|
|
|
|
throw new AccessDeniedException($translator->trans('Group does not exist in course')); |
|
|
|
|
throw new AccessDeniedException($this->translator->trans('Group does not exist in course')); |
|
|
|
|
}*/ |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|