doutuan8887 2016-08-16 18:05
浏览 48
已采纳

cakePHP 3自定义注册和散列错误

(I'm new to cakePHP 3 so be gentle :-) )

I play with Auth and build the Examples. Now I have two problems.

My first problem: The password hashing don't work (He insert the passwords as plain text). Maybe because I changed the fields. My second problem: How can I check if the mail is already in use (exists in Database) before he save the new record.

Here is my Code

Model/Table/UsersTable.php

namespace App\Model\Table;

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

class UsersTable extends Table{

 public function initialize(array $config)
{
    parent::initialize($config);
    $this->table('fc_admin_login');
}
public function validationDefault(Validator $validator)
{
return $validator
 ->notEmpty('log_mail', 'Geben Sie eine gültige E-Mail ein')
 ->notEmpty('log_pw', 'Geben Sie ein Passwort ein')
 ->add('repeat_password', 'no-misspelling', [
     'rule' => ['compareWith', 'admin_login_password_hash'],
                 'message' => 'Passwords are not equal',]);
         }}

Model/Entity/User.php

namespace App\Model\Entity;

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

class User extends Entity
{

protected $_accessible = [
  '*' => true,
  'id' => false
];

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

Controller/UsersController.php

public function add()
{     
    $user = $this->Users->newEntity();
    if ($this->request->is('post'))
    {
        $user = $this->Users->patchEntity($user, $this->request->data);
        if ($this->Users->save($user)) 
        {
            $this->Flash->success(__('Inserted'));
            return $this->redirect(['action' => 'add']);
        }
        $this->Flash->error(__('Error'));
    }
    $this->set('user', $user);
}

Template/Users/add.ctp

<div class="panel-body">
                 <?= $this->Form->create($user) ?>
                                <fieldset>
                                    <div class="form-group">
                                        <?= $this->Form->label('log_mail', 'Mail'); ?>
                                        <?= $this->Form->input('log_mail', array('type' => 'email', 'label' => false, 'placeholder' => 'name@provider.com', 'class' => 'form-control')); ?>
                                    </div>
                                    <div class="form-group">
                                        <?= $this->Form->label('log_pw', 'Ihr Passwort'); ?>
                                        <?= $this->Form->input('log_pw', array('type' => 'password', 'label' => false, 'placeholder' => '********', 'class' => 'form-control')); ?>
                                    </div>
                                    <div class="form-group">
                                        <?= $this->Form->label('repeat_password', 'Repeat pw'); ?>
                                        <?= $this->Form->input('repeat_password', array('type' => 'password', 'label' => false, 'placeholder' => '********', 'class' => 'form-control')); ?>
                                    </div>
                                </fieldset>
                                <hr/>
                                <?= $this->Form->button('Submit', ['type' => 'submit', 'class' => 'btn btn-info btn-block']); ?>
                                <?= $this->Form->end() ?>
            </div>
  • 写回答

1条回答 默认 最新

  • doutongya8378 2016-08-17 01:14
    关注

    The mutator on your entity has the wrong field name or you have the wrong name of field in your form. You either need to correct the form name or the mutator name.

    <div class="form-group">
        <?= $this->Form->label('password', 'Ihr Passwort'); ?>
        <?= $this->Form->input('password', array('type' => 'password', 'label' => false, 'placeholder' => '********', 'class' => 'form-control')); ?>
    </div>
    

    OR

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

    To check for uniqueness of fields add a buildRules method to your table like so:

    use Cake\ORM\RulesChecker;
    
    public function buildRules(RulesChecker $rules)
    {
        $rules->add($rules->isUnique(['log_mail']));
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?