Skip to content

Commit

Permalink
feat(Log): Made Log entity autonomous without any relationship, only …
Browse files Browse the repository at this point in the history
…loose references
  • Loading branch information
ambroisemaupate committed Jun 28, 2023
1 parent 471a571 commit e0dd99c
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 39 deletions.
40 changes: 40 additions & 0 deletions lib/RoadizCoreBundle/migrations/Version20230628170203.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace RZ\Roadiz\Migrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20230628170203 extends AbstractMigration
{
public function getDescription(): string
{
return 'Changed user_id column type in log table to string.';
}

public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE log DROP FOREIGN KEY FK_8F3F68C5A76ED395');
$this->addSql('DROP INDEX IDX_8F3F68C5A76ED395 ON log');
$this->addSql('ALTER TABLE log CHANGE user_id user_id VARCHAR(36) DEFAULT NULL');
}

public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE log CHANGE user_id user_id INT DEFAULT NULL');
$this->addSql('ALTER TABLE log ADD CONSTRAINT FK_8F3F68C5A76ED395 FOREIGN KEY (user_id) REFERENCES users (id) ON UPDATE NO ACTION ON DELETE SET NULL');
$this->addSql('CREATE INDEX IDX_8F3F68C5A76ED395 ON log (user_id)');
}

public function isTransactional(): bool
{
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public function handle(bool $disabled = false)

protected function handleSearchParam(string $search): void
{
parent::handleSearchParam($search);
$this->query = trim($search);
}

Expand Down
25 changes: 17 additions & 8 deletions lib/RoadizCoreBundle/src/Entity/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ class Log extends AbstractEntity
public const DEBUG = Logger::DEBUG;
public const LOG = Logger::INFO;

#[ORM\ManyToOne(targetEntity: User::class)]
#[ORM\JoinColumn(name: 'user_id', referencedColumnName: 'id', unique: false, onDelete: 'SET NULL')]
#[ORM\Column(name: 'user_id', type: 'string', length: 36, unique: false, nullable: true)]
#[SymfonySerializer\Groups(['log_user'])]
#[Serializer\Groups(['log_user'])]
protected ?User $user = null;
// @phpstan-ignore-next-line
protected int|string|null $userId = null;

#[ORM\Column(name: 'username', type: 'string', length: 255, nullable: true)]
#[SymfonySerializer\Groups(['log_user'])]
Expand Down Expand Up @@ -119,12 +119,21 @@ public function __construct(int $level, string $message)
}

/**
* @return User|null
* @deprecated Use additionalData or username instead
* @return int|string|null
*/
public function getUser(): ?User
public function getUserId(): int|string|null
{
return $this->user;
return $this->userId;
}

/**
* @param int|string|null $userId
* @return Log
*/
public function setUserId(int|string|null $userId): Log
{
$this->userId = $userId;
return $this;
}

/**
Expand All @@ -134,7 +143,7 @@ public function getUser(): ?User
*/
public function setUser(User $user): Log
{
$this->user = $user;
$this->userId = $user->getId();
$this->username = $user->getUsername();
return $this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ abstract class AbstractEntityListManager implements EntityListManagerInterface
protected ?array $queryArray = null;
protected ?int $currentPage = null;
protected ?int $itemPerPage = null;
protected ?string $searchPattern = null;
protected bool $displayNotPublishedNodes;
protected bool $displayAllNodesStatuses;
protected bool $allowRequestSorting = true;
Expand Down Expand Up @@ -136,6 +137,7 @@ public function getAssignation(): array
'pageCount' => $this->getPageCount(),
'itemPerPage' => $this->getItemPerPage(),
'itemCount' => $this->getItemCount(),
'search' => $this->searchPattern,
'nextPageQuery' => null,
'previousPageQuery' => null,
];
Expand Down Expand Up @@ -272,7 +274,7 @@ protected function handleRequestQuery(bool $disabled): void

protected function handleSearchParam(string $search): void
{
// Do nothing on abstract
$this->searchPattern = $search;
}

