Search code examples
phpyii2adminrbac

yii2 rbac's logout is not appearing


I installed yii2 2 admin, I was making rbac, I followed a good video tutorial. everyhting is working well but only logout in my main.php not appearing. here is my main.php, how could I fix it? i tried a lot to find my mistake, but even my controller looks ok, however, without filte, my logout is working well, it is apprearing:

 echo Nav::widget([
        'options' => ['class' => 'navbar-nav navbar-right'],
        'items' => Helper::filter($menuItems),

    ]);


<?php

/* @var $this \yii\web\View */
/* @var $content string */

use yii\helpers\Html;
use yii\bootstrap\Nav;
use yii\bootstrap\NavBar;
use yii\widgets\Breadcrumbs;
use app\assets\AppAsset;
use mdm\admin\components\Helper;

AppAsset::register($this);
?>
<?php $this->beginPage() ?>
<!DOCTYPE html>
<html lang="<?= Yii::$app->language ?>">
<head>
    <meta charset="<?= Yii::$app->charset ?>">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <?= Html::csrfMetaTags() ?>
    <title><?= Html::encode($this->title) ?></title>
    <?php $this->head() ?>
</head>
<body>
<?php $this->beginBody() ?>

<div class="wrap">


    <?php

$menuItems = [
            ['label' => 'bosh sahifa', 'url' => ['/admin/default/index']],
            ['label' => 'postlarimiz', 'url' => ['/admin/post/index']],
            ['label' => 'user management', 'url' => ['/rbac/default/index']],
             Yii::$app->user->isGuest ? (
                ['label' => 'Login', 'url' => ['/site/login']]
            ) : (
                '<li>'
                . Html::beginForm(['/site/logout'], 'post')
                . Html::submitButton(
                    'Logout (' . Yii::$app->user->identity->username . ')',
                    ['class' => 'btn btn-link logout']
                )
                . Html::endForm()
                . '</li>'
            )

        ];

    NavBar::begin([
        'brandLabel' => 'My Company',
        'brandUrl' => Yii::$app->homeUrl, 
        'options' => [
            'class' => 'navbar-inverse navbar-fixed-top',
        ],
    ]);
    echo Nav::widget([
        'options' => ['class' => 'navbar-nav navbar-right'],
        'items' => Helper::filter($menuItems),

    ]);
    NavBar::end();
    ?>

    <div class="container">
        <?= Breadcrumbs::widget([
            'links' => isset($this->params['breadcrumbs']) ? $this->params['breadcrumbs'] : [],
        ]) ?>
        <?= $content ?>
    </div>
</div>

<footer class="footer">
    <div class="container">

    </div>
</footer>

<?php $this->endBody() ?>
</body>
</html>
<?php $this->endPage() ?>

Solution

  • you're using \mdm\admin\components\Helper::filter($menuItems) to remove menu items that the current user cannot access (this includes login and logout actions), so you either need to add rbac exception for those actions

    'as access'           => [
        'class'        => 'mdm\admin\components\AccessControl',
        'allowActions' => [
            'site/logout', // or 'site/*' if you prefer
        ]
    ],
    

    or create a Guest role in the admin module that has access to those routes
    see docs for example config