Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
26 changes: 18 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,30 @@ Documentation is available in this repository via `.md` files but also packaged

A bundle that provides quick password protection on Contents.

## How does it work?
# 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>{{ ibexa_content_name(content) }}</h2>
<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
60 changes: 60 additions & 0 deletions bundle/Command/CleanTokenCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?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 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
{
public function __construct(
protected ProtectedTokenStorageRepository $protectedTokenStorageRepository,
) {
parent::__construct();
}

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

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

$entities = $this->protectedTokenStorageRepository->findExpired();

$io->comment(sprintf('%d entities to delete', count($entities)));

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

$this->protectedTokenStorageRepository->flush();

$io->success(sprintf('%d entities deleted', count($entities)));
$io->success('Done.');

return Command::SUCCESS;
}
}
82 changes: 74 additions & 8 deletions bundle/Controller/Admin/ProtectedAccessController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@

use DateTime;
use Doctrine\ORM\EntityManagerInterface;
use eZ\Publish\API\Repository\Values\Content\Location;
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 Symfony\Component\Form\FormFactoryInterface;
Expand All @@ -28,20 +31,28 @@

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,
FormFactoryInterface $formFactory,
EntityManagerInterface $entityManager,
RouterInterface $router,
ContentTagInterface $responseTagger,
?ProtectedAccess $access = null
?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 @@ -56,20 +67,24 @@ public function handle(
$entityManager->flush();
$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('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,
Expand All @@ -83,11 +98,62 @@ public function remove(
$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('ibexa.content.view', ['contentId' => $location->contentId,
'locationId' => $location->id,
]).
]).
'#ibexa-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);
}
}
}
}
91 changes: 0 additions & 91 deletions bundle/Core/SiteAccessAwareEntityManagerFactory.php

This file was deleted.

21 changes: 20 additions & 1 deletion bundle/DependencyInjection/NovaeZProtectedContentExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,33 @@

use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

class NovaeZProtectedContentExtension extends Extension
class NovaeZProtectedContentExtension extends Extension implements PrependExtensionInterface
{
public function load(array $configs, ContainerBuilder $container): void
{
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yaml');
}

public function prepend(ContainerBuilder $container)
{
$config = [
'orm' => [
'entity_mappings' => [
'eZProtectedContentBundle' => [
'type' => 'annotation',
'dir' => __DIR__.'/../Entity',
'prefix' => 'Novactive\Bundle\eZProtectedContentBundle\Entity',
'is_bundle' => false,
],
],
],
];

$container->prependExtensionConfig('ibexa', $config);
}
}
Loading