Skip to main content

drf-guard

Create flexible and simple to use access rules for Django REST Framework(DRF). Works with both class based DRF permissions, Django permissions and Django groups. This library allows you to build complex access rules in a very simple way, it allows you to combine permissions and groups with logical operators.

Have you ever had multiple permissions or groups and wanted to be able to do something like below to your endpoint?.

# Check if user has certain permissions with `and`, `or` & `not` operators
permissions: (IsAdmin Or (IsObjectOwner And IsAllowedToEdit))

Or

# Evaluate if user in certain groups with `and`, `or` & `not` operators
groups: ('admin' Or 'client' And Not 'seller')

Well you are not alone, this library allows you to do that with And, Or & Not operators to each endpoint however you want regardless whether you are using class based DRF permissions, Django permissions or Django grops, it can deal with all those.

Requirements

  • Python >= 3.5
  • Django >= 1.11
  • Django REST Framework >= 3.5

Installing

pip install drf-guard

Getting started

Using drf-guard is very simple, below is an example

# views.py

# Import operators & permissions from drf_guard
from drf_guard.operators import And, Or, Not
from drf_guard.permissions import HasRequiredGroups, HasRequiredPermissions


class UserViewSet(viewsets.ModelViewSet):
    queryset = User.objects.all()
    serializer_class = UserSerializer

    # Use drf_guard permissions here
    permission_classes = (HasRequiredGroups, HasRequiredPermissions)

    # Now define access rules for your API endpoint with groups and permissions as you wish
    access_rules = {
         'GET': {
             'list': {
                 # To access this the user must belongs to admin or client group
                 'groups': ['admin', Or, 'client'],
                 'permissions': [IsAuthenticated]  # Also the user must be authenticated
             },
             'retrieve': {
                 'groups': [Not, 'admin'],  # The user must not be in admin group
                 'permissions': [IsAuthenticated, And, IsAllowedUser]  # Must be authenticated and allowed
             },
         },

         'POST': {
             'groups': [],  # Don't allow at all(This evaluates to False always)
             'permissions': []  # Don't allow at all(This evaluates to False always)
         },

         'PUT': {
             'groups': '__any__',  # Belongs to any group or none
             'permissions': '__any__'  # Has any permission or none
         },

         'PATCH': {
             'groups': ['client', And, Not, 'admin'],  # User belongs to client and not admin group
             'permissions': [IsAuthenticated, IsAllowedUser]  # This is = [IsAuthenticated, And, IsAllowedUser]
         },

         'DELETE': {
             'groups': ['client', Or, [Not, 'client', And, 'admin']],  # You can basically do any combination
             'permissions': [IsAuthenticated]
         }
    }

What's important here is to know what goes into groups and permissions

  • Groups takes group names and Django group objects, so you can use those operators however you want with these two, you can even mix the two types together, e.g
'groups': [Group.objects.get(name='admin'), Or, 'client']
  • Permissions takes DRF permissions(class based), Django permission objects and Django permission names(codenames), so you can use those operators however you want with these three, you can even use all three types together, e.g
'permissions': [IsAuthenticated, And, Permissions.objects.get('view_user'), Or, 'change_user']

Note:

  • And, Or & Not are the equvalent operators for and, or & not respectively
  • Unlike and, or & not the operators And, Or & Not have no precedence they are evaluated from left to right, if you want precedence use list or tuple to make one i.e [IsAuthenticated, And, [IsAdmin, Or, IsClient]]
  • The '__any__' on groups/permissions stands for any group/permission or none
  • The GET-list stands for permission & groups in GET: /users/ route
  • The GET-retrieve stands for groups & permissions in GET: /users/{id}/ routes
  • The POST stands for groups & permissions in POST: /users/ route
  • The PUT stands for groups & permissions in PUT: /users/{id}/ routes
  • The PATCH stands for groups & permissions in PATCH: /users/{id}/ routes
  • The DELETE stands for groups & permissions in DELETE: /users/{id}/ routes

Metadata

Release files for drf-guard 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for drf-guard 0.3.0
File Size Uploaded
drf-guard-0.3.0.tar.gz 7.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for drf-guard 0.3.0
File Interpreter ABI Platform
drf_guard-0.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 16.2 kB

Release files / drf-guard-0.3.0.tar.gz

Download URL drf-guard-0.3.0.tar.gz
Size 7.4 kB
Tags Source
SHA-256 checksum
How to use checksums
4524a6cd9e2669704162372dc065796afb9220e9b15915f715630292cc58ba64
BLAKE2b-256 checksum
How to use checksums
77cecdb482ceaa549751fcd6862e08cd37ef721bf9720d38b826aeb7f3da03db
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.2

Release files / drf_guard-0.3.0-py3-none-any.whl

Download URL drf_guard-0.3.0-py3-none-any.whl
Size 8.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c79abf453dd3835a13affe1eb01f376bb35ecf60d4d7910f05e9818969877a55
BLAKE2b-256 checksum
How to use checksums
85fd2ce99b25b457932d677074c4d7a5d39fd1ed722dcf16c015fa9b8c0f3755
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.2

Release history Release notifications | RSS feed

This release

0.3.0 This release

2 release files

0.2.0

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page