dongtuoleng8624 2013-12-15 15:55
浏览 48
已采纳

Cakephp身份验证:2.4.2

My problem is I have a different model name for the user table: Breeder. My login page always says incorrect username or password, because the password entered is not hashed.

Here is the view:

<?php
    echo $this->Form->create();
    echo $this->Form->input('username');
    echo $this->Form->input('password');
    echo $this->Form->end('Log in');
    echo $this->Html->link('Register', '/breeders/register');
?>

Here is the AppController:

class AppController extends Controller {

public $helpers = array('Html', 'Form', 'Session');
public $components = array('Session', 'Paginator',
  'Auth' => array(
    'loginAction' => array('controller' => 'breeders', 'action' => 'login'),
    'loginRedirect' => array('controller' => 'breeders', 'action' => 'desk'),
    'logoutRedirect' => array('controller' => 'breeders', 'action' => 'login'),
    'authorize' => array('Controller'),
    'authenticate' => array(
      'Form' => array(
        'fields' => array(
          'username' => 'login',
          'password' => 'password'),
        'passwordHasher' => array(
          'className' => 'Simple',
          'hashType' => 'sha256'
          )
        )
      )
    )
  );

public function isAuthorized($user)
{
    return true;
}

public function beforeFilter() {

    $this->Auth->allow('login', 'logout', 'register', 'profile');
}
}

My login method:

public function login() {
  $this->set('title_for_layout', __('Connection'));

  if ($this->Session->read('Auth.Breeder')) {
    return $this->redirect('/');
  }

  if ($this->request->is('post')) {
    if ($this->Auth->login()) {
      return $this->redirect($this->Auth->redirectUrl());
    }
  }
}

And the beforeSave method in the model:

public function beforeSave($options = array()) {

  if (!$this->id) {
    $passwordHasher = new SimplePasswordHasher();
    $this->data['Breeder']['password'] = $passwordHasher->hash(
      $this->data['Breeder']['password']
      );
  }
  return true;
}

I don't know what I need to add to make the password be hashed. Any help will be welcome.

  • 写回答

2条回答 默认 最新

  • dtds8802 2013-12-15 18:35
    关注

    As I couldn't do it without manual hashing, I used this code in the Controller login function:

    public function login() {
    
      $this->set('title_for_layout', __('Connection'));
    
      if ($this->Session->read('Auth.Breeder')) {
        return $this->redirect('/');
      }
    
      if ($this->request->is('post')) {
        $passwordHasher = new SimplePasswordHasher();
        $this->request->data['Breeder']['password'] = $passwordHasher->hash(
          $this->request->data['Breeder']['password']
        );
        $breeder = $this->Breeder->find('first', array('conditions' => array('Breeder.login' => $this->request->data['Breeder']['username']), 'fields' => array('id')));
    
        $this->request->data['Breeder'] = array_merge($this->request->data['Breeder'], array('id' => $breeder['Breeder']['id']));
    
        if ($this->Auth->login($this->request->data['Breeder'])) {
          return $this->redirect($this->Auth->redirectUrl());
        }
      }
    }
    

    If anyone has a better solution, please don't hesitate to write it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?