hiding menu items from other users....

hi…

i have a menu like below

======================================================

home || aboutus || contact || login || cdr || system

======================================================

i want to hide the "cdr" and "system" items from the guest and any other user except admin…

i have done it for guest…plz see it

[color="#FF0000"]rray(‘label’=>‘Permission’, ‘url’=>array(’/permission’),‘visible’=>!Yii::app()->user->isGuest),[/color]

but i can’t do for the other users except admin…

when i sign in by a user account i want to hide these two menu items from it…

plz tell me the way…

thanks

you can save the ‘type’ of the user at login using Yii::app()->user->setState(‘isAdmin’, true) and then test its value using ‘visible’=>Yii::app()->user->getState(‘isAdmin’) (or simply Yii::app()->user->isAdmin)

yes i have done this already as u said "Yii::app()->user->isAdmin"

but i want to differ with admin and other registered users…

i want to show all menu items to admin but some to other users…

As alexb suggested, first set the state at login i.e

Yii::app()->user->setState(‘isAdmin’, true);

then while creating your protected menu item, instead of

array(‘label’=>‘Permission’, ‘url’=>array(’/permission’),‘visible’=>!Yii::app()->user->isGuest),

use

array(‘label’=>‘Permission’, ‘url’=>array(’/permission’),‘visible’=>Yii::app()->user->isAdmin),

and for other menu items, do not specify any condition for visible i.e.

array(‘label’=>‘About us’, ‘url’=>array(’/about’) ),

As others have suggested, you may control the visibility of a menu item based on Yii::app()->user->isAdmin. However, before you can do this, you need to determine who is "admin" based on some logic. I handle it this way:

In my user table, I have a numeric userlevel field. I have 3 types of users: regular (userlevel: 0 -4), admin (userlevel >4) and superuser (userlevel >=10 ).

In the authenticate() function of UserIdentity.php, I add the following code to set isAdminUser and isSuperUser:


			if ( $user->userlevel > 4) 

			{

				yii::app()->user->setState(isAdminUser,true);

			} else {

				yii::app()->user->setState(isAdminUser,false);

			}

			

			if ( $user->userlevel >= 10) 

			{

				yii::app()->user->setState(isSuperUser,true);

			} else {

				yii::app()->user->setState(isSuperUser,false);

			}

			



And I also extend CWebUser like this:


<?php

class raviCWebUser extends CWebUser

{


	public function isAdminUser() {

		return $this->getState('isAdminUser');	

	}

	


	public function isSuperUser() {

		return $this->getState('isSuperUser');	

	}

}




And I also need to tell Yii that I want to use a different CWebUser by including the following in the main configuration file:


		'user'=>array(

			'class' => 'application.components.raviCWebUser',		

			'allowAutoLogin'=>false,

			'loginUrl' => array('site/login'),

		),	



After this is done, I am able to control the menu like this:


array("url"=>array(

				   "route"=>"/products"),

				   "label"=>"Products",

				   "visible"=>  !Yii::app()->user->isGuest && Yii::app()->user->isAdminUser()

				   ),



I hope this may help.

sorry for late respond… i’m out of office these days…

you can try using solutions in previous replies.

and since we meet in Right module extension… maybe you want to check it by role…


"visible"=>Yii::app()->getUser()->checkAccess('[module.]controller.action'),

1 Like

thanks to u all people…specialy @ tax14

i am very new to yii…

i have started it 1 month ago…

now i have learnt it 50%…

inshaallah i will continue and so on…

thanks again

one minor correction in the solution suggested by friend "tax14"

In the authentication method,

line should be like

yii::app()->user->setState("isAdminUser",true);

instead of

yii::app()->user->setState(isAdminUser,true);

ok, I did everything as "tax14" provided and got following error: Property "CWebApplication.user" is read only.

I also keep user levels in other table:


+-----------+       +-----------+

|    user   |       |   post    |

+-----------+       +-----------+

| ....      |   +---| id        |

| post_id   |>--+   | level     |

| ....      |       | ....      |

+-----------+       +-----------+

So how to set relations in model, that I could take it as "$user->level"?

any ideas?

the best idea to hide/show element for different user role is … to use session for user.

You can use following steps as ,

step : 1 => create session for different user in controller as ,


if($model->validate() && $model->login())

	{

	    if($empData['E_ROLE']=='Admin'){         //  check role admin from database.

		Yii::app()->session['adminUser'] = "admin";   // start session admin

		$this->redirect(array("adminPage"));

	    }else{

		$this->redirect(array("otherPage"));

	    }

	}

setp : 2 => now you can use this session anywhere in system , as ,


if(isset(Yii::app()->session['adminUser'])){


... ...

create element here for admin

... ...

}

step : 3 => dont forgot to unset session on Sign Out.


public function actionLogout()

	{

		if(isset(Yii::app()->session['adminUser']))

		{

			unset(Yii::app()->session['adminUser']);

		}

		Yii::app()->user->logout();

		$this->redirect(Yii::app()->homeUrl);

	}