-2

I am working on yii2. I have a menu named SIM Management in which there are some sub-menus like shown below.

enter image description here

There are 3 Roles added in the system which are

  1. SIM Manager with role id 6
  2. Production Manager with role id 7
  3. 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

enter image description here

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.

11
  • UserMenu::mainItems() this function only returns your main menu items, i-e SIM Management in this example? How are you fetching child menu items eg SIM List, SIM Issue, where is code for that? Feb 22, 2018 at 11:05
  • @IrfanAshraf Kindly see my updated question
    – Moeez
    Feb 22, 2018 at 11:14
  • Looks like your $menu->getSubMenuItems() is returning all sub menus regardless of allow_access value. You'll have to add similar where condition as you have in mainItems method i-e $query->andWhere("(FIND_IN_SET(". Yii::$app->user->identity->user_role.",allow_access)) <> 0 "); Feb 22, 2018 at 12:50
  • that is correct you need to add the getSubMenuItems() function code i will update my answer accordingly Feb 22, 2018 at 21:10
  • @MuhammadOmerAslam ok I'll be waiting :)
    – Moeez
    Feb 23, 2018 at 3:10

1 Answer 1

1
+50

You do not need to add a check but you have to add the query to use FIND_IN_SET like below in a separate function that calls the subMenuItems() from the UserMenu model.

public function subMenuItems(){
    $query =  self::find();
    $query->where(['=','parent_id', $this->id]);

    if(!Yii::$app->user->isGuest) {          
        $query->andWhere(new \yii\db\Expression('FIND_IN_SET(:user_role,allow_access)<>0'));
        $query->addParams([':user_role'=>Yii::$app->user->identity->user_role]);
        $query->orderBy('display_order desc');
    }

    return $query->all();
}

This will return you the menu items that you need for the current user is allowed. you just need to display it no more checks need to be added.

Although this is not the conventional approach that you have used, you should have created a MenuHelper that should return the Menu items array that could be used inside the Nav,Menu or SideMenu widget provided by Yii2, but hope it helps you out.

37
  • I'll apply it and let you know :)
    – Moeez
    Feb 22, 2018 at 9:31
  • sure take your time. Feb 22, 2018 at 9:32
  • Tried and it's giving me Illegal string offset ':qp1' in E:\xampp\htdocs\inventory-web\vendor\yiisoft\yii2\db\QueryBuilder.php at $params[$phname] = $value
    – Moeez
    Feb 22, 2018 at 9:35
  • OOPS wait a mistak :D Feb 22, 2018 at 9:37
  • $query->addParams(':user_role',Yii::$app->user->identity->user_role); it's giving me method call uses 2 parameters, but signature uses 1 parameters
    – Moeez
    Feb 22, 2018 at 9:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.