2016-08-16 3 views
0

(Ich bin neu in CakePHP 3 so sanft sein :-))CakePHP 3 custome Registrierung und Hashing Fehler

ich mit Auth spielen und die Beispiele bauen. Jetzt habe ich zwei Probleme.

Mein erstes Problem: Das Passwort Hashing funktioniert nicht (Er fügt die Passwörter als Klartext ein). Vielleicht, weil ich die Felder gewechselt habe. Mein zweites Problem: Wie kann ich überprüfen, ob die Mail bereits verwendet wird (in Datenbank vorhanden), bevor er den neuen Datensatz speichert.

Hier ist mein Code

Modell/Tabelle/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',]); 
     }} 

Modell/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' => '[email protected]', '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> 

Antwort

0

Der Mutator auf Ihrem Unternehmen hat die falschen Feldnamen oder Sie haben die falschen Namen des Feldes in der Form. Sie müssen entweder den Formularnamen oder den Mutatornamen korrigieren.

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

ODER

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

für Einzigartigkeit von Feldern Um zu überprüfen, fügen Sie ein buildRules Methode auf den Tisch wie so:

use Cake\ORM\RulesChecker; 

public function buildRules(RulesChecker $rules) 
{ 
    $rules->add($rules->isUnique(['log_mail'])); 

}