Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
21 changes: 16 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,27 @@ A bundle that provides quick password protection on Contents.
# How it works

Allows you to add 1 on N password on a Content in the Admin UI.
Once a protection is set, the Content becomes Protected.
In this situation you can have 3 new variables in the view full
- canReadProtectedContent (always)
- requestProtectedContentPasswordForm (if content is protected by password)
- requestProtectedContentEmailForm (if content is protected with email verification)

Once a Password is set, the Content becomes Protected. In this situation you will have 2 new variables in the view full.
Allowing you do:
```twig
<h2>{{ ez_content_name(content) }}</h2>
{% if not canReadProtectedContent %}
<p>This content has been protected by a password</p>
<div class="protected-content-form">
{{ form(requestProtectedContentPasswordForm) }}
</div>
{% if requestProtectedContentPasswordForm is defined %}
<p>This content has been protected by a password</p>
<div class="protected-content-form">
{{ form(requestProtectedContentPasswordForm) }}
</div>
{% elseif requestProtectedContentEmailForm is defined %}
<p>This content has been protected by an email verification</p>
<div class="protected-content-form">
{{ form(requestProtectedContentEmailForm) }}
</div>
{% endif %}
{% else %}
{% for field in content.fieldsByLanguage(language|default(null)) %}
<h3>{{ field.fieldDefIdentifier }}</h3>
Expand Down
65 changes: 65 additions & 0 deletions bundle/Command/CleanTokenCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?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\Command;

use Doctrine\ORM\EntityManagerInterface;
use Novactive\Bundle\eZProtectedContentBundle\Entity\ProtectedTokenStorage;
use Novactive\Bundle\eZProtectedContentBundle\Repository\ProtectedTokenStorageRepository;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

class CleanTokenCommand extends Command
{
/**
* @var EntityManagerInterface
*/
private $entityManager;

public function __construct(EntityManagerInterface $entityManager)
{
parent::__construct();
$this->entityManager = $entityManager;

}

protected function configure(): void
{
$this
->setName('novaezprotectedcontent:cleantoken')
->setDescription('Remove expired token in the DB');
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$io = new SymfonyStyle($input, $output);

/** @var ProtectedTokenStorageRepository $protectedTokenStorageRepository */
$protectedTokenStorageRepository = $this->entityManager->getRepository(ProtectedTokenStorage::class);

$entities = $protectedTokenStorageRepository->findExpired();

foreach ($entities as $entity) {
$this->entityManager->remove($entity);
}

$this->entityManager->flush();

$io->success(sprintf('%d entities deleted', count($entities)));
$io->success('Done.');
}
}
120 changes: 94 additions & 26 deletions bundle/Controller/Admin/ProtectedAccessController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,43 @@

use DateTime;
use Doctrine\ORM\EntityManagerInterface;
use eZ\Publish\API\Repository\Values\Content\Location;
use EzSystems\PlatformHttpCacheBundle\PurgeClient\PurgeClientInterface;
use Ibexa\Contracts\Core\Repository\Values\Content\Content;
use Ibexa\Contracts\Core\Repository\Values\Content\Location;
use Ibexa\Contracts\Core\Repository\Values\Content\Query;
use Ibexa\Contracts\HttpCache\Handler\ContentTagInterface;
use Ibexa\Core\Repository\SiteAccessAware\Repository;
use Novactive\Bundle\eZProtectedContentBundle\Entity\ProtectedAccess;
use Novactive\Bundle\eZProtectedContentBundle\Form\ProtectedAccessType;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\Form\FormFactory;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\RouterInterface;

class ProtectedAccessController
{
public function __construct(
protected readonly Repository $repository,
protected readonly \Ibexa\Contracts\Core\Search\Handler $searchHandler,
protected readonly \Ibexa\Contracts\Core\Persistence\Handler $persistenceHandler,
) { }

/**
* @Route("/handle/{locationId}/{access}", name="novaezprotectedcontent_bundle_admin_handle_form",
* defaults={"accessId": null})
*/
//#[Route(path: '/handle/{locationId}/{access}', name: 'novaezprotectedcontent_bundle_admin_handle_form')]
public function handle(
Location $location,
int $locationId,
Request $request,
FormFactory $formFactory,
FormFactoryInterface $formFactory,
EntityManagerInterface $entityManager,
RouterInterface $router,
PurgeClientInterface $httpCachePurgeClient,
?ProtectedAccess $access = null
ContentTagInterface $responseTagger,
?ProtectedAccess $access = null,
): RedirectResponse {
if ($request->isMethod('post')) {
$location = $this->repository->getLocationService()->loadLocation($locationId);
$now = new DateTime();
if (null === $access) {
$access = new ProtectedAccess();
Expand All @@ -52,38 +63,95 @@ public function handle(
$access->setUpdated($now);
$entityManager->persist($access);
$entityManager->flush();
$httpCachePurgeClient->purge(
[
'location-'.$location->id,
'location-'.$location->parentLocationId,
]
);
$responseTagger->addLocationTags([$location->id]);
$responseTagger->addParentLocationTags([$location->parentLocationId]);

$content = $location->getContent();
$this->reindexContent($content);
if ($access->isProtectChildren()) {
$this->reindexChildren($content);
}
}
}

return new RedirectResponse($router->generate($location).'#ez-tab-location-view-protect-content#tab');
return new RedirectResponse(
$router->generate('ibexa.content.view', ['contentId' => $location->contentId,
'locationId' => $location->id,
]).
'#ibexa-tab-location-view-protect-content#tab'
);
}

/**
* @Route("/remove/{locationId}/{access}", name="novaezprotectedcontent_bundle_admin_remove_protection")
*/
#[Route(path: '/remove/{locationId}/{access}', name: 'novaezprotectedcontent_bundle_admin_remove_protection')]
public function remove(
Location $location,
EntityManagerInterface $entityManager,
RouterInterface $router,
ProtectedAccess $access,
PurgeClientInterface $httpCachePurgeClient
int $access,
ContentTagInterface $responseTagger
): RedirectResponse {
$access = $entityManager->find(ProtectedAccess::class, $access);
$entityManager->remove($access);
$entityManager->flush();
$responseTagger->addLocationTags([$location->id]);
$responseTagger->addParentLocationTags([$location->parentLocationId]);

$httpCachePurgeClient->purge(
[
'location-'.$location->id,
'location-'.$location->parentLocationId,
]
$content = $location->getContent();
$this->reindexContent($content);
if ($access->isProtectChildren()) {
$this->reindexChildren($content);
}

return new RedirectResponse(
$router->generate('ibexa.content.view', ['contentId' => $location->contentId,
'locationId' => $location->id,
]).
'#ibexa-tab-location-view-protect-content#tab'
);
}

return new RedirectResponse($router->generate($location).'#ez-tab-location-view-protect-content#tab');
/**
* @param Content $content
* @return void
*/
protected function reindexContent(Content $content)
{
$contentId = $content->id;
$contentVersionNo = $content->getVersionInfo()->versionNo;

$this->searchHandler->indexContent(
$this->persistenceHandler->contentHandler()->load($contentId, $contentVersionNo)
);

$locations = $this->persistenceHandler->locationHandler()->loadLocationsByContent($contentId);
foreach ($locations as $location) {
$this->searchHandler->indexLocation($location);
}
}

protected function reindexChildren(Content $content, int $limit = 100)
{
$locations = $this->repository->getLocationService()->loadLocations($content->contentInfo);
$pathStringArray = [];
foreach ($locations as $location) {
/** @var Location $location */
$pathStringArray[] = $location->pathString;
}

if ($pathStringArray) {
$query = new Query();
$query->limit = $limit;
$query->filter = new Query\Criterion\LogicalAnd([
new Query\Criterion\Subtree($pathStringArray)
]);
$query->sortClauses = [
new Query\SortClause\ContentId(),
// new Query\SortClause\Visibility(), // domage..
];
$searchResult = $this->repository->getSearchService()->findContent($query);
foreach ($searchResult->searchHits as $hit) {
$this->reindexContent($hit->valueObject);
}
}
}
}
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;
}
}
Loading