mirror of
https://github.com/archtechx/virtualcolumn.git
synced 2025-12-12 08:24:04 +00:00
Handle encrypted columns which are set to null (#19)
* 🧪 Add a test for a null value * 🐛 Don't consider null types for decryption
This commit is contained in:
parent
7371aac2ab
commit
65f9000328
2 changed files with 7 additions and 3 deletions
|
|
@ -45,7 +45,7 @@ trait VirtualColumn
|
||||||
foreach ($this->getAttribute(static::getDataColumn()) ?? [] as $key => $value) {
|
foreach ($this->getAttribute(static::getDataColumn()) ?? [] as $key => $value) {
|
||||||
$attributeHasEncryptedCastable = in_array(data_get($this->getCasts(), $key), $encryptedCastables);
|
$attributeHasEncryptedCastable = in_array(data_get($this->getCasts(), $key), $encryptedCastables);
|
||||||
|
|
||||||
if ($attributeHasEncryptedCastable && $this->valueEncrypted($value)) {
|
if ($value && $attributeHasEncryptedCastable && $this->valueEncrypted($value)) {
|
||||||
$this->attributes[$key] = $value;
|
$this->attributes[$key] = $value;
|
||||||
} else {
|
} else {
|
||||||
$this->setAttribute($key, $value);
|
$this->setAttribute($key, $value);
|
||||||
|
|
|
||||||
|
|
@ -143,13 +143,16 @@ class VirtualColumnTest extends TestCase
|
||||||
'json' => json_encode(['foo', 'bar']), // 'encrypted:json'
|
'json' => json_encode(['foo', 'bar']), // 'encrypted:json'
|
||||||
'object' => (object) json_encode(['foo', 'bar']), // 'encrypted:object'
|
'object' => (object) json_encode(['foo', 'bar']), // 'encrypted:object'
|
||||||
'custom' => 'foo', // Custom castable – 'EncryptedCast::class'
|
'custom' => 'foo', // Custom castable – 'EncryptedCast::class'
|
||||||
|
'null_value' => null, // 'encrypted'
|
||||||
]);
|
]);
|
||||||
|
|
||||||
foreach($encryptedAttributes as $key => $expectedValue) {
|
foreach($encryptedAttributes as $key => $expectedValue) {
|
||||||
$savedValue = $model->getAttributes()[$key]; // Encrypted
|
$savedValue = $model->getAttributes()[$key]; // Encrypted
|
||||||
|
|
||||||
$this->assertTrue($model->valueEncrypted($savedValue));
|
if ($savedValue !== null) {
|
||||||
$this->assertNotEquals($expectedValue, $savedValue);
|
$this->assertTrue($model->valueEncrypted($savedValue));
|
||||||
|
$this->assertNotEquals($expectedValue, $savedValue);
|
||||||
|
}
|
||||||
|
|
||||||
$retrievedValue = $model->$key; // Decrypted
|
$retrievedValue = $model->$key; // Decrypted
|
||||||
|
|
||||||
|
|
@ -178,6 +181,7 @@ class MyModel extends ParentModel
|
||||||
'json' => 'encrypted:json',
|
'json' => 'encrypted:json',
|
||||||
'object' => 'encrypted:object',
|
'object' => 'encrypted:object',
|
||||||
'custom' => EncryptedCast::class,
|
'custom' => EncryptedCast::class,
|
||||||
|
'null_value' => 'encrypted',
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue