我正在使用 cakephp 2.7.8 来构建管理面板。我的项目包含多个管理员而不是用户,这就是我在数据库而不是用户中拥有admins
表的原因。
使用BlowfishHasher进行密码散列,但在创建新记录(添加新用户)时,密码不是哈希值,只是字符串存储在密码表中。
查询表格:
CREATE TABLE `admins` (
`id` char(36) NOT NULL,
`username` varchar(50) DEFAULT NULL,
`password` varchar(255) DEFAULT NULL,
`gender` varchar(45) DEFAULT NULL,
`created` datetime DEFAULT NULL,
`modified` datetime DEFAULT NULL,
PRIMARY KEY (`id`))
管理员型号:Admin.php
<?php
App::uses('AppModel', 'Model');
App::uses('BlowfishPasswordHasher','Controller/Component/Auth');
/**
* Admin Model
*
*/
class Admin extends AppModel {
/**
* Display field
*
* @var string
*/
public $displayField = 'first_name';
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;
}
}
管理员控制器:AdminsController.php
<?php
App::uses('AppController', 'Controller');
/**
* Admins Controller
*
* @property Admin $Admin
* @property PaginatorComponent $Paginator
* @property FlashComponent $Flash
* @property SessionComponent $Session
*/
class AdminsController extends AppController {
/**
* Components
*
* @var array
*/
public $components = array('Paginator', 'Flash', 'Session');
/**
* index method
*
* @return void
*/
public function index() {
$this->Admin->recursive = 0;
$this->set('admins', $this->Paginator->paginate());
}
/**
* login function
*/
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'));
}
}
/**
* logout function
*/
public function logout(){
return $this->redirect($this->Auth->logout());
}
}
App Controller:AppController.php
<?php
App::uses('Controller', 'Controller');
/**
* @package app.Controller
* @link http://book.cakephp.org/2.0/en/controllers.html#the-app-controller
*/
class AppController extends Controller {
public $components = array(
'Flash',
'Auth' => array(
'loginRedirect'=>array(
'controller'=>'admins',
'action'=>'index'
),
'logoutRedirect'=>array(
'controller'=>'admins',
'action'=>'login'
),
'authenticate'=>array(
'Form'=>array(
'passwordHasher'=>'Blowfish'
)
)
)
);
function beforeFilter() {
$this->Auth->authenticate = array(
AuthComponent::ALL => array(
'userModel' => 'Admin'
)
);
$this->Auth->allow('login','add','index');
}
}
答案 0 :(得分:0)
这一行有错误: -
if(isset($this->data[$this->alias['password']])){
应该是: -
if(isset($this->data[$this->alias]['password'])){