yii : how to redirect to different page after logged in?

旧巷老猫 提交于 2020-01-05 10:04:20

问题


Q : how to redirect to different page after logged in?

status : I've custom user role and I want to redirect to different page for different user.

e.g hr manager -> employee/index, account manager -> account/index.

I'm using yii 1.1.xx and rights module. yii and rights module are going with green.

update question's info

this is the rank tbl

this is user tbl

If the rank is Account Manager, the web site will redirect to account/index.

If the rank is HR manager, the web site will redirect to employee/index.


回答1:


if(Yii::app()->user->checkAccess('hrManager',Yii::app()->user->getId())==1)
{
    $this->redirect(array("employee/index"));
} else if(Yii::app()->user->checkAccess('accountManager',Yii::app()->user->getId())==1) {
    $this->redirect(array("account/index"));
}

Where hrManager and accountManager are the corresponding access rules for the HR Manager and Account Manager logins. This will check to see if the user has 'HR Manager' level access, if so redirect them there, and then checks if the user has 'Account Manager' level access, if so redirects them to that page.

Obviously you can customise to how it suits your app, as the above code won't redirect anywhere if the user has neither HR Manager or Account Manager access, so you'll need another catcher.

Edit:

The hrManager string is the rbac authitem name, do you have tables authassignment authitem and authitemchild set up for your rbac?

If not, you can use this method a little differently using your ranking table. you can either set it up using the rank_id, but you'd have to load the user model (if you haven't already got it loaded) which might not be the most efficient way of doing it.

$user = User::model()->findbyPk(Yii::app()->user->getId());
if($user->rank_id==3)
{
    $this->redirect(array("employee/index"));
} else if($user->rank_id==2)) {
    $this->redirect(array("account/index"));
}

The other way would be to load the users rank into the Yii::app()->user param after login so you don't have to load the user model before the redirect, maybe you have this set up already for however you are granting access to pages?

if(Yii::app()->user->rank_id==3)
{
    $this->redirect(array("employee/index"));
} else if(Yii::app()->user->rank_id==2)) {
    $this->redirect(array("account/index"));
}



回答2:


Something like $this->redirect(array("controller/admin")); ?



来源:https://stackoverflow.com/questions/12104472/yii-how-to-redirect-to-different-page-after-logged-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!