From dd2c78009e697d23355ff7add42ef776b338c5eb Mon Sep 17 00:00:00 2001 From: Jean-Danyel Routhier Date: Fri, 1 Dec 2023 15:24:35 -0500 Subject: [PATCH] fix: Fix handling of proxies in DoctrineEncryptSubscriber The values returned by `$unitOfWork->getIdentityMap()` may be instances of `Proxy`, which are entities that have not been loaded yet. In this case, the `@Encrypted` properties of proxies should not be encrypted/decrypted, as it unnecessarily loads the entities. --- src/Subscribers/DoctrineEncryptSubscriber.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/Subscribers/DoctrineEncryptSubscriber.php b/src/Subscribers/DoctrineEncryptSubscriber.php index 06f05df4..b3e14d4d 100644 --- a/src/Subscribers/DoctrineEncryptSubscriber.php +++ b/src/Subscribers/DoctrineEncryptSubscriber.php @@ -11,6 +11,7 @@ use Doctrine\ORM\Event\PreUpdateEventArgs; use Doctrine\ORM\Event\OnFlushEventArgs; use Doctrine\Common\Annotations\Reader; +use Doctrine\Common\Proxy\Proxy; use Doctrine\Common\Util\ClassUtils; use Ambta\DoctrineEncryptBundle\Encryptors\EncryptorInterface; use ReflectionProperty; @@ -161,6 +162,10 @@ public function preFlush(PreFlushEventArgs $preFlushEventArgs) foreach ($unitOfWOrk->getIdentityMap() as $entityName => $entityArray) { if (isset($this->cachedDecryptions[$entityName])) { foreach ($entityArray as $entityId => $instance) { + if ($instance instanceof Proxy && !$instance->__isInitialized()) { + continue; + } + $this->processFields($instance); } } @@ -198,6 +203,10 @@ public function postFlush(PostFlushEventArgs $postFlushEventArgs) $unitOfWork = $postFlushEventArgs->getEntityManager()->getUnitOfWork(); foreach ($unitOfWork->getIdentityMap() as $entityMap) { foreach ($entityMap as $entity) { + if ($entity instanceof Proxy && !$entity->__isInitialized()) { + continue; + } + $this->processFields($entity, false); } }