【问题标题】:Auth and ACL with condition in CakePHPCakePHP 中的 Auth 和 ACL 条件
【发布时间】:2012-09-06 13:30:17
【问题描述】:

我找不到解决问题的方法。 我有一个使用 Auth 组件和 ACL 组件的 CakePHP 网站。 我不希望不活跃的用户能够登录。

我发现 Auth 组件中的 userScope 可以做到这一点。 因此,在 beforeFilter 内的 AppController 中,我添加了以下内容:

    $this->Auth->userScope = array('User.active' => 1);

当然在我的 UserController beforeFilter 中,调用了父方法。

但是,这不起作用,我仍然可以使用活动设置为 0 的用户登录。 我想可能是因为 ACL 组件?

这是我在 AppController 中的 beforFilter

    public function beforeFilter()
    {
    if (!$this->Session->check('Auth.User'))
        $this->layout = 'identification';
    $this->Auth->allow('display');

    //Configure AuthComponent
    $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
    $this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login');
    $this->Auth->loginRedirect = array('controller' => 'users', 'action' => 'welcome');
    $this->Auth->userScope = array('User.active' => 1);
    }

我错过了什么?

【问题讨论】:

  • 你使用的是 cakePHP 2.0 还是 1.3?

标签: php cakephp acl before-filter


【解决方案1】:

如果你不成功,你可以随时使用替代方案:

$user = $this->Auth->user();
if($user['User']['active'] == 0){
   $this->redirect($this->Auth->logout());
   $this->Session->setFlash('You are not active.');
}

【讨论】:

  • 好的,我在 $this->Auth->login() 方法之后进行了检查,如果用户不活跃,则调用 $this->Auth->logout()制作并重定向到一个好的布局。但这并不是很好,因为我需要登录用户才能注销用户。对安全性不太好......
  • 我知道,但在您找到更好的解决方案之前,这是目前最好的拍摄,如果您找到了,请随时在此处发布。 :D
【解决方案2】:

您使用的代码对 Cake 2 无效。请参阅 http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#configuring-authentication-handlers

下面是一些应该可以工作的代码:

$this->Auth->authenticate = array('Form' => array('scope' => array('User.active' => 1)));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多