From 676b563046cb60c872c96f42711401b5b6c09cd4 Mon Sep 17 00:00:00 2001 From: Canicio Sacha Date: Thu, 2 Nov 2023 16:30:35 +0100 Subject: [PATCH 01/20] feat: 108625 Update entity to handle email protection + generate new FormType --- bundle/Entity/ProtectedAccess.php | 24 +++++++-- bundle/Form/ProtectedAccessType.php | 4 +- .../Form/RequestEmailProtectedAccessType.php | 46 +++++++++++++++++ bundle/Listener/PreContentView.php | 51 ++++++++++++++----- .../translations/ezprotectedcontent.en.yml | 2 + .../translations/ezprotectedcontent.fr.yml | 2 + .../views/tabs/protected_content.html.twig | 3 ++ 7 files changed, 114 insertions(+), 18 deletions(-) create mode 100644 bundle/Form/RequestEmailProtectedAccessType.php diff --git a/bundle/Entity/ProtectedAccess.php b/bundle/Entity/ProtectedAccess.php index ba2a022..6a96962 100644 --- a/bundle/Entity/ProtectedAccess.php +++ b/bundle/Entity/ProtectedAccess.php @@ -38,8 +38,7 @@ class ProtectedAccess implements ContentInterface /** * @var string * - * @ORM\Column(type="string", length=255, nullable=false) - * @Assert\NotBlank() + * @ORM\Column(type="string", length=255, nullable=true) * @Assert\Length(max=255) */ protected $password; @@ -51,6 +50,13 @@ class ProtectedAccess implements ContentInterface */ protected $enabled; + /** + * @var bool + * + * @ORM\Column(type="boolean", nullable=false) + */ + protected $asEmail = false; + /** * @var bool * @@ -76,12 +82,24 @@ public function setId(int $id): self return $this; } + public function getAsEmail(): bool + { + return $this->asEmail ?? false; + } + + public function setAsEmail(bool $asEmail): self + { + $this->asEmail = $asEmail; + + return $this; + } + public function getPassword(): string { return $this->password ?? ''; } - public function setPassword(string $password): self + public function setPassword(?string $password = ''): self { $this->password = $password; diff --git a/bundle/Form/ProtectedAccessType.php b/bundle/Form/ProtectedAccessType.php index 6b5d384..40cda92 100644 --- a/bundle/Form/ProtectedAccessType.php +++ b/bundle/Form/ProtectedAccessType.php @@ -31,7 +31,9 @@ public function buildForm(FormBuilderInterface $builder, array $options): void ['label' => 'tab.table.th.children_protection', 'required' => false] ) ->add('enabled', CheckboxType::class, ['label' => 'tab.table.th.enabled', 'required' => false]) - ->add('password', TextType::class, ['required' => true, 'label' => 'tab.table.th.password']); + ->add('password', TextType::class, ['required' => false, 'label' => 'tab.table.th.password']) + ->add('asEmail', CheckboxType::class, ['label' => 'tab.table.th.as_email', 'required' => false]) + ; } public function configureOptions(OptionsResolver $resolver): void diff --git a/bundle/Form/RequestEmailProtectedAccessType.php b/bundle/Form/RequestEmailProtectedAccessType.php new file mode 100644 index 0000000..4a82e19 --- /dev/null +++ b/bundle/Form/RequestEmailProtectedAccessType.php @@ -0,0 +1,46 @@ +add( + 'email', + EmailType::class, + [ + 'required' => true, + 'label' => 'tab.table.th.email', + ] + ); + + $builder->add('submit', SubmitType::class); + } + + public function configureOptions(OptionsResolver $resolver): void + { + $resolver->setDefaults( + [ + 'translation_domain' => 'ezprotectedcontent', + ] + ); + } +} diff --git a/bundle/Listener/PreContentView.php b/bundle/Listener/PreContentView.php index ba47bdb..1dd9318 100644 --- a/bundle/Listener/PreContentView.php +++ b/bundle/Listener/PreContentView.php @@ -17,6 +17,7 @@ use eZ\Publish\Core\MVC\Symfony\Event\PreContentViewEvent; use eZ\Publish\Core\MVC\Symfony\View\ContentView; use Novactive\Bundle\eZProtectedContentBundle\Entity\ProtectedAccess; +use Novactive\Bundle\eZProtectedContentBundle\Form\RequestEmailProtectedAccessType; use Novactive\Bundle\eZProtectedContentBundle\Form\RequestProtectedAccessType; use Symfony\Component\Form\FormFactoryInterface; use Symfony\Component\HttpFoundation\RequestStack; @@ -78,18 +79,25 @@ public function onPreContentView(PreContentViewEvent $event) $canRead = $this->permissionResolver->canUser('private_content', 'read', $content); if (!$canRead) { - $cookies = $this->requestStack->getCurrentRequest()->cookies; - foreach ($cookies as $name => $value) { - if (PasswordProvided::COOKIE_PREFIX !== substr($name, 0, \strlen(PasswordProvided::COOKIE_PREFIX))) { - continue; - } - if (str_replace(PasswordProvided::COOKIE_PREFIX, '', $name) !== $value) { - continue; - } - foreach ($protections as $protection) { - /** @var ProtectedAccess $protection */ - if (md5($protection->getPassword()) === $value) { - $canRead = true; + $request = $this->requestStack->getCurrentRequest(); + + if ($request->query->has('mail') && $request->query->has('token')) { + dump($this->requestStack->getCurrentRequest()->query->get('token')); + dump($this->requestStack->getCurrentRequest()->query->get('mail')); + } else { + $cookies = $request->cookies; + foreach ($cookies as $name => $value) { + if (PasswordProvided::COOKIE_PREFIX !== substr($name, 0, \strlen(PasswordProvided::COOKIE_PREFIX))) { + continue; + } + if (str_replace(PasswordProvided::COOKIE_PREFIX, '', $name) !== $value) { + continue; + } + foreach ($protections as $protection) { + /** @var ProtectedAccess $protection */ + if (md5($protection->getPassword()) === $value) { + $canRead = true; + } } } } @@ -97,8 +105,23 @@ public function onPreContentView(PreContentViewEvent $event) $contentView->addParameters(['canReadProtectedContent' => $canRead]); if (!$canRead) { - $form = $this->formFactory->create(RequestProtectedAccessType::class); - $contentView->addParameters(['requestProtectedContentPasswordForm' => $form->createView()]); + if ($this->getContentProtectionType($protections) == 'by_mail') { + $form = $this->formFactory->create(RequestEmailProtectedAccessType::class); + $contentView->addParameters(['requestProtectedContentEmailForm' => $form->createView()]); + } else { + $form = $this->formFactory->create(RequestProtectedAccessType::class); + $contentView->addParameters(['requestProtectedContentPasswordForm' => $form->createView()]); + } + } + } + + private function getContentProtectionType(array $protections): string { + foreach ($protections as $protection) { + /** @var ProtectedAccess $protection */ + if ( !is_null($protection->getPassword()) && $protection->getPassword() != '' ) { + return 'by_password'; + } } + return 'by_mail'; } } diff --git a/bundle/Resources/translations/ezprotectedcontent.en.yml b/bundle/Resources/translations/ezprotectedcontent.en.yml index 3c8db4b..b7b963e 100644 --- a/bundle/Resources/translations/ezprotectedcontent.en.yml +++ b/bundle/Resources/translations/ezprotectedcontent.en.yml @@ -7,6 +7,8 @@ tab.modal.buttons.add: "Add" tab.table.th.password: "Password" tab.table.th.children_protection: "Protect Children?" tab.table.th.enabled: "Enabled?" +tab.table.th.as_email: "Protection by mail" +tab.table.th.email: "Your email" tab.table.th.remove: "Remove" tab.yes: "YES" diff --git a/bundle/Resources/translations/ezprotectedcontent.fr.yml b/bundle/Resources/translations/ezprotectedcontent.fr.yml index 2039255..7f5f963 100644 --- a/bundle/Resources/translations/ezprotectedcontent.fr.yml +++ b/bundle/Resources/translations/ezprotectedcontent.fr.yml @@ -7,6 +7,8 @@ tab.modal.buttons.add: "Ajouter" tab.table.th.password: "Mot de passe" tab.table.th.children_protection: "Protéger les contenus enfants?" tab.table.th.enabled: "Activer?" +tab.table.th.as_email: "Protection par email" +tab.table.th.email: "Votre adresse email" tab.table.th.remove: "Supprimer" tab.yes: "OUI" diff --git a/bundle/Resources/views/tabs/protected_content.html.twig b/bundle/Resources/views/tabs/protected_content.html.twig index 92c314e..a343510 100644 --- a/bundle/Resources/views/tabs/protected_content.html.twig +++ b/bundle/Resources/views/tabs/protected_content.html.twig @@ -24,6 +24,7 @@ {{ form_row(form.password) }} {{ form_row(form.protectChildren) }} {{ form_row(form.enabled) }} + {{ form_row(form.asEmail) }}