Cakephp管理员和用户登录同时无法正常工作

时间:2014-04-15 07:55:57

标签: cakephp authentication cakephp-2.0 cakephp-2.4

我的网站中有两种类型的用户。 1.用户(前端) 2.Admin(后端)

我使用cakephp auth组件登录这两种类型的用户。 我的问题是,当我使用一种类型的用户登录时,我将自动登录到其他类型的用户,例如我从前端登录用户,但是当我刷新后备份时,它将显示logedin和管理员类型用户的相同问题。

以下是我的auth代码,分别为admin和user。

这是app / Controller

中的AppController.php文件
public function beforeFilter() {
    if($this->Auth->user()){
    $this->set('logged_in', true);
    }else{
    $this->set('logged_in', false);
    }
    //Configure AuthComponent
    $this->Auth->userScope = array('User.is_active' => '1');
    $this->Auth->loginAction = array('controller' => 'users', 'action' => 'signin','plugin' => 'umgmt');
    $this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'signin','plugin' => 'umgmt');
    $this->Auth->loginRedirect = array('controller' => 'users', 'action' => 'index','plugin' => false);

    }

这是我的管理员插件控制器代码

public $components = array(
    'Auth' => array(
            'authenticate' => array(
                'Form' => array(
                    'userModel' => 'Admin',
                    'fields' => array(
                        'username' => 'username',
                        'password' => 'password'
                    ),
                     'scope'=>array('Admin.is_active' => 1)
                )
            )
        )
    );


public function beforeFilter() {

    $this->Auth->loginAction = array('controller' => 'admins', 'action' => 'index');
    $this->Auth->logoutRedirect = array('controller' => 'admins', 'action' => 'index');
    $this->Auth->loginRedirect = array('controller' => 'admins', 'action' => 'dashboard');

}

我正在使用cakephp 2.4.1

请帮帮我

1 个答案:

答案 0 :(得分:0)

好。首先请注意,您不需要设置logged_inout变量,因为AuthComponent通过AuthComponent::loggedIn()方法提供此功能。

其次,您尚未设置Auth类型。根据本书,使用Controller通常是明智的,这样您就可以基于每个操作管理身份验证。

因此,您需要更新组件配置或beforeFilter()以包含授权类型。

$this->Auth->authorize = array('Controller');

然后,您可以使用isAuthorized()中的AppController方法根据所使用的路由前缀检查用户的角色。

所有这些实施都是covered in the Cake book in the chapter on Auth