【发布时间】:2016-06-26 15:46:37
【问题描述】:
我有以下代码通过电子邮件中的链接重置我的密码(效果很好)。
在我的用户控制器中:
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;
}
在密码和确认密码字段输入两个相同的密码后,我收到以下错误:
未知查找方法“SELECT Users.id AS
Users__id, Users.username ASUsers__username,Users.password ASUsers__password,Users.email ASUsers__email, Users.role ASUsers__role, Users.token ASUsers__tokenFROM users Users WHERE Users.token = :c0"
【问题讨论】:
-
我想你的意思是
$this->Users->findByToken($token);我也不知道你为什么要先搜索 $this->Users,然后使用 TableRegister 获取 Users 模型的另一个实例.... -
@AD7six 基本上我试图从用户表中获取令牌数据以确定需要重置哪个用户的密码。然后获取输入的密码,在用户表中搜索用户的密码,然后覆盖它。
-
@JvO 在 CakePHP 3 Cookbook 中,更新数据的方法使用 TableRegistry & get 例如:
$articlesTable = TableRegistry::get('Articles');然后$article = $articlesTable->get(12),其中检索到的数据的 id 为 12。我正在使用在找到 id 时生成的令牌,并且它与数据库中的关联电子邮件有关,我想知道什么是构造它的方法,以便它引用生成的令牌的 id。 -
我知道,这是 Cookbook 样板代码。但是:在控制器 X 中,自动有一个指向模型的 $this->X。所以创建一个新对象只是在浪费内存。至于查找id:想法是按令牌搜索。它返回一个用户对象并从中获取 id:$resetpw->id。就是这样。
标签: php cakephp passwords cakephp-3.0 change-password