protected function handleOrderingParam(string $field, string $ordering): void
Expand Down
18 changes: 1 addition & 17 deletions lib/RoadizCoreBundle/src/ListManager/EntityListManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace RZ\Roadiz\CoreBundle\ListManager;

use Doctrine\ORM\Tools\Pagination\Paginator as DoctrinePaginator;
use Doctrine\Persistence\ObjectManager;
use RZ\Roadiz\Core\AbstractEntities\PersistableInterface;
use RZ\Roadiz\Core\AbstractEntities\TranslationInterface;
Expand All @@ -13,7 +14,6 @@
use RZ\Roadiz\CoreBundle\Repository\NodeRepository;
use RZ\Roadiz\CoreBundle\Repository\StatusAwareRepository;
use Symfony\Component\HttpFoundation\Request;
use Doctrine\ORM\Tools\Pagination\Paginator as DoctrinePaginator;

/**
* Perform basic filtering and search over entity listings.
Expand All @@ -28,7 +28,6 @@ class EntityListManager extends AbstractEntityListManager
protected ?Paginator $paginator = null;
protected ?array $orderingArray = null;
protected ?array $filteringArray = null;
protected ?string $searchPattern = null;
protected ?array $assignation = null;
protected ?TranslationInterface $translation = null;

Expand Down Expand Up @@ -134,11 +133,6 @@ public function handle(bool $disabled = false)
}
}

protected function handleSearchParam(string $search): void
{
$this->searchPattern = $search;
}

protected function handleOrderingParam(string $field, string $ordering): void
{
$this->orderingArray = [
Expand Down Expand Up @@ -188,16 +182,6 @@ protected function createPaginator(): void
$this->paginator->setDisplayingAllNodesStatuses($this->isDisplayingAllNodesStatuses());
}

/**
* @return array
*/
public function getAssignation(): array
{
return array_merge(parent::getAssignation(), [
'search' => $this->searchPattern,
]);
}

/**
* @return int
*/
Expand Down
20 changes: 19 additions & 1 deletion lib/RoadizCoreBundle/src/ListManager/QueryBuilderListManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ class QueryBuilderListManager extends AbstractEntityListManager
protected ?Paginator $paginator = null;
protected string $identifier;
protected bool $debug = false;
/**
* @var null|callable
*/
protected $searchingCallable = null;

/**
* @param Request|null $request
Expand All @@ -33,12 +37,26 @@ public function __construct(
$this->debug = $debug;
}

/**
* @param callable|null $searchingCallable
* @return QueryBuilderListManager
*/
public function setSearchingCallable(?callable $searchingCallable): QueryBuilderListManager
{
$this->searchingCallable = $searchingCallable;
return $this;
}

/**
* @param string $search
*/
protected function handleSearchParam(string $search): void
{
// Implement your custom logic
parent::handleSearchParam($search);

if (\is_callable($this->searchingCallable)) {
\call_user_func($this->searchingCallable, $this->queryBuilder, $search);
}
}

public function handle(bool $disabled = false)
Expand Down
30 changes: 30 additions & 0 deletions lib/RoadizCoreBundle/src/Repository/LogRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace RZ\Roadiz\CoreBundle\Repository;

use Doctrine\ORM\Query;
use Doctrine\ORM\QueryBuilder;
use Doctrine\ORM\Tools\Pagination\Paginator;
use Doctrine\Persistence\ManagerRegistry;
use RZ\Roadiz\CoreBundle\Entity\Log;
Expand Down Expand Up @@ -57,4 +58,33 @@ public function findLatestByNodesSources(int $maxResult = 5): Paginator

return new Paginator($qb2->getQuery(), true);
}

public function findByNode(Node $node): array
{
$qb = $this->getAllRelatedToNodeQueryBuilder($node);
return $qb->getQuery()->getResult();
}

