我在开发CakePHP2的身份验证系统时遇到问题,其中用户存储在数据库表participants
中(不像通常那样users
)。
它根本不会对参与者进行身份验证。
表participants
具有下一个结构:
CREATE TABLE IF NOT EXISTS `participants` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`confirmed` tinyint(1) NOT NULL DEFAULT '0',
`name` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`password` char(40) COLLATE utf8_unicode_ci DEFAULT NULL,
`token` char(32) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`)
)
文件AppController.php的内容如下:
App::uses('Controller', 'Controller');
class AppController extends Controller {
public $components = array('Cookie', 'Session', 'Auth');
function beforeFilter() {
$this->Auth->userModel = 'Participant';
$this->Auth->fields = array('username' => 'name', 'password' => 'password');
$this->Auth->loginAction = array('controller' => 'participants', 'action' => 'login');
//$this->Auth->logoutRedirect = array('controller' => 'participants', 'action' => 'logout'); // i will use this later
//$this->Auth->loginRedirect = array('controller' => 'participants', 'action' => 'index');
}
}
File ParticipantsController.php的内容如下:
App::uses('AppController', 'Controller');
class ParticipantsController extends AppController {
function beforeFilter() {
parent::beforeFilter();
$this->Auth->allowedActions = array('registration', 'login', 'forgotten', 'recreate', 'confirm');
}
function login() {
if ($this->Auth->login()) {
$this->redirect(array('controller' => 'participants', 'action'=>'view'));
} else {
// it always end-up here
//pr($this->data);
//pr(AuthComponent::password($this->data['Participant']['password']));
//exit;
$this->Session->setFlash( __('Error, please try again.', true) );
}
}
我不知道这里有什么问题,你能帮我解决一下我在这里缺少的东西吗?
答案 0 :(得分:1)
我认为可能是您的字段配置和userModel
$this->Auth->fields
我的工作代码更接近:
$this->Auth->authenticate = array(
'Form' => array('userModel' => 'Participant'
, 'fields' => array('username' => 'name', 'password' => 'password')
)
);
答案 1 :(得分:0)
试试这个:
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow(array('registration', 'login',
'forgotten', 'recreate', 'confirm'));
}
为什么你的功能在控制器中不公开?