我有一个表名admin_users,我试图在这里使用auth组件。所以我用appcontroller编写了下面的代码
public $components = array('Session','RequestHandler','Paginator'=>array('limit'=>4),'Auth'=>array(
'loginAction'=>array(
'controller'=>'adminusers',
'action'=>'login'
),
'loginRedirect' => array('controller' => 'adminusers','action' => 'index'),
'logoutRedirect' => array('controller' => 'adminusers','action' => 'index'),
'authError'=>'You can not access this page!!',
));
对于我写过的用户
public function beforeFilter() {
//$this->Auth->allow();
$this->set('logged_in', $this->Auth->loggedIn());
$this->set('current_user',$this->Auth->user());
parent::beforeFilter();
$this->Paginator->settings = array(
'limit'=>4
);
}
我在adminusers controller
中调用了login()方法 public function login() {
$this->layout = 'login';
if ($this->request->is('post')) {
if ($this->Auth->login()) {
return $this->redirect($this->Auth->redirect());
}
$this->Session->setFlash(__('Invalid username or password, try again'));
}
}
对于我在AdminUser模型中编写的哈希密码
public function beforeSave($options = array()) {
if (isset($this->data[$this->alias]['password'])) {
$passwordHasher = new SimplePasswordHasher();
$this->data[$this->alias]['password'] = $passwordHasher->hash(
$this->data[$this->alias]['password']
);
}
return true;
}
这是login.ctp
<?php echo $this->Form->create('AdminUser'); ?>
<?php
echo $this->Form->input('username',array(
'label' => false,
'placeholder'=>'UserName'
));
echo $this->Form->input('password',array(
'label' => false,
'placeholder'=>'Password'
));
?>
使用allow()mathod后,我创建了一个用户,这里哈希密码工作正常。但问题是当我尝试登录时,它给了我“无效的用户名或密码,再试一次”。
答案 0 :(得分:1)
在login.ctp中创建一个AdminUser。 Auth使用模型“用户”作为默认值。 您需要在Auth-Component中定义自己的模型。
public $components = array('Session','RequestHandler','Paginator'=>array('limit'=>4),'Auth'=>array(
'loginAction'=>array(
'controller'=>'adminusers',
'action'=>'login'
),
'loginRedirect' => array('controller' => 'adminusers','action' => 'index'),
'logoutRedirect' => array('controller' => 'adminusers','action' => 'index'),
'authError'=>'You can not access this page!!',
'authenticate' => array(
'Form' => array(
'userModel' => 'AdminUser',
'passwordHasher' => 'Blowfish'
)
)
));