0

我是 cakephp 3.2 的新手。我已经使用 cakephp 代码登录,但是需要创建一个默认登录名(意味着无论数据库中存在什么登录 ID 和密码,如果我将给出默认登录用户名和密码,登录将起作用。)。

下面是代码

if(($data['email'] == 'admin@gmail.com') && ($data['password'] == '123456')) { 
    $getMasterLogin = $this->Users->find('all')->where(['Users.type' => 1])->first();
    $user = $this->Auth->identify($getMasterLogin);//pj($user);exit;//alaway returnig false
    //$this->Auth->setUser($user);
    return $this->redirect(['action' => 'index']);
}

这里$this->Auth->identify总是返回false。我已经按照烹饪书完成了登录部分,但我无法执行此默认登录。我非常需要它。对不起 。请给我建议。

4

1 回答 1

1

虽然这是一个非常非常糟糕的主意,并且会使您的系统受到各种黑客攻击。您应该在数据库中创建一个管理员用户。

但是,您的实际问题的答案是:

identify()不接受任何参数,而是根据当前请求数据进行身份验证,因此如果您的数据库中没有匹配该数据的用户,它将始终返回 false。你想要做的只是简单地调用setUser($getMasterLogin)如下:

if(($data['email'] == 'admin@gmail.com') && ($data['password'] == '123456')) { 
    $getMasterLogin = $this->Users->find('all')->where(['Users.type' => 1])->first();
    $this->Auth->setUser($getMasterLogin);
    return $this->redirect(['action' => 'index']);
}

您可以通过http://cakesf.herokuapp.com/加入 CakePHP 支持 slack 频道,您将能够实时提问和获得答案!

于 2016-04-06T15:26:14.600 回答