Skip to content
Merged
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
33 changes: 20 additions & 13 deletions src/EventListener/DoctrineEncryptListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Doctrine\ORM\Event\OnFlushEventArgs;
use Doctrine\ORM\Events;
use Doctrine\Persistence\Event\LifecycleEventArgs;
use Doctrine\Persistence\ObjectManager;
use ReflectionProperty;
use SpecShaper\EncryptBundle\Encryptors\EncryptorInterface;
use SpecShaper\EncryptBundle\Exception\EncryptException;
Expand Down Expand Up @@ -40,6 +41,9 @@ class DoctrineEncryptListener implements DoctrineEncryptListenerInterface

private bool $isDisabled;

/**
* @param EntityManagerInterface $em Deprecated in favour of fetching object manager from event args.
*/
public function __construct(
private readonly EncryptorInterface $encryptor,
private readonly EntityManagerInterface $em,
Expand Down Expand Up @@ -77,14 +81,15 @@ public function onFlush(OnFlushEventArgs $args): void
return;
}

$unitOfWork = $this->em->getUnitOfWork();
$objectManager = $args->getObjectManager();
$unitOfWork = $objectManager->getUnitOfWork();

foreach ($unitOfWork->getScheduledEntityInsertions() as $entity) {
$this->processFields($entity, true, true);
$this->processFields($objectManager, $entity, true, true);
}

foreach ($unitOfWork->getScheduledEntityUpdates() as $entity) {
$this->processFields($entity, true, false);
$this->processFields($objectManager, $entity, true, false);
}
}

Expand All @@ -99,7 +104,7 @@ public function postLoad(LifecycleEventArgs $args): void
$entity = $args->getObject();

// Decrypt the entity fields.
$this->processFields($entity, false, false);
$this->processFields($args->getObjectManager(), $entity, false, false);
}

/**
Expand Down Expand Up @@ -130,19 +135,19 @@ public function getEncryptionableProperties(array $allProperties): array
/**
* Process (encrypt/decrypt) entities fields.
*/
protected function processFields(object $entity, bool $isEncryptOperation, bool $isInsert): bool
protected function processFields(ObjectManager $objectManager,object $entity, bool $isEncryptOperation, bool $isInsert): bool
{
// Get the encrypted properties in the entity.
$properties = $this->getEncryptedFields($entity);
$properties = $this->getEncryptedFields($objectManager, $entity);

// If no encrypted properties, return false.
if (empty($properties)) {
return false;
}

$unitOfWork = $this->em->getUnitOfWork();
$unitOfWork = $objectManager->getUnitOfWork();
$oid = spl_object_id($entity);
$meta = $this->em->getClassMetadata(get_class($entity));
$meta = $objectManager->getClassMetadata(get_class($entity));

foreach ($properties as $refProperty) {

Expand Down Expand Up @@ -201,9 +206,11 @@ public function postUpdate(LifecycleEventArgs $args): void
$entity = $args->getObject();
$oid = spl_object_id($entity);

$objectManager = $args->getObjectManager();

if (isset($this->rawValues[$oid])) {
$className = get_class($entity);
$meta = $this->em->getClassMetadata($className);
$meta = $objectManager->getClassMetadata($className);
foreach ($this->rawValues[$oid] as $prop => $rawValue) {
$refProperty = $meta->getReflectionProperty($prop);
$refProperty->setValue($entity, $rawValue);
Expand All @@ -216,9 +223,9 @@ public function postUpdate(LifecycleEventArgs $args): void
/**
* @return array<string, ReflectionProperty>
*/
protected function getEncryptedFields(object $entity): array
protected function getEncryptedFields(ObjectManager $objectManager, object $entity): array
{
$reflectionClass = $this->getOriginalEntityReflection($entity);
$reflectionClass = $this->getOriginalEntityReflection($objectManager, $entity);

$className = $reflectionClass->getName();

Expand Down Expand Up @@ -257,9 +264,9 @@ private function isEncryptedProperty(ReflectionProperty $refProperty)
return false;
}

protected function getOriginalEntityReflection($entity): \ReflectionClass
protected function getOriginalEntityReflection(ObjectManager $objectManager, $entity): \ReflectionClass
{
$realClassName = $this->em->getClassMetadata(get_class($entity))->getName();
$realClassName = $objectManager->getClassMetadata(get_class($entity))->getName();
return new \ReflectionClass($realClassName);
}
}
5 changes: 4 additions & 1 deletion src/EventListener/DoctrineEncryptListenerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
interface DoctrineEncryptListenerInterface
{
public const ENCRYPTED_SUFFIX = '<ENC>';


/**
* @param EntityManagerInterface $em Deprecated in favour of getting object manager from event args.
*/
public function __construct(
EncryptorInterface $encryptor,
EntityManagerInterface $em,
Expand Down