Switch layout based on user role

Standard

You can assign different layouts to different users using following filter class

1. Add LayoutFilter.php to your protected/components/ folder

class LayoutFilter extends CFilter
{

    protected function preFilter($filterChain)
    {
        // logic being applied before the action is executed
        $roles = Yii::app()->user->getState('roles');
        if (!is_array($roles))
            $roles = array();

        if (in_array(User::ROLE_ADMIN, $roles))
        {
            $filterChain->controller->layout = 'admin/column2';
        } else
        {
            $filterChain->controller->layout = '//layouts/column2';
        }

        return parent::preFilter($filterChain);
    }

    protected function postFilter($filterChain)
    {
    // logic being applied after the action is executed
        return parent::postFilter($filterChain);
    }

}

2. Update protected/components/Controller.php and add following filters

public function filters()
{
    return array(
        'rights',
        array('application.components.LayoutFilter'),
    );
}

3. Update all controllers and add following filters

public function filters()
{
    return parent::filters() + array(
        'postOnly + delete', // we only allow deletion via POST request
    );
}
Advertisements

15 thoughts on “Switch layout based on user role

  1. balaji

    i m using only one login page, i want to login user and admin. when user login change userlayout and when admin login change adminlayout. otherwise i dont no need front and back panels.
    Pls help me.

  2. You can have a role column in your users table, and set that role to the user at login time in UserIdentity.php like

    Yii::app()->user->setState(‘role’, $user->role);

    And you can check that role at any time anywhere in the application, on the basis of user role you can easily switch layout in any action of any controller by

    if(Yii::app()->user->getState(‘role’) == ‘admin’)
    {
    $this->layout = ‘//layouts/column2’;
    }

    • balaji

      This line of coding not working for me sir. im using yii rights and yii user extensions..so cannot work.

      shall i send my project..pls help me,

      Thanxs for your valuable time for me sir,

      • balaji

        I got the solution..I m very very Happy sir..

        ————————————————————————————
        //Login Controller
        ————————————————————————————
        if (Yii::app()->user->isSuperuser)
        //some actions here ..
        $this->redirect(array(‘/mailbox’));
        else
        $this->redirect(Yii::app()->user->returnUrl);

        ————————————————————————————
        //particular module view control..i.e., mailbox controller
        if (Yii::app()->user->isSuperuser)
        {
        $this->layout=’//layouts/colomn1′;
        }
        else
        {
        $this->layout=’//layouts/colomn2′;
        }

        ————————————————————————————–

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: