Skip to content

Commit

Permalink
feat(Node): New NodeInterface to gather Node doctrine entity and …
Browse files Browse the repository at this point in the history
…its DTOs into Workflow and Widgets
  • Loading branch information
ambroisemaupate committed Jun 7, 2024
1 parent d296690 commit 90e7d4e
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 40 deletions.
15 changes: 15 additions & 0 deletions lib/Models/src/Core/AbstractEntities/NodeInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace RZ\Roadiz\Core\AbstractEntities;

/**
* Node interface to be implemented by Node Doctrine entity and DTOs.
*/
interface NodeInterface extends PersistableInterface
{
public function getChildrenOrder(): string;

public function getChildrenOrderDirection(): string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

namespace RZ\Roadiz\CoreBundle\DependencyInjection\Compiler;

use RZ\Roadiz\CoreBundle\Entity\Node;
use RZ\Roadiz\CoreBundle\Model\NodeTreeDto;
use RZ\Roadiz\Core\AbstractEntities\NodeInterface;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
Expand All @@ -24,11 +23,8 @@ public function process(ContainerBuilder $container): void
$workflowId = 'state_machine.node';
$registryDefinition = $container->getDefinition('workflow.registry');

$strategyDefinition = new Definition(InstanceOfSupportStrategy::class, [Node::class]);
$strategyDefinition = new Definition(InstanceOfSupportStrategy::class, [NodeInterface::class]);
$strategyDefinition->setPublic(false);
$dtoStrategyDefinition = new Definition(InstanceOfSupportStrategy::class, [NodeTreeDto::class]);
$dtoStrategyDefinition->setPublic(false);
$registryDefinition->addMethodCall('addWorkflow', [new Reference($workflowId), $strategyDefinition]);
$registryDefinition->addMethodCall('addWorkflow', [new Reference($workflowId), $dtoStrategyDefinition]);
}
}
3 changes: 2 additions & 1 deletion lib/RoadizCoreBundle/src/Entity/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use RZ\Roadiz\Contracts\NodeType\NodeTypeInterface;
use RZ\Roadiz\Core\AbstractEntities\LeafInterface;
use RZ\Roadiz\Core\AbstractEntities\LeafTrait;
use RZ\Roadiz\Core\AbstractEntities\NodeInterface;
use RZ\Roadiz\Core\AbstractEntities\TranslationInterface;
use RZ\Roadiz\CoreBundle\Api\Filter as RoadizFilter;
use RZ\Roadiz\CoreBundle\Model\AttributableInterface;
Expand Down Expand Up @@ -66,7 +67,7 @@
),
ApiFilter(PropertyFilter::class)
]
class Node extends AbstractDateTimedPositioned implements LeafInterface, AttributableInterface, Loggable
class Node extends AbstractDateTimedPositioned implements LeafInterface, AttributableInterface, Loggable, NodeInterface
{
use LeafTrait;
use AttributableTrait;
Expand Down
4 changes: 2 additions & 2 deletions lib/RoadizCoreBundle/src/Model/NodeTreeDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

namespace RZ\Roadiz\CoreBundle\Model;

use RZ\Roadiz\Core\AbstractEntities\PersistableInterface;
use RZ\Roadiz\Core\AbstractEntities\NodeInterface;
use RZ\Roadiz\CoreBundle\Entity\Node;

/**
* Doctrine Data transfer object to represent a Node in a tree.
*/
final class NodeTreeDto implements PersistableInterface
final class NodeTreeDto implements NodeInterface
{
public NodeTypeTreeDto $nodeType;
public NodesSourcesTreeDto $nodeSource;
Expand Down
5 changes: 3 additions & 2 deletions lib/RoadizCoreBundle/src/Repository/NodeRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Doctrine\ORM\Tools\Pagination\Paginator;
use Doctrine\Persistence\ManagerRegistry;
use RZ\Roadiz\Contracts\NodeType\NodeTypeFieldInterface;
use RZ\Roadiz\Core\AbstractEntities\NodeInterface;
use RZ\Roadiz\Core\AbstractEntities\PersistableInterface;
use RZ\Roadiz\Core\AbstractEntities\TranslationInterface;
use RZ\Roadiz\CoreBundle\Doctrine\Event\QueryBuilder\QueryBuilderApplyEvent;
Expand Down Expand Up @@ -1038,10 +1039,10 @@ public function findByReverseNodeAndFieldAndTranslation(
}

/**
* @param Node $node
* @param NodeInterface $node
* @return array<int>
*/
public function findAllOffspringIdByNode(Node $node): array
public function findAllOffspringIdByNode(NodeInterface $node): array
{
$theOffsprings = [];
$in = \array_filter([(int) $node->getId()]);
Expand Down
29 changes: 15 additions & 14 deletions lib/RoadizCoreBundle/src/Security/Authorization/Voter/NodeVoter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Doctrine\Persistence\ManagerRegistry;
use Psr\Cache\CacheItemPoolInterface;
use RZ\Roadiz\Core\AbstractEntities\NodeInterface;
use RZ\Roadiz\CoreBundle\Entity\Node;
use RZ\Roadiz\CoreBundle\Entity\NodesSources;
use RZ\Roadiz\CoreBundle\Security\Authorization\Chroot\NodeChrootResolver;
Expand Down Expand Up @@ -74,7 +75,7 @@ protected function supports(string $attribute, mixed $subject): bool
return false;
}

if ($subject instanceof Node || $subject instanceof NodesSources) {
if ($subject instanceof NodeInterface || $subject instanceof NodesSources) {
return true;
}

Expand Down Expand Up @@ -114,7 +115,7 @@ protected function voteOnAttribute(string $attribute, mixed $subject, TokenInter
};
}

private function isNodeInsideUserChroot(Node $node, Node $chroot, bool $includeChroot = false): bool
private function isNodeInsideUserChroot(NodeInterface $node, NodeInterface $chroot, bool $includeChroot = false): bool
{
if (!$includeChroot && $chroot->getId() === $node->getId()) {
return false;
Expand All @@ -137,7 +138,7 @@ private function isNodeInsideUserChroot(Node $node, Node $chroot, bool $includeC
return \in_array($node->getId(), $offspringIds, true);
}

private function isGrantedWithUserChroot(Node $node, UserInterface $user, array|string $roles, bool $includeChroot): bool
private function isGrantedWithUserChroot(NodeInterface $node, UserInterface $user, array|string $roles, bool $includeChroot): bool
{
$chroot = $this->chrootResolver->getChroot($user);
if (null === $chroot) {
Expand Down Expand Up @@ -173,7 +174,7 @@ private function canEmptyTrash(UserInterface $user): bool
}


private function canCreate(Node $node, UserInterface $user): bool
private function canCreate(NodeInterface $node, UserInterface $user): bool
{
/*
* Creation is allowed only if node is inside user chroot,
Expand All @@ -182,7 +183,7 @@ private function canCreate(Node $node, UserInterface $user): bool
return $this->isGrantedWithUserChroot($node, $user, 'ROLE_ACCESS_NODES', true);
}

private function canRead(Node $node, UserInterface $user): bool
private function canRead(NodeInterface $node, UserInterface $user): bool
{
/*
* Read is allowed only if node is inside user chroot,
Expand All @@ -191,12 +192,12 @@ private function canRead(Node $node, UserInterface $user): bool
return $this->isGrantedWithUserChroot($node, $user, 'ROLE_ACCESS_NODES', true);
}

private function canReadLogs(Node $node, UserInterface $user): bool
private function canReadLogs(NodeInterface $node, UserInterface $user): bool
{
return $this->isGrantedWithUserChroot($node, $user, ['ROLE_ACCESS_NODES', 'ROLE_ACCESS_LOGS'], false);
}

private function canEditContent(Node $node, UserInterface $user): bool
private function canEditContent(NodeInterface $node, UserInterface $user): bool
{
/*
* Edition is allowed only if node is inside user chroot,
Expand All @@ -205,17 +206,17 @@ private function canEditContent(Node $node, UserInterface $user): bool
return $this->isGrantedWithUserChroot($node, $user, 'ROLE_ACCESS_NODES', false);
}

private function canEditTags(Node $node, UserInterface $user): bool
private function canEditTags(NodeInterface $node, UserInterface $user): bool
{
return $this->isGrantedWithUserChroot($node, $user, ['ROLE_ACCESS_NODES', 'ROLE_ACCESS_TAGS'], false);
}

private function canEditRealms(Node $node, UserInterface $user): bool
private function canEditRealms(NodeInterface $node, UserInterface $user): bool
{
return $this->isGrantedWithUserChroot($node, $user, ['ROLE_ACCESS_NODES', 'ROLE_ACCESS_REALM_NODES'], false);
}

private function canDuplicate(Node $node, UserInterface $user): bool
private function canDuplicate(NodeInterface $node, UserInterface $user): bool
{
/*
* Duplication is allowed only if node is inside user chroot,
Expand All @@ -224,22 +225,22 @@ private function canDuplicate(Node $node, UserInterface $user): bool
return $this->isGrantedWithUserChroot($node, $user, 'ROLE_ACCESS_NODES', false);
}

private function canEditSetting(Node $node, UserInterface $user): bool
private function canEditSetting(NodeInterface $node, UserInterface $user): bool
{
return $this->isGrantedWithUserChroot($node, $user, 'ROLE_ACCESS_NODES_SETTING', false);
}

private function canEditStatus(Node $node, UserInterface $user): bool
private function canEditStatus(NodeInterface $node, UserInterface $user): bool
{
return $this->isGrantedWithUserChroot($node, $user, 'ROLE_ACCESS_NODES_STATUS', false);
}

private function canDelete(Node $node, UserInterface $user): bool
private function canDelete(NodeInterface $node, UserInterface $user): bool
{
return $this->isGrantedWithUserChroot($node, $user, 'ROLE_ACCESS_NODES_DELETE', false);
}

private function canEditAttribute(Node $node, UserInterface $user): bool
private function canEditAttribute(NodeInterface $node, UserInterface $user): bool
{
return $this->isGrantedWithUserChroot($node, $user, 'ROLE_ACCESS_NODE_ATTRIBUTES', false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
</p>
{% endif %}
</div>
{% if not node.Locked %}
{% if not node.locked %}
<div class="wrapper node-actions">
<header class="uk-nav-header">{% trans %}statuses{% endtrans %}</header>
<p>
Expand All @@ -76,7 +76,7 @@
href="#"><i class="uk-icon-rz-visibility-mini"></i> {% trans %}node.show{% endtrans %}</a>
{% endif %}
</p>
{% if not node.isPublished and workflow_can(node, 'publish') %}
{% if not node.published and workflow_can(node, 'publish') %}
<p>
<a data-action="publish"
data-status="status"
Expand Down
27 changes: 14 additions & 13 deletions lib/Rozier/src/Widgets/NodeTreeWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Themes\Rozier\Widgets;

use Doctrine\Persistence\ManagerRegistry;
use RZ\Roadiz\Core\AbstractEntities\NodeInterface;
use RZ\Roadiz\Core\AbstractEntities\TranslationInterface;
use RZ\Roadiz\CoreBundle\Entity\Document;
use RZ\Roadiz\CoreBundle\Entity\Node;
Expand All @@ -22,9 +23,9 @@ final class NodeTreeWidget extends AbstractWidget
{
public const SESSION_ITEM_PER_PAGE = 'nodetree_item_per_page';
/**
* @var iterable<NodeTreeDto|Node>|null
* @var array<NodeInterface>|null
*/
private ?iterable $nodes = null;
private ?array $nodes = null;
private ?Tag $tag = null;
private bool $stackTree = false;
private ?array $filters = null;
Expand Down Expand Up @@ -120,12 +121,12 @@ public function setAdditionalCriteria(array $additionalCriteria): NodeTreeWidget
}

/**
* @param Node|NodeTreeDto|null $parent
* @param NodeInterface|null $parent
* @param bool $subRequest
*
* @return bool
*/
protected function canOrderByParent(Node|NodeTreeDto|null $parent = null, bool $subRequest = false): bool
protected function canOrderByParent(?NodeInterface $parent = null, bool $subRequest = false): bool
{
if (true === $subRequest || null === $parent) {
return false;
Expand All @@ -143,14 +144,14 @@ protected function canOrderByParent(Node|NodeTreeDto|null $parent = null, bool $
}

/**
* @param Node|NodeTreeDto|null $parent
* @param NodeInterface|null $parent
* @param bool $subRequest Default: false
* @param array $additionalCriteria Default: []
* @return NodeTreeDtoListManager
* @throws \ReflectionException
*/
protected function getListManager(
Node|NodeTreeDto|null $parent = null,
?NodeInterface $parent = null,
bool $subRequest = false,
array $additionalCriteria = []
): NodeTreeDtoListManager {
Expand Down Expand Up @@ -205,23 +206,23 @@ protected function getListManager(
}

/**
* @param Node|NodeTreeDto|null $parent
* @param NodeInterface|null $parent
* @param bool $subRequest Default: false
* @return array<NodeTreeDto>
* @throws \ReflectionException
*/
public function getChildrenNodes(Node|NodeTreeDto|null $parent = null, bool $subRequest = false): array
public function getChildrenNodes(NodeInterface|null $parent = null, bool $subRequest = false): array
{
return $this->getListManager($parent, $subRequest)->getEntities();
}

/**
* @param Node|NodeTreeDto|null $parent
* @param NodeInterface|null $parent
* @param bool $subRequest Default: false
* @return array<NodeTreeDto>
* @throws \ReflectionException
*/
public function getReachableChildrenNodes(Node|NodeTreeDto|null $parent = null, bool $subRequest = false): array
public function getReachableChildrenNodes(?NodeInterface $parent = null, bool $subRequest = false): array
{
return $this->getListManager($parent, $subRequest, [
'nodeType.reachable' => true,
Expand Down Expand Up @@ -270,10 +271,10 @@ public function getAvailableTranslations(): array
}

/**
* @return iterable<NodeTreeDto|Node>
* @return array<NodeInterface>
* @throws \ReflectionException
*/
public function getNodes(): iterable
public function getNodes(): array
{
if ($this->includeRootNode && null !== $this->getRootNode()) {
return [$this->getRootNode()];
Expand All @@ -290,7 +291,7 @@ public function getNodes(): iterable
/**
* @return array<TagTreeDto>
*/
public function getTags(Node|NodeTreeDto|null $node): array
public function getTags(?NodeInterface $node): array
{
if (null === $node) {
return [];
Expand Down

0 comments on commit 90e7d4e

Please sign in to comment.