Skip to content

Search sort filter sandeep #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,58 @@ Laravel/Lumen database encryption at database side using native AES_DECRYPT and
Automatically encrypt and decrypt fields in your Models.

## Install

### 1. Composer

```bash
composer require danielpardamean/laravel-mysql-encrypt
composer require tapanderasari/laravel-mysql-encrypt
```

### 2. Publish config (optional)

`Laravel`

```bash
php artisan vendor:publish --provider="DanielPardamean\MysqlEncrypt\Providers\LaravelServiceProvider"
php artisan vendor:publish --provider="TapanDerasari\MysqlEncrypt\Providers\LaravelServiceProvider"
```

`Lumen`

```bash
mkdir -p config
cp vendor/danielpardamean/laravel-mysql-encrypt/config/config.php config/mysql-encrypt.php
cp vendor/tapanderasari/laravel-mysql-encrypt/config/config.php config/mysql-encrypt.php
```

### 3. Configure Provider

`Laravel`

- For Laravel 5.5 or later, the service provider is automatically loaded, skip this step.

- For Laravel 5.4 or earlier, add the following to `config/app.php`:

```php
'providers' => array(
DanielPardamean\\MysqlEncrypt\\Providers\\LaravelServiceProvider::class
TapanDerasari\\MysqlEncrypt\\Providers\\LaravelServiceProvider::class
);
```

`Lumen`

- For Lumen, add the following to `bootstrap/app.php`:

```php
$app->register(DanielPardamean\MysqlEncrypt\Providers\LumenServiceProvider::class);
$app->register(TapanDerasari\MysqlEncrypt\Providers\LumenServiceProvider::class);
```

### 4. Set encryption key in `.env` file

```
APP_AESENCRYPT_KEY=yourencryptionkey
```

## Update Models

```php
<?php

Expand All @@ -66,17 +78,21 @@ class User extends Model
```

## Validators

`unique_encrypted`

```
unique_encrypted:<table>,<field(optional)>
```

`exists_encrypted`

```
exists_encrypted:<table>,<field(optional)>
```

## Scopes

Custom Local scopes available:

`whereEncrypted`
Expand All @@ -88,6 +104,7 @@ Custom Local scopes available:
Global scope `DecryptSelectScope` automatically booted in models using `Encryptable` trait.

## Schema columns to support encrypted data

```php
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
Expand All @@ -105,4 +122,6 @@ DB::statement('ALTER TABLE `users` ADD `telephone` VARBINARY(50)');
```

