CakePHP 2.x无法阻止已删除和被禁止的用户登录

CakePHP 2.x无法阻止已删除和被禁止的用户登录,cakephp,login,cakephp-2.0,cakephp-2.1,Cakephp,Login,Cakephp 2.0,Cakephp 2.1,我有一些代码可以防止被删除和禁止的用户登录。要清除头脑,状态为-2表示该用户已被删除,-1表示该用户已被禁止。下面的代码在本地运行良好,但在现场运行时很糟糕。状态为-1或-2的用户仍然可以登录。我找不到问题出在哪里 if ($this->Auth->login()) { //first check if the user's status is -1 or -2. $status = $this->Auth->user('status'); i

我有一些代码可以防止被删除和禁止的用户登录。要清除头脑,状态为-2表示该用户已被删除,-1表示该用户已被禁止。下面的代码在本地运行良好,但在现场运行时很糟糕。状态为-1或-2的用户仍然可以登录。我找不到问题出在哪里

if ($this->Auth->login()) {
    //first check if the user's status is -1 or -2. 
    $status = $this->Auth->user('status');

    if ($status == '-1') {
        $this->Auth->logout();
        $this->Session->setFlash(__('This account has been banned. Please contact with us.'));
        $this->redirect('/');
    } elseif ($status == '-2') {
        $this->Auth->logout();
        $this->Session->setFlash(__('This account has been deleted, and is not usable anymore.'));
        $this->redirect('/');
    }

    //something else
}

通过在复选框中调用
$this->Auth->login()
,用户将登录

您可以避免这种情况,并在登录之前检查用户信息,或者可以将状态标志添加到用户的作用域中

$this->Auth->authenticate = array(
    AuthComponent::ALL => array(
        'userModel' => 'User',
        'scope' => array('User.status' => '> 0)
    ),
    'Form',
    'Basic'
);
这会将
状态
字段检查添加到登录过程中

如果您想像示例中那样自定义消息,可以在处理登录之前检查用户信息上的值:

$user = $this->User->findByUsername($this->data['User']['username']);
if (!empty($user)) {
    if ($user['User']['status'] == -1) {
        // Set message for banned account
    }
    if ($user['User']['status'] == -2) {
        // Set message for deleted account
    }
    $this->redirect( ... ); // Redirect away
}

if ($this->Auth->login()) {
    // Normal login process
}

范围答案是最好的,但条件中有一个小错误,而不是

'scope' => array('User.status' => '> 0)
这行应该是

'scope' => array('User.status >' => 0)
这是在条件数组中添加比较运算符的典型cakephp方法


(我本想对此发表评论,但如果是新加入此网站,则更容易创建答案。)

我不知道范围键,它是2.2版中的新功能。谢谢你的信息!但现在这不适合我。第二种解决方案在本地运行良好,但在现场运行时效果不佳。我已经做了很多调试,但仍然无法解决问题。您好。现在它也可以在现场使用。我没有做任何承诺,只是删除了一个缓存文件,它的路径是app/tmp/cache/persisten/myapp\u cake\u core\u file\u map。真奇怪!范围键对于CakePHP 2来说并不新鲜。