Check user's permission

Hi there,
I’m developing a new plugin for Grav and I need to display menu elements on admin, depending on permission.

I’ve got my menu item like this:
if ( $this->isAdmin() )
{
$this->enable([
‘onAdminMenu’ => [‘onAdminMenu’, 0]
]);
}
public function onAdminMenu()
{
$this->grav[‘twig’]->plugins_hooked_nav[‘New Menu Item’] = [
‘route’ => $this->route.’-admin’,
‘icon’ => ‘fa-space-shuttle’
];
}

How can I check user’s permissions to render a different menu item for each user?

Example:
For user with configuration: ‘false’, display “Menu item 1” and for user with configuration: ‘true’ display “Menu item 1” and “Menu item 2”

Alternative:
How can I check if the user has a specific name?

Ok, got it!

In user account yaml add:

your_config_name:
config: ‘true’

and, under admin, add rights to your page:
your_plugin-page1: 'true’
your_plugin-page2: 'true’
your_plugin-page3: 'true’
your_plugin-page4: ‘true’

Then, in plugin’s class:

public function onAdminMenu()
{
if ( $this->grav[‘user’]->authorize(‘your_config_name.config’) == 1 ) {
$this->grav[‘twig’]->plugins_hooked_nav[‘PLUGIN_TRANSLATION.STRING’] = [
‘route’ => $this->route.’-admin’,
‘icon’ => ‘fa-space-shuttle’
];
}

    if ( $this->grav['user']->authorize('client.dash') == 1 ) {
        $this->grav['twig']->plugins_hooked_nav['PLUGIN_TRANSLATION.STRING'] = [
            'route' => $this->route.'-page1',
            'icon'  => 'fa-dashboard'
        ];
        $this->grav['twig']->plugins_hooked_nav['PLUGIN_TRANSLATION.STRING'] = [
            'route' => $this->route.'-page2',
            'icon'  => 'fa-ambulance'
        ];
        $this->grav['twig']->plugins_hooked_nav['PLUGIN_TRANSLATION.STRING'] = [
            'route' => $this->route.'-page3',
            'icon'  => 'fa-credit-card-alt'
        ];
        $this->grav['twig']->plugins_hooked_nav['PLUGIN_TRANSLATION.STRING'] = [
            'route' => $this->route.'-page4',
            'icon'  => 'fa-briefcase'
        ];
    }
}

where
$this->route = ‘your_plugin’;

Because of this (in nav.html.twig, Admin plugin):

{% if grav.twig.plugins_hooked_nav %}
{% for label, item in grav.twig.plugins_hooked_nav %}
* {% if authorize([‘admin.’ ~ item.route, ‘admin.super’]) %}*




  • {{ label|tu }}


  • {% endif %}
    {% endfor %}
    {% endif %}

    Hope this will be useful for someone else!

    Can you wrap the code snippets in triple backticks, or triple dashes, so they are rendered correctly by the forum? Thanks!

    Sorry, I tried to edit post and answer but seems like I can’t anymore.