django-nested-inline icon indicating copy to clipboard operation
django-nested-inline copied to clipboard

[ISSUE] 403:Forbidden instead of Read-Only View when has_change_permission() returns False

Open gascarcella opened this issue 5 years ago • 6 comments

When has_change_permission() returns Falseit should return a Read-Only view, I'm getting 403:Forbidden instead.

Example code

class SomeAdmin(NestedModelAdmin):
    def has_change_permission(self, request, obj=None):
        return False

Expected Behavior Get Read-only view Result 403:Forbidden

gascarcella avatar Feb 19 '20 16:02 gascarcella

Here is the validation in NestedModelAdmin:

    @csrf_protect_m
    @transaction.atomic
    def change_view(self, request, object_id, form_url='', extra_context=None):
        "The 'change' admin view for this model."
        ...
        if not self.has_change_permission(request, obj):
            raise PermissionDenied

Here is the Django admin original check

            if request.method == 'POST':
                if not self.has_change_permission(request, obj):
                    raise PermissionDenied
            else:
                if not self.has_view_or_change_permission(request, obj):
                    raise PermissionDenied

Is read-only view disabled for NestedModelAdmin?

gascarcella avatar Feb 20 '20 13:02 gascarcella

Thanks for the report. I agree with you that it should return a read-only view instead of a 403, I will look into this soon

OskarPersson avatar Feb 20 '20 13:02 OskarPersson

I tried skipping that validation in NestedModelAdmin, and it returns a ChangeView instead of ReadOnlyView

gascarcella avatar Feb 20 '20 15:02 gascarcella

Please check if #110 fixes this issue :)

OskarPersson avatar Feb 20 '20 17:02 OskarPersson

It does fix the read-only view for the parent object. But all the inlines are editable (they should be read-only too, as in original Django Admin).

Screenshot of test result: image

Thanks for you quick answer btw!

NestedModelAdmin fits excelent for a new-starting project, but the whole project aims to use both read-only and change views, i'm glad you helped out fixing it

EDIT:

Here is the excepted result, using django.contrib.admin.ModelAdmin: image

EDIT 2: I forgot to mention, that i've tested with NestedStackedInline and also with default admin.StackedInline during the test (Both given the same result)

gascarcella avatar Feb 20 '20 19:02 gascarcella

I've updated the PR, please try it again. We should probably move the conversation there instead :)

OskarPersson avatar Feb 20 '20 21:02 OskarPersson