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']));
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化