2FAuth/app/Models/Traits/CanEncryptField.php
2024-10-27 08:16:48 +01:00

36 lines
838 B
PHP

<?php
namespace App\Models\Traits;
use App\Facades\Settings;
use Illuminate\Support\Facades\Crypt;
trait CanEncryptField
{
/**
* Returns an acceptable value
*/
private function decryptOrReturn(mixed $value) : mixed
{
// Decipher when needed
if (Settings::get('useEncryption') && $value) {
try {
return Crypt::decryptString($value);
} catch (\Exception $ex) {
return __('errors.indecipherable');
}
} else {
return $value;
}
}
/**
* Encrypt a value
*/
private function encryptOrReturn(mixed $value) : mixed
{
// should be replaced by laravel 8 attribute encryption casting
return Settings::get('useEncryption') ? Crypt::encryptString($value) : $value;
}
}