【发布时间】: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