nnseva / django-access

Django-Access - the application introducing dynamic evaluation-based instance-level (row-level) access rights control for Django
GNU Lesser General Public License v3.0
78 stars 6 forks source link

Django 5.0.6 compatibility #18

Open LokkiDog opened 5 months ago

LokkiDog commented 5 months ago

An error occurs when checking form fields for changes (as far as I understand it).

Django==5.0.6 django-access==0.1.2b2

The error is in the file access/admin.py 498

perm = bool(manager.apply_able(ability, obj.__class__.objects.filter(id=obj.id), request).exists()))

manager.apply_able(ability, obj.__class__.objects.filter(id=obj.id), request) - already gives bool value,exists() - unnecessary

123

Here is the error trace:

Traceback (most recent call last):
  File "C:\Users\izvekov\Projects\cliphar_crm\.venv\Lib\site-packages\django\core\handlers\exception.py", line 55, in inner
    response = get_response(request)
               ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\izvekov\Projects\cliphar_crm\.venv\Lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\izvekov\Projects\cliphar_crm\.venv\Lib\site-packages\django\contrib\admin\options.py", line 716, in wrapper
    return self.admin_site.admin_view(view)(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\izvekov\Projects\cliphar_crm\.venv\Lib\site-packages\django\utils\decorators.py", line 188, in _view_wrapper
    result = _process_exception(request, e)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\izvekov\Projects\cliphar_crm\.venv\Lib\site-packages\django\utils\decorators.py", line 186, in _view_wrapper
    response = view_func(request, *args, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\izvekov\Projects\cliphar_crm\.venv\Lib\site-packages\django\views\decorators\cache.py", line 80, in _view_wrapper
    response = view_func(request, *args, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\izvekov\Projects\cliphar_crm\.venv\Lib\site-packages\django\contrib\admin\sites.py", line 240, in inner
    return view(request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\izvekov\Projects\cliphar_crm\.venv\Lib\site-packages\django\contrib\admin\options.py", line 1948, in change_view
    return self.changeform_view(request, object_id, form_url, extra_context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\izvekov\Projects\cliphar_crm\.venv\Lib\site-packages\django\utils\decorators.py", line 48, in _wrapper
    return bound_method(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\izvekov\Projects\cliphar_crm\.venv\Lib\site-packages\django\utils\decorators.py", line 188, in _view_wrapper
    result = _process_exception(request, e)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\izvekov\Projects\cliphar_crm\.venv\Lib\site-packages\django\utils\decorators.py", line 186, in _view_wrapper
    response = view_func(request, *args, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\izvekov\Projects\cliphar_crm\.venv\Lib\site-packages\django\contrib\admin\options.py", line 1804, in changeform_view
    return self._changeform_view(request, object_id, form_url, extra_context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\izvekov\Projects\cliphar_crm\.venv\Lib\site-packages\django\contrib\admin\options.py", line 1838, in _changeform_view
    fieldsets = self.get_fieldsets(request, obj)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\izvekov\Projects\cliphar_crm\apps\users\admin.py", line 134, in get_fieldsets
    fields = self.get_fields(request, obj=obj)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\izvekov\Projects\cliphar_crm\apps\users\admin.py", line 147, in get_fields
    fields = list(super(AccessUserAdmin, self).get_fields(request, obj)) or []
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\izvekov\Projects\cliphar_crm\.venv\Lib\site-packages\django\contrib\admin\options.py", line 395, in get_fields
    form = self._get_form_for_get_fields(request, obj)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\izvekov\Projects\cliphar_crm\.venv\Lib\site-packages\django\contrib\admin\options.py", line 786, in _get_form_for_get_fields
    return self.get_form(request, obj, fields=None)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\izvekov\Projects\cliphar_crm\.venv\Lib\site-packages\django\contrib\auth\admin.py", line 98, in get_form
    return super().get_form(request, obj, **defaults)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\izvekov\Projects\cliphar_crm\.venv\Lib\site-packages\django\contrib\admin\options.py", line 799, in get_form
    readonly_fields = self.get_readonly_fields(request, obj)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\izvekov\Projects\cliphar_crm\apps\users\admin.py", line 83, in get_readonly_fields
    readonly_fields = super(AccessUserAdmin, self).get_readonly_fields(request, obj) or []
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\izvekov\Projects\cliphar_crm\.venv\Lib\site-packages\access\admin.py", line 241, in get_readonly_fields
    if not self.has_basic_change_permission(request, obj):
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\izvekov\Projects\cliphar_crm\.venv\Lib\site-packages\access\admin.py", line 204, in has_basic_change_permission
    return self._check_request_object_ability(obj, 'changeable', request)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\izvekov\Projects\cliphar_crm\.venv\Lib\site-packages\access\admin.py", line 498, in _check_request_object_ability
    perm = bool(manager.apply_able(ability, obj.__class__.objects.filter(id=obj.id), request).exists())
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'bool' object has no attribute 'exists'
nnseva commented 5 months ago

Actually, the apply_able returns a value from the user-defined functional authorization checks, defined by the ApplyAblePlugin . The user-defined functional check should obviously return queryset in any case.

Contrary, the CheckAblePlugin user-defined functional authorization check should return either False, or dictionary.

Therefore, the most probable problem is your own access profile, where you probably use ApplyAblePlugin with your custom functional check returning boolean, like:

        ApplyAblePlugin(
            changeable=lambda queryset, request: return False
        )

instead of properly defined:

        ApplyAblePlugin(
            changeable=lambda queryset, request: return queryset.none()
        )