首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CakePHP 3密码重置

CakePHP 3密码重置
EN

Stack Overflow用户
提问于 2016-06-26 15:46:37
回答 1查看 5.1K关注 0票数 0

我有以下的代码来重置我的密码通过一个链接通过电子邮件(这很好)。

在我的用户控制器中:

代码语言:javascript
复制
 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文件:

重新设置你的密码?

代码语言:javascript
复制
<?= $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() ?>

关于比较这两个密码,我还有以下规则:

代码语言:javascript
复制
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”

EN

回答 1

Stack Overflow用户

发布于 2016-06-27 08:57:49

前言

在这个问题中有很多奇怪的东西,所以在继续之前,有些需要阅读:

  • 博客教程
  • 检索数据
  • 博客教程-八月
  • 身份验证

这些部分提供了足够的信息,以便具有逻辑上可以工作的代码结构。之所以提到这一点,是因为问题中有以下代码:

  • TableRegistry::get('ControllerName')使用-没有必要
  • Table->findByToken('literal-string') -将始终返回相同的东西(没有)
  • $table->find($notAString); -与查找方法的api不匹配。

更新字段只是编辑

重要的是要认识到,从根本上说,重置用户的密码只是编辑操作。下面是编辑操作摘自博客教程的示例

代码语言:javascript
复制
// 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);
}

有以下步骤:

  • 查找文章实体
  • 更新文章实体
  • 保存文章实体
  • 一些错误处理

这些步骤与问题场景中所需的步骤相同。根据以下问题对上述代码进行调整:

代码语言:javascript
复制
// 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表中而不是散列中,并且登录将无法工作。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38040250

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档