Skip to content
This repository was archived by the owner on Mar 2, 2023. It is now read-only.

Commit 7a06a77

Browse files
committed
master - made internal functions private, added scopes in readme
1 parent a58ecc3 commit 7a06a77

File tree

3 files changed

+50
-27
lines changed

3 files changed

+50
-27
lines changed

phpunit.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
</testsuites>
1818
<filter>
1919
<whitelist processUncoveredFilesFromWhitelist="true">
20-
<directory suffix=".php">./src</directory>
20+
<directory suffix=".php">./src/Models</directory>
2121
</whitelist>
2222
</filter>
2323
<logging>

readme.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ You can use Validator on these fields with:
4646
- exists_encrypted
4747

4848
exists_encrypted:<table>,<field(optional)>
49+
50+
You cannot use basic where, orWhere, orderBy on encrypted fields so there are 3 predefined scopes that you can use as a replacer:
51+
52+
- whereEncrypted
53+
- orWhereEncrypted
54+
- orderByEncrypted
4955

5056
Possibility to anonymize data:
5157

@@ -70,3 +76,5 @@ The method accepts a locale parameter, if you want to use faker with localizatio
7076
If is not specified by any method above, the default Faker local will be used by default
7177

7278
Note: Model is not automatically saved!
79+
80+
_Happy coding!_

src/Models/BaseModel.php

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function getAttribute($key)
5050
}
5151

5252
if (in_array($key, $this->encrypted)) {
53-
$value = $this->aesDecrypt($value);
53+
return $this->aesDecrypt($value);
5454
}
5555

5656
return $value;
@@ -65,7 +65,7 @@ public function getAttribute($key)
6565
*
6666
* @return false|string
6767
*/
68-
protected function aesDecrypt($val, $cypher = 'aes-128-ecb', $mySqlKey = true)
68+
private function aesDecrypt($val, $cypher = 'aes-128-ecb', $mySqlKey = true)
6969
{
7070
$secret = getenv('ENCRYPTION_KEY');
7171

@@ -81,13 +81,14 @@ protected function aesDecrypt($val, $cypher = 'aes-128-ecb', $mySqlKey = true)
8181
*
8282
* @return string
8383
*/
84-
protected function generateMysqlAesKey($key)
84+
private function generateMysqlAesKey($key)
8585
{
8686
$generatedKey = str_repeat(chr(0), 16);
8787

8888
for ($i = 0, $len = strlen($key); $i < $len; $i++) {
8989
$generatedKey[$i % 16] = $generatedKey[$i % 16] ^ $key[$i];
9090
}
91+
9192
return $generatedKey;
9293
}
9394

@@ -117,7 +118,7 @@ public function setAttribute($key, $value)
117118
*
118119
* @return false|string
119120
*/
120-
protected function aesEncrypt($val, $cypher = 'aes-128-ecb', $mySqlKey = true)
121+
private function aesEncrypt($val, $cypher = 'aes-128-ecb', $mySqlKey = true)
121122
{
122123
$secret = getenv('ENCRYPTION_KEY');
123124

@@ -181,53 +182,67 @@ public function getAnonymizable()
181182
return $this->anonymizable;
182183
}
183184

185+
/**
186+
* Anonymize model fields
187+
*
188+
* @param string|null $locale
189+
*/
190+
public function anonymize($locale = null)
191+
{
192+
$faker = Factory::create($locale ?? (getenv('FAKER_LOCALE') ?? Factory::DEFAULT_LOCALE));
193+
194+
foreach ($this->anonymizable as $field => $type) {
195+
if (in_array($field, $this->attributes)) {
196+
$method = $type[0];
197+
198+
if (count($type) > 1) {
199+
$this->$field = call_user_func([$faker, $method], array_slice($type, 1));
200+
} else {
201+
$this->$field = $faker->$method;
202+
}
203+
}
204+
}
205+
}
206+
184207
/**
185208
* where for encrypted columns
186209
*
187210
* @param $query
188-
* @param $field
211+
* @param $column
189212
* @param $value
190213
*
191214
* @return mixed
192215
*/
193-
public function scopeWhereEncrypted($query, $field, $value)
216+
public function scopeWhereEncrypted($query, $column, $value)
194217
{
195-
return $query->whereRaw('AES_DECRYPT(' . $field . ', "' . getenv("ENCRYPTION_KEY") . '") LIKE "' . $value . '" COLLATE utf8mb4_general_ci');
218+
return $query->whereRaw('AES_DECRYPT(' . $column . ', "' . getenv("ENCRYPTION_KEY") . '") LIKE "' . $value . '" COLLATE utf8mb4_general_ci');
196219
}
197220

198221
/**
199222
* orWhere for encrypted columns
200223
*
201224
* @param $query
202-
* @param $field
225+
* @param $column
203226
* @param $value
204227
*
205228
* @return mixed
206229
*/
207-
public function scopeOrWhereEncrypted($query, $field, $value)
230+
public function scopeOrWhereEncrypted($query, $column, $value)
208231
{
209-
return $query->orWhereRaw('AES_DECRYPT(' . $field . ', "' . getenv("ENCRYPTION_KEY") . '") LIKE "' . $value . '" COLLATE utf8mb4_general_ci');
232+
return $query->orWhereRaw('AES_DECRYPT(' . $column . ', "' . getenv("ENCRYPTION_KEY") . '") LIKE "' . $value . '" COLLATE utf8mb4_general_ci');
210233
}
211234

212235
/**
213-
* Anonymize model fields
236+
* orderBy for encrypted columns
214237
*
215-
* @param string|null $locale
238+
* @param $query
239+
* @param $column
240+
* @param $direction
241+
*
242+
* @return mixed
216243
*/
217-
public function anonymize($locale = null)
244+
public function scopeOrderByEncrypted($query, $column, $direction)
218245
{
219-
$faker = Factory::create($locale ?? (getenv('FAKER_LOCALE') ?? Factory::DEFAULT_LOCALE));
220-
221-
foreach ($this->anonymizable as $field => $type) {
222-
if (in_array($field, $this->attributes)) {
223-
$method = $type[0];
224-
225-
if (count($type) > 1) {
226-
$this->$field = call_user_func([$faker, $method], array_slice($type, 1));
227-
} else {
228-
$this->$field = $faker->$method;
229-
}
230-
}
231-
}
246+
return $query->orderByRaw('AES_DECRYPT(' . $column . ', "' . getenv("ENCRYPTION_KEY") . '") ' . $direction);
232247
}
233248
}

0 commit comments

Comments
 (0)