RBAC and navbar, how?

Hello,

Does anyone knows how to hide or show links in the navbar according to the user role please?

Thank you,

Ben

Hi,

there is no built-in feature to do that, you should build dymamically the navbar and only add those items depending on permission & roles of the curent user.

ciao

B)

Hi Raoul thank you.

Ben

The default main layout shows how to do this by building the nav items array and then adding it to the NavBar, that way you can use simple logic like if ( Yii::$app->user->can(‘permission_admin’) ) to add items to the array (or not).


NavBar::begin([

                'brandLabel' => 'My Company',

                'brandUrl' => Yii::$app->homeUrl,

                'options' => [

                    'class' => 'navbar-inverse navbar-fixed-top',

                ],

            ]);

            

            $items = [

                    ['label' => 'Home', 'url' => ['/site/index']],

                    ['label' => 'About', 'url' => ['/site/about']],

                    ['label' => 'Contact', 'url' => ['/site/contact']],

                    ['label' => 'Books', 'url' => ['/book/index']],

                     ['label' => 'Authors', 'url' => ['/author/index']],

                    Yii::$app->user->isGuest ?

                        ['label' => 'Login', 'url' => ['/site/login']] :

                        ['label' => 'Logout (' . Yii::$app->user->identity->username . ')',

                            'url' => ['/site/logout'],

                            'linkOptions' => ['data-method' => 'post']],

                ];

            if ( Yii::$app->user->can('permission_admin') )

                $items[] = ['label' => 'Permissions', 'url' => ['/admin/assignment']];

            

            echo Nav::widget([

                'options' => ['class' => 'navbar-nav navbar-right'],

                'items' => $items,

            ]);

            NavBar::end();

1 Like

aThank you for the reply,

this seems to be a neat way to do it thank you for sharing, just a question please, when you type this:




   if ( Yii::$app->user->can('permission_admin') )

                $items[] = ['label' => 'Permissions', 'url' => ['/admin/assignment']];



if my role is "admin" do I still need to type "permission_admin" ? or just "admin"?

Also when I add your code as below I get an error:

[php]

        NavBar::begin([


            'brandLabel' => 'Kollox Forum',


            'brandUrl' => Yii::$app->homeUrl,


            'options' => [


                'class' => 'navbar navbar-fixed-top',


            ],


        ]);


        $menuItems = [


            ['label' => 'Home', 'url' => ['/site/index']],


            ['label' => 'About', 'url' => ['/site/about']],


            ['label' => 'Contact', 'url' => ['/site/contact']],


        ];


        if (Yii::$app->user->isGuest) {


            $menuItems[] = ['label' => 'Signup', 'url' => ['/site/signup']];


            $menuItems[] = ['label' => 'Login', 'url' => ['/site/login']];


        } else {


            $menuItems[] = [


                'label' => 'Logout (' . Yii::$app->user->identity->username . ')',


                'url' => ['/site/logout'],


                'linkOptions' => ['data-method' => 'post']


            ];


        }


         if ( Yii::$app->user->can('admin') )


         $menuItems[] = ['label' => 'Permissions', 'url' => ['/book']]; 


        echo Nav::widget([


            'options' => ['class' => 'navbar-nav navbar-right'],


            'items' => $menuItems,


        ]);


        NavBar::end();

#####################ERROR#######################

PHP Fatal Error – yii\base\ErrorException

Call to a member function checkAccess() on null

  1. in C:\xampp\htdocs\forum\vendor\yiisoft\yii2\web\User.php at line 662

653654655656657658659660661662663664665666667668669 * caching is effective only within the same request and only works when $params = [].

 * @return boolean whether the user can perform the operation as specified by the given permission.


 */


public function can($permissionName, $params = [], $allowCaching = true)


{


    $auth = Yii::$app->getAuthManager();


    if ($allowCaching && empty($params) && isset($this->_access[$permissionName])) {


        return $this->_access[$permissionName];


    }


    $access = $auth->checkAccess($this->getId(), $permissionName, $params);


    if ($allowCaching && empty($params)) {


        $this->_access[$permissionName] = $access;


    }





    return $access;


}

}

  1. yii\base\ErrorHandler::handleFatalError()

[/PHP]

Thank you,

Ben

You need to configure your auth correctly in the config to be, for instance, DbAuthManager, that will get rid of the error of auth being null.

The name is just the name of the role or permission. Mine is called "permission_admin", so you can just use "admin" if you want.