【发布时间】:2019-09-12 09:19:05
【问题描述】:
我正在尝试实现auth登录并注册cakephp 3.7.4
我已经为 UsersController 添加方法使用了以下代码
public function add()
{
$this->viewBuilder()->setLayout('login');
$user = $this->Users->newEntity();
if ($this->request->is('post')) {
$post = $this->request->getData();
$post['created'] = date('Y-m-d H:i:s');
$post['modified'] = date('Y-m-d H:i:s');
$user = $this->Users->patchEntity($user, $post);
if ($this->Users->save($user)) {
$this->Flash->success(__('The user has been saved.'));
return $this->redirect(['action' => 'login']);
}
$this->Flash->error(__('Unable to add the user.'));
}
$this->set('user', $user);
}
但它不能以哈希格式保存密码
我也创建了实体并使用了这个功能,但它也没有帮助我
class User extends Entity
{
protected $_accessible = [
'email' => true,
'password' => true
];
protected $_hidden = [
'password'
];
protected function _setPassword($password){
return(new DefaultPasswordHasher)->hash($password);
}
}
【问题讨论】:
-
“无法保存”非常模糊。您收到错误消息吗?它是否保存未散列的密码?它会保存一个空白密码吗?
-
@GregSchmidt 不,它不会产生错误。它将保存我们输入的相同密码
-
debug($user);在newEntity调用之后。是什么类型的?它是预期的User实体还是通用对象? -
为什么要自己添加修改和创建?尝试将 TimestampBehavior 添加到您的用户表。我建议使用调试器,例如 xdebug 并在 _setPassword() 中设置断点。
-
@Seb 这是密码散列的主要原因吗
标签: cakephp cakephp-3.0 cakephp-3.7