2013-03-27 174 views
1

在我的蛋糕2.2的应用程序,我有以下beforeFilter()在我的应用程序控制器设置:CakePHP的 - 验证logoutRedirect不工作的管理员用户

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"); 

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

所以,我怎样才能得到所有用户,无论用户类型来重定向到/用户NON管理方法/ 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); 
} 
+0

检查你可以在你的路由文件中使用重定向路由来破解这个,但这并不理想。 PS,你的资本布尔人伤害了我的眼睛。 – 2013-04-02 09:14:23

回答

1

我猜的情况是,用户实际上没有会话过期时登出。除非用户明确地登出(执行你的UsersController一个lougout动作,我假设),这样例如

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

是logoutRedirect可能是行不通的。
如果会话过期,用户将被授权查看该页面,并且重定向将转到Auth-> unauthorizedRedirect。

对于你想要做什么,我会使用的方法,如果用户登录的AppController中

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

beforeFilter或者如果你想

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

嗨,谢谢你的答案 - 我试过这个,我认为它会工作,但不幸的是,它会打破会话超时,因为当我添加上面的代码会话永远不会超时?!任何其他想法? – 2013-04-05 13:09:13

+0

当我尝试手动访问登录页面时,还会使用任一示例导致重定向循环。 – 2013-04-05 13:20:17

+0

我编辑了修复循环的答案,只需将“登录”更改为您的登录操作名称即可。我发现会话没有超时很奇怪,因为我所展示的代码实际上并没有对会话做任何事情,只是一个重定向...我的应用程序中有相同的代码,并且正在踢我每隔几秒钟。 core.php中的超时值和安全级别值是什么? – Nunser 2013-04-05 13:41:43

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