Skip to content

Commit

Permalink
Fix issue where abstract eloquent casts were not encrypted
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenvanassche committed Jul 25, 2024
1 parent 372b3fd commit 8980ee5
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 14 deletions.
11 changes: 5 additions & 6 deletions src/Support/EloquentCasts/DataEloquentCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,13 @@ public function set($model, string $key, $value, array $attributes): ?string
throw CannotCastData::shouldBeTransformableData($model::class, $key);
}

if ($isAbstractClassCast) {
return json_encode([
$value = $isAbstractClassCast
? json_encode([
'type' => $this->dataConfig->morphMap->getDataClassAlias($value::class) ?? $value::class,
'data' => json_decode($value->toJson(), associative: true, flags: JSON_THROW_ON_ERROR),
]);
}

$value = $value->toJson();
])
: $value->toJson();
;

if (in_array('encrypted', $this->arguments)) {
return Crypt::encryptString($value);
Expand Down
4 changes: 4 additions & 0 deletions tests/Fakes/Models/DummyModelWithEncryptedCasts.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Spatie\LaravelData\Tests\Fakes\Models;

use Illuminate\Database\Eloquent\Model;
use Spatie\LaravelData\DataCollection;
use Spatie\LaravelData\Tests\Fakes\AbstractData\AbstractData;
use Spatie\LaravelData\Tests\Fakes\SimpleData;
use Spatie\LaravelData\Tests\Fakes\SimpleDataCollection;

Expand All @@ -11,6 +13,8 @@ class DummyModelWithEncryptedCasts extends Model
protected $casts = [
'data' => SimpleData::class.':encrypted',
'data_collection' => SimpleDataCollection::class.':'.SimpleData::class.',encrypted',
'abstract_data' => AbstractData::class.':encrypted',
'abstract_collection' => DataCollection::class . ':' . AbstractData::class.',encrypted',
];

protected $table = 'dummy_model_with_casts';
Expand Down
34 changes: 26 additions & 8 deletions tests/Support/EloquentCasts/DataEloquentCastTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,17 +135,12 @@
});

it('can save an encrypted data object', function () {
// Save the encrypted data to the database
DummyModelWithEncryptedCasts::create([
$model = DummyModelWithEncryptedCasts::create([
'data' => new SimpleData('Test'),
]);

// Retrieve the model from the database without Eloquent casts
$model = DB::table('dummy_model_with_casts')
->first();

try {
Crypt::decryptString($model->data);
Crypt::decryptString($model->getRawOriginal('data'));
$isEncrypted = true;
} catch (DecryptException $e) {
$isEncrypted = false;
Expand All @@ -155,7 +150,6 @@
});

it('can load an encrypted data object', function () {
// Save the encrypted data to the database
DummyModelWithEncryptedCasts::create([
'data' => new SimpleData('Test'),
]);
Expand All @@ -165,3 +159,27 @@

expect($model->data)->toEqual(new SimpleData('Test'));
});

it('can load and save an abstract defined data object', function () {
$abstractA = new AbstractDataA('A\A');

$modelId = DummyModelWithEncryptedCasts::create([
'abstract_data' => $abstractA,
])->id;

$model = DummyModelWithEncryptedCasts::find($modelId);

expect($model->abstract_data)
->toBeInstanceOf(AbstractDataA::class)
->a->toBe('A\A');


try {
Crypt::decryptString($model->getRawOriginal('abstract_data'));
$isEncrypted = true;
} catch (DecryptException $e) {
$isEncrypted = false;
}

expect($isEncrypted)->toBeTrue();
});

0 comments on commit 8980ee5

Please sign in to comment.