我的问题是我为用户表设置了不同的型号名称:Breeder。我的登录页面总是说错误的用户名或密码,因为输入的密码没有经过哈希处理。
以下是观点:
<?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');
?>
这是AppController:
class AppController扩展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');
}
}
我的登录方式:
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());
}
}
}
模型中的beforeSave方法:
public function beforeSave($options = array()) {
if (!$this->id) {
$passwordHasher = new SimplePasswordHasher();
$this->data['Breeder']['password'] = $passwordHasher->hash(
$this->data['Breeder']['password']
);
}
return true;
}
我不知道需要添加什么才能使密码被哈希化。欢迎任何帮助。
答案 0 :(得分:0)
由于在没有手动哈希的情况下无法做到这一点,我在Controller登录功能中使用了这段代码:
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());
}
}
}
如果有人有更好的解决方案,请不要犹豫,写下来。
答案 1 :(得分:0)
为什么不使用
更改用户模型array('userModel'=&gt;'Breeder')