用户添加到数据库但无法登录cakePHP2.6.0
User in added to the database but there is no way to login cakePHP2.6.0
有没有人能给我一个提示,为什么我登录不起作用?
可以添加用户,也可以创建 Blowfish 密码,并将其中的数据集保存到数据库中,但我无法登录。
型号
App::uses('AppModel', 'Model');
App::uses('BlowfishPasswordHasher', 'Controller/Component/Auth');
class Benutzer extends AppModel{
public $useTable = 'Benutzer';
public $validate = array(
'username' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'benötigtes Feld'
)
),
'passwort' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'benötigtes Feld'
)
),
'rolle' => array(
'valid' => array(
'rule' => array('inList', array('admin', 'author', 'borrow', 'archive')),
'message' => 'Please enter a valid role',
'allowEmpty' => false)
)
);
public function beforeSave($options = array()) {
if (isset($this->data[$this->alias]['passwort'])) {
$passwordHasher = new BlowfishPasswordHasher();
$this->data[$this->alias]['passwort'] = $passwordHasher->hash($this->data[$this->alias]['passwort']);
}
return true;
}
应用程序控制器
public $components = array(
'DebugKit.Toolbar',
'Session',
'Paginator' => array(
'className' => 'Bancha.BanchaPaginator'),
'Auth' => array(
'loginRedirect' => array(
'controller' => 'posts',
'action' => 'index'
),
'logoutRedirect' => array(
'controller' => 'pages',
'action' => 'display',
'home'
),
'authenticate' => array(
'Form' => array(
'passwordHasher' => 'Blowfish'
)
),
)
);
public function beforeFilter() {
$this->Auth->allow('index', 'view');
}
登录
public function login() {
if ($this->request->is('post')) {
if ($this->Auth->login()) {
return $this->redirect($this->Auth->redirectUrl());
}
$this->Session->setFlash(__('Invalid username or password, try again'));
}
}
登录 ctp-文件
<div class="users form">
<?php echo $this->Session->flash('auth'); ?>
<?php echo $this->Form->create('Benutzer'); ?>
<fieldset>
<legend>
<?php echo __('Please enter your username and password'); ?>
</legend>
<?php echo $this->Form->input('username', array('lable' => 'Username', 'type' => 'text'));?>
<?php echo $this->Form->input('passwort', array('lable' => 'Passwort', 'type' => 'password'));?>
</fieldset>
<?php echo $this->Form->end(__('Login')); ?>
</div>
问题是,我还没有定义 'fields'
'authenticate' => array(
'Form' => array(
'fields' => array(
'username' => 'username', //Default is 'username' in the userModel
'password' => 'passwort' //Default is 'password' in the userModel
)
有没有人能给我一个提示,为什么我登录不起作用?
可以添加用户,也可以创建 Blowfish 密码,并将其中的数据集保存到数据库中,但我无法登录。
型号
App::uses('AppModel', 'Model');
App::uses('BlowfishPasswordHasher', 'Controller/Component/Auth');
class Benutzer extends AppModel{
public $useTable = 'Benutzer';
public $validate = array(
'username' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'benötigtes Feld'
)
),
'passwort' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'benötigtes Feld'
)
),
'rolle' => array(
'valid' => array(
'rule' => array('inList', array('admin', 'author', 'borrow', 'archive')),
'message' => 'Please enter a valid role',
'allowEmpty' => false)
)
);
public function beforeSave($options = array()) {
if (isset($this->data[$this->alias]['passwort'])) {
$passwordHasher = new BlowfishPasswordHasher();
$this->data[$this->alias]['passwort'] = $passwordHasher->hash($this->data[$this->alias]['passwort']);
}
return true;
}
应用程序控制器
public $components = array(
'DebugKit.Toolbar',
'Session',
'Paginator' => array(
'className' => 'Bancha.BanchaPaginator'),
'Auth' => array(
'loginRedirect' => array(
'controller' => 'posts',
'action' => 'index'
),
'logoutRedirect' => array(
'controller' => 'pages',
'action' => 'display',
'home'
),
'authenticate' => array(
'Form' => array(
'passwordHasher' => 'Blowfish'
)
),
)
);
public function beforeFilter() {
$this->Auth->allow('index', 'view');
}
登录
public function login() {
if ($this->request->is('post')) {
if ($this->Auth->login()) {
return $this->redirect($this->Auth->redirectUrl());
}
$this->Session->setFlash(__('Invalid username or password, try again'));
}
}
登录 ctp-文件
<div class="users form">
<?php echo $this->Session->flash('auth'); ?>
<?php echo $this->Form->create('Benutzer'); ?>
<fieldset>
<legend>
<?php echo __('Please enter your username and password'); ?>
</legend>
<?php echo $this->Form->input('username', array('lable' => 'Username', 'type' => 'text'));?>
<?php echo $this->Form->input('passwort', array('lable' => 'Passwort', 'type' => 'password'));?>
</fieldset>
<?php echo $this->Form->end(__('Login')); ?>
</div>
问题是,我还没有定义 'fields'
'authenticate' => array(
'Form' => array(
'fields' => array(
'username' => 'username', //Default is 'username' in the userModel
'password' => 'passwort' //Default is 'password' in the userModel
)