From d66e3060b3728e1de0f5598163d1d83b87899740 Mon Sep 17 00:00:00 2001 From: Sebastien Curt <sebastien.curt@tetras-libre.fr> Date: Thu, 20 Jan 2022 11:14:50 +0100 Subject: [PATCH] Allow acces to the capsule in edition mode with the url "capule/edit/${path}" --- src/Builder/CapsuleBuilder.php | 4 +-- src/Controller/CapsuleController.php | 33 ++++++++++++++++++- src/Controller/ProjectController.php | 4 +-- src/Entity/Capsule.php | 10 +++--- templates/capsule/index.html.twig | 8 ++--- .../project_view.html.twig} | 0 tests/functional/ProjectControllerTest.php | 4 +-- 7 files changed, 47 insertions(+), 16 deletions(-) rename templates/{capsule/preview.html.twig => project/project_view.html.twig} (100%) diff --git a/src/Builder/CapsuleBuilder.php b/src/Builder/CapsuleBuilder.php index ea2dca1..30be954 100644 --- a/src/Builder/CapsuleBuilder.php +++ b/src/Builder/CapsuleBuilder.php @@ -46,7 +46,7 @@ class CapsuleBuilder public function withPreviewLink(string $preview_link): CapsuleBuilder { - $this->capsule->setPreviewLink($preview_link); + $this->capsule->setPath($preview_link); $this->hasRequiredPreviewLink = true; return $this; } @@ -63,7 +63,7 @@ class CapsuleBuilder "The call of CapsuleBuilder::withPassword should be " . "called before CapsuleBuilder::createEditionLink" ); - $this->capsule->setEditionLink($this->capsule->getPreviewLink() . + $this->capsule->setEditionLink($this->capsule->getPath() . "/?p=" . $this->capsule->getPassword()); $this->hasRequiredEditionLink = true; } diff --git a/src/Controller/CapsuleController.php b/src/Controller/CapsuleController.php index 551729f..f07cd37 100644 --- a/src/Controller/CapsuleController.php +++ b/src/Controller/CapsuleController.php @@ -12,6 +12,7 @@ use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Uid\Uuid; +use Symfony\Contracts\Translation\TranslatorInterface; class CapsuleController extends AbstractController { @@ -93,7 +94,37 @@ class CapsuleController extends AbstractController $url = $this->getParameter('app.legacy_external_prefix') . '/' . $path . "/?w=1"; return $this->render( - 'capsule/preview.html.twig', + 'project/project_view.html.twig', + [ 'url' => $url ] + ); + } + + /** + * @Route("capsule/edit/{path}", name="edit_capsule") + */ + public function edit(string $path, CapsuleRepository $capsuleRepository, TranslatorInterface $translator): Response + { + $current_user = $this->getUser(); + + if (! $current_user instanceof User) { + return $this->redirectToRoute('app_logout'); + } + + $capsule = $capsuleRepository->findOneBy(['link_path' => $path, 'creation_author' => $current_user]); + if (null === $capsule) { + $this->addFlash('warning', $translator->trans('capsule.edition_not_allowed')); + return $this->redirectToRoute('capsule_list'); + } + + $file_path = '../legacy/' . $path; + if (!file_exists($file_path)) { + return $this->render('capsule/capsule_not_found.html.twig'); + } + + $url = $this->getParameter('app.legacy_external_prefix') . '/' . $capsule->getEditionLink(); + + return $this->render( + 'project/project_view.html.twig', [ 'url' => $url ] ); } diff --git a/src/Controller/ProjectController.php b/src/Controller/ProjectController.php index 3e08982..6dcc988 100644 --- a/src/Controller/ProjectController.php +++ b/src/Controller/ProjectController.php @@ -27,11 +27,11 @@ class ProjectController extends AbstractController chdir('../legacy/'); $capsule_name = $capsule->getName(); - $capsule_directory = $capsule->getPreviewLink(); + $capsule_directory = $capsule->getPath(); if (file_exists($capsule_directory)) { $this->addFlash( - 'project_already_exists', + 'warning', $translator->trans( 'project.already_exists', [ diff --git a/src/Entity/Capsule.php b/src/Entity/Capsule.php index 3b4a39d..6fbe87c 100644 --- a/src/Entity/Capsule.php +++ b/src/Entity/Capsule.php @@ -62,7 +62,7 @@ class Capsule * * @ORM\Column(name="link", type="string", length=255, nullable=false) */ - private string $preview_link; + private string $link_path; /** * @@ -121,14 +121,14 @@ class Capsule return $this; } - public function getPreviewLink(): string + public function getPath(): string { - return $this->preview_link; + return $this->link_path; } - public function setPreviewLink(string $preview_link): Capsule + public function setPath(string $preview_link): Capsule { - $this->preview_link = $preview_link; + $this->link_path = $preview_link; return $this; } diff --git a/templates/capsule/index.html.twig b/templates/capsule/index.html.twig index 251d356..37abbc1 100644 --- a/templates/capsule/index.html.twig +++ b/templates/capsule/index.html.twig @@ -21,7 +21,7 @@ </form> </div> - {% for flashWarning in app.flashes('project_already_exists') %} + {% for flashWarning in app.flashes('warning') %} <div class="text-center alert alert-warning col-5 mx-auto my-5" role="alert"> {{ flashWarning }} </div> @@ -39,7 +39,7 @@ <div class="capsule-item pb-4 m-5"> <div class="d-flex flex-column flex-md-row justify-content-center align-items-center mt-sm-4"> <div class="list-item"> - <a href="/capsule/preview/{{ capsule.getPreviewLink() }}" class="capsule-title"> + <a href="/capsule/preview/{{ capsule.getPath() }}" class="capsule-title"> {{ capsule.getName() }} </a> </div> @@ -51,7 +51,7 @@ <div class="d-flex flex-column flex-md-row justify-content-center align-items-center"> <div class="m-4 ratio ratio-16x9"> <iframe - src="/capsule/preview/{{ capsule.getPreviewLink() }}" + src="/capsule/preview/{{ capsule.getPath() }}" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen> </iframe> @@ -74,7 +74,7 @@ Delete capsule </a> </div> - <a href="{{ capsule.getEditionLink() }}" class="list-item lh-md"> + <a href="capsule/edit/{{ capsule.getPath() }}" class="list-item lh-md"> <button class="standard-button p-2"> Edit capsule </button> diff --git a/templates/capsule/preview.html.twig b/templates/project/project_view.html.twig similarity index 100% rename from templates/capsule/preview.html.twig rename to templates/project/project_view.html.twig diff --git a/tests/functional/ProjectControllerTest.php b/tests/functional/ProjectControllerTest.php index 8687740..7160a29 100644 --- a/tests/functional/ProjectControllerTest.php +++ b/tests/functional/ProjectControllerTest.php @@ -117,7 +117,7 @@ class ProjectControllerTest extends WebTestCase } $capsule_name_in_db = $capsule_in_db->getName(); - $capsule_directory = $capsule_in_db->getPreviewLink(); + $capsule_directory = $capsule_in_db->getPath(); $this->assertDirectoryExists($capsule_directory); $this->assertDirectoryIsReadable($capsule_directory); @@ -160,7 +160,7 @@ class ProjectControllerTest extends WebTestCase } $capsule_name_in_db = $capsule_in_db->getName(); - $capsule_directory = $capsule_in_db->getPreviewLink(); + $capsule_directory = $capsule_in_db->getPath(); $this->assertDirectoryExists($capsule_directory); $this->assertDirectoryIsReadable($capsule_directory); -- GitLab