I am working on yii2
. I have a menu named SIM Management
in which there are some sub-menus like shown below.
There are 3 Roles
added in the system which are
- SIM Manager with role id
6
- Production Manager with role id
7
- SIM Tester with role id
8
SIM Manager has all access to the module. Now I want to give access of sub-menus to the other users.
Production Manager will just view SIM Return
and Meter To SIM Mapping
. SIM Tester will just view SIM Return
.
There are multiple modules in the system which are handled according to the user role. This handling is done via Databases. Below is the snap of the table
Here you can see there is SIM Management
and in allow access
i have added 4 role id's while 1
is the admin. Also in front of SIM Return
and Mapping
, the roles are defined. But as the parent id 62
is same for all of them so that's why all the module is visible to others.
In my main.php
I am calling a function named menuItems
.
<?php
$menu_items = \app\models\UserMenu::mainItems();
?>
The function detail is
public static function mainItems(){
$query = UserMenu::find();
$query->where(['parent_id' => 0]);
if(!Yii::$app->user->isGuest) {
$query->andWhere("(FIND_IN_SET(". Yii::$app->user->identity->user_role.",allow_access)) <> 0 ");
$query->orderBy('display_order desc');
}
return $query->all();
}
The roles are defined in the Roles Model
const ROLE_SUPER_ADMIN = 1;
const ROLE_ADMIN = 2;
const ROLE_TEAM_LEAD = 3;
const ROLE_FIELD_WORKER = 4;
const ROLE_INVENTORY_MANAGEMENT = 5;
const ROLE_SIM_MANAGER = 6;
const ROLE_PRODUCTION_MANAGER = 7;
const ROLE_SIM_TESTER = 8;
Now, here I want to set a query to check the user role and from that user role it will allow the menus to be viewed
In above function, a check can be placed like below
if (Yii::$app->user->identity->user_role == Roles::ROLE_PRODUCTION_MANAGER)
{
// here I want to set a query that will only send the specific menu Items.
}
Update 1
Below is my main.php
code. Here all the menus and sub-menus resides
<?php
$_a = "/" . Yii::$app->controller->id . "/" . Yii::$app->controller->action->id;
?>
<?php
$menu_items = \app\models\UserMenu::mainItems();
?>
<!-- Sidebar Menu -->
<ul class="sidebar-menu">
<li><a href="<?= Yii::$app->request->baseUrl ?>"><i class="fa fa-dashboard"></i><span> Dashboard</span></a>
</li>
<li class="header">SURVEY</li>
<?php if(!Yii::$app->user->isGuest){ ?>
<?php foreach ($menu_items as $menu) {
$arr = explode("/",$menu->link);
if(!Rbac::allowAccess($arr[1], $arr[2])){
continue;
}
?>
<li class="<?= $menu->isMainActive(Yii::$app->controller->id) ? 'active' : '' ?>"><a href="#">
<i class="<?= $menu->fa_icon ?>"></i> <span><?= $menu->title ?></span>
<span class="pull-right-container"><i class="fa fa-angle-left pull-right"></i>
</a><?php if ($menu->getHasSubMenu()) { ?>
<ul class="treeview-menu">
<?php foreach ($menu->getSubMenuItems() as $sub) {
$arrs = explode("/", $sub->link);
if (!Rbac::allowAccess($arrs[1], $arr[2])) {
// continue;
}
?>
<li class="<?= $sub->isSubActive($_a) ? 'active' : '' ?>"><a
href="<?= Yii::$app->request->baseUrl ?><?= $sub->link ?>"><?= $sub->title ?></a>
</li>
<?php } ?>
</ul>
<?php } ?>
</li>
<?php if($menu->break==1) {?>
<li class="header">INSTALLATION</li>
<?php } ?>
<?php } ?>
<?php } ?>
Any help will be highly Appreciated.