简体   繁体   English

CakePHP Auth组件在登录前检查用户

[英]CakePHP Auth Component Check User Before Login

I want to prevent banned users from logging in to the site and give them a message that they are banned. 我想阻止被禁止的用户登录该网站并向他们发送禁止他们的消息。 I tried to use isAuthorized() for this but it allows the user to login and only after that denies him permission to the unauthorized actions. 我尝试使用isAuthorized(),但它允许用户登录,并且只有在拒绝他批准未授权的操作之后才允许用户登录。

So, basically I want to know where to put the condition that would check if the user table as banned = true, before the login process takes place. 所以,基本上我想知道在登录过程发生之前将检查用户表是否为banned = true的条件放在哪里。 Right now my login function is empty as its being automatically controlled by the Auth Component. 现在我的登录功能是空的,因为它由Auth组件自动控制。

Finally, I found a solution by going through the API. 最后,我通过API找到了解决方案。 I wonder if anyone has used this ever, cause nobody pointed me to this, or maybe I wasn't clear enough. 我想知道是否有人曾经使用过这个,因为没有人指出我,或者我可能不够清楚。 Anyways, to add a condition to the login process you just have put it in the variable $this->Auth->userScope 无论如何,要为登录过程添加一个条件,你只需将它放在变量$ this-> Auth-> userScope中

So, to check if a user is banned I just added this line to the beforeFilter() in my AppController, 因此,要检查用户是否被禁止,我只是将此行添加到AppController中的beforeFilter()中,

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

Hope this helps someone. 希望这有助于某人。

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

This can be done when you include your Auth Component. 包含Auth组件时可以执行此操作。 This probably saves some tiny amount of overhead as $this->Auth->userScope isn't called every time a controller is parsed. 这可能会节省一些微小的开销,因为每次解析控制器时都不会调用$this->Auth->userScope

public $components = array(
    'Auth' => array(
        'authenticate' => array(
            'Form' => array(
                'passwordHasher' => 'Blowfish',
                'scope' => array('User.banned' => 0)
            )
        ),
        'authorize' => array('Controller')
    )
);

If you have the whole Auth system already up and running, why don't you just follow the KISS principle and revoke their password or alter there username? 如果您已经启动并运行整个Auth系统,为什么不按照KISS原则撤销密码或更改用户名? If they are not longer able to authenticate with your system as they could earlier they should be able to deduce that they are banned. 如果他们不能再像以前那样能够对您的系统进行身份验证,那么他们应该能够推断出他们被禁止了。

If that doesn't suffice, then additionally you could add the code below. 如果这还不够,那么另外你可以添加下面的代码。

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'));
}

Edit 1: For a more dynamically approach like you pointed out in your comment, you could check the is_banned column of the user record under concern in your UsersController::beforeFilter() and set your flash message accordingly. 编辑1:对于您在评论中指出的更动态的方法,您可以在UsersController::beforeFilter()检查所关注的用户记录的is_banned列,并相应地设置您的Flash消息。 Also make a redirect based on the outcome of $this->Session->read('Auth.User.is_banned') . 还可以根据$this->Session->read('Auth.User.is_banned')的结果进行重定向。 Maybe you want to have a look at the output of <?php debug $this->Session->read('Auth.User) ?> before attacking your problem. 也许你想在攻击你的问题之前看一下<?php debug $this->Session->read('Auth.User) ?>

Edit 2: My fault. 编辑2:我的错。 You could store the is_banned somewhere in the Session via $this->Session->write(...) . 您可以通过$this->Session->write(...)将is_banned存储在Session中的某个位置。 After you read an is_banned = true you can log the user out. 读取is_banned = true您可以将用户注销。

you have to use: 你必须使用:

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

where you can check if the user is banned or no. 在哪里可以检查用户是否被禁止。 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;
}

I believe this is the correct way. 我相信这是正确的方法。

Having read your last comment on Nik's way, I think that you could just refine your original solution by logging the user out manually via $this->Auth->logout() at the appropriate place in your code (followed by a redirect). 在阅读了关于Nik的最后评论之后,我认为您可以通过在代码中的适当位置通过$ this-> Auth-> logout()手动记录用户来优化原始解决方案(然后重定向)。 This way it should look like he/she never logged in. 这样它应该看起来像他/她从未登录过。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM