Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8528453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:48:17+00:00 2026-06-11T08:48:17+00:00

Q : How can I show display different menu(s) by user role? Description :

  • 0

Q : How can I show display different menu(s) by user role?

Description : the app has many roles. e.g HR manager, Account Manager, Operating Manager, Employee, Operator, …., etc. I used rights and yii-user modules to create those roles. Those roles have different functions. So the app will show different menu for different user’s role after logged in. Now, I can lock the function for different user. e.g when HR manger logged in, he/she can’t route to other function of user role. But I don’t know how to show the HR Menu for Hr Manager, only.

I am not a newbie for yii. but I’m a newbie for those modules (rihgts and yii-user).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T08:48:18+00:00Added an answer on June 11, 2026 at 8:48 am

    If you’re using RBAC you can set the ‘visible’ param of CMenu items depending on the users privileges, for example;

    $this->widget('zii.widgets.CMenu',array(
        'items'=>array(
            array(
                'label'=>'Home',
                'url'=>array('site/index'),
            ),
            array(
                'label'=>'HR',
                'url'=>array('/hr/index'),
                'visible'=>Yii::app()->user->checkAccess('hr')
            ),
            array(
                'label'=>'Accounts',
                'url'=>array('/account/index'),
                'visible'=>Yii::app()->user->checkAccess('account')
            ),
            array(
                'label'=>'Operations',
                'url'=>array('/operations/index'),
                'visible'=>Yii::app()->user->checkAccess('operations')
            ),
        ),
    );
    

    This way users will only be able to see the items in the menu if they have access privileges for that area.

    [EDIT]

    As per simaremare’s comment below, you can force caching of this query beyond the current request by extending CWebUser. Firstly, set your user to run through your new class (we’ll call it TWebUser), so in your main.php config file;

    'components'=>array(
        'user'=>array(
            ...
            'class'=>'TWebUser',
            ...
        ),
        ...
    ),
    

    Now we need to create TWebUser to cache these beyond the current request (which is what CWebUser does (source code):

    class TWebUser extends CWebUser
    {
        private $_access=array();
    
        public function checkAccess($operation,$params=array(),$allowCaching=true)
        {
            if($allowCaching && $params===array() && isset($this->_access[$operation]))
                return $this->_access[$operation];
    
            $cache = Yii::app()->session['checkAccess'];
            if($allowCaching && !$this->getIsGuest() && isset($cache[$operation]) && time() - $cache[$operation]['t'] < 1800)
            {
                $checkAccess = $cache[$operation]['p'];
            } else {
                $checkAccess = Yii::app()->getAuthManager()->checkAccess($operation,$this->getId(),$params);
    
                if($allowCaching && !$this->getIsGuest())
                {
                    $access = isset($cache) ? $cache : array();
                    $access[$operation] = array('p'=>$checkAccess, 't'=>time());
                    Yii::app()->session['checkAccess'] = $access;
                }
            }
    
            return $this->_access[$operation] = $checkAccess;
        }
    }
    

    Now your access results will be set for the whole session. This does mean that if you edit the RBAC permissions for a given account, they’ll have to log out and log in again to see the new changes reflected in the browser.

    I hope that helps! I’m sure I found this workaround from someone else (probably on SO), but I can’t find the original post to give them credit.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a tab bar app that has to display in different languages depending
I can't get the Action Bar to display my action items. They all show
It seems that we can show layers and even use a different zPosition for
In my iPhone app, I am using a single tableview to display different sets
I have a couple of functions. Basically a menu where a user can choose
How can I show data from two different elements in a single column? or
how can i show html tags like this html.actionlinlk(<b>bla bla</b>, null) it dislay bla
can anyone recommend a website crawler that can show me all of the links
if so can show simple example, ex in jquery ajax.post
How I can show only 2 posts on my websites first page? I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.