我可以添加用戶,並且他們出現在數據庫中,但是當我將他們的用戶名和密碼輸入登錄表單時,我會收到「用戶名無效和密碼,再試一次。「我已經設置了它,以便在嘗試編輯或向頁面添加帖子時以及在單擊「登錄」鏈接時要求登錄詳細信息。CakePHP - 可以添加用戶,但用戶無法登錄()
用戶模型是user.php的:
class User extends AppModel {
public $validate = array(
'username' => array(
'required' => array(
'rule' => 'notBlank',
'message' => 'A username is required'
)
),
'password' => array(
'required' => array(
'rule' => 'notBlank',
'message' => 'A password is required'
)
),
'role' => array(
'valid' => array(
'rule' => array('inList', array('admin', 'author')),
'message' => 'Please enter a valid role',
'allowEmpty' => false
)
)
);
public function beforeSave($options = array()) {
if (isset($this -> data[$this -> alias]['password'])) {
$passwordHasher = new BlowfishPasswordHasher();
$this -> data[$this -> alias]['password'] = $passwordHasher -> hash(
$this -> data[$this -> alias]['password']
);
}
return true;
}
}
UsersController.php:
App::uses('AppController', 'Controller');
class UsersController extends AppController {
public function beforeFilter() {
parent::beforeFilter();
$this -> Auth -> allow('add', 'logout');
}
public function login() {
if ($this -> request -> is('post')) {
if ($this -> Auth -> login()) {
return $this -> redirect($this -> Auth -> redirectUrl());
}
$this -> Flash -> error(__('Invalid username or password, try again'));
}
}
public function logout() {
return $this -> redirect($this -> Auth -> logout());
}
public function index() {
$this -> User -> recursive = 0;
$this -> set('users', $this -> paginate());
}
public function view($id = null) {
$this -> User -> id = $id;
if (!$this -> User -> exists()) {
throw new NotFoundException(__('Invalid user'));
}
$this -> set('user', $this -> User -> findById($id));
}
public function add() {
if ($this -> request -> is('post')) {
$this -> User -> create();
if ($this -> User -> save($this -> request -> data)) {
$this -> Flash -> success(__('The user has been saved'));
return $this -> redirect(array('action' => 'index'));
}
$this -> Flash -> error(
__('The user could not be saved. Please, try again.')
);
}
}
public function edit($id = null) {
$this -> User -> id = $id;
if (!$this -> User -> exists()) {
throw new NotFoundException(__('Invalid user'));
}
if ($this -> request -> is('post') || $this -> request -> is('put')) {
if ($this -> User -> save($this -> request -> data)) {
$this -> Flash -> success(__('The user has been saved'));
return $this -> redirect(array('action' => 'index'));
}
$this -> Flash -> error(
__('This user could not be saved. Please, try again.')
);
} else {
$this -> request -> data = $this -> User - findById($id);
unset($this -> request -> data['User']['password']);
}
}
public function delete($id = null) {
$this -> request -> allowMethod('post');
$this -> User -> id = $id;
if (!$this -> User -> exists()) {
throw new NotFoundException(__('Invalid user'));
}
if ($this -> User -> delete()) {
$this -> Flash -> success(__('User deleted'));
return $this -> redirect(array('action' => 'index'));
}
$this -> Flash -> error(__('User was not deleted.'));
return $this -> redirect(array('action' => 'index'));
}
AppController.php:
class AppController extends Controller {
public $components = array(
'DebugKit.Toolbar',
'Flash',
'Auth' => array(
'loginRedirect' => array(
'controller' => 'posts',
'action' => 'index'
),
'logoutRedirect' => array(
'controller' => 'pages',
'action' => 'display',
'home'
),
'authenticate' => array(
'Form' => array(
'passwordHasher' => 'Blowfish'
)
),
'authorize' => array('Controller')
)
);
public function isAuthorized($user) {
if (isset($user['role']) && $user['role'] === 'admin') {
return true;
}
return false;
}
public function beforeFilter() {
$this -> Auth -> allow('index', 'view');
}
}
login.ctp:
<div class="users form">
<?php echo $this -> Flash -> render('auth'); ?>
<?php echo $this -> Form -> create('user'); ?>
<fieldset>
<legend>
<?php echo __('Please enter you username and password'); ?>
</legend>
<?php
echo $this -> Form -> input('username');
echo $this -> Form -> input ('password');
?>
</fieldset>
<?php echo $this -> Form -> end(__('Login')); ?>
</div>
密碼似乎散列好。我環顧了一些類似的問題,但似乎沒有解決我的問題。版本是CakePHP 2.8.5。 我無法在編碼中發現錯誤。爲什麼它總是給我「無效的用戶名和密碼,再試一次。」?
取而代之的是在數據庫中足夠長的時間來採取哈希密碼字段密碼?如果沒有,它會截斷密碼,並且永遠不會正確驗證 –
嘗試檢查Cake用來檢索用戶的查詢。 – drmonkeyninja
@ mcgowan.b數據庫中的密碼被賦予了VARCHAR(255),所以這應該不成問題。 –