Skip to content

Commit

Permalink
Merge pull request #27 from TNAJanssen/master
Browse files Browse the repository at this point in the history
Use propertyAccess instead of getter setter
  • Loading branch information
dev-marcel authored Apr 27, 2020
2 parents af38348 + f8a31fa commit 96059b3
Showing 1 changed file with 20 additions and 58 deletions.
78 changes: 20 additions & 58 deletions Subscribers/DoctrineEncryptSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -255,57 +256,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) == "<ENC>") {
$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 a string and the encryption suffix (<ENC>) tag is there (decrypt) or not (encrypt).
* The encryption suffix (<ENC>) will be added at the end of an encrypted string so it is marked as encrypted. Also protects against double encryption/decryption
*/
$suffixLength = (0 - strlen($this->suffix));
if($encryptorMethod == "decrypt") {
if(!is_null($getInformation) and !empty($getInformation)) {
if(substr($getInformation, $suffixLength) == $this->suffix) {
$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(), $suffixLength) != $this->suffix) {
$this->encryptCounter++;
$currentPropValue = $this->encryptor->encrypt($entity->$getter());
$entity->$setter($currentPropValue);
}
}
}
} else {
if(!is_null($value) and !empty($value)) {
if(substr($value, -5) != "<ENC>") {
$this->encryptCounter++;
$currentPropValue = $this->encryptor->encrypt($value);
$pac->setValue($entity, $refProperty->getName(), $currentPropValue);
}
}
}
Expand All @@ -322,21 +294,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);
}
Expand Down

0 comments on commit 96059b3

Please sign in to comment.