Issue
I have created a registration form by following this tutorial https://book.cakephp.org/3.0/en/tutorials-and-examples/blog-auth-example/auth.html, but the password is saving in the database without encryption as plain text. Please help to sort out my issue. Here is my code:
Add.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>
Entity/User.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);
}
}
?>
Solution
Either your namespaces are wrong, or you've put the code in the wrong place. If the code is supposed to live inside the PanelAdmin
plugin, then you shouldn't use the App
namespace for your model/entity classes, but the PanelAdmin
one.
Answered By - ndm
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.