public function getAllRelatedToNodeQueryBuilder(Node $node): QueryBuilder
{
$qb = $this->createQueryBuilder('obj');
$qb->andWhere($qb->expr()->orX(
$qb->expr()->andX(
$qb->expr()->eq('obj.entityClass', ':nodeClass'),
$qb->expr()->in('obj.entityId', ':nodeId')
),
$qb->expr()->andX(
$qb->expr()->eq('obj.entityClass', ':nodeSourceClass'),
$qb->expr()->in('obj.entityId', ':nodeSourceId')
),
));
$qb->addOrderBy('obj.datetime', 'DESC');
$qb->setParameter('nodeClass', Node::class);
$qb->setParameter('nodeSourceClass', NodesSources::class);
$qb->setParameter('nodeId', [$node->getId()]);
$qb->setParameter('nodeSourceId', $node->getNodeSources()->map(function (NodesSources $ns) {
return $ns->getId();
})->toArray());
return $qb;
}
}
1 change: 0 additions & 1 deletion lib/RoadizCoreBundle/src/TwigExtension/LogExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ public function getEditPath(?object $log): ?string
]);
}
break;

}

return null;
Expand Down
8 changes: 4 additions & 4 deletions lib/Rozier/src/Controllers/HistoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,20 @@ public function indexAction(Request $request): Response
* List user logs action.
*
* @param Request $request
* @param int $userId
* @param int|string $userId
*
* @return Response
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
* @throws \Doctrine\ORM\TransactionRequiredException
*/
public function userAction(Request $request, int $userId): Response
public function userAction(Request $request, int|string $userId): Response
{
$this->denyAccessUnlessGranted(['ROLE_BACKEND_USER', 'ROLE_ACCESS_LOGS']);

if (
!($this->isGranted(['ROLE_ACCESS_USERS', 'ROLE_ACCESS_LOGS'])
|| ($this->getUser() instanceof User && $this->getUser()->getId() == $userId))
|| ($this->getUser() instanceof User && $this->getUser()->getId() === $userId))
) {
throw $this->createAccessDeniedException("You don't have access to this page: ROLE_ACCESS_USERS");
}
Expand All @@ -94,7 +94,7 @@ public function userAction(Request $request, int $userId): Response
*/
$listManager = $this->createEntityListManager(
Log::class,
['user' => $user],
['userId' => $user->getId()],
['datetime' => 'DESC']
);
$listManager->setDisplayingNotPublishedNodes(true);
Expand Down
22 changes: 15 additions & 7 deletions lib/Rozier/src/Controllers/Nodes/HistoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@

namespace Themes\Rozier\Controllers\Nodes;

use Doctrine\ORM\QueryBuilder;
use RZ\Roadiz\CoreBundle\Entity\Log;
use RZ\Roadiz\CoreBundle\Entity\Node;
use RZ\Roadiz\CoreBundle\Entity\NodesSources;
use RZ\Roadiz\CoreBundle\Entity\Translation;
use RZ\Roadiz\CoreBundle\ListManager\QueryBuilderListManager;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
Expand Down Expand Up @@ -35,13 +38,18 @@ public function historyAction(Request $request, int $nodeId): Response
throw new ResourceNotFoundException();
}

$listManager = $this->createEntityListManager(
Log::class,
[
'nodeSource' => $node->getNodeSources()->toArray(),
],
['datetime' => 'DESC']
);
$qb = $this->em()
->getRepository(Log::class)
->getAllRelatedToNodeQueryBuilder($node);

$listManager = new QueryBuilderListManager($request, $qb, 'obj');
$listManager->setSearchingCallable(function (QueryBuilder $queryBuilder, string $search) {
$queryBuilder->andWhere($queryBuilder->expr()->orX(
$queryBuilder->expr()->like('obj.message', ':search'),
$queryBuilder->expr()->like('obj.channel', ':search')
));
$queryBuilder->setParameter('search', '%' . $search . '%');
});
$listManager->setDisplayingNotPublishedNodes(true);
$listManager->setDisplayingAllNodesStatuses(true);
/*
Expand Down

0 comments on commit e0dd99c

Please sign in to comment.