我正在编辑某人在CakePHP中制作的网站,但我之前没有Cake的经验。我正在阅读手册,但发现它很难理解,所以我想在这里发一个问题,看看能否得到任何快速答案。
我认为此代码用于显示登录框,您只能使用用户名test和密码123123登录
var $components = array("Auth", "Acl");
function beforeFilter(){
$this->Security->loginOptions = array(
'type' => 'basic',
'realm' => 'Authenticate Emergency Response Center'
);
$this->Security->loginCredentials = array(
'test' => '123123'
);
$this->Security->requireLogin();
$this->_bindToSite();
parent::beforeFilter();
}
我希望登录框仍然显示,但我想使用数据库中的信息自动填充loginCredentials数组。我有一个名为'运算符'的表,其中包含字段'user_id'和'password'。
有人可以告诉我如何更改上面的代码以允许存储在运算符表中的任何用户名/密码登录吗?
感谢您的帮助
答案 0 :(得分:1)
从数据库构建数组并将$this->Security->loginCredentials
设置为等于您的数组。
答案 1 :(得分:1)
您可以将大部分辛苦工作留给CakePHP的Auth组件,但如果您想使用默认“用户”以外的模型,则只需在beforeFilter()
中进行设置即可。执行此操作的最佳位置可能是app_controller.php
目录中的app\
。
function beforeFilter(){
$this->Auth->userModel = 'Operator';
$this->Auth->fields = array(
'username' => 'user_id',
'password' => 'password'
);
);