2FAuth/app/Models/Traits/CanEncryptField.php

36 lines
838 B
PHP
Raw Normal View History

2024-10-18 14:28:45 +02:00
<?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;
}
}