## License
The MIT License (MIT). Please see [License File](https://github.com/danielpardamean/laravel-mysql-encrypt/blob/master/LICENSE) for more information.

The MIT License (MIT). Please
see [License File](https://github.com/danielpardamean/laravel-mysql-encrypt/blob/master/LICENSE) for more information.
26 changes: 13 additions & 13 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "danielpardamean/laravel-mysql-encrypt",
"description": "Laravel 9.x Database encryption mysql side",
"name": "tapanderasari/laravel-mysql-encrypt",
"description": "Laravel Database encryption mysql side",
"keywords": [
"laravel",
"danielpardamean",
"tapanderasari",
"mysql",
"encryption",
"php",
Expand All @@ -12,19 +12,19 @@
"license": "MIT",
"authors": [
{
"name": "Daniel pardamean",
"email": "danielpardamean14@gmail.com"
"name": "Tapan Derasari",
"email": "tapanderasari89@gmail.com"
}
],
"require": {
"php": "^8.0",
"illuminate/database": "^9.0",
"illuminate/support": "^9.0"
"illuminate/database": "^10.0",
"illuminate/support": "^10.0"
},
"autoload": {
"psr-4": {
"DanielPardamean\\MysqlEncrypt\\": "src/",
"DanielPardamean\\MysqlEncrypt\\Tests\\": "tests/"
"TapanDerasari\\MysqlEncrypt\\": "src/",
"TapanDerasari\\MysqlEncrypt\\Tests\\": "tests/"
},
"files": [
"src/helpers.php"
Expand All @@ -39,16 +39,16 @@
"extra": {
"laravel": {
"providers": [
"DanielPardamean\\MysqlEncrypt\\Providers\\LaravelServiceProvider"
"TapanDerasari\\MysqlEncrypt\\Providers\\LaravelServiceProvider"
]
}
},
"minimum-stability": "dev",
"prefer-stable": true,
"require-dev": {
"orchestra/testbench": "^6.28",
"pestphp/pest": "^1.23",
"pestphp/pest-plugin-laravel": "^1.4"
"orchestra/testbench": "^8.0",
"pestphp/pest": "^2.0",
"pestphp/pest-plugin-laravel": "^2.0"
},
"scripts": {
"test": "./vendor/bin/pest"
Expand Down
2 changes: 0 additions & 2 deletions config/config.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

return [

'key' => env('APP_AESENCRYPT_KEY'),

];
8 changes: 4 additions & 4 deletions src/Providers/LaravelServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php

namespace DanielPardamean\MysqlEncrypt\Providers;
namespace TapanDerasari\MysqlEncrypt\Providers;

use DanielPardamean\MysqlEncrypt\Traits\ValidatesEncrypted;
use Illuminate\Support\ServiceProvider;
use TapanDerasari\MysqlEncrypt\Traits\ValidatesEncrypted;

class LaravelServiceProvider extends ServiceProvider
{
Expand All @@ -15,7 +15,7 @@ class LaravelServiceProvider extends ServiceProvider
public function boot()
{
$this->publishes([
__DIR__.'/../../config/config.php' => config_path('mysql-encrypt.php'),
__DIR__ . '/../../config/config.php' => config_path('mysql-encrypt.php'),
], 'config');

$this->addValidators();
Expand All @@ -26,6 +26,6 @@ public function boot()
*/
public function register()
{
$this->mergeConfigFrom(__DIR__.'/../../config/config.php', 'mysql-encrypt');
$this->mergeConfigFrom(__DIR__ . '/../../config/config.php', 'mysql-encrypt');
}
}
6 changes: 3 additions & 3 deletions src/Providers/LumenServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php

namespace DanielPardamean\MysqlEncrypt\Providers;
namespace TapanDerasari\MysqlEncrypt\Providers;

use Illuminate\Support\ServiceProvider;
use DanielPardamean\MysqlEncrypt\Traits\ValidatesEncrypted;
use TapanDerasari\MysqlEncrypt\Traits\ValidatesEncrypted;

class LumenServiceProvider extends ServiceProvider
{
Expand All @@ -16,7 +16,7 @@ public function boot()
{
$this->app->configure('mysql-encrypt');

$path = realpath(__DIR__.'/../../config/config.php');
$path = realpath(__DIR__ . '/../../config/config.php');

$this->mergeConfigFrom($path, 'mysql-encrypt');

Expand Down
8 changes: 4 additions & 4 deletions src/Scopes/DecryptSelectScope.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace DanielPardamean\MysqlEncrypt\Scopes;
namespace TapanDerasari\MysqlEncrypt\Scopes;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
Expand All @@ -12,8 +12,8 @@ class DecryptSelectScope implements Scope
/**
* Apply the scope to a given Eloquent query builder.
*
* @param \Illuminate\Database\Eloquent\Builder $builder
* @param \Illuminate\Database\Eloquent\Model $model
* @param \Illuminate\Database\Eloquent\Builder $builder
* @param \Illuminate\Database\Eloquent\Model $model
* @return void
*/
public function apply(Builder $builder, Model $model)
Expand All @@ -26,7 +26,7 @@ public function apply(Builder $builder, Model $model)
return $builder->addSelect(...$columns);
}

$select = collect($columns)->map(function($column) use ($encryptable) {
$select = collect($columns)->map(function ($column) use ($encryptable) {
return (in_array($column, $encryptable)) ? db_decrypt($column) : $column;
});

Expand Down
8 changes: 4 additions & 4 deletions src/Traits/Encryptable.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

namespace DanielPardamean\MysqlEncrypt\Traits;
namespace TapanDerasari\MysqlEncrypt\Traits;

use DanielPardamean\MysqlEncrypt\Scopes\DecryptSelectScope;
use TapanDerasari\MysqlEncrypt\Scopes\DecryptSelectScope;

trait Encryptable
{
Expand All @@ -16,7 +16,7 @@ public static function bootEncryptable()

/**
* @param string $key
* @param mixed $value
* @param mixed $value
*
* @return mixed
*/
Expand All @@ -37,7 +37,7 @@ public function encryptable(): array
return $this->encryptable ?? [];
}

/**
/**
* where for encrypted columns
*
* @param $query
Expand Down
23 changes: 12 additions & 11 deletions src/Traits/ValidatesEncrypted.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
<?php

namespace DanielPardamean\MysqlEncrypt\Traits;
namespace TapanDerasari\MysqlEncrypt\Traits;

use PDOException;
use InvalidArgumentException;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\Validator;
use InvalidArgumentException;
use PDOException;


trait ValidatesEncrypted
{
/**
/**
* Validators.
*
* @return void
Expand All @@ -26,7 +27,7 @@ public function addValidators()
$field = isset($parameters[1]) ? $parameters[1] : $attribute;
$ignore = isset($parameters[2]) ? $parameters[2] : null;

$items = DB::select("SELECT count(*) as aggregate FROM `".$parameters[0]."` WHERE AES_DECRYPT(`".$field."`, '".config("app.key")."') LIKE '".$value."' COLLATE utf8mb4_general_ci".($ignore ? " AND id != ".$ignore : ''));
$items = DB::select("SELECT count(*) as aggregate FROM `" . $parameters[0] . "` WHERE AES_DECRYPT(`" . $field . "`, '" . config("app.key") . "') LIKE '" . $value . "' COLLATE utf8mb4_general_ci" . ($ignore ? " AND id != " . $ignore : ''));

return $items[0]->aggregate == 0;
});
Expand All @@ -39,7 +40,7 @@ public function addValidators()

$field = isset($parameters[1]) ? $parameters[1] : $attribute;

$items = DB::select("SELECT count(*) as aggregate FROM `".$parameters[0]."` WHERE AES_DECRYPT(`".$field."`, '".config("app.key")."') LIKE '".$value."' COLLATE utf8mb4_general_ci");
$items = DB::select("SELECT count(*) as aggregate FROM `" . $parameters[0] . "` WHERE AES_DECRYPT(`" . $field . "`, '" . config("app.key") . "') LIKE '" . $value . "' COLLATE utf8mb4_general_ci");

return $items[0]->aggregate > 0;
});
Expand All @@ -48,9 +49,9 @@ public function addValidators()
/**
* Require a certain number of parameters to be present.
*
* @param int $count
* @param array $parameters
* @param string $rule
* @param int $count
* @param array $parameters
* @param string $rule
* @return void
*
* @throws \InvalidArgumentException
Expand All @@ -65,14 +66,14 @@ public function requireParameterCount($count, $parameters, $rule)
/**
* The table must exist.
*
* @param string $table
* @param string $table
* @return void
*
* @throws PDOException
*/
public function requireTableExists($table)
{
if (! Schema::hasTable($table)) {
if (!Schema::hasTable($table)) {
throw new PDOException("Table $table not found.");
}
}
Expand Down
11 changes: 6 additions & 5 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/**
* Encrypt value.
*
* @param mixed $value
* @param mixed $value
* @return \Illuminate\Database\Query\Expression
*/
function db_encrypt($value)
Expand All @@ -23,11 +23,12 @@ function db_encrypt($value)
}
}


if (!function_exists('db_decrypt')) {
/**
* Decrpyt value.
*
* @param mixed $column
* @param mixed $column
* @return \Illuminate\Database\Query\Expression
*/
function db_decrypt($column)
Expand All @@ -43,9 +44,9 @@ function db_decrypt($column)
/**
* Decrpyt value.
*
* @param string $column
* @param string $value
* @param string $operator
* @param string $column
* @param string $value
* @param string $operator
* @return string
*/
function db_decrypt_string($column, $value, $operator = 'LIKE')
Expand Down
5 changes: 3 additions & 2 deletions tests/Models/Testing.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?php

namespace DanielPardamean\MysqlEncrypt\Tests\Models;
namespace TapanDerasari\MysqlEncrypt\Tests\Models;

use DanielPardamean\MysqlEncrypt\Traits\Encryptable;
use Illuminate\Database\Eloquent\Model;
use TapanDerasari\MysqlEncrypt\Traits\Encryptable;


class Testing extends Model
{
Expand Down
Loading