添加新用户时隐藏密码 [英] Hashing password when adding new user
本文介绍了添加新用户时隐藏密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
CakePHP 2.4
CakePHP 2.4
添加新用户时,我必须在将密码存储到数据库之前对其进行哈希。为此,我做了:
While adding a new user I must hash passwords before storing it in the Database. To do so I did:
//UsersController
public $helpers = array('Html', 'Form');
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array('controller' => 'users', 'action' => 'home', 'home'),
'logoutRedirect' => array('controller' => 'users', 'action' => 'logout')
)
);
public function add(){
if($this->request->is('post')){
$this->User->create();
if($this->User->save($this->request->data)){
$this->Session->setFlash('Saved!');
}
}
}
//UserModel
public function beforeSave(){
if(isset($this->data[$this->alias]['password']))
$this->data[$this->alias]['password'] = md5($this->data[$this->alias]['password']);
return true;
}
//add.ctp
echo $this->Form->create();
echo $this->Form->input('text', array('label'=>'Username', 'name'=>'username'));
echo $this->Form->input('text', array('label'=>'Full name', 'name'=>'fullname'));
echo $this->Form->input('password', array('label'=>'Password', 'name'=>'password'));
echo $this->Form->input('text', array('label'=>'Password hint', 'name'=>'pass_hint'));
echo $this->Form->input('text', array('label'=>'Email', 'name'=>'email'));
echo $this->Form->input('text', array('label'=>'Contact', 'name'=>'cell'));
echo $this->Form->end('Create account');
但是密码是不经哈希存储的!
But the thing is passwords are being stored without being hashed!
提前感谢...
更新:已将视图代码更改为
echo $this->Form->create('User');
echo $this->Form->input('username', array('label'=>'Username'));
echo $this->Form->input('fullname', array('label'=>'Full name'));
echo $this->Form->input('pwd', array('label'=>'Password', 'type'=>'password'));
echo $this->Form->input('pass_hint', array('label'=>'Password hint'));
echo $this->Form->input('email', array('label'=>'Email'));
echo $this->Form->input('cell', array('label'=>'Contact'));
echo $this->Form->end('Create account');
推荐答案
我不知道为什么,项目与用户表,发现CakePHP 2.4命名模型为 User
而不是 UserModel
。所以,我将从 UserModel
重命名为用户
,并且magically为我工作:)
I don't know why but I just baked a project with user table and found that CakePHP 2.4 is naming the model as User
rather than UserModel
. So, I renamed mine from UserModel
to User
and magically is worked for me :)
这篇关于添加新用户时隐藏密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文