-
Notifications
You must be signed in to change notification settings - Fork 3
Feat 108625 form mail protection #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
s-canicio
wants to merge
21
commits into
release-0.x.x
Choose a base branch
from
feat-108625-form-mail-protection
base: release-0.x.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
676b563
feat: 108625 Update entity to handle email protection + generate new …
0fb45dd
feat: 108625 Store token to access protected content and sendEmail
5020002
feat: 108625 Change receiver to get from parameters
eca146e
feat: 108625 Fix translations issue for mail link + add test for view…
39f6091
feat: 108625 Change container for parameterBag to getParams
76ca647
feat: 108625 add command to remove expired token
3dc8a41
feat: 108625 change token generation
b7d960d
feat: 108625 Use translation for mail subject
0f92353
feat: 108625 refacto query to match token
472fa9d
feat: 108625 Add custom balise for description and replacement at lin…
4eeec36
feat: 108625 Fix queryException for mail search
189d73c
feat: 108625 Add clean command to cron
5ee049b
feat: 108625 Fix translation for courriel / email
6e2d447
feat: 108625 add more translations for protected area
585179d
feat: 108625 fix mistake by swapping translations in file
3b5f6f0
feat: 108625 update documentation
b00579b
feat: 108625 fix typo in translations
57dcf2f
Reindex contents after a protection change
RemyNovactive 2385d57
Reindex contents after a protection change
RemyNovactive 39ee4e2
remove Reindex contents after a protection change
RemyNovactive 4aa122e
Merge remote-tracking branch 'origin/feat-108625-form-mail-protection…
RemyNovactive File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * NovaeZProtectedContentBundle. | ||
| * | ||
| * @package Novactive\Bundle\eZProtectedContentBundle | ||
| * | ||
| * @author Novactive | ||
| * @copyright 2023 Novactive | ||
| * @license https://github.com/Novactive/eZProtectedContentBundle/blob/master/LICENSE MIT Licence | ||
| */ | ||
| declare(strict_types=1); | ||
|
|
||
| namespace Novactive\Bundle\eZProtectedContentBundle\Entity; | ||
|
|
||
| use DateTime; | ||
| use Doctrine\ORM\Mapping as ORM; | ||
| use Symfony\Component\Validator\Constraints as Assert; | ||
|
|
||
| /** | ||
| * @ORM\Entity(repositoryClass="Novactive\Bundle\eZProtectedContentBundle\Repository\ProtectedTokenStorageRepository") | ||
| * @ORM\Table(name="novaezprotectedcontentstorage") | ||
| */ | ||
| class ProtectedTokenStorage { | ||
| /** | ||
| * @var int | ||
| * | ||
| * @ORM\Id | ||
| * @ORM\Column(type="integer") | ||
| * @ORM\GeneratedValue(strategy="AUTO") | ||
| */ | ||
| private $id; | ||
|
|
||
| /** | ||
| * @var DateTime | ||
| * @ORM\Column(name="created", type="datetime") | ||
| */ | ||
| private $created; | ||
|
|
||
| /** | ||
| * @var string | ||
| * | ||
| * @ORM\Column(type="string", length=255, nullable=false) | ||
| * @Assert\Length(max=255) | ||
| */ | ||
| protected $token; | ||
|
|
||
| /** | ||
| * @var string | ||
| * | ||
| * @ORM\Column(type="string", length=255, nullable=false) | ||
| * @Assert\Length(max=255) | ||
| */ | ||
| protected $mail; | ||
|
|
||
| /** | ||
| * @var int | ||
| * | ||
| * @ORM\Column(type="integer") | ||
| */ | ||
| protected $content_id; | ||
|
|
||
| public function getId(): int | ||
| { | ||
| return $this->id; | ||
| } | ||
|
|
||
| public function setId(int $id): void | ||
| { | ||
| $this->id = $id; | ||
| } | ||
| public function getCreated(): DateTime | ||
| { | ||
| return $this->created; | ||
| } | ||
|
|
||
| public function setCreated(DateTime $created): void | ||
| { | ||
| $this->created = $created; | ||
| } | ||
|
|
||
| public function getToken(): string | ||
| { | ||
| return $this->token; | ||
| } | ||
|
|
||
| public function setToken(string $token): void | ||
| { | ||
| $this->token = $token; | ||
| } | ||
|
|
||
| public function getMail(): string | ||
| { | ||
| return $this->mail; | ||
| } | ||
|
|
||
| public function setMail(string $mail): void | ||
| { | ||
| $this->mail = $mail; | ||
| } | ||
|
|
||
| public function getContentId(): int | ||
| { | ||
| return $this->content_id; | ||
| } | ||
|
|
||
| public function setContentId(int $content_id): void | ||
| { | ||
| $this->content_id = $content_id; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| <?php | ||
| /** | ||
| * NovaeZProtectedContentBundle. | ||
| * | ||
| * @package Novactive\Bundle\eZProtectedContentBundle | ||
| * | ||
| * @author Novactive | ||
| * @copyright 2019 Novactive | ||
| * @license https://github.com/Novactive/eZProtectedContentBundle/blob/master/LICENSE MIT Licence | ||
| */ | ||
| declare(strict_types=1); | ||
|
|
||
| namespace Novactive\Bundle\eZProtectedContentBundle\Form; | ||
|
|
||
| use Symfony\Component\Form\AbstractType; | ||
| use Symfony\Component\Form\Extension\Core\Type\EmailType; | ||
| use Symfony\Component\Form\Extension\Core\Type\HiddenType; | ||
| use Symfony\Component\Form\Extension\Core\Type\SubmitType; | ||
| use Symfony\Component\Form\Extension\Core\Type\TextType; | ||
| use Symfony\Component\Form\FormBuilderInterface; | ||
| use Symfony\Component\OptionsResolver\OptionsResolver; | ||
|
|
||
| class RequestEmailProtectedAccessType extends AbstractType | ||
| { | ||
| public function buildForm(FormBuilderInterface $builder, array $options): void | ||
| { | ||
| $builder->add( | ||
| 'email', | ||
| EmailType::class, | ||
| [ | ||
| 'required' => true, | ||
| 'label' => 'tab.table.th.email', | ||
| ] | ||
| ); | ||
| $builder->add('content_id', HiddenType::class); | ||
| $builder->add('submit', SubmitType::class); | ||
| } | ||
|
|
||
| public function configureOptions(OptionsResolver $resolver): void | ||
| { | ||
| $resolver->setDefaults( | ||
| [ | ||
| 'translation_domain' => 'ezprotectedcontent', | ||
| ] | ||
| ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| <?php | ||
| /** | ||
| * NovaeZProtectedContentBundle. | ||
| * | ||
| * @package Novactive\Bundle\eZProtectedContentBundle | ||
| * | ||
| * @author Novactive | ||
| * @copyright 2019 Novactive | ||
| * @license https://github.com/Novactive/eZProtectedContentBundle/blob/master/LICENSE MIT Licence | ||
| */ | ||
| declare(strict_types=1); | ||
|
|
||
| namespace Novactive\Bundle\eZProtectedContentBundle\Listener; | ||
|
|
||
| use DateTime; | ||
| use Doctrine\ORM\EntityManagerInterface; | ||
| use Exception; | ||
| use Novactive\Bundle\eZProtectedContentBundle\Entity\ProtectedAccess; | ||
| use Novactive\Bundle\eZProtectedContentBundle\Entity\ProtectedTokenStorage; | ||
| use Novactive\Bundle\eZProtectedContentBundle\Form\RequestEmailProtectedAccessType; | ||
| use Swift_Message; | ||
| use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; | ||
| use Symfony\Component\Form\FormFactoryInterface; | ||
| use Symfony\Component\HttpFoundation\RedirectResponse; | ||
| use Symfony\Component\HttpKernel\Event\GetResponseEvent; | ||
| use Swift_Mailer; | ||
| use Symfony\Component\Translation\TranslatorInterface; | ||
|
|
||
| class EmailProvided | ||
| { | ||
| protected const SENDMAIL_ERROR = 'Impossible d\'envoyer le lien formaté à l\'adresse mail %s'; | ||
| /** | ||
| * @var FormFactoryInterface | ||
| */ | ||
| private $formFactory; | ||
| /** | ||
| * @var Swift_Mailer | ||
| */ | ||
| private $mailer; | ||
| /** | ||
| * @var Swift_Message | ||
| */ | ||
| protected $messageInstance; | ||
|
|
||
| /** | ||
| * @var EntityManagerInterface | ||
| */ | ||
| private $entityManager; | ||
|
|
||
| /** | ||
| * @var TranslatorInterface | ||
| */ | ||
| private $translator; | ||
|
|
||
| /** | ||
| * @var ParameterBagInterface | ||
| */ | ||
| private $parameterBag; | ||
|
|
||
| public function __construct( | ||
| FormFactoryInterface $formFactory, | ||
| Swift_Mailer $mailer, | ||
| EntityManagerInterface $entityManager, | ||
| TranslatorInterface $translator, | ||
| ParameterBagInterface $parameterBag | ||
| ) | ||
| { | ||
| $this->formFactory = $formFactory; | ||
| $this->mailer = $mailer; | ||
| $this->entityManager = $entityManager; | ||
| $this->translator = $translator; | ||
| $this->parameterBag = $parameterBag; | ||
| $this->messageInstance = new Swift_Message(); | ||
|
|
||
| } | ||
|
|
||
| public function onKernelRequest(GetResponseEvent $event): void | ||
| { | ||
| if (!$event->isMasterRequest()) { | ||
| return; | ||
| } | ||
| $form = $this->formFactory->create(RequestEmailProtectedAccessType::class); | ||
|
|
||
| $request = $event->getRequest(); | ||
| $form->handleRequest($request); | ||
|
|
||
| if ($form->isSubmitted() && $form->isValid()) { | ||
| $data = $form->getData(); | ||
| $contentId = intval($data['content_id']); | ||
| $randomString = bin2hex(random_bytes(16)); | ||
| $token = substr($randomString, 0, 16); | ||
|
|
||
| $access = new ProtectedTokenStorage(); | ||
| $access->setMail($data['email']); | ||
| $access->setContentId($contentId); | ||
| $access->setCreated(new DateTime()); | ||
| $access->setToken($token); | ||
|
|
||
| $this->entityManager->persist($access); | ||
| $this->entityManager->flush(); | ||
|
|
||
| $currentUrl = $request->getScheme().'://'.$request->getHost().$request->getBaseUrl().$request->getRequestUri(); | ||
| $accessUrl = $currentUrl."?mail=".$data['email']."&token=".$token; | ||
| $this->sendMail($contentId, $data['email'], $accessUrl); | ||
| $response = new RedirectResponse($request->getRequestUri()."?waiting_validation=".$data['email']); | ||
| $response->setPrivate(); | ||
| $event->setResponse($response); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @throws Exception | ||
| */ | ||
| private function sendMail(int $contentId, string $receiver, string $link): void { | ||
| /** @var ProtectedAccess $protectedAccess */ | ||
| $protectedAccess = $this->entityManager->getRepository(ProtectedAccess::class)->findOneBy(['contentId' => $contentId]); | ||
|
|
||
| $message = $this->messageInstance | ||
| ->setSubject('Access to protected content') | ||
s-canicio marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ->setFrom($this->parameterBag->get('default_sender_email')) | ||
| ->setTo($receiver) | ||
| ->setContentType('text/html') | ||
| ->setBody( | ||
| $protectedAccess->getEmailMessage() | ||
| . "</br><a href='$link'>".$this->translator->trans('mail.link', [], 'ezprotectedcontent')."</a>" | ||
| ); | ||
s-canicio marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| try { | ||
| $this->mailer->send($message); | ||
| } catch (Exception $exception) { | ||
| throw new Exception(sprintf(self::SENDMAIL_ERROR, $receiver)); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.