【问题标题】:Cannot Hash Password in Cakephp 3无法在 Cakephp 3 中散列密码
【发布时间】:2017-11-19 11:49:15
【问题描述】:

我按照本教程https://book.cakephp.org/3.0/en/tutorials-and-examples/blog-auth-example/auth.html 创建了一个注册表单,但密码保存在数据库中,没有加密为纯文本。请帮助解决我的问题。 这是我的代码:

添加.ctp

<h1>Register new user </h1>


<div class="users form">
<?= $this->Form->create($user) ?>
    <fieldset>
        <legend><?= __('Add User') ?></legend>
        <?= $this->Form->control('username') ?>
        <?= $this->Form->control('password') ?>

   </fieldset>
<?= $this->Form->button(__('Submit')); ?>
<?= $this->Form->end() ?>
</div>

实体/用户.php

<?php
namespace App\Model\Entity;

use Cake\Auth\DefaultPasswordHasher;
use Cake\ORM\Entity;

class User extends Entity
{

    // Make all fields mass assignable except for primary key field "id".
 // Make all fields mass assignable for now.
protected $_accessible = ['*' => true];

protected function _setPassword($password) {
    return (new DefaultPasswordHasher)->hash($password);
}

    // ...
}

?>

UsersTable.php

<?php
namespace App\Model\Table;

use Cake\ORM\Table;
use Cake\Validation\Validator;
use Cake\Auth\DefaultPasswordHasher;

class UsersTable extends Table
{
   public function validationDefault(Validator $validator)
    {
        return $validator
        ->notEmpty('username', 'A username is required')
        ->notEmpty('password', 'A password is required');

    }


}

?>

UsersController.php

<?php

namespace PanelAdmin\Controller;

use Cake\Controller\Controller;
use Cake\ORM\TableRegistry;
use Cake\Event\Event;

class UsersController extends AppController

{

 public function beforeFilter(Event $event)
    {
        parent::beforeFilter($event);
        $this->Auth->allow('add');
    }

     public function index()
     {
        $this->set('users', $this->Users->find('all'));
    }

    public function view($id)
    {
        $user = $this->Users->get($id);
        $this->set(compact('user'));
    }

    public function add()
    {
        $user = $this->Users->newEntity();
        if ($this->request->is('post')) {
            // Prior to 3.4.0 $this->request->data() was used.
            $user = $this->Users->patchEntity($user, $this->request->getData());
            //debug($user); die;
            if ($this->Users->save($user)) {
                $this->Flash->success(__('The user has been saved.'));
                return $this->redirect(['action' => 'add']);
            }
            $this->Flash->error(__('Unable to add the user.'));
        }
        $this->set('user', $user);
    }

    }

?>

目录结构:

【问题讨论】:

  • 你的代码看起来不错,你确定你已经上传了更新的文件并删除了缓存文件吗?
  • @Ofir Baruch:是的,我确定。我认为我的模型文件没有被调用,因为如果我调试它没有任何区别,什么都不会打印。
  • 检查了这个链接 github.com/cakephp/cakephp/issues/3863 但没有任何效果
  • 你有一个名为 Users 的数据库表,对吧?
  • 是的:是用户。

标签: php cakephp cakephp-3.0


【解决方案1】:

要么你的命名空间有误,要么你把代码放错了地方。如果代码应该存在于 PanelAdmin 插件中,那么您不应为模型/实体类使用 App 命名空间,而应使用 PanelAdmin 命名空间。

【讨论】:

    猜你喜欢
    • 2017-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-18
    • 1970-01-01
    • 2018-12-25
    • 2019-07-17
    • 2013-03-24
    相关资源
    最近更新 更多