From 87bc43235633f04827d7d1756b4a3d59dfbfd8b1 Mon Sep 17 00:00:00 2001 From: Julio Montoya Date: Sun, 24 Nov 2019 12:04:16 +0100 Subject: [PATCH] Add session support for resources --- src/CoreBundle/Controller/ResourceController.php | 16 +++++++++++----- .../Controller/CourseControllerTrait.php | 8 ++++++++ .../EventListener/CourseAccessListener.php | 2 -- .../EventListener/CourseListener.php | 3 +++ .../EventListener/SessionAccessListener.php | 13 +++++++++++-- .../Resources/views/Resource/index.html.twig | 8 ++++---- 6 files changed, 37 insertions(+), 13 deletions(-) diff --git a/src/CoreBundle/Controller/ResourceController.php b/src/CoreBundle/Controller/ResourceController.php index dfba9c9f0f..bef611d257 100644 --- a/src/CoreBundle/Controller/ResourceController.php +++ b/src/CoreBundle/Controller/ResourceController.php @@ -133,7 +133,13 @@ class ResourceController extends AbstractResourceController implements CourseCon $translation = $this->translator; $courseIdentifier = $course->getCode(); - $routeParams = ['tool' => $tool, 'type' => $type, 'cidReq' => $courseIdentifier, 'id']; + $routeParams = [ + 'tool' => $tool, + 'type' => $type, + 'cidReq' => $courseIdentifier, + 'id', + 'id_session' => $this->getSessionId(), + ]; // Title link. $grid->getColumn('title')->setTitle($this->trans('Name')); @@ -210,7 +216,7 @@ class ResourceController extends AbstractResourceController implements CourseCon $grid->setHiddenColumns(['iid']); // Delete mass action. - if ($this->isGranted(ResourceNodeVoter::ROLE_CURRENT_COURSE_TEACHER)) { + if ($this->isGranted(ResourceNodeVoter::DELETE, $this->getCourse())) { $deleteMassAction = new MassAction( 'Delete', 'ChamiloCoreBundle:Resource:deleteMass', @@ -240,7 +246,7 @@ class ResourceController extends AbstractResourceController implements CourseCon $myRowAction->addManipulateRender($setNodeParameters); $grid->addRowAction($myRowAction); - if ($this->isGranted(ResourceNodeVoter::ROLE_CURRENT_COURSE_TEACHER)) { + if ($this->isGranted(ResourceNodeVoter::EDIT, $this->getCourse())) { // Edit action. $myRowAction = new RowAction( $translation->trans('Edit'), @@ -728,8 +734,7 @@ class ResourceController extends AbstractResourceController implements CourseCon $this->setBreadCrumb( $request); //$helper = $this->container->get('oneup_uploader.templating.uploader_helper'); //$endpoint = $helper->endpoint('courses'); - $session = $this->getSession(); - $sessionId = $session ? $session->getId() : 0; + $sessionId = $this->getSessionId(); return $this->render( '@ChamiloTheme/Resource/upload.html.twig', @@ -962,6 +967,7 @@ class ResourceController extends AbstractResourceController implements CourseCon 'tool' => $tool, 'type' => $type, 'cidReq' => $this->getCourse()->getCode(), + 'id_session' => $this->getSessionId(), ] ); } diff --git a/src/CourseBundle/Controller/CourseControllerTrait.php b/src/CourseBundle/Controller/CourseControllerTrait.php index c91a0795cd..0fc9483252 100644 --- a/src/CourseBundle/Controller/CourseControllerTrait.php +++ b/src/CourseBundle/Controller/CourseControllerTrait.php @@ -46,4 +46,12 @@ trait CourseControllerTrait { return $this->session; } + + /** + * @return int + */ + public function getSessionId() + { + return $this->session ? $this->getSession()->getId() : 0; + } } diff --git a/src/CourseBundle/EventListener/CourseAccessListener.php b/src/CourseBundle/EventListener/CourseAccessListener.php index 8601d92c67..820f389c0d 100644 --- a/src/CourseBundle/EventListener/CourseAccessListener.php +++ b/src/CourseBundle/EventListener/CourseAccessListener.php @@ -12,8 +12,6 @@ use Symfony\Component\HttpFoundation\RequestStack; * Class CourseAccessListener * In and outs of a course * This listeners is always called when user enters the course home. - * - * @package Chamilo\CourseBundle\EventListener */ class CourseAccessListener { diff --git a/src/CourseBundle/EventListener/CourseListener.php b/src/CourseBundle/EventListener/CourseListener.php index df825dd307..a1c55c5a43 100644 --- a/src/CourseBundle/EventListener/CourseListener.php +++ b/src/CourseBundle/EventListener/CourseListener.php @@ -150,6 +150,9 @@ class CourseListener $sessionHandler->set('session_name', $session->getName()); $sessionHandler->set('id_session', $session->getId()); $sessionHandler->set('sessionObj', $session); + + $container->get('twig')->addGlobal('sessionObj', $session); + } else { throw new NotFoundHttpException($translator->trans('Session not found')); } diff --git a/src/CourseBundle/EventListener/SessionAccessListener.php b/src/CourseBundle/EventListener/SessionAccessListener.php index 8d31cd7d99..e6ba0b6b7d 100644 --- a/src/CourseBundle/EventListener/SessionAccessListener.php +++ b/src/CourseBundle/EventListener/SessionAccessListener.php @@ -10,8 +10,6 @@ use Symfony\Component\HttpFoundation\RequestStack; /** * Class SessionAccessListener. - * - * @package Chamilo\CourseBundle\EventListener */ class SessionAccessListener { @@ -22,16 +20,27 @@ class SessionAccessListener */ protected $request; + /** + * SessionAccessListener constructor. + * + * @param EntityManager $em + */ public function __construct(EntityManager $em) { $this->em = $em; } + /** + * @param RequestStack $requestStack + */ public function setRequest(RequestStack $requestStack) { $this->request = $requestStack->getCurrentRequest(); } + /** + * @param SessionAccess $event + */ public function onSessionAccessEvent(SessionAccess $event) { $user = $event->getUser(); diff --git a/src/ThemeBundle/Resources/views/Resource/index.html.twig b/src/ThemeBundle/Resources/views/Resource/index.html.twig index 6f83893cd3..135ccaf4cd 100644 --- a/src/ThemeBundle/Resources/views/Resource/index.html.twig +++ b/src/ThemeBundle/Resources/views/Resource/index.html.twig @@ -1,18 +1,18 @@ {% extends "@ChamiloTheme/Layout/layout_one_col.html.twig" %} {% block content %} - {% if is_granted('ROLE_CURRENT_COURSE_TEACHER') %} + {% if is_granted('EDIT', course) %}
+ href="{{ url('chamilo_core_resource_new_folder', {'tool': tool, 'type': type, 'cidReq': course.code, 'id_session': sessionObj.id, 'id': id }) }}"> {{ 'New folder' | trans }} + href="{{ url('chamilo_core_resource_new', { 'tool': tool, 'type': type, 'cidReq': course.code, 'id_session': sessionObj.id, 'id': id }) }}"> {{ 'Create new document' | trans }} + href="{{ url('chamilo_core_resource_upload', { 'tool': tool, 'type': type, 'cidReq': course.code, 'id_session': sessionObj.id, 'id': id }) }}"> {{ 'Upload' | trans }}