更新数据库加密相关操作
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
<?php
|
||||
namespace common\core;
|
||||
|
||||
use common\services\DataEncryptor;
|
||||
use Yii;
|
||||
use yii\base\Exception;
|
||||
use yii\db\ActiveRecord;
|
||||
|
||||
@@ -15,4 +17,37 @@ class BaseActiveRecord extends ActiveRecord
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增和编辑数据的时候
|
||||
* @param $insert
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function beforeSave($insert)
|
||||
{
|
||||
if (parent::beforeSave($insert)) {
|
||||
// 加密ID卡信息之前检查是否已经加密过
|
||||
if (!empty($this->idcard) && !is_encrypted($this->idcard)) {
|
||||
$this->idcard = (new DataEncryptor(\Yii::$app->params['ase_key']))->encrypt($this->idcard);
|
||||
}
|
||||
// 加密ID卡信息之前检查是否已经加密过
|
||||
if (!empty($this->id_card) && !is_encrypted($this->id_card)) {
|
||||
$this->id_card = (new DataEncryptor(\Yii::$app->params['ase_key']))->encrypt($this->id_card);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function afterFind()
|
||||
{
|
||||
parent::afterFind();
|
||||
// 解密ID卡信息之前检查是否已经是明文
|
||||
if (!empty($this->id_card) && is_encrypted($this->id_card)) {
|
||||
$this->id_card = (new DataEncryptor(Yii::$app->params['ase_key']))->decrypt($this->id_card);
|
||||
}
|
||||
if (!empty($this->idcard) && is_encrypted($this->idcard)) {
|
||||
$this->idcard = (new DataEncryptor(Yii::$app->params['ase_key']))->decrypt($this->idcard);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user