Skip to content
Open
Show file tree
Hide file tree
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 …
Nov 2, 2023
0fb45dd
feat: 108625 Store token to access protected content and sendEmail
Nov 6, 2023
5020002
feat: 108625 Change receiver to get from parameters
Nov 7, 2023
eca146e
feat: 108625 Fix translations issue for mail link + add test for view…
Nov 7, 2023
39f6091
feat: 108625 Change container for parameterBag to getParams
Nov 7, 2023
76ca647
feat: 108625 add command to remove expired token
Nov 7, 2023
3dc8a41
feat: 108625 change token generation
Nov 7, 2023
b7d960d
feat: 108625 Use translation for mail subject
Nov 7, 2023
0f92353
feat: 108625 refacto query to match token
Nov 8, 2023
472fa9d
feat: 108625 Add custom balise for description and replacement at lin…
Nov 9, 2023
4eeec36
feat: 108625 Fix queryException for mail search
Nov 9, 2023
189d73c
feat: 108625 Add clean command to cron
Nov 9, 2023
5ee049b
feat: 108625 Fix translation for courriel / email
Nov 13, 2023
6e2d447
feat: 108625 add more translations for protected area
Nov 13, 2023
585179d
feat: 108625 fix mistake by swapping translations in file
Nov 14, 2023
3b5f6f0
feat: 108625 update documentation
Nov 14, 2023
b00579b
feat: 108625 fix typo in translations
Nov 14, 2023
57dcf2f
Reindex contents after a protection change
RemyNovactive May 27, 2025
2385d57
Reindex contents after a protection change
RemyNovactive May 27, 2025
39ee4e2
remove Reindex contents after a protection change
RemyNovactive May 27, 2025
4aa122e
Merge remote-tracking branch 'origin/feat-108625-form-mail-protection…
RemyNovactive May 27, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 38 additions & 3 deletions bundle/Entity/ProtectedAccess.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -51,13 +50,27 @@ class ProtectedAccess implements ContentInterface
*/
protected $enabled;

/**
* @var bool
*
* @ORM\Column(type="boolean", nullable=false)
*/
protected $asEmail = false;

/**
* @var bool
*
* @ORM\Column(type="boolean", nullable=false)
*/
protected $protectChildren;

/**
* @var string
*
* @ORM\Column(type="string", nullable=true)
*/
protected $emailMessage;

public function __construct()
{
$this->enabled = true;
Expand All @@ -76,12 +89,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;

Expand Down Expand Up @@ -109,4 +134,14 @@ public function setProtectChildren(bool $protectChildren): void
{
$this->protectChildren = $protectChildren;
}

public function getEmailMessage(): ?string
{
return $this->emailMessage;
}

public function setEmailMessage(string $emailMessage): void
{
$this->emailMessage = $emailMessage;
}
}
111 changes: 111 additions & 0 deletions bundle/Entity/ProtectedTokenStorage.php
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;
}
}
7 changes: 6 additions & 1 deletion bundle/Form/ProtectedAccessType.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
namespace Novactive\Bundle\eZProtectedContentBundle\Form;

use Novactive\Bundle\eZProtectedContentBundle\Entity\ProtectedAccess;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

Expand All @@ -31,7 +33,10 @@ 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])
->add('emailMessage', TextareaType::class, ['label' => 'tab.table.th.message_email', 'required' => false])
;
}

public function configureOptions(OptionsResolver $resolver): void
Expand Down
47 changes: 47 additions & 0 deletions bundle/Form/RequestEmailProtectedAccessType.php
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',
]
);
}
}
134 changes: 134 additions & 0 deletions bundle/Listener/EmailProvided.php
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')
->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>"
);

try {
$this->mailer->send($message);
} catch (Exception $exception) {
throw new Exception(sprintf(self::SENDMAIL_ERROR, $receiver));
}
}
}
Loading