Skip to content

Commit

Permalink
use encryption in cached settings
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenvanassche committed Sep 7, 2022
1 parent 7b1cafe commit ee1b1a0
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
26 changes: 25 additions & 1 deletion src/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Spatie\LaravelSettings\Events\SettingsLoaded;
use Spatie\LaravelSettings\Events\SettingsSaved;
use Spatie\LaravelSettings\SettingsRepositories\SettingsRepository;
use Spatie\LaravelSettings\Support\Crypto;

abstract class Settings implements Arrayable, Jsonable, Responsable
{
Expand Down Expand Up @@ -120,12 +121,35 @@ public function __isset($name)

public function __serialize(): array
{
return $this->toCollection()->all();
/** @var Collection $encrypted */
/** @var Collection $nonEncrypted */
[$encrypted, $nonEncrypted] = $this->toCollection()->partition(
fn($value, string $name) => $this->config->isEncrypted($name)
);

return array_merge(
$encrypted->map(fn($value) => Crypto::encrypt($value))->all(),
$nonEncrypted->all()
);
}

public function __unserialize(array $data): void
{
$this->loaded = false;

$this->ensureConfigIsLoaded();

/** @var Collection $encrypted */
/** @var Collection $nonEncrypted */
[$encrypted, $nonEncrypted] = collect($data)->partition(
fn($value, string $name) => $this->config->isEncrypted($name)
);

$data = array_merge(
$encrypted->map(fn($value) => Crypto::decrypt($value))->all(),
$nonEncrypted->all()
);

$this->loadValues($data);
}

Expand Down
28 changes: 27 additions & 1 deletion tests/SettingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@

assertDatabaseMissing('migrations', ['migration' => '2018_11_21_091111_create_fake_settings']);
assertDatabaseHas('migrations', ['migration' => '2018_11_21_091111_create_fake_table']);
})->skip(fn () => Str::startsWith(app()->version(), '7'), 'No support for dumping migrations in Laravel 7');
})->skip(fn() => Str::startsWith(app()->version(), '7'), 'No support for dumping migrations in Laravel 7');

it('will not contact the repository when loading cached settings', function () {
useEnabledCache($this->app);
Expand All @@ -458,6 +458,32 @@
expect($log)->toHaveCount(0);
});

it('will cache encrypted setting', function () {
useEnabledCache($this->app);

$data = [
'string' => 'Hello',
'nullable' => null,
'cast' => new DateTime('2020-05-16'),
];

$cache = resolve(SettingsCache::class);

$cache->put(new DummyEncryptedSettings($data));

$serialized = Cache::get('settings.'.DummyEncryptedSettings::class);

expect($serialized)->not()->toContain($data['string']);

$this->setRegisteredSettings([
DummyEncryptedSettings::class,
]);

$decrypted = resolve(DummyEncryptedSettings::class);

expect($decrypted->string)->toEqual($data['string']);
});

it('can clear a settings cache', function () {
useEnabledCache($this->app);

Expand Down

0 comments on commit ee1b1a0

Please sign in to comment.