generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added: support for encrypting the note field
- Loading branch information
1 parent
bf4b09b
commit c7c7e9e
Showing
7 changed files
with
71 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace EngineDigital\Note; | ||
|
||
use Illuminate\Contracts\Database\Eloquent\CastsAttributes; | ||
use Illuminate\Support\Facades\Crypt; | ||
|
||
class EncryptNoteCast implements CastsAttributes | ||
{ | ||
public function get($model, string $key, $value, array $attributes): string | ||
{ | ||
$enabled = config('model-notes.encrypt_notes'); | ||
|
||
try { | ||
return $enabled ? Crypt::decryptString($value) : $value; | ||
} catch (DecryptException $e) { | ||
throw $e; | ||
} | ||
} | ||
|
||
public function set($model, string $key, $value, array $attributes): string | ||
{ | ||
$enabled = config('model-notes.encrypt_notes'); | ||
|
||
return $enabled ? Crypt::encryptString($value) : $value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace EngineDigital\Note\Tests; | ||
|
||
use EngineDigital\Note\Note; | ||
|
||
class NoteEncryptionTest extends TestCase | ||
{ | ||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
config()->set('model-notes.encrypt_notes', true); | ||
|
||
$createNotes = require __DIR__ . '/../database/migrations/create_model_notes_table.php.stub'; | ||
$createNotes->up(); | ||
} | ||
|
||
/** @test */ | ||
public function notes_can_be_encrypted() | ||
{ | ||
$content = 'Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.'; | ||
$note = Note::create([ | ||
'note' => $content, | ||
'model_type' => Note::class, | ||
'model_id' => 1, | ||
]); | ||
|
||
$this->assertNotEquals($content, $note->getAttributes()['note']); | ||
$this->assertEquals($content, $note->note); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters