Eloquent自帶了一個轉(zhuǎn)換器encrypted
class UserIdentity extends Model
{
protected $table = 'users_identity';
protected function casts(): array
{
return [
'real_name' => 'encrypted',
'id_card_number' => 'encrypted',
];
}
}
可以用于數(shù)據(jù)入出庫自動加解密,身份證、手機號等隱私信息在laravel中我都這樣儲存,即使被脫褲也能很好保證用戶信息安全。
在webman中我沒有只能在app\functions.php文件中這樣做
use support\Model;
use Illuminate\Encryption\Encrypter;
// 生成Encrypter給Model
$key = base64_decode('AZ1qOTeasS9gvas8gCVDRFmhZ1ngx+x6qXR1QSyUZ4Y=');
$enc = new Encrypter($key, 'aes-256-cbc');
Model::encryptUsing($enc);
寫在functions.php中是因為這玩意只需要注入baseModel一次,webman的生命周期,我實在找不到第二個合適的位置
希望@walkor大哥評估下這玩意有沒有必要嵌入webman/database,順便把key和cipher配置常態(tài)化,官方寫入config中。
另外加解密是很重要常用的東西,除了model也會有其他加密需求,而且一個項目使用大多數(shù)情況同一個key足以,illuminate/encryption也完全滿足大多數(shù)需求,沒必要讓webman用戶每次調(diào)用加解密都手動調(diào)用一次該類或自己手動實現(xiàn)一個加解密,Yii和laravel都是內(nèi)置了該功能,請@walkor大哥評估下有沒有必要把這東西做成webman基礎(chǔ)的組件,從config自動讀取配置,每次直接調(diào)用單例即可
字段值加密非常常用,我現(xiàn)在的實現(xiàn)方式是:
// 加密字段
protected $encryptable = [
'email',
'phone',
'company',
'title',
'content',
];
// 修改器:在保存數(shù)據(jù)時加密
public function setAttribute($key, $value)
{
if (in_array($key, $this->encryptable)) {
$value = Crypt::encrypt($value);
}
return parent::setAttribute($key, $value);
}
// 訪問器:在獲取數(shù)據(jù)時解密
public function getAttribute($key)
{
$value = parent::getAttribute($key);
if (in_array($key, $this->encryptable) && !is_null($value)) {
$value = Crypt::decrypt($value);
}
return $value;
}
感覺不太優(yōu)雅,期望有一個更好的方案
webman支持兩種ORM,THink和Eloquent, 如果用的EloquentORM的自行composer require就可以了,直接使用
THinkORM大概要自行適配