Skip to content

Commit

Permalink
Added: support for encrypting the note field
Browse files Browse the repository at this point in the history
  • Loading branch information
james2doyle committed Jul 29, 2021
1 parent bf4b09b commit c7c7e9e
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 1 deletion.
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
"keywords": [
"EngineDigital",
"laravel",
"model-notes"
"tenant",
"multi-tenancy",
"encryption",
"notes"
],
"homepage": "https://github.com/enginedigital/model-notes",
"license": "MIT",
Expand Down
1 change: 1 addition & 0 deletions config/model-notes.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
'json',
],
'model_primary_key_attribute' => 'model_id',
'encrypt_notes' => false,
'tenant_model' => null, // App\Models\Company::class
'tenant_resolver' => null, // a class that uses `__invoke` to get the id of the current tenant
];
27 changes: 27 additions & 0 deletions src/EncryptNoteCast.php
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;
}
}
1 change: 1 addition & 0 deletions src/Note.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Note extends Model
// protected $guarded = [];

protected $casts = [
'note' => EncryptNoteCast::class,
'created_at' => 'datetime',
'updated_at' => 'datetime',
];
Expand Down
32 changes: 32 additions & 0 deletions tests/NoteEncryptionTest.php
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);
}
}
4 changes: 4 additions & 0 deletions tests/NoteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public function notes_can_be_created()

$this->assertTrue($note->exists());
$this->assertEquals(config('model-notes.note_default_type'), $note->fresh()->type);
// check that the raw value matches - which is not the case when using encryption
$this->assertEquals($note->note, $note->getAttributes()['note']);
}

/** @test */
Expand Down Expand Up @@ -60,6 +62,8 @@ public function notes_can_stringified()
]);

$this->assertEquals($expected, (string)$note);
// check that the raw value matches - which is not the case when using encryption
$this->assertEquals($expected, $note->getAttributes()['note']);
}

/** @test */
Expand Down
2 changes: 2 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ protected function getPackageProviders($app)
public function getEnvironmentSetUp($app)
{
config()->set('database.default', 'testing');
// used for testing encryption
config()->set('app.key', 'base64:UmEsQyuFBEwGbpbSdfICjV3v4DGtSjMElnikafE7c9k=');

// $migration = require __DIR__ . '/../database/migrations/create_model_notes_table.php.stub';
// $migration->up();
Expand Down

0 comments on commit c7c7e9e

Please sign in to comment.