From 62ff38465524b74837a561309c18418c20a09969 Mon Sep 17 00:00:00 2001 From: Alexander Janssen Date: Tue, 21 Feb 2017 21:35:39 +0100 Subject: [PATCH] Add embedded support --- Subscribers/DoctrineEncryptSubscriber.php | 75 ++++++----------------- 1 file changed, 19 insertions(+), 56 deletions(-) diff --git a/Subscribers/DoctrineEncryptSubscriber.php b/Subscribers/DoctrineEncryptSubscriber.php index 8b38887d..591ac43b 100644 --- a/Subscribers/DoctrineEncryptSubscriber.php +++ b/Subscribers/DoctrineEncryptSubscriber.php @@ -12,6 +12,7 @@ use Doctrine\Common\Util\ClassUtils; use \ReflectionClass; use Ambta\DoctrineEncryptBundle\Encryptors\EncryptorInterface; +use Symfony\Component\PropertyAccess\PropertyAccess; /** * Doctrine event subscriber which encrypt/decrypt entities @@ -239,56 +240,28 @@ public function processFields($entity, $isEncryptOperation = true) { $this->handleEmbeddedAnnotation($entity, $refProperty, $isEncryptOperation); continue; } - /** - * If followed standards, method name is getPropertyName, the propertyName is lowerCamelCase - * So just uppercase first character of the property, later on get and set{$methodName} wil be used - */ - $methodName = ucfirst($refProperty->getName()); - /** * If property is an normal value and contains the Encrypt tag, lets encrypt/decrypt that property */ if ($this->annReader->getPropertyAnnotation($refProperty, self::ENCRYPTED_ANN_NAME)) { - - /** - * If it is public lets not use the getter/setter - */ - if ($refProperty->isPublic()) { - $propName = $refProperty->getName(); - $entity->$propName = $this->encryptor->$encryptorMethod($refProperty->getValue()); - } else { - //If private or protected check if there is an getter/setter for the property, based on the $methodName - if ($reflectionClass->hasMethod($getter = 'get' . $methodName) && $reflectionClass->hasMethod($setter = 'set' . $methodName)) { - - //Get the information (value) of the property - try { - $getInformation = $entity->$getter(); - } catch(\Exception $e) { - $getInformation = null; + $pac = PropertyAccess::createPropertyAccessor(); + $value = $pac->getValue($entity, $refProperty->getName()); + if($encryptorMethod == "decrypt") { + if(!is_null($value) and !empty($value)) { + if(substr($value, -5) == "") { + $this->decryptCounter++; + $currentPropValue = $this->encryptor->decrypt(substr($value, 0, -5)); + $pac->setValue($entity, $refProperty->getName(), $currentPropValue); } - - /** - * Then decrypt, encrypt the information if not empty, information is an string and the tag is there (decrypt) or not (encrypt). - * The will be added at the end of an encrypted string so it is marked as encrypted. Also protects against double encryption/decryption - */ - if($encryptorMethod == "decrypt") { - if(!is_null($getInformation) and !empty($getInformation)) { - if(substr($getInformation, -5) == "") { - $this->decryptCounter++; - $currentPropValue = $this->encryptor->decrypt(substr($getInformation, 0, -5)); - $entity->$setter($currentPropValue); - } - } - } else { - if(!is_null($getInformation) and !empty($getInformation)) { - if(substr($entity->$getter(), -5) != "") { - $this->encryptCounter++; - $currentPropValue = $this->encryptor->encrypt($entity->$getter()); - $entity->$setter($currentPropValue); - } - } + } + } else { + if(!is_null($value) and !empty($value)) { + if(substr($value, -5) != "") { + $this->encryptCounter++; + $currentPropValue = $this->encryptor->encrypt($value); + $pac->setValue($entity, $refProperty->getName(), $currentPropValue); } } } @@ -305,21 +278,11 @@ private function handleEmbeddedAnnotation($entity, $embeddedProperty, $isEncrypt { $reflectionClass = new ReflectionClass($entity); $propName = $embeddedProperty->getName(); - $methodName = ucfirst($propName); - if ($embeddedProperty->isPublic()) { - $embeddedEntity = $embeddedProperty->getValue(); - } else { - if ($reflectionClass->hasMethod($getter = 'get' . $methodName) && $reflectionClass->hasMethod($setter = 'set' . $methodName)) { + $pac = PropertyAccess::createPropertyAccessor(); + + $embeddedEntity = $pac->getValue($entity, $propName); - //Get the information (value) of the property - try { - $embeddedEntity = $entity->$getter(); - } catch(\Exception $e) { - $embeddedEntity = null; - } - } - } if ($embeddedEntity) { $this->processFields($embeddedEntity, $isEncryptOperation); }