CakePHP 2.1向登录页面添加字段

时间:2012-06-14 03:43:10

标签: php cakephp cakephp-2.1

我已经创建了一个包含三个字段的基本登录表单,它们是“公司”,“员工”和“密码”我尝试使用默认的Auth组件:

public function login() {
    if ($this->request->is('post')) {
        if ($this->Auth->login()) {
            $this->redirect($this->Auth->redirect());
        } else {
            $this->Session->setFlash(__('Invalid username or password, try again'));
        }
    }
}

但由于这会分析默认的“用户名”和“密码”字段,我无法登录。如何更改Auth功能以验证我的三个字段并登录?

很抱歉,如果这是一个noob问题,我读了http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html,但没有任何帮助。

1 个答案:

答案 0 :(得分:1)

尝试以下方法:

public function beforeFilter() {
    $this->Auth->authenticate = array(
        'Form' => array(
            'fields' => array(
                'username' => 'employee', 
            ),
        ),
    );
}

public function login() {
    if ($this->request->is('post')) {
        $this->Auth->authenticate['Form']['scope'] = array(
            'User.company_id' => $this->request->data['User']['company_id'],
            // or however this condition needs to be setup as
        );
        if ($this->Auth->login()) {
             // login success
        } else {
             // login fail
        }
    }
}

基本上,将第三个条件添加为范围。