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.
Related
I'm using Krajee DateTimePicker, which uses Bootstrap DateTimePicker. I'm trying to change the defaultViewDate option through javascript. Unfortanatly i couldn't target the component correctly with js.
The DateTimePicker widget is defined as followed:
echo DatePicker::widget([
'model' => $model,
'attribute' => 'employment_period_from',
'attribute2' => 'employment_period_to',
'options' => ['placeholder' => Yii::t('app', 'From')],
'options2' => ['placeholder' => Yii::t('app', 'To')],
'type' => DatePicker::TYPE_RANGE,
'separator' => Yii::t('app', 'to'),
'form' => $form,
'pluginOptions' => [
'defaultViewDate' => date('2017-01-01'),
'format' => 'yyyy.mm.dd',
'autoclose' => true,
]
]);
I already tried:
$('#datetimepicker').data("DateTimePicker").defaultViewDate('2012-01-01');
$('#datetimepicker').datetimepicker('setDefaultViewDate', '2012-01-01')
with no success; gives me js erros: Uncaught TypeError: $(...).datetimepicker is not a function
So is there a way todo so?
Thanks!
Edit:
added an id to the widget and used the kvDatepicker function
...
'id' => 'datepickerWidget',
...
and now i tried the follwoing
$('#datepickerWidget').kvDatepicker('setDefaultViewDate', '2012-01-01')
which at least doesn't give me an error; still doesn't change anything...
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']]])
]);
?>
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"),
],
],
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']
]
]
]
];
}