|
1
|
|
|
<?php |
|
2
|
|
|
namespace Magros\Encryptable; |
|
3
|
|
|
|
|
4
|
|
|
use Illuminate\Support\Facades\DB; |
|
5
|
|
|
use Illuminate\Support\Facades\Validator; |
|
6
|
|
|
use Illuminate\Support\ServiceProvider; |
|
7
|
|
|
|
|
8
|
|
|
class EncryptServiceProvider extends ServiceProvider |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* Bootstrap the application services. |
|
12
|
|
|
* |
|
13
|
|
|
* This method is called after all other service providers have |
|
14
|
|
|
* been registered, meaning you have access to all other services |
|
15
|
|
|
* that have been registered by the framework. |
|
16
|
|
|
* |
|
17
|
|
|
* @return void |
|
18
|
|
|
*/ |
|
19
|
|
|
public function boot() |
|
20
|
|
|
{ |
|
21
|
|
|
if ($this->app->runningInConsole()) { |
|
22
|
|
|
$this->commands([ |
|
23
|
|
|
EncryptModel::class, |
|
24
|
|
|
DecryptModel::class |
|
25
|
|
|
]); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
$this->publishes([ |
|
29
|
|
|
__DIR__ . '/config/encrypt.php' => config_path('encrypt.php'), |
|
30
|
|
|
], 'config'); |
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
$this->bootValidators(); |
|
34
|
|
|
|
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Register the application services. |
|
39
|
|
|
* |
|
40
|
|
|
* @return void |
|
41
|
|
|
*/ |
|
42
|
|
|
public function register() |
|
43
|
|
|
{ |
|
44
|
|
|
$this->mergeConfigFrom(__DIR__ . '/config/encrypt.php', 'encrypt'); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
private function bootValidators() |
|
49
|
|
|
{ |
|
50
|
|
|
$encrypter = new Encrypter(); |
|
51
|
|
|
|
|
52
|
|
|
$countRecords = function ($table, $column, $value) use ($encrypter) { |
|
53
|
|
|
$value = $encrypter->encrypt(strtolower($value)); |
|
54
|
|
|
return DB::table($table)->where($column, $value)->count(); |
|
55
|
|
|
}; |
|
56
|
|
|
|
|
57
|
|
|
Validator::extend('exists_encrypted', function ($attribute, $value, $parameters, $validator) use($countRecords) { |
|
|
|
|
|
|
58
|
|
|
$table = $parameters[0]; |
|
59
|
|
|
$column = $parameters[1]; |
|
60
|
|
|
|
|
61
|
|
|
return $countRecords($table,$column,$value) > 0; |
|
62
|
|
|
}); |
|
63
|
|
|
|
|
64
|
|
|
Validator::extend('unique_encrypted', function ($attribute, $value, $parameters, $validator) use($countRecords) { |
|
|
|
|
|
|
65
|
|
|
$table = $parameters[0]; |
|
66
|
|
|
$column = $parameters[1]; |
|
67
|
|
|
return $countRecords($table,$column,$value) == 0; |
|
68
|
|
|
}); |
|
69
|
|
|
} |
|
70
|
|
|
} |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.