Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hide objects from changelist in admin if user has no view_permission #132

Open
dzerrenner opened this issue Dec 22, 2020 · 1 comment
Open

Comments

@dzerrenner
Copy link

dzerrenner commented Dec 22, 2020

Hi, i'm not sure if i understand the docs correctly, but i had the expectation that all items which have no view permission from the user would be hidden in django-admin's changelist. That is not the case in my current setup.

I did the following:

  • use RulesModel instead django's Model class.
  • added rules_permissions to the Model's Meta with a perdicate for each of add, view, change and delete according to my needs.
  • use ObjectPermissionsModelAdmin instead of ModelAdmin in the admin.
  • Checking with User.has_perm works as intended

When displaying the changelist, these predicates get called with obj=None.

I got it working by overriding the get_queryset method of ObjectPermissionsModelAdmin:

    def get_queryset(self, request):
        qs = super().get_queryset(request)
        if request.user.is_superuser:
            return qs

        # remove all items from the queryset if the user has no view_permission
        item_pks_to_remove = [item.pk for item in qs if not self.has_view_permission(request, item)]
        qs = qs.exclude(pk__in=item_pks_to_remove)
        return qs

Here's my question: I don't have any clue if this has some unforseen consequences that could break something. Is there a better way to do it? Or is my setup wrong? Maybe i've missed something to exclude the items without view_permission. Looking at the sources, I'm pretty confident, that the items are not hidden by default, but not sure.

@keyvanm
Copy link

keyvanm commented Feb 3, 2021

Was looking for something like this. It is perhaps too slow to check for permissions per object in the queryset. What if you have millions of rows of data for a model?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants