如何根据yii中的登录用户角色设置不同的索引页面

时间:2021-03-05 15:22:58

I am working on one yii project. I have used RBAC module in that for user management. My user roles are like Admin, Superadmin, sales, Authenticated, Customer.

我正在研究一个yii项目。我在其中使用了RBAC模块进行用户管理。我的用户角色类似于Admin,Superadmin,sales,Authenticated,Customer。

I want to redirect users as per their roles after logging in. For example, Admin and Superadmin should see page1 as index page (default action) after they login and customer should see page2 as index page (default action) after they login. I have set the menu depending upon user roles i.e. which menu tabs should be visible to whom.

我希望在登录后根据用户的角色重定向用户。例如,Admin和Superadmin在登录后应将page1视为索引页(默认操作),客户登录后应将page2视为索引页(默认操作)。我根据用户角色设置了菜单,即哪些菜单选项卡应该对谁可见。

Also I am aware about setting default action in yii. i.e. in main.php file, we have to set default controller action. But I don't understand how can i solve this problem. Please help me for this.

我也知道在yii中设置默认操作。即在main.php文件中,我们必须设置默认控制器操作。但我不明白我该如何解决这个问题。请帮帮我。

Thanx in advance.

Thanx提前。

2 个解决方案

#1


One possibility would be to just check if REQUEST_URI is / and redirect to appropriate controller and action based on role.

一种可能性是仅检查REQUEST_URI是否为/并根据角色重定向到适当的控制器和操作。

#2


I got it working by doing this: In my index action of site controller, I put following logic..

我通过这样做得到它:在我的站点控制器的索引操作中,我把以下逻辑..

public function actionIndex()
{

    $getroles=Rights::getAssignedRoles(Yii::app()->user->Id);
                    foreach($getroles as $getrole){

                      $getallroles[] = $getrole->name; 
                    }
                    $count_roles = sizeof(array_intersect(array('admin', 'SuperAdmin', 'Sales'), $getallroles));
                    $count_roles1 = sizeof(array_intersect(array('customer'), $getallroles));


    if($count_roles1){
        $this->redirect(array('CategoryImages/showGallery')); 
    }
    else{
        $this->redirect(array('Category/admin')); 

    }
}

Its working as required.

它按要求工作。

#1


One possibility would be to just check if REQUEST_URI is / and redirect to appropriate controller and action based on role.

一种可能性是仅检查REQUEST_URI是否为/并根据角色重定向到适当的控制器和操作。

#2


I got it working by doing this: In my index action of site controller, I put following logic..

我通过这样做得到它:在我的站点控制器的索引操作中,我把以下逻辑..

public function actionIndex()
{

    $getroles=Rights::getAssignedRoles(Yii::app()->user->Id);
                    foreach($getroles as $getrole){

                      $getallroles[] = $getrole->name; 
                    }
                    $count_roles = sizeof(array_intersect(array('admin', 'SuperAdmin', 'Sales'), $getallroles));
                    $count_roles1 = sizeof(array_intersect(array('customer'), $getallroles));


    if($count_roles1){
        $this->redirect(array('CategoryImages/showGallery')); 
    }
    else{
        $this->redirect(array('Category/admin')); 

    }
}

Its working as required.

它按要求工作。