Skip to content
Grav 2.0 is officially stable. Read the announcement →
Archive

Check user's permission

Started by Muut Archive 10 years ago · 3 replies · 1184 views
10 years ago

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?

👍 1
10 years ago

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'
];
}

PHP
    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']) %}*
    <li class="{{ (location == item.route) ? 'selected' : '' }}">
    <a href="{{ base_url_relative }}/{{ item.route }}">
    <i class="fa fa-fw {{ item.icon }}"></i>
    <em>{{ label|tu }}</em>
    </a>
    </li>
    {% endif %}
    {% endfor %}
    {% endif %}

Hope this will be useful for someone else!

10 years ago

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

10 years ago

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

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 2001 9 years ago
Archive · by Muut Archive, 9 years ago
2 1342 9 years ago
Archive · by Muut Archive, 9 years ago
2 4550 9 years ago
Archive · by Muut Archive, 9 years ago
1 3403 9 years ago
Archive · by Muut Archive, 9 years ago
3 1494 9 years ago