6

我想阻止被禁止的用户登录该站点并向他们发送一条消息,表明他们已被禁止。我尝试为此使用 isAuthorized() 但它允许用户登录,并且只有在此之后才拒绝他对未经授权的操作的许可。

所以,基本上我想知道在登录过程发生之前将检查用户表是否为禁止=真的条件放在哪里。现在我的登录功能是空的,因为它是由 Auth 组件自动控制的。

4

5 回答 5

13

最后,我通过API找到了解决方案。我想知道是否有人曾经使用过这个,因为没有人向我指出这个,或者我可能不够清楚。无论如何,要将条件添加到登录过程中,您只需将其放入变量 $this->Auth->userScope

所以,为了检查一个用户是否被禁止,我只是在我的 AppController 中的 beforeFilter() 中添加了这一行,

$this->Auth->userScope = array('User.banned'=>0);

希望这可以帮助某人。

于 2010-08-25T23:39:20.837 回答
6

或者: $this->Auth->userScope = array('User.banned'=>0);

这可以在您包含 Auth 组件时完成。这可能会节省一些开销,因为$this->Auth->userScope每次解析控制器时都不会调用。

public $components = array(
    'Auth' => array(
        'authenticate' => array(
            'Form' => array(
                'passwordHasher' => 'Blowfish',
                'scope' => array('User.banned' => 0)
            )
        ),
        'authorize' => array('Controller')
    )
);
于 2014-09-07T23:19:31.923 回答
3

如果您已经启动并运行了整个 Auth 系统,为什么不遵循 KISS 原则并撤销他们的密码或更改用户名呢?如果他们不再能够像之前那样通过您的系统进行身份验证,他们应该能够推断出他们被禁止了。

如果这还不够,那么您还可以添加下面的代码。

function login() {
  if ($this->Session->read('Auth.User')) {
    $this->Session->setFlash('You are alreadylogged in!~~~~~~~~~~~');   
  }
  $this->Session->setFlash('You have been banned!');    
  $this->redirect(array('controller'=>'users','action'=>'index'));
}

编辑 1:对于您在评论中指出的更动态的方法,您可以检查您关注的用户记录的 is_banned 列,UsersController::beforeFilter()并相应地设置您的闪存消息。还要根据$this->Session->read('Auth.User.is_banned'). <?php debug $this->Session->read('Auth.User) ?>也许你想在攻击你的问题之前看看输出。

编辑2:我的错。您可以将 is_banned 通过$this->Session->write(...). 阅读后,is_banned = true您可以注销用户。

于 2010-08-25T10:03:03.313 回答
2

你必须使用:

/** Function is executed after the login*/
function isAuthorized() {
return true;
}

您可以在其中检查用户是否被禁止。IE

/** Function is executed after the login*/
function isAuthorized() {
if($this->Auth->user('banned') == 1){ //column banned should be in the users table
       $this->Session->setFlash('You have been banned!');    
       return false;
    }
    return true;
}

我相信这是正确的方法。

于 2010-08-25T11:50:24.497 回答
1

在阅读了您对 Nik 方式的最后评论后,我认为您可以通过在代码中的适当位置通过 $this->Auth->logout() 手动注销用户来优化您的原始解决方案(然后是重定向)。这样看起来他/她从未登录过。

于 2010-08-25T12:55:38.840 回答