【发布时间】:2015-08-27 22:07:34
【问题描述】:
我希望在添加/编辑某些表单字段时对其进行加密,并在通过 cake 查找它们时对其进行解密。 这是在 v2.7.2 中适用于我的代码:
core.php
Configure::write('Security.key','secretkey');
app/model/patient.php.
public $encryptedFields = array('patient_surname', 'patient_first_name');
public function beforeSave($options = array()) {
foreach($this->encryptedFields as $fieldName){
if(!empty($this->data[$this->alias][$fieldName])){
$this->data[$this->alias][$fieldName] = Security::encrypt(
$this->data[$this->alias][$fieldName],
Configure::read('Security.key')
);
}
}
return true;
}
public function afterFind($results, $primary = false) {
foreach ($results as $key => $val) {
foreach($this->encryptedFields as $fieldName) {
if (@is_array($results[$key][$this->alias])) {
$results[$key][$this->alias][$fieldName] = Security::decrypt(
$results[$key][$this->alias][$fieldName],
Configure::read('Security.key')
);
}
}
}
return $results;
}
据我了解,我必须将 $this->data[] 替换为模型的生成实体,并将 afterFind 方法替换为虚拟字段,但我无法将它们全部放在一起。
【问题讨论】:
标签: php cakephp cakephp-3.0