我已经实施了一个crypt
可以附加到 AR 模型的行为类,以便附加属性将以加密形式存储并以解密字符串形式检索。
class User extends CActiveRecord
{
public function behaviors()
{
return array(
'crypt' => array(
// this assumes that the behavior is in the folder: protected/behaviors/
'class' => 'application.behaviors.CryptBehavior',
// this sets that the attributes to be encrypted/decrypted are encryptedfieldname of the model
'attributes' => array('password'),
'useAESMySql' => true
)
);
}
}
这工作正常。我也在上我的定制课程Myuser
这延伸了User
模型来编写我的自定义函数,这样如果我在我的user
表和重新生成模型,我不会丢失自己的功能。
如果我移动我的behavior
类的函数MyUser
, 该行为没有依附并且没有按预期工作.
class MyUser extends User
{
public function behaviors()
{
return array(
'crypt' => array(
// this assumes that the behavior is in the folder: protected/behaviors/
'class' => 'application.behaviors.CryptBehavior',
// this sets that the attributes to be encrypted/decrypted are encryptedfieldname of the model
'attributes' => array('password'),
'useAESMySql' => true
)
);
}
public function customfn1()
{
//some code goes here...
}
}
任何帮助,将不胜感激。
参考链接:地穴行为 http://www.yiiframework.com/forum/index.php/topic/26435-aes-encryption/page__view__findpost__p__127764
这是工作解决方案。我需要测试所有场景。感谢@bool.dev 的贡献。
class MyUser extends User
{
public static function model($className=__CLASS__)
{
return parent::model($className);
}
public function behaviors()
{
return array(
'crypt' => array(
// this assumes that the behavior is in the folder: protected/behaviors/
'class' => 'application.behaviors.CryptBehavior',
// this sets that the attributes to be encrypted/decrypted are encryptedfieldname of the model
'attributes' => array('password'),
'useAESMySql' => true
)
);
}
public static function getUserByID($id)
{
//validation of $id goes here..
return MyUser::model()->findByPk($id);
}
}
在我的控制器中
$userModel = MyUser::getUserByID(1);
在我看来
$userModel->password; //gives me the decrypted password; for easy understanding, i used password field here....
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)