Skip to content

Conversation

@NoumbissiValere
Copy link
Contributor

@NoumbissiValere NoumbissiValere commented Jul 25, 2020

Fixed issue by making sure users can access just the models
they are permitted to access.

Fixes #103
Closes #104

@coveralls
Copy link

coveralls commented Jul 25, 2020

Coverage Status

Coverage increased (+0.004%) to 99.471% when pulling 0af49a0 on issue-103-menu-not-respect-permissions into b1488f4 on master.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.3%) to 99.211% when pulling 971e935 on issue-103-menu-not-respect-permissions into b1488f4 on master.

label = item.get('label', model_class._meta.verbose_name_plural)
model_admin = site._registry[model_class]
if not request or model_admin.has_module_permission(request):
if not request or (permitted_models and model.lower() in permitted_models):
Copy link
Member

@nemesifier nemesifier Jul 26, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is fragile, there could be two models using the same name but in different apps.

This would be better and more elegant:

request.user.has_perm(item['model'])

But it generates one query for every request, I think we can do the following: openwisp/openwisp-users#153 (review)

@NoumbissiValere NoumbissiValere force-pushed the issue-103-menu-not-respect-permissions branch from 971e935 to e85f820 Compare July 27, 2020 20:55
@NoumbissiValere NoumbissiValere force-pushed the issue-103-menu-not-respect-permissions branch 3 times, most recently from ce09cfb to 9c294a8 Compare July 29, 2020 06:24
@okraits
Copy link
Member

okraits commented Jul 29, 2020

@NoumbissiValere What about #104?

@NoumbissiValere
Copy link
Contributor Author

@okraits the first commit on this PR is #104. That was the solution I thought of since I couldn't push to your Branch

@okraits
Copy link
Member

okraits commented Jul 29, 2020

@okraits the first commit on this PR is #104. That was the solution I thought of since I couldn't push to your Branch

I'm sorry, I didn't see that! Thank you for clarifying!

Copy link
Member

@nemesifier nemesifier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I forgot to send this review

self.assertContains(response, 'class="shelf"')

def test_can_see_unpermitted_projects(self):
Permission.objects.filter(codename__endswith='shelf').delete()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

superuser must always be allowed, this test is wrong. Keep the superuser test but change it to expect the menu item is shown.
Add another test for a non superuser which does not have the permission.

Use good names please.

test_superuser_always_sees_menu_items

test_operator_cant_see_menu_item

or similar but self explanatory

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. Even though i thought we are focusing on the fact that if a user have a permission on a module he should see its menu item, otherwise he doesn't see it irrespective of who he is.

continue
if not request or has_permission:
menu.append({'url': url, 'label': label, 'class': model.lower()})
return menu
Copy link
Member

@nemesifier nemesifier Jul 28, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

too complex. BTW, it breaks the UI for superusers.

I tried this which seems to work:

def build_menu(request):
    default_items = getattr(settings, 'OPENWISP_DEFAULT_ADMIN_MENU_ITEMS', [])
    custom_items = getattr(settings, 'OPENWISP_ADMIN_MENU_ITEMS', [])
    items = custom_items or default_items
    menu = []
    # loop over each item to build the menu
    # and check user has permission to see each item
    for item in items:
        app_label, model = item['model'].split('.')
        model_class = registry.apps.get_model(app_label, model)
        model_label = model.lower()
        url = reverse('admin:{}_{}_changelist'.format(app_label, model_label))
        label = item.get('label', model_class._meta.verbose_name_plural)
        view_perm = f'{app_label}.view_{model_label}'
        change_perm = f'{app_label}.change_{model_label}'
        user = request.user
        # use cached helper from openwisp-users if available
        has_permission_method = user.has_permission if hasattr(user, 'has_permission') else user.has_perm
        if has_permission_method(view_perm) or has_permission_method(change_perm):
            menu.append({'url': url, 'label': label, 'class': model_label})
    return menu

Notice that I made the request param mandatory, I don't remember why I made it optional, maybe to ease testing, but better remove that.

Fixed issue by making sure users can access just the models
they are permitted to access.

Fixes #103
@NoumbissiValere NoumbissiValere force-pushed the issue-103-menu-not-respect-permissions branch from 9c294a8 to 43bb49e Compare July 29, 2020 17:12
@NoumbissiValere NoumbissiValere self-assigned this Aug 13, 2020
@nemesifier nemesifier added the bug label Aug 13, 2020
Copy link
Member

@nemesifier nemesifier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work 👍

@nemesifier nemesifier merged commit 1143fde into master Aug 13, 2020
@nemesifier nemesifier deleted the issue-103-menu-not-respect-permissions branch August 13, 2020 22:28
pushpitkamboj pushed a commit to pushpitkamboj/openwisp-utils that referenced this pull request Jan 27, 2026
…penwisp#123

- Added package type detection for npm, docker, ansible, and openwrt-agents
- Enhanced version parsing for all package types
- Updated bump_version() to handle all package type version formats
- Added 25 new tests for non-Python packages

Fixes openwisp#522
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[admin-theme] menu doesn't respect assigned permissions

4 participants