/** * @param FilterControllerEvent $event */ public function onKernelController(FilterControllerEvent $event) { $controller = $event->getController(); if (!is_array($controller)) { return; } // This controller implements ToolInterface? Then set the course/session if ($controller[0] instanceof ToolInterface) { //if ($controller[0] instanceof ToolBaseController) { //$token = $event->getRequest()->query->get('token'); $kernel = $event->getKernel(); $request = $event->getRequest(); /** @var ContainerInterface $container */ $container = $this->container; // Course // The 'course' variable example "123" for this URL: courses/123/ $courseCode = $request->get('course'); // Detect if the course was set with a cidReq: if (empty($courseCode)) { $courseCodeFromRequest = $request->get('cidReq'); $courseCode = $courseCodeFromRequest; } /** @var EntityManager $em */ $em = $container->get('doctrine')->getManager(); $securityChecker = $container->get('security.authorization_checker'); if (!empty($courseCode)) { /** @var Course $course */ $course = $em->getRepository('ChamiloCoreBundle:Course')->findOneByCode($courseCode); if ($course) { // Session $sessionId = $request->get('id_session'); if (empty($sessionId)) { // Check if user is allowed to this course // See CourseVoter.php if (false === $securityChecker->isGranted(CourseVoter::VIEW, $course)) { throw new AccessDeniedException('Unauthorised access to course!'); } } else { $session = $em->getRepository('ChamiloCoreBundle:Session')->find($sessionId); if ($session) { //$course->setCurrentSession($session); $controller[0]->setSession($session); $session->setCurrentCourse($course); // Check if user is allowed to this course-session // See SessionVoter.php if (false === $securityChecker->isGranted(SessionVoter::VIEW, $session)) { throw new AccessDeniedException('Unauthorised access to session!'); } } else { throw new NotFoundHttpException('Session not found'); } } // Example 'chamilo_notebook.controller.notebook:indexAction' $controllerAction = $request->get('_controller'); $controllerActionParts = explode(':', $controllerAction); $controllerNameParts = explode('.', $controllerActionParts[0]); $controllerName = $controllerActionParts[0]; $toolName = null; if (isset($controllerNameParts[1]) && $controllerNameParts[1] == 'controller') { $toolName = $this->container->get($controllerName)->getToolName(); $action = str_replace('action', '', $controllerActionParts[1]); $actionLabel = $toolName . '.' . $action; } $container->get('twig')->addGlobal('tool_name', $toolName); // Legacy code $courseInfo = api_get_course_info($course->getCode()); $container->get('twig')->addGlobal('course', $course); $request->getSession()->set('_real_cid', $course->getId()); $request->getSession()->set('_cid', $course->getCode()); $request->getSession()->set('_course', $courseInfo); /* Sets the controller course in order to use $this->getCourse() */ $controller[0]->setCourse($course); } else { throw new NotFoundHttpException('Course not found'); } } } }
/** * Controller. * * @param FilterControllerEvent $event The event */ public function onKernelController(FilterControllerEvent $event) { $request = $this->request = $event->getRequest(); if (false === $this->isEnabled()) { return; } $controller = $event->getController(); $metadata = $this->getControllerActionMetadata($controller); if (null === $metadata || 0 === count($metadata->param_converter_bag)) { // no annotations defined for this controller return; } foreach ($metadata->param_converter_bag as $param_converter) { $class = $param_converter->class; $bag = $param_converter->id_source; $unique_identifier = $request->{$bag}->get($param_converter->id_name, null); $entity = null; try { if (null === $unique_identifier) { throw new \InvalidArgumentException('Unable to find identifier with provided attribute: ' . $param_converter->id_name); } } catch (\InvalidArgumentException $e) { if (true === $param_converter->required) { throw $e; } } if (null !== $unique_identifier) { $entity = $event->getKernel()->getApplication()->getEntityManager()->find($class, $unique_identifier); if (null === $entity) { throw new NotFoundHttpException("No `{$class}` exists with uid `{$unique_identifier}`."); } } $request->attributes->set($param_converter->name, $entity); } }