Skip to main content

Django module for converting regular django permissions to CASL-Style rules

Project description

CASL Django is an app that converts the usual Django permissions to CASL-Style rules.

CASL is an isomorphic authorization JavaScript library: https://github.com/stalniy/casl/

Example

Having a rule called my_app.change_item, will generate a CASL rule:

{
    subject: 'my_app/item',
    action: 'change'
}

If you have more than one rule for the same subject, for example:

my_app.change_item
my_app.add_item
my_app.remove_item

This will generate:

{
    subject: 'my_app/item',
    actions: ['add', 'change', 'remove']
}

Configuration

By default, the length for subject and action fields is 128 characters, you can increase the length of them by adding to your settings file:

CASL_DJANGO = {
    'subject-length': 256,
    'action-length': 256
}

Custom Permissions

Sometimes you’d like to have custom permissions for your users, given this, you can add to your user using the Permission’s add_permission class method:

from casl_django.casl.permissions import Permissions

...

my_custom_permission = Permissions.create(subject="navigation", action="index")

...

Permissions.set_user_permission(user=user, permission=my_custom_permission)

Or you can import casl_django.models.UserPermission and create objects as desired.

Quick start

  1. Add “casl_django” to your INSTALLED_APPS setting like this:

    INSTALLED_APPS = [
        ...
        'casl_django',
    ]
  2. Run python manage.py migrate to create the models.

API

By default, your user object should contain a related relationship called casl_permissions. You can filter by permission__subject and permission__action and finally get the rules with the queryset method bundle().

bundle()

This method it’s included in the QuerySet’s for UserPermissions (user.casl_permissions) and for CASLPermission (CASLPermission.objects).

This method returns a list like the following:

[
    // These are regular django permissions transformed to CASL-Style rules
    {'subject': 'products/item', actions: ['add', 'change']},
    // These are CASLPermissions objects
    {'subject': 'navigation', actions: ['index', 'products']}
]

The bundle consists in grouping same-subject rules and the actions, having less data to send over the wire.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

casl-django-0.4.1.tar.gz (11.2 kB view details)

Uploaded Source

File details

Details for the file casl-django-0.4.1.tar.gz.

File metadata

  • Download URL: casl-django-0.4.1.tar.gz
  • Upload date:
  • Size: 11.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.7.0

File hashes

Hashes for casl-django-0.4.1.tar.gz
Algorithm Hash digest
SHA256 ac820f4c958f2314ae15e01388c150c154821a0f07c09419bce44bd7d0c1437e
MD5 54b258bc46d066ddde71763e5f9eeb86
BLAKE2b-256 2e8f858bf0baa2d259a7e95f98dac3fa685f6832578bbe6883d63c8c68e0d4a2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page