Skip to content

Commit

Permalink
Adiciona os fields de conditions no decrypt da select
Browse files Browse the repository at this point in the history
  • Loading branch information
joacir committed Mar 23, 2022
1 parent e368d7e commit 1b55b2b
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 42 deletions.
58 changes: 27 additions & 31 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,33 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="tests/bootstrap.php">
<php>
<ini name="memory_limit" value="-1"/>
<ini name="apc.enable_cli" value="1"/>
<!-- E_ALL => 32767 -->
<!-- E_ALL & ~E_USER_DEPRECATED => 16383 -->
<ini name="error_reporting" value="16383"/>
</php>

<!-- Add any additional test suites you want to run here -->
<testsuites>
<testsuite name="CakeAes">
<directory>tests/TestCase/</directory>
</testsuite>
</testsuites>

<!-- Setup a listener for fixtures -->
<listeners>
<listener class="Cake\TestSuite\Fixture\FixtureInjector">
<arguments>
<object class="Cake\TestSuite\Fixture\FixtureManager"/>
</arguments>
</listener>
</listeners>

<filter>
<whitelist>
<directory suffix=".php">src/</directory>
</whitelist>
</filter>

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="tests/bootstrap.php" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage>
<include>
<directory suffix=".php">src/</directory>
</include>
</coverage>
<php>
<ini name="memory_limit" value="-1"/>
<ini name="apc.enable_cli" value="1"/>
<!-- E_ALL => 32767 -->
<!-- E_ALL & ~E_USER_DEPRECATED => 16383 -->
<ini name="error_reporting" value="16383"/>
</php>
<!-- Add any additional test suites you want to run here -->
<testsuites>
<testsuite name="CakeAes">
<directory>tests/TestCase/</directory>
</testsuite>
</testsuites>
<!-- Setup a listener for fixtures -->
<listeners>
<listener class="Cake\TestSuite\Fixture\FixtureInjector">
<arguments>
<object class="Cake\TestSuite\Fixture\FixtureManager"/>
</arguments>
</listener>
</listeners>
</phpunit>
33 changes: 33 additions & 0 deletions phpunit.xml.dist.bak
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="tests/bootstrap.php">
<php>
<ini name="memory_limit" value="-1"/>
<ini name="apc.enable_cli" value="1"/>
<!-- E_ALL => 32767 -->
<!-- E_ALL & ~E_USER_DEPRECATED => 16383 -->
<ini name="error_reporting" value="16383"/>
</php>

<!-- Add any additional test suites you want to run here -->
<testsuites>
<testsuite name="CakeAes">
<directory>tests/TestCase/</directory>
</testsuite>
</testsuites>

<!-- Setup a listener for fixtures -->
<listeners>
<listener class="Cake\TestSuite\Fixture\FixtureInjector">
<arguments>
<object class="Cake\TestSuite\Fixture\FixtureManager"/>
</arguments>
</listener>
</listeners>

<filter>
<whitelist>
<directory suffix=".php">src/</directory>
</whitelist>
</filter>

</phpunit>
41 changes: 33 additions & 8 deletions src/Model/Behavior/EncryptBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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)) {
Expand Down Expand Up @@ -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);
}
Expand All @@ -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)) {
Expand All @@ -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
*
Expand Down
6 changes: 3 additions & 3 deletions src/Model/Database/Type/AesType.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

namespace CakeAes\Model\Database\Type;

use Cake\Core\Exception\Exception;
use Cake\Database\DriverInterface;
use Cake\Database\Type\BinaryType;

Expand All @@ -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
{
/**
Expand All @@ -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)));
}
}

0 comments on commit 1b55b2b

Please sign in to comment.