Skip to content

Commit 1873cd5

Browse files
committed
develop/v0.1.0: Added migration, model and helpers.
1 parent ee13a53 commit 1873cd5

File tree

8 files changed

+930
-1
lines changed

8 files changed

+930
-1
lines changed
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,15 @@
1212
],
1313

1414
// Define the DB encryption key. Will be used to encrypt and decrypt data.
15-
'key' => env('DB_ENCRYPTION_KEY'),
15+
// ------------------------------------------------------------
16+
'key' => env('DB_ENCRYPT_KEY'),
17+
18+
// Local database Primary key format
19+
// Options: 'int' (default) or 'uuid'
20+
// ------------------------------------------------------------
21+
'db' => [
22+
'primary_key_format' => env('DB_ENCRYPT_DB_PRIMARY_KEY_FORMAT', 'int'), // int or uuid (36)
23+
],
1624

1725
// ... more to follow
1826

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Wazza\DbEncrypt\Database\Factories;
4+
5+
use Carbon\Carbon;
6+
use Illuminate\Database\Eloquent\Factories\Factory;
7+
8+
class EncryptedAttributesFactory extends Factory
9+
{
10+
/**
11+
* Define the model's default state.
12+
*
13+
* @return array<string, mixed>
14+
*/
15+
public function definition(): array
16+
{
17+
return [
18+
'entity_id' => null,
19+
'state_id' => null,
20+
'type' => $this->faker->randomElement(['physical', 'billing', 'postal']),
21+
'building_name' => $this->faker->word(),
22+
'floor_number' => $this->faker->word(),
23+
'address1' => $this->faker->streetAddress(),
24+
'address2' => $this->faker->secondaryAddress(),
25+
'city' => $this->faker->city(),
26+
'postcode' => $this->faker->postcode(),
27+
'comments' => $this->faker->text(2000),
28+
];
29+
}
30+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class CreateEncryptedAttributesTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
// load the local db primary key format
17+
$dbPkFormat = config('db-encrypt.db.primary_key_format', 'int');
18+
19+
// create the table
20+
Schema::create('encrypted_attributes', function (Blueprint $table) use ($dbPkFormat) {
21+
// define tables engine and charset
22+
$table->engine = 'InnoDB';
23+
$table->charset = 'utf8mb4';
24+
$table->collation = 'utf8mb4_unicode_ci';
25+
26+
// define columns
27+
$table->id();
28+
$table->string('object_type', 64)->nullable()->comment('The local object type - e.g. order, entity, user, etc.');
29+
if ($dbPkFormat === 'uuid') {
30+
$table->string('object_id', 36)->nullable()->comment('The local object unique ID (primary key - `uuid`)');
31+
} else {
32+
$table->unsignedBigInteger('object_id')->nullable()->comment('The local object unique ID (primary key - auto-incremented `int`)');
33+
}
34+
$table->string('attribute', 64)->nullable()->comment('The attribute name, e.g. email, phone, etc.');
35+
$table->string('hash_index', 64)->nullable()->comment('SHA-256 hash of the attribute value (hex format) for fast searching');
36+
$table->text('encrypted_value')->nullable()->comment('The encrypted value of the attribute (Base64 or binary-safe string)');
37+
$table->timestamps();
38+
39+
// add some indexes (we need one on all columns for searching)
40+
$table->index('object_id');
41+
$table->index('hash_index');
42+
$table->index('attribute');
43+
44+
// composite indexes
45+
$table->unique(['object_type', 'object_id', 'attribute'], 'object_type_attribute_unique');
46+
$table->index(['object_type', 'attribute', 'hash_index'], 'object_type_attribute_hash_index');
47+
});
48+
}
49+
50+
/**
51+
* Reverse the migrations.
52+
*
53+
* @return void
54+
*/
55+
public function down()
56+
{
57+
Schema::dropIfExists('encrypted_attributes');
58+
}
59+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Wazza\DbEncrypt\Http\Controllers;
4+
5+
use Wazza\DbEncrypt\Http\Controllers\Logger\LogController;
6+
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
7+
use Illuminate\Foundation\Bus\DispatchesJobs;
8+
use Illuminate\Foundation\Validation\ValidatesRequests;
9+
use Illuminate\Routing\Controller;
10+
11+
class BaseController extends Controller
12+
{
13+
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
14+
15+
/**
16+
* The logger instance
17+
*
18+
* @var LogController
19+
*/
20+
public $logger;
21+
22+
/**
23+
* Create a new CrmController instance.
24+
*
25+
* @param string|null $logIdentifier
26+
* @throws BindingResolutionException
27+
* @throws NotFoundExceptionInterface
28+
* @throws ContainerExceptionInterface
29+
*/
30+
public function __construct(?string $logIdentifier = null)
31+
{
32+
// set the logger instance
33+
$this->logger = new LogController($logIdentifier);
34+
}
35+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Wazza\DbEncrypt\Http\Controllers;
4+
5+
use Wazza\DbEncrypt\Http\Controllers\BaseController;
6+
use Wazza\DbEncrypt\Models\EncryptedAttributes;
7+
use Illuminate\Support\Facades\App;
8+
use Illuminate\Database\Eloquent\Model;
9+
use Illuminate\Contracts\Container\BindingResolutionException;
10+
use Psr\Container\NotFoundExceptionInterface;
11+
use Psr\Container\ContainerExceptionInterface;
12+
use Exception;
13+
14+
/**
15+
* Sync Class CrmController
16+
* Example: (new CrmController())->setModel($user)->execute();
17+
*
18+
* @package Wazza\DbEncrypt\Http\Controllers
19+
* @version 1.0.0
20+
* @todo convert the log class to be injected into the controller instead of using the facade
21+
*/
22+
23+
class DnEncryptController extends BaseController
24+
{
25+
/**
26+
* Create a new CrmController instance and define the log identifier (blank will create a new one)
27+
*
28+
* @param string|null $logIdentifier
29+
* @return void
30+
* @throws BindingResolutionException
31+
* @throws NotFoundExceptionInterface
32+
* @throws ContainerExceptionInterface
33+
*/
34+
public function __construct(?string $logIdentifier = null)
35+
{
36+
// parent constructor
37+
parent::__construct($logIdentifier);
38+
39+
// ...
40+
}
41+
}

0 commit comments

Comments
 (0)