1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Omatech\Enigma\Database\Eloquent; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Query\Builder as QueryBuilder; |
6
|
|
|
use Omatech\Enigma\Database\Eloquent\Builder as EloquentBuilder; |
7
|
|
|
use Omatech\Enigma\Enigma; |
8
|
|
|
use Omatech\Enigma\Exceptions\RepeatedAttributesException; |
9
|
|
|
use Omatech\Enigma\Jobs\IndexHydrate; |
10
|
|
|
use ParagonIE\CipherSweet\Exception\BlindIndexNotFoundException; |
11
|
|
|
use ParagonIE\CipherSweet\Exception\CryptoOperationException; |
12
|
|
|
use SodiumException; |
13
|
|
|
use Throwable; |
14
|
|
|
|
15
|
|
|
trait HasEnigma |
16
|
|
|
{ |
17
|
|
|
private $engine; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Get the table associated with the model. |
21
|
|
|
* |
22
|
|
|
* @return string |
23
|
|
|
*/ |
24
|
|
|
abstract public function getTable(); |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Create a new Eloquent model instance. |
28
|
|
|
* |
29
|
|
|
* @param array $attributes |
30
|
|
|
* @return void |
31
|
|
|
* @throws Throwable |
32
|
|
|
*/ |
33
|
56 |
|
public function __construct(array $attributes = []) |
34
|
|
|
{ |
35
|
56 |
|
parent::__construct($attributes); |
36
|
|
|
|
37
|
56 |
|
$this->engine = new Enigma; |
38
|
|
|
|
39
|
56 |
|
$repeated = count(array_intersect($this->getLaravelEncryptable(), $this->getEnigmaEncryptable())); |
40
|
|
|
|
41
|
56 |
|
throw_if($repeated, new RepeatedAttributesException('One or more attributes are repeated on both encryptation types.')); |
42
|
52 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return array |
46
|
|
|
*/ |
47
|
56 |
|
public function getLaravelEncryptable(): array |
48
|
|
|
{ |
49
|
56 |
|
return $this->laravelEncryptable ?? []; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return array |
54
|
|
|
*/ |
55
|
56 |
|
public function getEnigmaEncryptable(): array |
56
|
|
|
{ |
57
|
56 |
|
return $this->enigmaEncryptable ?? []; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Boot trait on the model. |
62
|
|
|
* |
63
|
|
|
* @return void |
64
|
|
|
*/ |
65
|
56 |
|
public static function bootHasEnigma(): void |
66
|
|
|
{ |
67
|
|
|
static::saving(static function ($model) { |
68
|
52 |
|
$model->encrypt( |
69
|
52 |
|
$model->getDirty() |
70
|
|
|
); |
71
|
56 |
|
}); |
72
|
|
|
|
73
|
|
|
static::retrieved(static function ($model) { |
74
|
52 |
|
$model->decrypt( |
75
|
52 |
|
$model->attributes |
76
|
|
|
); |
77
|
56 |
|
}); |
78
|
|
|
|
79
|
|
|
static::saved(static function ($model) { |
80
|
52 |
|
if (! empty($model->getEnigmaEncryptable())) { |
81
|
48 |
|
dispatch(new IndexHydrate(get_class($model), $model->id)) |
82
|
48 |
|
->onQueue('enigma'); |
83
|
|
|
} |
84
|
|
|
|
85
|
52 |
|
$model->decrypt( |
86
|
52 |
|
$model->attributes |
87
|
|
|
); |
88
|
56 |
|
}); |
89
|
56 |
|
|
90
|
|
|
static::created(static function ($model) { |
91
|
|
|
if (! empty($model->getEnigmaEncryptable())) { |
92
|
|
|
dispatch(new IndexHydrate(get_class($model), $model->id)) |
93
|
|
|
->onQueue('enigma'); |
94
|
|
|
} |
95
|
|
|
}); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
52 |
|
* @param array $attributes |
100
|
|
|
* @return $this |
101
|
52 |
|
* @throws BlindIndexNotFoundException |
102
|
48 |
|
* @throws CryptoOperationException |
103
|
48 |
|
* @throws SodiumException |
104
|
48 |
|
* @throws \ParagonIE\CipherSweet\Exception\BlindIndexNameCollisionException |
105
|
|
|
*/ |
106
|
48 |
|
public function encrypt(array $attributes = ['*']): self |
107
|
|
|
{ |
108
|
|
|
foreach ($this->getEnigmaEncryptable() as $column) { |
109
|
|
|
if (isset($attributes[$column])) { |
110
|
|
|
$this->{$column} = $this->engine->encrypt( |
111
|
52 |
|
$this->getTable(), |
112
|
4 |
|
$column, |
113
|
4 |
|
$this->{$column} |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
} |
117
|
52 |
|
|
118
|
|
|
foreach ($this->getLaravelEncryptable() as $column) { |
119
|
|
|
if (isset($attributes[$column])) { |
120
|
|
|
$this->{$column} = encrypt($this->{$column}); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return $this; |
125
|
52 |
|
} |
126
|
|
|
|
127
|
52 |
|
/** |
128
|
48 |
|
* @param string[] $attributes |
129
|
48 |
|
* @return $this |
130
|
48 |
|
* @throws CryptoOperationException |
131
|
|
|
*/ |
132
|
48 |
|
public function decrypt($attributes = ['*']): self |
133
|
|
|
{ |
134
|
|
|
foreach ($this->getEnigmaEncryptable() as $column) { |
135
|
|
|
if (isset($attributes[$column])) { |
136
|
|
|
$this->{$column} = $this->engine->decrypt( |
137
|
52 |
|
$this->getTable(), |
138
|
4 |
|
$column, |
139
|
4 |
|
$this->{$column} |
140
|
|
|
); |
141
|
|
|
} |
142
|
|
|
} |
143
|
52 |
|
|
144
|
|
|
foreach ($this->getLaravelEncryptable() as $column) { |
145
|
|
|
if (isset($attributes[$column])) { |
146
|
|
|
$this->{$column} = decrypt($this->{$column}); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return $this; |
151
|
|
|
} |
152
|
52 |
|
|
153
|
|
|
/** |
154
|
52 |
|
* Create a new Eloquent query builder for the model. |
155
|
|
|
* |
156
|
|
|
* @param QueryBuilder $query |
157
|
|
|
* @return EloquentBuilder |
158
|
|
|
*/ |
159
|
|
|
public function newEloquentBuilder($query) |
160
|
|
|
{ |
161
|
|
|
return new EloquentBuilder($query); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|