From b6169c8ef489d37d54182ff661d0718366108931 Mon Sep 17 00:00:00 2001 From: "Angel S. Moreno" Date: Sat, 13 Feb 2021 14:49:25 -0500 Subject: [PATCH 1/3] updated composer to point to cakephp 4.* libs --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index b516bf5..ecb9927 100644 --- a/composer.json +++ b/composer.json @@ -14,11 +14,11 @@ ], "require": { "php": ">=5.4.16", - "cakephp/orm": "3.*" + "cakephp/orm": "4.*" }, "require-dev": { "phpunit/phpunit": "*", - "cakephp/cakephp": "3.*" + "cakephp/cakephp": "4.*" }, "autoload": { "psr-4": { From f38611ffc0417cd938b267ee2b16f42829965526 Mon Sep 17 00:00:00 2001 From: "Angel S. Moreno" Date: Sat, 13 Feb 2021 14:58:55 -0500 Subject: [PATCH 2/3] fixed previously deprecated ->config() --- composer.json | 2 +- src/Model/Behavior/CipherBehavior.php | 49 ++++++++++++++------------- 2 files changed, 27 insertions(+), 24 deletions(-) diff --git a/composer.json b/composer.json index ecb9927..028a197 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,7 @@ } ], "require": { - "php": ">=5.4.16", + "php": ">=7.1", "cakephp/orm": "4.*" }, "require-dev": { diff --git a/src/Model/Behavior/CipherBehavior.php b/src/Model/Behavior/CipherBehavior.php index 5652d03..a81566c 100644 --- a/src/Model/Behavior/CipherBehavior.php +++ b/src/Model/Behavior/CipherBehavior.php @@ -1,8 +1,11 @@ null, 'salt' => null, ]; - + /** * Initialize behavior configuration * @param array $config Configuration passed to the behavior - * @throws \Cake\Core\Exception\Exception * @return void + * @throws Exception */ - public function initialize(array $config) + public function initialize(array $config): void { - $fields = $this->config('fields'); + $fields = $this->getConfig('fields'); if (empty($fields)) { - throw new \Cake\Core\Exception\Exception('Empty fields in CipherBehavior'); + throw new Exception('Empty fields in CipherBehavior'); } - - foreach ($this->config('fields') as $fieldName => $fieldType) { + + foreach ($this->getConfig('fields') as $fieldName => $fieldType) { if (!is_string($fieldName) || !is_string($fieldType) || empty($fieldName) || empty($fieldType)) { - throw new \Cake\Core\Exception\Exception('Field type need to be specified in CypherBehavior fields'); + throw new Exception('Field type need to be specified in CypherBehavior fields'); } try { // Throws exception if type is not valid Type::build($fieldType); } catch (\InvalidArgumentException $ex) { - throw new \Cake\Core\Exception\Exception(sprintf('Field type %s not valid for field %s', $fieldType, $fieldName)); + throw new Exception(sprintf('Field type %s not valid for field %s', $fieldType, $fieldName)); } } - - $key = $this->config('key'); + + $key = $this->getConfig('key'); if (empty($key)) { $key = Configure::read('App.Encrypt.key'); if (empty($key)) { - throw new \Cake\Core\Exception\Exception('App.Encrypt.key config value is empty'); + throw new Exception('App.Encrypt.key config value is empty'); } - $this->config('key', $key); + $this->getConfig('key', $key); } - - $salt = $this->config('salt'); + + $salt = $this->getConfig('salt'); if (empty($salt)) { $salt = Configure::read('App.Encrypt.salt'); if (empty($salt)) { - throw new \Cake\Core\Exception\Exception('App.Encrypt.salt config value is empty'); + throw new Exception('App.Encrypt.salt config value is empty'); } - $this->config('salt', $salt); + $this->getConfig('salt', $salt); } } @@ -80,7 +83,7 @@ public function initialize(array $config) public function beforeSave(Event $event, Entity $entity) { $entity->_cyphered = []; - foreach ($this->config('fields') as $field => $type) { + foreach ($this->getConfig('fields') as $field => $type) { if ($entity->has($field)) { $value = $entity->get($field); // Convert values to db representation before encrypting them @@ -120,10 +123,10 @@ public function afterSave(Event $event, Entity $entity, ArrayObject $options) */ public function beforeFind(Event $event, Query $query, ArrayObject $options, $primary) { - $fields = $this->config('fields'); + $fields = $this->getConfig('fields'); $driver = $this->_table->connection()->driver(); - $formatter = function (\Cake\Collection\CollectionInterface $results) use ($fields, $driver) { + $formatter = function (\Cake\Collection\CollectionInterface $results) use ($fields, $driver) { return $results->each(function ($entity) use ($fields, $driver) { if ($entity instanceof \Cake\Datasource\EntityInterface) { foreach ($fields as $field => $type) { @@ -151,7 +154,7 @@ public function beforeFind(Event $event, Query $query, ArrayObject $options, $pr */ public function beforeMarshal(Event $event, ArrayObject $data, ArrayObject $options) { - $fields = $this->config('fields'); + $fields = $this->getConfig('fields'); foreach ($fields as $field => $type) { if (isset($data[$field])) { @@ -167,7 +170,7 @@ public function beforeMarshal(Event $event, ArrayObject $data, ArrayObject $opti */ public function encrypt($value) { - return Security::encrypt($value, $this->config('key'), $this->config('salt')); + return Security::encrypt($value, $this->getConfig('key'), $this->getConfig('salt')); } /** @@ -180,6 +183,6 @@ public function decrypt($cryptedValue) if (is_resource($cryptedValue)) { $cryptedValue = stream_get_contents($cryptedValue); } - return Security::decrypt($cryptedValue, $this->config('key'), $this->config('salt')); + return Security::decrypt($cryptedValue, $this->getConfig('key'), $this->getConfig('salt')); } } From 3e45c3af3ffa68563320190e70b8f888dd3f9a4c Mon Sep 17 00:00:00 2001 From: "Angel S. Moreno" Date: Sun, 14 Feb 2021 15:23:29 -0500 Subject: [PATCH 3/3] remove deprecated methods, code clean up and php 7.2 adoptions --- composer.json | 2 +- src/Model/Behavior/CipherBehavior.php | 32 +++++++++++++++++---------- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/composer.json b/composer.json index 028a197..7a77417 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,7 @@ } ], "require": { - "php": ">=7.1", + "php": ">=7.2", "cakephp/orm": "4.*" }, "require-dev": { diff --git a/src/Model/Behavior/CipherBehavior.php b/src/Model/Behavior/CipherBehavior.php index a81566c..8bec66b 100644 --- a/src/Model/Behavior/CipherBehavior.php +++ b/src/Model/Behavior/CipherBehavior.php @@ -4,9 +4,12 @@ namespace CipherBehavior\Model\Behavior; use ArrayObject; +use Cake\Collection\CollectionInterface; use Cake\Core\Configure; use Cake\Core\Exception\Exception; +use Cake\Database\DriverInterface; use Cake\Database\Type; +use Cake\Datasource\EntityInterface; use Cake\Event\Event; use Cake\ORM\Behavior; use Cake\ORM\Entity; @@ -74,6 +77,11 @@ public function initialize(array $config): void } } + protected function getTableConnectionDriver(): DriverInterface + { + return $this->table()->getConnection()->getDriver(); + } + /** * Encrypt values before saving to DB * @param Event $event Event object @@ -87,7 +95,7 @@ public function beforeSave(Event $event, Entity $entity) if ($entity->has($field)) { $value = $entity->get($field); // Convert values to db representation before encrypting them - $dbValue = Type::build($type)->toDatabase($value, $this->_table->connection()->driver()); + $dbValue = Type::build($type)->toDatabase($value, $this->getTableConnectionDriver()); $cryptedValue = $this->encrypt($dbValue); $entity->set($field, $cryptedValue); $entity->_cyphered[$field] = $value; @@ -118,17 +126,16 @@ public function afterSave(Event $event, Entity $entity, ArrayObject $options) * @param Event $event Event object * @param Query $query Query object * @param ArrayObject $options Query options array - * @param type $primary Root/associated query * @return void */ - public function beforeFind(Event $event, Query $query, ArrayObject $options, $primary) + public function beforeFind(Event $event, Query $query, ArrayObject $options) { $fields = $this->getConfig('fields'); - $driver = $this->_table->connection()->driver(); + $driver = $this->getTableConnectionDriver(); - $formatter = function (\Cake\Collection\CollectionInterface $results) use ($fields, $driver) { + $formatter = function (CollectionInterface $results) use ($fields, $driver) { return $results->each(function ($entity) use ($fields, $driver) { - if ($entity instanceof \Cake\Datasource\EntityInterface) { + if ($entity instanceof EntityInterface) { foreach ($fields as $field => $type) { if ($entity->has($field)) { $value = $entity->get($field); @@ -165,24 +172,25 @@ public function beforeMarshal(Event $event, ArrayObject $data, ArrayObject $opti /** * Encrypt a value - * @param type $value Value to be encrypted - * @return type Encrypted value + * @param string $value Value to be encrypted + * @return string Encrypted value */ - public function encrypt($value) + public function encrypt(string $value): string { return Security::encrypt($value, $this->getConfig('key'), $this->getConfig('salt')); } /** * Decrypt an encrypted value - * @param type $cryptedValue Value to be decrypted - * @return type Decrypted value + * @param resource|string $cryptedValue Value to be decrypted + * @return string */ - public function decrypt($cryptedValue) + public function decrypt($cryptedValue): string { if (is_resource($cryptedValue)) { $cryptedValue = stream_get_contents($cryptedValue); } + return Security::decrypt($cryptedValue, $this->getConfig('key'), $this->getConfig('salt')); } }