NarūnasK
NarūnasK

Reputation: 4950

In the Wagtail how to disable certain admin menu items for the certain users?

For the group of users I want to disable some menu items. I thought I will use the following:

from wagtail.contrib.modeladmin.options import ModelAdmin as WModelAdmin

class WPartnerAdmin(WModelAdmin):
    ...
    def get_menu_item(self, order=None):
        menu_item = super().get_menu_item(order=order)
        # if (user_discrimination_logic):
        #    menu_item.is_shown = lambda *a: False
        return menu_item

But it seems that I don’t have access to the request object in the Wagtail ModelAdmin, therefore don’t know how to extract the user data. Is there a way?

Upvotes: 2

Views: 2357

Answers (3)

hunterbunter
hunterbunter

Reputation: 113

Wagtail's built in permission system stops the user from viewing the actual modeladmin objects, but the available options still show up in the menu even for groups without permission to view them. When they try to view the objects, people without view permission are sent to the login screen and told they have insufficient permission.

If you want to hide admin menu items for particular users that are all part of a specific group, or even multiple groups, you can do it using wagtail's construct_main_menu hook. Add a wagtail_hooks.py file in the app folder. Menu items are edited in place.

To remove two menu items called "secrets1" and "secrets2" for users that are in any of "regular_user" or "bad_user" groups, you could do something like this:

    from wagtail.core import hooks

    @hooks.register('construct_main_menu')
    def hide_explorer_items_from_users(request, menu_items):
        if request.user.groups.filter(name__in=["regular_user", "bad_user"]):
            menu_items[:] = [
                item for item in menu_items if item.name not in ['secrets1', 'secrets2']
            ]

This is fine for removing items, but if plan on adding menu items this way, Wagtail's Hooks Documentation warns you that menu items added this way will be missing associated javascript includes, so you should use the @register_admin_menu_item hook for adding items instead.

Upvotes: 2

rafaponieman
rafaponieman

Reputation: 1630

You can use Wagtail's Hooks functionality, particularly the construct_main_menu hook:

Create a wagtail_hooks.py file in your corresponding application, with something like the following (from the Wagtail Docs):

from wagtail.core import hooks

@hooks.register('construct_main_menu')
def hide_explorer_menu_item_from_frank(request, menu_items):
  if request.user.username == 'frank':
    menu_items[:] = [item for item in menu_items if item.name != 'explorer']

Upvotes: 4

gasman
gasman

Reputation: 25227

For most purposes, this can be done without any code changes, through the permissions system.

Under Settings -> Groups, define a group with permissions over the objects they should be able to edit (such as pages, images and documents), and ensure the 'Can access Wagtail admin' permission is checked. Then, in Settings -> Users, go to the Roles tab for each user and add them to that group (and ensure Administrator is unchecked). Any menu items that the user doesn't have permission over will be hidden.

Upvotes: -1

Related Questions