我有以下的代码来重置我的密码通过一个链接通过电子邮件(这很好)。
在我的用户控制器中:
public function resetpw($token = null) {
$resetpw = $this->Users->findByToken('id');
if ($this->request->is('post')) {
$pw = $this->request->data['password'];
$pwtable = TableRegistry::get('Users');
$newpw = $pwtable->find($resetpw);
$newpw->password = $pw;
if ($pwtable->save($newpw)) {
$this->Flash->success(__('Your password has been successfully updated.'));
return $this->redirect(['action' => 'login']);
}
else {
$this->Flash->error(__('Your password could not be saved. Please, try again.'));
}
}
}重置密码CTP文件:
重新设置你的密码?
<?= $this->Flash->render(); ?>
<?= $this->Form->create() ?>
<fieldset>
<legend><?= __('Please enter your new password.') ?></legend>
<?= $this->Form->input('password', ['label' => 'New Password']) ?>
<?= $this->Form->input('confirmpassword', ['type' => 'password', 'label' => 'Confirm New Password']) ?>
</fieldset>
<?= $this->Form->button(__('Update password')); ?>
<?= $this->Form->end() ?>关于比较这两个密码,我还有以下规则:
public function validatePasswords($validator)
{
$validator->add('confirmpassword', 'no-misspelling', [
'rule' => ['compareWith', 'password'],
'message' => 'The passwords are not the same.',
]);
return $validator;
}在为password & comfirmpassword字段输入两个相同的密码后,我会得到以下错误:
未知查找方法“选择Users.id作为
Users__id,Users.username作为Users__username,Users.password作为Users__password,Users.email作为Users__email,Users.role作为Users__role,Users.token作为Users__token从用户的Users.token = :c0”
发布于 2016-06-27 08:57:49
前言
在这个问题中有很多奇怪的东西,所以在继续之前,有些需要阅读:
这些部分提供了足够的信息,以便具有逻辑上可以工作的代码结构。之所以提到这一点,是因为问题中有以下代码:
TableRegistry::get('ControllerName')使用-没有必要Table->findByToken('literal-string') -将始终返回相同的东西(没有)$table->find($notAString); -与查找方法的api不匹配。更新字段只是编辑
重要的是要认识到,从根本上说,重置用户的密码只是编辑操作。下面是编辑操作摘自博客教程的示例
// src/Controller/ArticlesController.php
public function edit($id = null)
{
$article = $this->Articles->get($id);
if ($this->request->is(['post', 'put'])) {
$this->Articles->patchEntity($article, $this->request->data);
if ($this->Articles->save($article)) {
$this->Flash->success(__('Your article has been updated.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('Unable to update your article.'));
}
$this->set('article', $article);
}有以下步骤:
这些步骤与问题场景中所需的步骤相同。根据以下问题对上述代码进行调整:
// src/Controller/UsersController.php
public function edit($token = null)
{
$user = $this->Users->findByToken($token)->first();
if (!$user) {
$this->Flash->success(__('No user with that token found.'));
return $this->redirect('/');
}
if ($this->request->is(['post'])) {
$user->password = $this->request->data['password'];
$user->confirmpassword = $this->request->data['confirmpassword'];
if ($this->Users->save($user)) {
$this->Flash->success(__('Your password has been successfully updated.'));
return $this->redirect(['action' => 'login']);
}
$this->Flash->error(__('Your password could not be saved. Please, try again.'));
}
}破解密码?
问题中没有提到它,但上面的代码要求用户实体为设置密码时散列
在密码被持久化到数据库之前,您负责对密码进行散列,最简单的方法是在用户实体中使用setter函数: 名称空间App\Model\ Entity;使用Cake\Auth\DefaultPasswordHasher;使用Cake\ORM\Entity;类用户扩展实体{ // .受保护函数_setPassword($password) { if (strlen($password) > 0) {返回(新DefaultPasswordHasher)->hash($password);} // .}
否则,纯文本密码将保存在users表中而不是散列中,并且登录将无法工作。
https://stackoverflow.com/questions/38040250
复制相似问题