1
0
Fork 0
mirror of https://github.com/archtechx/virtualcolumn.git synced 2025-12-12 09:24:02 +00:00

Add encrypted casts support + allow using the trait on multiple models (#14)

* Add encrypted casts test (wip)

* Handle and test 'encrypted' casts

* Add APP_KEY to phpunit.xml

* Update attribute casting in VirtualColumn

* Test casting of all default 'encrypted' castables

* Fix code style (php-cs-fixer)

* Handle custom castables in VirtualColumn

* Add custom encrypted castable

* Test custom encrypted castable, refactor test

* Move EncryptedCast class to VirtualColumnTest

* Correct expected/actual value order in assertions

* Break code style (testing)

* Fix code style (php-cs-fixer)

* Check Laravel CI version (testing)

* dd() Laravel version

* Delete dd()

* Delete get() and set() types

* Use non-lowercase custom cast class strings

* Check hasCast manually

* Correct encrypted castable logic

* Update src/VirtualColumn.php

* Use `$dataEncoded` bool instead of `$dataEncodingStatus` string

* Don't accept unused `$e`

* Refactor `encodeAttributes()`

* Use `$model->getCustomColumns()` instead of `static::getCustomColumns()`

* Use `$model` instead of `static` where possible

* Correct test

* Revert `static` -> `$model` changes

* Correct typo

* Refactor `$afterListeneres`

* Fix code style (php-cs-fixer)

* Make static things non-static in VirtualColumn

* Change method to non-static in test

* Add base class that uses VirtualColumn in tests

* Add encrypted castables docblock

* Fix merge

* Fix ParentModel change

* make $this and $model use clear and consistent

---------

Co-authored-by: Samuel Štancl <samuel.stancl@gmail.com>
Co-authored-by: Samuel Štancl <samuel@archte.ch>
This commit is contained in:
lukinovec 2023-11-08 10:08:34 +01:00 committed by GitHub
parent 925249bb72
commit 72de33a3d4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 216 additions and 108 deletions

View file

@ -1,13 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" bootstrap="vendor/autoload.php" colors="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd"> <phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" bootstrap="vendor/autoload.php" colors="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.4/phpunit.xsd">
<coverage> <coverage/>
<include>
<directory suffix=".php">./src</directory>
</include>
<exclude>
<file>./src/routes.php</file>
</exclude>
</coverage>
<testsuites> <testsuites>
<testsuite name="Unit"> <testsuite name="Unit">
<directory suffix="Test.php">./tests</directory> <directory suffix="Test.php">./tests</directory>
@ -15,6 +8,7 @@
</testsuites> </testsuites>
<php> <php>
<env name="APP_ENV" value="testing"/> <env name="APP_ENV" value="testing"/>
<env name="APP_KEY" value="base64:+osRhaqQtOcYM79fhVU8YdNBs/1iVJPWYUr9zvTPCs0="/>
<env name="BCRYPT_ROUNDS" value="4"/> <env name="BCRYPT_ROUNDS" value="4"/>
<env name="CACHE_DRIVER" value="redis"/> <env name="CACHE_DRIVER" value="redis"/>
<env name="MAIL_DRIVER" value="array"/> <env name="MAIL_DRIVER" value="array"/>
@ -24,4 +18,12 @@
<env name="DB_DATABASE" value=":memory:"/> <env name="DB_DATABASE" value=":memory:"/>
<env name="AWS_DEFAULT_REGION" value="us-west-2"/> <env name="AWS_DEFAULT_REGION" value="us-west-2"/>
</php> </php>
<source>
<include>
<directory suffix=".php">./src</directory>
</include>
<exclude>
<file>./src/routes.php</file>
</exclude>
</source>
</phpunit> </phpunit>

28
phpunit.xml.bak Normal file
View file

@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" bootstrap="vendor/autoload.php" colors="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd">
<coverage>
<include>
<directory suffix=".php">./src</directory>
</include>
<exclude>
<file>./src/routes.php</file>
</exclude>
</coverage>
<testsuites>
<testsuite name="Unit">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<php>
<env name="APP_ENV" value="testing"/>
<env name="APP_KEY" value="base64:+osRhaqQtOcYM79fhVU8YdNBs/1iVJPWYUr9zvTPCs0="/>
<env name="BCRYPT_ROUNDS" value="4"/>
<env name="CACHE_DRIVER" value="redis"/>
<env name="MAIL_DRIVER" value="array"/>
<env name="QUEUE_CONNECTION" value="sync"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>
<env name="AWS_DEFAULT_REGION" value="us-west-2"/>
</php>
</phpunit>

View file

@ -4,6 +4,9 @@ declare(strict_types=1);
namespace Stancl\VirtualColumn; namespace Stancl\VirtualColumn;
use Illuminate\Contracts\Encryption\DecryptException;
use Illuminate\Support\Facades\Crypt;
/** /**
* This trait lets you add a "data" column functionality to any Eloquent model. * This trait lets you add a "data" column functionality to any Eloquent model.
* It serializes attributes which don't exist as columns on the model's table * It serializes attributes which don't exist as columns on the model's table
@ -13,74 +16,119 @@ namespace Stancl\VirtualColumn;
*/ */
trait VirtualColumn trait VirtualColumn
{ {
public static $afterListeners = []; /**
* Encrypted castables have to be handled using a special approach that prevents the data from getting encrypted repeatedly.
*
* The default encrypted castables ('encrypted', 'encrypted:array', 'encrypted:collection', 'encrypted:json', 'encrypted:object')
* are already handled, so you can use this array to add your own encrypted castables.
*/
public static array $customEncryptedCastables = [];
/** /**
* We need this property, because both created & saved event listeners * We need this property, because both created & saved event listeners
* decode the data (to take precedence before other created & saved) * decode the data (to take precedence before other created & saved)
* listeners, but we don't want the data to be decoded twice. * listeners, but we don't want the data to be decoded twice.
*
* @var string
*/ */
public $dataEncodingStatus = 'decoded'; public bool $dataEncoded = false;
protected static function decodeVirtualColumn(self $model): void protected function decodeVirtualColumn(): void
{ {
if ($model->dataEncodingStatus === 'decoded') { if (! $this->dataEncoded) {
return; return;
} }
foreach ($model->getAttribute(static::getDataColumn()) ?? [] as $key => $value) { $encryptedCastables = array_merge(
$model->setAttribute($key, $value); static::$customEncryptedCastables,
$model->syncOriginalAttribute($key); ['encrypted', 'encrypted:array', 'encrypted:collection', 'encrypted:json', 'encrypted:object'], // Default encrypted castables
);
foreach ($this->getAttribute($this->getDataColumn()) ?? [] as $key => $value) {
$attributeHasEncryptedCastable = in_array(data_get($this->getCasts(), $key), $encryptedCastables);
if ($attributeHasEncryptedCastable && $this->valueEncrypted($value)) {
$this->attributes[$key] = $value;
} else {
$this->setAttribute($key, $value);
} }
$model->setAttribute(static::getDataColumn(), null); $this->syncOriginalAttribute($key);
$model->dataEncodingStatus = 'decoded';
} }
protected static function encodeAttributes(self $model): void $this->setAttribute($this->getDataColumn(), null);
$this->dataEncoded = false;
}
protected function encodeAttributes(): void
{ {
if ($model->dataEncodingStatus === 'encoded') { if ($this->dataEncoded) {
return; return;
} }
foreach ($model->getAttributes() as $key => $value) { $dataColumn = $this->getDataColumn();
if (! in_array($key, static::getCustomColumns())) { $customColumns = $this->getCustomColumns();
$current = $model->getAttribute(static::getDataColumn()) ?? []; $attributes = array_filter($this->getAttributes(), fn ($key) => ! in_array($key, $customColumns), ARRAY_FILTER_USE_KEY);
$model->setAttribute(static::getDataColumn(), array_merge($current, [ // Remove data column from the attributes
$key => $value, unset($attributes[$dataColumn]);
]));
unset($model->attributes[$key]); foreach ($attributes as $key => $value) {
unset($model->original[$key]); // Remove attribute from the model
} unset($this->attributes[$key]);
unset($this->original[$key]);
} }
$model->dataEncodingStatus = 'encoded'; // Add attribute to the data column
$this->setAttribute($dataColumn, $attributes);
$this->dataEncoded = true;
} }
public static function bootVirtualColumn() public function valueEncrypted(string $value): bool
{ {
static::registerAfterListener('retrieved', function ($model) { try {
// We always decode after model retrieval. Crypt::decryptString($value);
$model->dataEncodingStatus = 'encoded';
static::decodeVirtualColumn($model); return true;
}); } catch (DecryptException) {
return false;
}
}
// Encode if writing protected function decodeAttributes()
static::registerAfterListener('saving', [static::class, 'encodeAttributes']); {
static::registerAfterListener('creating', [static::class, 'encodeAttributes']); $this->dataEncoded = true;
static::registerAfterListener('updating', [static::class, 'encodeAttributes']);
$this->decodeVirtualColumn();
}
protected function getAfterListeners(): array
{
return [
'retrieved' => [
function () {
// Always decode after model retrieval
$this->dataEncoded = true;
$this->decodeVirtualColumn();
},
],
'saving' => [
[$this, 'encodeAttributes'],
],
'creating' => [
[$this, 'encodeAttributes'],
],
'updating' => [
[$this, 'encodeAttributes'],
],
];
} }
protected function decodeIfEncoded() protected function decodeIfEncoded()
{ {
if ($this->dataEncodingStatus === 'encoded') { if ($this->dataEncoded) {
static::decodeVirtualColumn($this); $this->decodeVirtualColumn();
} }
} }
@ -97,7 +145,7 @@ trait VirtualColumn
public function runAfterListeners($event, $halt = true) public function runAfterListeners($event, $halt = true)
{ {
$listeners = static::$afterListeners[$event] ?? []; $listeners = $this->getAfterListeners()[$event] ?? [];
if (! $event) { if (! $event) {
return; return;
@ -115,27 +163,22 @@ trait VirtualColumn
} }
} }
public static function registerAfterListener(string $event, callable $callback)
{
static::$afterListeners[$event][] = $callback;
}
public function getCasts() public function getCasts()
{ {
return array_merge(parent::getCasts(), [ return array_merge(parent::getCasts(), [
static::getDataColumn() => 'array', $this->getDataColumn() => 'array',
]); ]);
} }
/** /**
* Get the name of the column that stores additional data. * Get the name of the column that stores additional data.
*/ */
public static function getDataColumn(): string public function getDataColumn(): string
{ {
return 'data'; return 'data';
} }
public static function getCustomColumns(): array public function getCustomColumns(): array
{ {
return [ return [
'id', 'id',
@ -149,10 +192,10 @@ trait VirtualColumn
*/ */
public function getColumnForQuery(string $column): string public function getColumnForQuery(string $column): string
{ {
if (in_array($column, static::getCustomColumns(), true)) { if (in_array($column, $this->getCustomColumns(), true)) {
return $column; return $column;
} }
return static::getDataColumn() . '->' . $column; return $this->getDataColumn() . '->' . $column;
} }
} }

View file

@ -2,10 +2,12 @@
namespace Stancl\VirtualColumn\Tests; namespace Stancl\VirtualColumn\Tests;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Orchestra\Testbench\TestCase; use Orchestra\Testbench\TestCase;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Database\Eloquent\Model;
use Stancl\VirtualColumn\VirtualColumn; use Stancl\VirtualColumn\VirtualColumn;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
class VirtualColumnTest extends TestCase class VirtualColumnTest extends TestCase
{ {
@ -49,7 +51,7 @@ class VirtualColumnTest extends TestCase
$this->assertSame('xyz', $model->getOriginal('abc')); $this->assertSame('xyz', $model->getOriginal('abc'));
$this->assertSame(null, $model->data); $this->assertSame(null, $model->data);
// Model can be retrieved after update & is structure correctly // Model can be retrieved after update & is structured correctly
$model = MyModel::first(); $model = MyModel::first();
$this->assertSame('baz', $model->foo); $this->assertSame('baz', $model->foo);
@ -61,25 +63,25 @@ class VirtualColumnTest extends TestCase
public function model_is_always_decoded_when_accessed_by_user_event() public function model_is_always_decoded_when_accessed_by_user_event()
{ {
MyModel::retrieved(function (MyModel $model) { MyModel::retrieved(function (MyModel $model) {
$this->assertSame('decoded', $model->dataEncodingStatus); $this->assertFalse($model->dataEncoded);
}); });
MyModel::saving(function (MyModel $model) { MyModel::saving(function (MyModel $model) {
$this->assertSame('decoded', $model->dataEncodingStatus); $this->assertFalse($model->dataEncoded);
}); });
MyModel::updating(function (MyModel $model) { MyModel::updating(function (MyModel $model) {
$this->assertSame('decoded', $model->dataEncodingStatus); $this->assertFalse($model->dataEncoded);
}); });
MyModel::creating(function (MyModel $model) { MyModel::creating(function (MyModel $model) {
$this->assertSame('decoded', $model->dataEncodingStatus); $this->assertFalse($model->dataEncoded);
}); });
MyModel::saved(function (MyModel $model) { MyModel::saved(function (MyModel $model) {
$this->assertSame('decoded', $model->dataEncodingStatus); $this->assertFalse($model->dataEncoded);
}); });
MyModel::updated(function (MyModel $model) { MyModel::updated(function (MyModel $model) {
$this->assertSame('decoded', $model->dataEncodingStatus); $this->assertFalse($model->dataEncoded);
}); });
MyModel::created(function (MyModel $model) { MyModel::created(function (MyModel $model) {
$this->assertSame('decoded', $model->dataEncodingStatus); $this->assertFalse($model->dataEncoded);
}); });
@ -112,8 +114,9 @@ class VirtualColumnTest extends TestCase
// 'foo' is a custom column, 'data' is the virtual column // 'foo' is a custom column, 'data' is the virtual column
FooChild::create(['foo' => 'foo']); FooChild::create(['foo' => 'foo']);
$encodedFoo = DB::select('select * from foo_childs limit 1')[0]; $encodedFoo = DB::select('select * from foo_childs limit 1')[0];
// Assert that the model was encoded correctly // Assert that the model was encoded correctly
$this->assertNull($encodedFoo->data); $this->assertSame($encodedFoo->data, '[]');
$this->assertSame($encodedFoo->foo, 'foo'); $this->assertSame($encodedFoo->foo, 'foo');
// Create another child model of the same parent // Create another child model of the same parent
@ -121,49 +124,40 @@ class VirtualColumnTest extends TestCase
BarChild::create(['bar' => 'bar']); BarChild::create(['bar' => 'bar']);
$encodedBar = DB::select('select * from bar_childs limit 1')[0]; $encodedBar = DB::select('select * from bar_childs limit 1')[0];
$this->assertNull($encodedBar->data); $this->assertSame($encodedBar->data, '[]');
$this->assertSame($encodedBar->bar, 'bar'); $this->assertSame($encodedBar->bar, 'bar');
} }
// maybe add an explicit test that the saving() and updating() listeners don't run twice? // maybe add an explicit test that the saving() and updating() listeners don't run twice?
/** @test */
public function encrypted_casts_work_with_virtual_column() {
// Custom encrypted castables have to be specified in the $customEncryptedCastables static property
MyModel::$customEncryptedCastables = [EncryptedCast::class];
/** @var MyModel $model */
$model = MyModel::create($encryptedAttributes = [
'password' => 'foo', // 'encrypted'
'array' => ['foo', 'bar'], // 'encrypted:array'
'collection' => collect(['foo', 'bar']), // 'encrypted:collection'
'json' => json_encode(['foo', 'bar']), // 'encrypted:json'
'object' => (object) json_encode(['foo', 'bar']), // 'encrypted:object'
'custom' => 'foo', // Custom castable 'EncryptedCast::class'
]);
foreach($encryptedAttributes as $key => $expectedValue) {
$savedValue = $model->getAttributes()[$key]; // Encrypted
$this->assertTrue($model->valueEncrypted($savedValue));
$this->assertNotEquals($expectedValue, $savedValue);
$retrievedValue = $model->$key; // Decrypted
$this->assertEquals($expectedValue, $retrievedValue);
} }
class MyModel extends Model // Reset static property
{ MyModel::$customEncryptedCastables = [];
use VirtualColumn;
protected $guarded = [];
public $timestamps = false;
public static function getCustomColumns(): array
{
return [
'id',
'custom1',
'custom2',
];
}
}
class FooModel extends Model
{
use VirtualColumn;
protected $guarded = [];
public $timestamps = false;
public static function getCustomColumns(): array
{
return [
'id',
'custom1',
'custom2',
];
}
public static function getDataColumn(): string
{
return 'virtual';
} }
} }
@ -175,12 +169,53 @@ class ParentModel extends Model
protected $guarded = []; protected $guarded = [];
} }
class MyModel extends ParentModel
{
public $casts = [
'password' => 'encrypted',
'array' => 'encrypted:array',
'collection' => 'encrypted:collection',
'json' => 'encrypted:json',
'object' => 'encrypted:object',
'custom' => EncryptedCast::class,
];
}
class FooModel extends ParentModel
{
public function getCustomColumns(): array
{
return [
'id',
'custom1',
'custom2',
];
}
public function getDataColumn(): string
{
return 'virtual';
}
}
class EncryptedCast implements CastsAttributes
{
public function get($model, $key, $value, $attributes)
{
return Crypt::decryptString($value);
}
public function set($model, $key, $value, $attributes)
{
return Crypt::encryptString($value);
}
}
class FooChild extends ParentModel class FooChild extends ParentModel
{ {
public $table = 'foo_childs'; public $table = 'foo_childs';
public static function getCustomColumns(): array public function getCustomColumns(): array
{ {
return [ return [
'id', 'id',
@ -192,7 +227,7 @@ class BarChild extends ParentModel
{ {
public $table = 'bar_childs'; public $table = 'bar_childs';
public static function getCustomColumns(): array public function getCustomColumns(): array
{ {
return [ return [
'id', 'id',