Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Loggable] Invert logic to log everything but ignore some entities by config #2865

Open
zorn-v opened this issue Sep 3, 2024 · 0 comments
Open

Comments

@zorn-v
Copy link

zorn-v commented Sep 3, 2024

I figured out that not only I need this, so just left it here, how I implement it in symfony app.

# config/packages/doctrine_extensions.yaml
services:
    Gedmo\Loggable\LoggableListener:
        class: App\EventSubscriber\Loggable
        calls:
            - [ setSecurity, ['@security.helper'] ]
        tags:
            - { name: doctrine.event_listener, event: 'onFlush' }
            - { name: doctrine.event_listener, event: 'loadClassMetadata' }
            - { name: doctrine.event_listener, event: 'postPersist' }

Only getConfiguration need for this feature, but maybe other logic will be interested too.

<?php

namespace App\EventSubscriber;

use App\Entity\AuditLogEntry;
use Doctrine\Persistence\ObjectManager;
use Gedmo\Loggable\LogEntryInterface;
use Gedmo\Loggable\LoggableListener;
use Gedmo\Loggable\Mapping\Event\LoggableAdapter;
use Symfony\Bundle\SecurityBundle\Security;

class Loggable extends LoggableListener
{
    private Security $security;

    private string $lastAction;

    private array $lastChanges;

    public function setSecurity(Security $security)
    {
        $this->security = $security;
    }

    public function getConfiguration(ObjectManager $objectManager, $class): array
    {
        $config = parent::getConfiguration($objectManager, $class);
        $meta = $objectManager->getClassMetadata($class);
        if ($meta->reflClass->getAttributes('NotLoggable')) {
            return $config;
        }
        $config = array_merge($config, [
            'loggable' => true,
            'logEntryClass' => AuditLogEntry::class,
            'useObjectClass' => $class,
            'versioned' => array_merge($meta->getFieldNames(), $meta->getAssociationNames()),
        ]);
        self::$configurations[$this->name][$meta->getName()] = $config;

        return $config;
    }

    /**
     * @param AuditLogEntry $logEntry
     */
    protected function prePersistLogEntry($logEntry, $object): void
    {
        if (method_exists($object, '__toString')) {
            $logEntry->setObjectTitle((string)$object);
        }
        if (LogEntryInterface::ACTION_UPDATE !== $this->lastAction) {
            return;
        }
        $prevData = [];
        foreach ($this->lastChanges as $field => $changes) {
            $value = $changes[0];
            if (is_object($value) && method_exists($value, 'getId')) {
                $value = ['id' => $value->getId()];
            }
            $prevData[$field] = $value;
        }
        $logEntry->setPrevData($prevData);
    }

    protected function createLogEntry($action, $object, LoggableAdapter $ea): ?LogEntryInterface
    {
        /** @var ?\App\Entity\User */
        $user = $this->security->getUser();
        if ($user) {
            $this->setUsername($user->getFullNameShort());
        }
        // a little bit hacky
        $this->lastAction = $action;
        /** @var \Doctrine\ORM\EntityManagerInterface */
        $om = $ea->getObjectManager();
        $this->lastChanges = $ea->getObjectChangeSet($om->getUnitOfWork(), $object);

        return parent::createLogEntry($action, $object, $ea);
    }
}

and some entity that you did not want to be loggable

<?php

namespace App\Entity;

use App\Entity\Enum\RepairStatusEnum;
use App\Repository\RepairStatusLogRepository;
use Doctrine\ORM\Mapping as ORM;

#[\NotLoggable]
#[ORM\Entity(repositoryClass: RepairStatusLogRepository::class)]
class RepairStatusLog
//...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant