diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 358a4b3..54ed58c 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,33 +1,29 @@ - - - - - - - - - - - - - tests/TestCase/ - - - - - - - - - - - - - - - src/ - - - + + + + src/ + + + + + + + + + + + + + tests/TestCase/ + + + + + + + + + + diff --git a/phpunit.xml.dist.bak b/phpunit.xml.dist.bak new file mode 100644 index 0000000..358a4b3 --- /dev/null +++ b/phpunit.xml.dist.bak @@ -0,0 +1,33 @@ + + + + + + + + + + + + + + tests/TestCase/ + + + + + + + + + + + + + + + src/ + + + + diff --git a/src/Model/Behavior/EncryptBehavior.php b/src/Model/Behavior/EncryptBehavior.php index 0b67d20..c00d638 100644 --- a/src/Model/Behavior/EncryptBehavior.php +++ b/src/Model/Behavior/EncryptBehavior.php @@ -21,6 +21,7 @@ public function initialize(array $config): void { $this->_table->encryptFields = []; $this->_table->decryptedValues = []; + $this->_table->containEncryptedFields = []; if (!empty($config['fields'])) { $this->_table->encryptFields = $config['fields']; TypeFactory::map('aes', 'CakeAes\Model\Database\Type\AesType'); @@ -33,7 +34,6 @@ public function initialize(array $config): void public function beforeSave(EventInterface $event, EntityInterface $entity, ArrayObject $options): void { - $entity->setVirtual([]); foreach ($this->_table->encryptFields as $field) { $value = $entity->get($field); if (!empty($value)) { @@ -69,7 +69,8 @@ public function encrypt(string $value): QueryExpression public function beforeFind(EventInterface $event, Query $query, ArrayObject $options, $primary) { - $query = $this->decryptSelect($query); + $this->setContainFields($query); + $query = $this->decryptSelect($query, $primary); $query = $this->decryptWhere($query); $query = $this->decryptOrder($query); } @@ -80,14 +81,21 @@ public function beforeFind(EventInterface $event, Query $query, ArrayObject $opt * @param Query $query query para descriptografia * @return Query retorna a query com os campos da select modificados */ - public function decryptSelect(Query $query): Query + public function decryptSelect(Query $query, $primary): Query { $select = $query->clause('select'); if (empty($select)) { - $select = $this->_table - ->getSchema() - ->columns(); - } + if ($primary) { + $select = $this->_table + ->getSchema() + ->columns(); + } else { + if (!empty($this->_table->containEncryptedFields)) { + $select = $this->_table->containEncryptedFields; + $this->_table->containEncryptedFields = []; + } + } + } $fields = []; foreach ($select as $virtual => $field) { if ($this->isEncrypted($field)) { @@ -101,11 +109,28 @@ public function decryptSelect(Query $query): Query $fields[$virtual] = $field; } } - $query = $query->select($fields, true); + if (!empty($fields)) { + $query = $query->select($fields, true); + } return $query; } + protected function setContainFields($query) + { + $associations = $query->getContain(); + foreach ($associations as $name => $config) { + if (!$this->_table->hasAssociation($name)) { + continue; + } + $association = $this->_table->getAssociation($name); + $target = $association->getTarget(); + if ($target->hasBehavior('Encrypt') && !empty($config['fields'])) { + $target->containEncryptedFields = $config['fields']; + } + } + } + /** * Descriptografa os campos criptografados da clausula where * diff --git a/src/Model/Database/Type/AesType.php b/src/Model/Database/Type/AesType.php index 2071a18..db94326 100644 --- a/src/Model/Database/Type/AesType.php +++ b/src/Model/Database/Type/AesType.php @@ -3,7 +3,6 @@ namespace CakeAes\Model\Database\Type; -use Cake\Core\Exception\Exception; use Cake\Database\DriverInterface; use Cake\Database\Type\BinaryType; @@ -12,6 +11,7 @@ * * Use to convert AES_ENCRYPT data between PHP and the database types. */ +//class AesType extends BinaryType implements ExpressionTypeInterface class AesType extends BinaryType { /** @@ -28,8 +28,8 @@ public function toPHP($value, DriverInterface $driver) return null; } if (is_string($value) || is_numeric($value) || is_resource($value)) { - return $value; + return (string) $value; } - throw new Exception(sprintf('Unable to convert %s into binary.', gettype($value))); + throw new \Exception(sprintf('Unable to convert %s into binary.', gettype($value))); } }