0

我制作了一个包含三个字段的基本登录表单,它们是“公司”、“员工”和“密码”,我尝试使用默认的 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 功能以验证我的三个字段并让我登录?

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

4

1 回答 1

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
        }
    }
}

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

于 2012-06-14T05:02:11.293 回答