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
0 commit comments