【问题标题】:CakePHP - Auth logoutRedirect not working for Admin usersCakePHP - Auth logoutRedirect 不适用于管理员用户
【发布时间】:2013-03-27 16:07:48
【问题描述】:

在我的 cake 2.2 应用中,我的应用控制器中设置了以下 beforeFilter():

public function beforeFilter() {

    //Configure AuthComponent
    // Admin
    if($this->Auth->user('group_id') == '12') {
        $this->Auth->allow('admin_index'); 
        $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login', 'admin' => FALSE);
        $this->Auth->loginRedirect = array('controller' => 'pages', 'action' => 'index', 'admin' => TRUE);
        $this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login', 'admin' => FALSE);

        $this->set("group", "admin");

    // Staff
    }

    if($this->Auth->user('group_id') == '13') {
        $this->Auth->allow('admin_index'); 
        $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login', 'admin' => FALSE);
        $this->Auth->loginRedirect = array('controller' => 'pages', 'action' => 'index', 'admin' => TRUE);
        $this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login', 'admin' => FALSE);

        $this->set("group", "staff");

所以基本上我希望在会话到期时将所有用户(无论用户组)发送到 /users/login 。这适用于用户,但任何管理员用户都会被重定向到 admin/users/login 并在用户控制器错误中显示 Missing 方法(因为这不是管理员方法)。由于某种原因,'admin' => FALSE 不起作用。

那么,无论用户类型如何,如何让所有用户重定向到 /users/login 的非管理方法/url

    // Users
    } 

    if($this->Auth->user('group_id') == '14') {
        $this->Auth->allow(array('controller' => 'pages', 'action' => 'index', 'admin' => FALSE));
        $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login', 'admin' => FALSE);
        $this->Auth->loginRedirect = array('controller' => 'pages', 'action' => 'index', 'admin' => FALSE);
        $this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login', 'admin' => FALSE);

        $this->set("group", "user");
    }

    // General logout redirect (including expired session redirect)
    $this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login', 'admin' => FALSE);
}

【问题讨论】:

  • 你可以在你的路由文件中使用重定向路由来破解它,但这并不理想。 PS,你的大写布尔值伤害了我的眼睛。

标签: cakephp


【解决方案1】:

我猜发生的事情是会话到期时用户实际上并没有登录。除非用户明确退出(我假设在您的 UsersController 中执行 lougout 操作),例如这样

public function logout() {
    ... some code here...
    $this->Session->destroy();
    $this->redirect($this->Auth->logout());
}

logoutRedirect 可能不起作用。
如果会话过期,用户将无权查看该页面,并且重定向将转到Auth->unauthorizedRedirect。

对于您要执行的操作,我将使用一种方法检查用户是否在 AppController 的 beforeFilter 中登录

public function beforeFilter() {
    if (!$this->Auth->loggedIn() && $this->action != 'login') {
        $this->redirect(array('controller'=>'users', 'action'=>'login', 'admin'=>false));
    }
}

或者如果你愿意

public function beforeFilter() {
    if (!$this->Auth->loggedIn() && $this->action != 'login') {
        $this->redirect($this->Auth->logoutRedirect);
    }
}

【讨论】:

  • 您好,感谢您的回答-我已经尝试过了,我认为它会起作用,但不幸的是它会中断会话超时,因为当我添加上面的代码时会话永远不会超时?!还有其他想法吗?
  • 当我尝试手动访问登录页面时,使用您的任何一个示例都会导致重定向循环。
  • 我编辑了答案以修复循环,只需将“登录”更改为您的登录操作名称。我觉得会话没有超时很奇怪,因为我展示的代码实际上并没有对会话做任何事情,只是一个重定向......我的应用程序中有相同的代码并且正在把我踢出去每隔几秒钟。您在 core.php 中的超时和安全级别值是多少?
【解决方案2】:
public function admin_logout() {
    $this->Session->setFlash(__('Thanks for using Applired.com!'), 'default', array('class' => 'alert alert-success'));
    $this->Session->delete('user_to_register');
    $this->Session->destroy();
    $this->Auth->logout();
    return $this->redirect(array('controller' => 'dashboard', 'action' => 'login'));
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-22
    相关资源
    最近更新 更多