Skip to main content

Django RBAC Permissions

A Django package which enables RBAC with hierarchy and constraints enabled.

Quick Start

  1. Add django_rbac_permissions to your INSTALLED_APPS setting like this:

    INSTALLED_APPS = [
         ...
         'django_rbac_permissions',
         'django_json_widget'  # Optional
    ]
    

    If you want to see your JSONFields in a pretty widget, include django_json_widget in your INSTALLED_APPS as well.

  2. Run python manage.py migrate to create the models.

  3. If you want to define your modules (applications) in which you have resources which should check for role authorization, you must define a module configuration model. Ex:

    class RoleModuleConfiguration(models.Model):
          posts = models.BooleanField(default=True)
          users = models.BooleanField(default=True)
          role_name = models.CharField(max_length=255)
    

    Each BooleanField symbolizes a module (application) and decides whether the given role_name is authorized to access it.

    You must also set MODULE_CONFIGURATION_PATH as the path, in which your model is residing.

  4. If you also want to control the authorization in your admin page, you must add the middleware class CheckAdminRoleAuthorizationMiddleware to your MIDDLEWARE configuration in your Django settings file:

     MIDDLEWARE = [
     ...
     'rbac_permissions.middleware.CheckAdminRoleAuthorizationMiddleware'
     ]
    
  5. There are some optional configuration parameters that you can set in your settings.py file.

  • GRANT_NONEXISTENT_PATH_ACCESS decides whether you want to grant access to a nonexistent resource (in this case, this means either the url does not exist or the url is not added to any Transaction yet.) The default is False.
  • PERMISSION_DENIED_URL sets the url name of your view, which returns a HTTP_FORBIDDEN_403 status code. This will be the view, which will be redirected by the decorator user_groups_required, if the user is denied access. The default is permission-denied.
  • HTTP_FORBIDDEN_MESSAGE is the default message when the user is denied access. Completely optional.
  • ROLE_RULE_DENIED_ACCESS_MESSAGE is the message returned by the view, when the required role matches one of the user's roles, but the current rule set of the Transaction denies access to this user's role or roles. Completely optional.
  1. Start the development server and visit http://127.0.0.1:8000/admin/ to create a Role or Transaction.

Notes

If you internally route your views to other views in your application, there seems to be a bug related to Django's authentication backends, which results in removing the current user from the request context. So if you are using this pattern often in your code, make sure you pass 'rbac_user={current_user_id}' as a query parameter, so that the current authenticated user is fetched from the database instead from the request object.

This is a temporary solution, will be trying to find a more suitable solution.

Metadata

Release files for django-rbac-permissions 0.4

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

Source distribution (sdist)

Source distribution for django-rbac-permissions 0.4
File Size Uploaded
django_rbac_permissions-0.4.tar.gz 10.6 kB Details

Release files / django_rbac_permissions-0.4.tar.gz

Download URL django_rbac_permissions-0.4.tar.gz
Size 10.6 kB
Tags Source
SHA-256 checksum
How to use checksums
9e7abc69fe6b12f43397b09111261eb4254e028f9ead6199609b890c0347654f
BLAKE2b-256 checksum
How to use checksums
be89f008ed59b4fb4a29e88ef7bc330cc1e57ed7f12caeac5131223144b85292
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.15.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.5.2

Release history Release notifications | RSS feed

This release

0.4 This release

1 release file

0.3

1 release file

0.2

1 release file

0.1

1 release file

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