1414use Chamilo \CoreBundle \Repository \Node \CourseRepository ;
1515use Chamilo \CoreBundle \Repository \SystemTemplateRepository ;
1616use Chamilo \CoreBundle \Repository \TemplatesRepository ;
17+ use Chamilo \CoreBundle \Security \Authorization \Voter \CourseVoter ;
1718use Chamilo \CoreBundle \Settings \SettingsManager ;
1819use Chamilo \CourseBundle \Entity \CDocument ;
1920use Chamilo \CourseBundle \Repository \CDocumentRepository ;
@@ -40,6 +41,18 @@ public function createDocumentTemplate(Request $request, EntityManagerInterface
4041 return $ this ->json (['error ' => 'No image provided. ' ], Response::HTTP_BAD_REQUEST );
4142 }
4243
44+ $ document = $ entityManager ->getRepository (CDocument::class)->find ($ documentId );
45+ if (!$ document ) {
46+ return $ this ->json (['error ' => 'Document not found. ' ], Response::HTTP_NOT_FOUND );
47+ }
48+
49+ $ documentCourse = $ document ->getFirstResourceLink ()?->getCourse();
50+ if (!$ documentCourse instanceof Course) {
51+ throw $ this ->createAccessDeniedException ();
52+ }
53+
54+ $ this ->denyAccessUnlessGranted (CourseVoter::EDIT , $ documentCourse );
55+
4356 $ user = $ userHelper ->getCurrent ();
4457 $ course = null ;
4558 if ($ cid ) {
@@ -62,13 +75,8 @@ public function createDocumentTemplate(Request $request, EntityManagerInterface
6275 $ template ->setImage ($ asset );
6376 $ entityManager ->persist ($ template );
6477
65- $ document = $ entityManager ->getRepository (CDocument::class)->find ($ documentId );
66- if ($ document ) {
67- $ document ->setTemplate (true );
68- $ entityManager ->persist ($ document );
69- } else {
70- return $ this ->json (['error ' => 'Document not found. ' ], Response::HTTP_NOT_FOUND );
71- }
78+ $ document ->setTemplate (true );
79+ $ entityManager ->persist ($ document );
7280
7381 $ entityManager ->flush ();
7482
@@ -88,6 +96,18 @@ public function isDocumentTemplate(int $documentId, EntityManagerInterface $enti
8896 #[Route('/document-templates/{documentId}/delete ' , methods: ['POST ' ])]
8997 public function deleteDocumentTemplate (int $ documentId , EntityManagerInterface $ entityManager ): Response
9098 {
99+ $ document = $ entityManager ->getRepository (CDocument::class)->find ($ documentId );
100+ if (!$ document ) {
101+ return $ this ->json (['error ' => 'Document not found. ' ], Response::HTTP_NOT_FOUND );
102+ }
103+
104+ $ course = $ document ->getFirstResourceLink ()?->getCourse();
105+ if (!$ course instanceof Course) {
106+ throw $ this ->createAccessDeniedException ();
107+ }
108+
109+ $ this ->denyAccessUnlessGranted (CourseVoter::EDIT , $ course );
110+
91111 $ template = $ entityManager ->getRepository (Templates::class)->findOneBy (['refDoc ' => $ documentId ]);
92112
93113 if (!$ template ) {
@@ -96,13 +116,8 @@ public function deleteDocumentTemplate(int $documentId, EntityManagerInterface $
96116
97117 $ entityManager ->remove ($ template );
98118
99- $ document = $ entityManager ->getRepository (CDocument::class)->find ($ documentId );
100- if ($ document ) {
101- $ document ->setTemplate (false );
102- $ entityManager ->persist ($ document );
103- } else {
104- return $ this ->json (['error ' => 'Document not found. ' ], Response::HTTP_NOT_FOUND );
105- }
119+ $ document ->setTemplate (false );
120+ $ entityManager ->persist ($ document );
106121
107122 $ entityManager ->flush ();
108123
@@ -126,6 +141,8 @@ public function getAllTemplates(
126141 throw new NotFoundHttpException ('Course not found ' );
127142 }
128143
144+ $ this ->denyAccessUnlessGranted (CourseVoter::VIEW , $ course );
145+
129146 $ languageFilterEnabled = $ this ->isSettingEnabled (
130147 $ settingsManager ->getSetting ('language.template_activate_language_filter ' , true )
131148 );
0 commit comments