Reputation: 4342
i want to make a menu to be only available for admins, for now i have this code and show you in the code what i want to change
<?php
if(Yii::app()->user->name = 'admin') //for now i use this to be rendered only if the name of the user is admin, but i want to change it to be available for everyone who are admin.
{
$this->widget('bootstrap.widgets.TbNavBar',array(
'brandLabel'=>TbHtml::b(Yii::app()->name),
'color'=>TbHtml::NAVBAR_COLOR_INVERSE,
'items'=>array(
array(
'class'=>'bootstrap.widgets.TbNav',
'items'=>array(
array('label'=>'Home', 'url'=>array('/site/index')),
array('label'=>'About', 'url'=>array('/site/page', 'view'=>'about')),
array('label'=>'Contact', 'url'=>array('/site/contact')),
array('label'=>'Users', 'url'=>array('/user/index')),
),
),
array(
'class'=>'bootstrap.widgets.TbNav',
'htmlOptions'=>array('class'=>'pull-right'),
'items'=>array(
array('label'=>'Login', 'url'=>array('/site/login'), 'visible'=>Yii::app()->user->isGuest),
array('label'=>'Logout ('.Yii::app()->user->name.')', 'url'=>array('/site/logout'), 'visible'=>!Yii::app()->user->isGuest)
),
),
),
));
}
else
{
$this->widget('bootstrap.widgets.TbNavBar',array(
'brandLabel'=>TbHtml::b(Yii::app()->name),
'color'=>TbHtml::NAVBAR_COLOR_INVERSE,
'items'=>array(
array(
'class'=>'bootstrap.widgets.TbNav',
'items'=>array(
array('label'=>'Home', 'url'=>array('/site/index')),
array('label'=>'About', 'url'=>array('/site/page', 'view'=>'about')),
array('label'=>'Contact', 'url'=>array('/site/contact')),
),
),
array(
'class'=>'bootstrap.widgets.TbNav',
'htmlOptions'=>array('class'=>'pull-right'),
'items'=>array(
array('label'=>'Login', 'url'=>array('/site/login'), 'visible'=>Yii::app()->user->isGuest),
array('label'=>'Logout ('.Yii::app()->user->name.')', 'url'=>array('/site/logout'), 'visible'=>!Yii::app()->user->isGuest)
),
),
),
));
}
?>
im using (if) for each widget if the condition returns true, but what i want to change is.. the menu "user", to be only rendered for everyone thats admin, if not then not render and deny access.
Upvotes: 0
Views: 3966
Reputation: 1280
I found this to be a quick hack/solution https://stackoverflow.com/a/11131481. After experiencing a similar problem, that is what I came up with - ideally, I'd want to do this for all users of admin privilege (and not hard code in usernames).
Upvotes: 1
Reputation: 2719
See this link and this one too.
This example was taken from the Yii framework wiki:
Extend CWebUser by creating a WebUser.php file under protected/components/WebUser.php
<?php
class WebUser extends CWebUser {
// This is a function that checks the field 'role'
// in the User model to be equal to 1, that means it's admin
// access it by Yii::app()->user->isAdmin()
// If your role is stored in the same class as user you can do this
// otherwise you need to call your role class and return role for user
function isAdmin() {
$user = $this->loadUser(Yii::app()->user->id);
return intval($user->role) == 1;
}
}
Then you need to update your config/main.php:
'components'=>array(
'user'=>array(
'class' => 'WebUser',
),
),
So basically you have to extend CWebUser and add an isAdmin method. In that method you can return the role for the user or an array of roles and if it contains admin return true. Also don't forget to update the config/main.php file to include the extended class in your components. Then you should be able to call isAdmin anywhere in your app like this:
Yii::app()->user->isAdmin()
Hope this helps!
Upvotes: 1
Reputation: 6606
TbNav
knows the attribue visible
for its navigation items. So you should be able to do something like this:
$this->widget('bootstrap.widgets.TbNavBar',array(
'brandLabel'=>TbHtml::b(Yii::app()->name),
'color'=>TbHtml::NAVBAR_COLOR_INVERSE,
'items'=>array(
array(
'class'=>'bootstrap.widgets.TbNav',
'items'=>array(
array('label'=>'Home', 'url'=>array('/site/index')),
array('label'=>'About', 'url'=>array('/site/page', 'view'=>'about')),
array('label'=>'Contact', 'url'=>array('/site/contact')),
array(
'label'=>'Users',
'url'=>array('/user/index'),
'visible'=>(Yii::app()->user->name == 'admin'),
),
),
),
array(
'class'=>'bootstrap.widgets.TbNav',
'htmlOptions'=>array('class'=>'pull-right'),
'items'=>array(
array('label'=>'Login', 'url'=>array('/site/login'), 'visible'=>Yii::app()->user->isGuest),
array('label'=>'Logout ('.Yii::app()->user->name.')', 'url'=>array('/site/logout'), 'visible'=>!Yii::app()->user->isGuest)
),
),
),
));
Upvotes: 0