How do I configure or customize my Menu where in, for example, if I am an admin user, I can see everything on my navigation bar, like, in my case, the Users (list of Users where I can create, update, or delete one), Stores (just like Users, this is where I can configure a specific store), Transactions and then the Logout button. But when an ordinary user/staff logs in, the only thing he/she will see is the Transactions menu and a Logout button as well.
Please help.
Edit:
Here is my menu rendering code:
<?php
echo Menu::widget([
'options' => ['id' => "nav-mobile", 'class' => 'right side-nav'],
'items' => [
['label' => 'Home', 'url' => ['/site/index']],
['label' => 'About', 'url' => ['/site/about']],
['label' => 'Contact', 'url' => ['/site/contact']],
Yii::$app->user->isGuest ?
['label' => 'Login', 'url' => ['/site/login']] :
['label' => 'Logout (' . Yii::$app->user->identity->username . ')',
'url' => ['/site/logout'],
'linkOptions' => ['data-method' => 'post']
],
],
]);
?>
Each item in items array has visible property. Pass expression returning boolean value there.
'items' => [
// Show users section for administrators only
[
'label' => 'Users',
'url' => ['/users/index'],
'visible' => !Yii::$app->user->isGuest && Yii::$app->user->identity->user_type == User::USER_TYPE_SUPER_ADMIN,
],
],
Yoy can also put this condition in additional model method to avoid repeating, something like isAdmin() in User model.
Official documentation:
$items
I think this function is also usefully here
\Yii::$app->user->can("BackendUsersIndex")
'items' => [
// Show users section for administrators only
[
'label' => 'Users',
'url' => ['/users/index'],
'visible' => \Yii::$app->user->can("BackendUsersIndex"),
],
],
Related
I am quite new to Yii 2 and I am trying to hide some navbar items from users that are not admins. However, I somehow get an error stating that the User model has no such property called admin.
I am confused since the model has an attribute called admin with a boolean value. I have been searching on different websites but I can't seem to find a solution so far. Whenever I login as a User, no matter if it's an admin or not, the navbar items are shown.
Below is what I am trying to do in the main.php
<header>
<?php
NavBar::begin([
'brandLabel' => Yii::$app->name,
'brandUrl' => Yii::$app->homeUrl,
'options' => [
'class' => 'navbar navbar-expand-md navbar-dark bg-dark fixed-top',
],
]);
echo Nav::widget([
'options' => ['class' => 'navbar-nav'],
'items' => [
['label' => 'Home', 'url' => ['/site/index']],
['label' => 'About', 'url' => ['/site/about']],
['label' => 'Contact', 'url' => ['/site/contact']],
['label' => 'Team', 'url' => ['/team/index'], 'visible' => Yii::$app->user->admin ? true : false],
['label' => 'User', 'url' => ['/user/index'], 'visible' => Yii::$app->user->admin ? true : false],
Yii::$app->user->isGuest ? (
['label' => 'Login', 'url' => ['/site/login']]
) : (
'<li>'
. Html::beginForm(['/site/logout'], 'post', ['class' => 'form-inline'])
. Html::submitButton(
'Logout (' . Yii::$app->user->identity->username . ')',
['class' => 'btn btn-link logout']
)
. Html::endForm()
. '</li>'
)
],
]);
NavBar::end();
?>
</header>
I am not understanding why Yii::$app->user->adminis not working.
I have tried Yii::$app->user->getIdentity('admin') as well but it's not working either. As I said above, the User model does have an attribute called admin with a boolen value.
Any help is greatly appreciated.
That's because Yii::$app->user is not instance of your user model. It's instance of yii\web\User component.
If you've followed the guide, you've made your User model implement yii\web\IdentityInterface and set the model class as identityClass then you can access the user model as Yii::$app->user->identity or Yii::$app->user->getIdentity().
So, to access the admin property of user model you have to do
Yii::$app->user->identity->admin or Yii::$app->user->getIdentity()->admin.
Is there someone who can help me?
I make a dropdown on of my layout tab menu.
this my dropdown.
[
'label' => '(' . Yii::$app->user->identity->username . ')',
'items' => [
['label' => 'Change Password', 'url' => ['/site/changepassword']],
['label' => 'User Setting', 'url' => ['/user']],
['label' => 'test', 'url' => ['/leave-record/leave']],
'<li class="dropdown-header"></li>',
['label' => 'Logout', 'url' => ['/site/logout'],
'linkOptions' => ['data-method' => 'post']],
],
],
I've 3 user type in user model.
1. Master Admin
2. Normal Admin
3. Normal User
if Master Admin login, he can access all rows in dropdown. Then if normal admin login row "User Setting" become disable.
for an item you can use the visible attribute assign a proper condition
eg:
[
['label' => 'Change Password', 'url' => ['/site/changepassword']],
[
'label' => 'User Setting',
'url' => ['/user'],
'visible' => Yii::$app->User->can('masterAdmin'),
],
],
for two user type you could use a $check
$check = ((Yii::$app->User->can('masterAdmin') || Yii::$app->User->can('admin')) ? TRUE : FALSE;
.
...
'label' => 'User Setting',
'url' => ['/user'],
'visible' =>$check,
I just want to display menus from database in yii2 advanced template frontend. Also i have static menus. I am using menu widget
Here is my code
<?php
echo Menu::widget([
'options' => ['class' => 'about_content'],
'items' => CMS::getCMSPages(),
]);
?>
Here CMS::getCMSPages() will get the menus from database. And also i have static menu. So i added into the menu widget like this
<?php
echo Menu::widget([
'options' => ['class' => 'about_content'],
'items' => [[CMS::getCMSPages()],
['label' => 'contact', 'url' => ['site/index']]
]
]);
?>
But this is not working. Someone help me guys
CMS::getCMSPages() method should return properly prepared array of items. Something like this:
[
['label' => 'Home', 'url' => ['site/index']],
['label' => 'Products', 'url' => ['product/index'],
]
Also you should merge items array:
<?php
echo Menu::widget([
'options' => ['class' => 'about_content'],
'items' => array_merge(CMS::getCMSPages(), [['label' => 'contact', 'url' => ['site/index']]])
]);
?>
I want to disable certain textfields and dropdown lists to prevent user from changing its values. But whenever I try to, it doesn't collect/get the data of that specific disabled textfield or dropdown list.
Here's my view where I display my dropdown lists. It's inside a for loop:
echo $form->field($model1[$i], 'earning_item_id')->widget(Select2::classname(), [
'data' => $earningslistData,
'options' => ['placeholder' => '', 'prevOptionID' => $model1[$i]->earning_item_id, 'prevOptionName' => $earningslistData[$model1[$i]->earning_item_id],
"name" => "EarningDetails[".$i."][earning_item_id]", "row_count1" => $i],
//'disabled' => true,
'pluginOptions' => [
'allowClear' => true,
'label' => false
]
]);
Here's how it looks like without disabling them:
Then, when I save it, it looks like this:
But, when I disable the dropdown lists, it will give me this:
I think the Full Name comes from my model but I don't know why:
public function getFullName()
{
return $this->user ? $this->user->fname . ' ' . $this->user->lname : 'Full Name';
}
It goes the same when I disable a textfield:
echo $form->field($model, 'user_id')->widget(Select2::classname(), [
'data' => $listData,
'options' => ['placeholder' => 'Select a Staff'],
'disabled' => true,
'pluginOptions' => [
'allowClear' => true,
],
])->label('Employee Name');
I am using Kartik widgets for my form fields.
Is there a way to fix this? Please tell me how.
EDIT
Thanks to the commenters below I found out the difference between disabled and readonly. Since it's a dropdown list, here's what I did:
echo $form->field($model, 'user_id')->widget(Select2::classname(), [
'data' => $listData,
'options' => ['placeholder' => 'Select a Staff', ],
'pluginOptions' => [
'allowClear' => true,
],
])->label('Employee Name');
echo $form->field($model, 'user_id')->widget(Select2::classname(), [
'data' => $listData,
'options' => ['placeholder' => 'Select a Staff', 'style' => 'display:none'],
'pluginOptions' => [
'allowClear' => true,
],
])->label('');
Disabled html form field will not submit, the problem is not with yii itself. The solution in this case is to have 2 copies of the same field, one as disabled as you have already included and the other one hidden with the same value as below after the original one.
echo $form->field($model1[$i], 'earning_item_id')->hiddenInput()->label('');
I have been trying to get familiar with octobercms, but I've come across an issue I can't seem to resolve. I have a backend controller setup with views etc. Everything works, except that the sidebar isn't loading. Also the tab isn't getting the active state.
http://gyazo.com/25e019c1db34d5807c05ebb4b3277ac7
It should look something like this:
http://gyazo.com/c71a1e1dec7c1e6b81136b313b32da47
Here is a gist with my code: https://gist.github.com/muuknl/fedb8434219c7dbe5d04
If I forgot to give certain information, please let me know and thanks in advance for the help.
here is simple solution
in controller you need to write
BackendMenu::setContext('Archetypics.Team', 'website', 'team');
refer this https://octobercms.com/docs/backend/controllers-views-ajax#navigation-context
BackendMenu::setContext('Author.Plugin name', 'Menu code', 'Sub menu code');
you need to write same thing what you have written in plugin.php in registerNavigation() function
public function registerNavigation()
{
return [
// menu code
'website' => [
'label' => 'Website',
'url' => Backend::url('muukrls/archetypics/team'),
'icon' => 'icon-pencil',
'permissions' => ['archetypics.*'],
'order' => 500,
'sideMenu' => [
'home' => [
'label' => 'Homepage',
'icon' => 'icon-copy',
'url' => Backend::url('muukrls/archetypics/home'),
'permissions' => ['archetypics.home_access'],
],
'about' => [
'label' => 'About Page',
'icon' => 'icon-list-ul',
'url' => Backend::url('muukrls/archetypics/about'),
'permissions' => ['archetypics.about_access'],
],
// sub menu code
'team' => [
'label' => 'Team Members',
'icon' => 'icon-users',
'url' => Backend::url('muukrls/archetypics/team'),
'permissions' => ['archetypics.team_access']
]
]
]
];
}