Skip to content

[feature] Add class to implement view permissions in DRF #249

@nemesifier

Description

@nemesifier

DRF DjangoModelPermission does not takes into account the view permissions, and hence any user is allowed to view the objects even though they may not have view permission or change permission on that specific object.

We recently dealt with this in openwisp-network-topology, we could use a similar solution, adapted to be more generic:
https://github.com/openwisp/openwisp-network-topology/blob/3c3404748a9ecab65f3f4a389f94d2c75fbde732/openwisp_network_topology/api/views.py#L31-L51.

I think we could extend DjangoModelPermissions and add our logic so that:

  • if user is superuser, any request is allowed
  • if user is not superuser and has view or change permission, GET requests are allowed
  • if user is not superuser and does not have view or change permission, GET requests are not allowed

Here's a few possible tests explained below.

Preconditions:

  • given the operator group does not have view permission to a specific model
  • given the administrator group has change permission to the same model (view permission is omitted, ensure is missing)
  • given two API endpoints for this model: list and detail

Tests:

  • ensure an user of the operator group (not flagged as supersuser) cannot view both endpoints
  • ensure an user of the administrator group (not flagged as supersuser) can view both endpoints
  • add the view permission to the operator group, ensure the user now can view both endpoints but cannot change anything (put or delete)

Metadata

Metadata

Assignees

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions