mogilvie / EncryptBundle

Encryption bundle
89 stars 29 forks source link

Embeddable support #36

Closed russelomua closed 1 year ago

russelomua commented 1 year ago

Doctrine Embeddables feature allows to make a some kind of sub-entity.

I try to make a fork and i have an error from github "🦄 This page is taking too long to load.", so i make a patch and here it is:

Subject: [PATCH] fix doctrine embeddable
---
Index: Subscribers/DoctrineEncryptSubscriber.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/Subscribers/DoctrineEncryptSubscriber.php b/Subscribers/DoctrineEncryptSubscriber.php
--- a/Subscribers/DoctrineEncryptSubscriber.php (revision edf06dad2de0abb040e1d0757b06ae5bf2938dac)
+++ b/Subscribers/DoctrineEncryptSubscriber.php (revision 2afc3213b2756680f435ab62ffb49f0005a132d2)
@@ -177,7 +177,7 @@
                 $changeSet = $unitOfWork->getEntityChangeSet($entity);

                 // Encrypt value only if change has been detected by Doctrine (comparing unencrypted values, see postLoad flow)
-                if (isset($changeSet[$refProperty->getName()])) {
+                if (isset($changeSet[$key])) {
                     $encryptedValue = $this->encryptor->encrypt($value);
                     $refProperty->setValue($entity, $encryptedValue);
                     $unitOfWork->recomputeSingleEntityChangeSet($em->getClassMetadata(get_class($entity)), $entity);
@@ -187,7 +187,7 @@
                         $refProperty->setValue($entity, $value);
                     } else {
                         // Will be restored during postUpdate cycle
-                        $this->rawValues[$oid][$refProperty->getName()] = $value;
+                        $this->rawValues[$oid][$key] = $value;
                     }
                 }
             } else {
@@ -196,7 +196,7 @@
                 $refProperty->setValue($entity, $decryptedValue);

                 // Tell Doctrine the original value was the decrypted one.
-                $unitOfWork->setOriginalEntityProperty($oid, $refProperty->getName(), $decryptedValue);
+                $unitOfWork->setOriginalEntityProperty($oid, $key, $decryptedValue);
             }
         }

@@ -222,7 +222,7 @@
     }

     /**
-     * @return ReflectionProperty[]
+     * @return array<string, ReflectionProperty>
      */
     protected function getEncryptedFields(object $entity, EntityManagerInterface $em): array
     {
@@ -236,9 +236,9 @@

         $encryptedFields = [];

-        foreach ($meta->getReflectionProperties() as $refProperty) {
+        foreach ($meta->getReflectionProperties() as $key => $refProperty) {
             if ($this->isEncryptedProperty($refProperty)) {
-                $encryptedFields[] = $refProperty;
+                $encryptedFields[$key] = $refProperty;
             }
         }

ClassMetadata::getReflectionProperties() returns valid keys of properties

Screenshot 2023-02-12 at 13 57 33

and UnitOfWork is store original entity properties in same format (UnitOfWork::setOriginalEntityProperty will work correct):

Screenshot 2023-02-12 at 13 59 28

thanx

russelomua commented 1 year ago

Github is alive now, so here is a PR

russelomua commented 1 year ago

maybe tag repo and close issue?

mogilvie commented 1 year ago

Thanks, however, I thought the issue was about the use of doctrine embeddables. All that we've done here is provide named keys to the array instead of indexed number keys. I do want to investigate the embeddables though!

russelomua commented 1 year ago

Sure, do what you need. ClassMetadata::getReflectionProperties() already returns an indexed array and i just use these indexes and it available at first screen.

mogilvie commented 1 year ago

Thanks for your input @russelomua I'll close this for now. And I will look to using Embedables in my GDPR bundle to handled PersonalData objects. I'm still working on a few other pull requests at the moment. I'll publish a tagged release at the end of this week.