允许某些用户登录而不在cakephp中输入密码

时间:2015-10-08 09:48:08

标签: php cakephp login passwords cakephp-3.0

我正在使用cakephp v3。我启用了身份验证。假设我想要一个名为“XXX”的用户不使用密码登录。我在UsersController.php中有以下login()函数。如何修改以允许名为“XXX”的用户登录而无需输入密码?

public function login()
{
    if ($this->request->is('post')) {
        $user = $this->Auth->identify();
        if ($user) {
            $this->Auth->setUser($user);
            return $this->redirect($this->Auth->redirectUrl());
        }
        $this->Flash->error(__('Invalid username or password, try again'));
    }
} //public function login()

1 个答案:

答案 0 :(得分:1)

这样的事情应该可行。只需确保使用正确的方法为用户名获取正确的参数,对数据库中的用户也是如此。

public function login()
{
    if ($this->request->is('post')) {
        $username = $this->request->data['username'];

        if (username === 'XXX') {
            $user = TableRegistry::get('Users')->find()->where(['username' => $username]);
        } else {
            $user = $this->Auth->identify();
        }

        if ($user) {
            $this->Auth->setUser($user);
            return $this->redirect($this->Auth->redirectUrl());
        }
        $this->Flash->error(__('Invalid username or password, try again'));
    }
}