Skip to main content

A Django extension to define and use Groups and Permissions from a central place, per app.

Project description

https://github.com/serioeseGmbH/serious-django-permissions

serious-django-permissions is a Django extension that makes it possible to define Permissions in each app, and Groups in a central place. It makes these Permissions import-able and checkable via user.has_perm:

from some_app.permissions import ChangeSomethingPermission

def change_something(something, user):
    if not user.has_perm(ChangeSomethingPermission):
        raise PermissionDenied("You can't change this something.)
    else:
        return something.change()

Both permissions and groups can then be created in the database with a single manage.py call (see Quick start below).

Quick start

  1. Install the package with pip:

    pip install serious-django-permissions
  2. Add “serious_django_permissions” to your INSTALLED_APPS setting like this:

    INSTALLED_APPS = [
        ...
        'serious_django_permissions',
    ]
  3. Add serious_django_permissions.permissions.PermissionModelBackend to your AUTHENTICATION_BACKENDS setting. This enables you to do permission checks like user.has_perm(SomePermission):

    AUTHENTICATION_BACKENDS = [
        ...
        'serious_django_permissions.permissions.PermissionModelBackend',
    ]
  4. In each app that should define a permission, import serious_django_permissions.permissions.Permission and create subclasses of it.

    The name of your subclasses must end in Permission, and each subclass must define a description attribute. For instance, let’s say you have the file myapp/permissions.py:

    from serious_django_permissions.permissions import Permission
    
    from .models import MyModel
    
    class MyPermission(Permission):
        model = 'MyModel' # should be a model inside myapp.models, or not defined for global permissions.
        description = 'A description for this permission'
  5. Run python manage.py create_permissions to create all defined permissions on the database level.

  6. If you’d like to use the Groups feature as well:

    1. Create a file named something like some_app/groups.py inside one of your apps, or in the folder where your settings live. An example:

      from serious_django_permissions.groups import Group
      
      from app_one.permissions import\
          AppOnePermissionA, AppOnePermissionB
      from app_two.permissions import\
          AppTwoPermission
      
      class GroupA(Group):
          permissions = [
              AppOnePermissionA,
              AppTwoPermission
          ]
      
      class GroupB(Group):
          permissions = [
              AppOnePermissionB,
              AppTwoPermission
          ]
    2. Reference the defined groups file in your settings:

      DEFAULT_GROUPS_MODULE = 'some_app.groups'
    3. Run python manage.py create_groups to create all permissions and assign them to the groups.

Authorization

Setting a permission for a model does not automatically restrict the access to the model. Authorization still has to be checked via the view. Check the Django documentation on how to authorize access to views for further information.

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

serious-django-permissions-0.6.tar.gz (6.2 kB view details)

Uploaded Source

File details

Details for the file serious-django-permissions-0.6.tar.gz.

File metadata

  • Download URL: serious-django-permissions-0.6.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.30.0 CPython/3.6.3

File hashes

Hashes for serious-django-permissions-0.6.tar.gz
Algorithm Hash digest
SHA256 948c63865bc7c8c1c2af339aa756a2ab86b2690e25e53e6234c56f2086db7256
MD5 431ad2d614ebff90bde17b274c32fc92
BLAKE2b-256 bbb8c64f837df2c8b3a9037e2c381eaee0189e874ac03ac34aa53f6963e5929b

See more details on using hashes here.

Supported by

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