Skip to main content

django-auth-role

Code Climate https://travis-ci.org/tomi77/django-auth-role.svg?branch=master https://coveralls.io/repos/github/tomi77/django-auth-role/badge.svg?branch=master

Add roles to django-auth

Role are set of group of permissions and permissions. It’s fully customizable. Everything is in database.

Admin application allow to manage roles.

Installation

pip install django-auth-role

Quick start

Add authrole to INSTALLED_APPS (django.contrib.auth and django.contrib.contenttypes are also required) and AuthRoleBackend to AUTHENTICATION_BACKENDS.

INSTALLED_APPS = [
    ...
    'django.contrib.contenttypes',
    'django.contrib.auth',
    'authrole',
]

AUTHENTICATION_BACKENDS = (
    'authrole.auth.backends.AuthRoleBackend',
)

Extend auth.User.

from authrole.mixins import RoleMixin
from django.db import models

class MyUser(RoleMixin, models.Model):
    user = models.OneToOneField('auth.User', related_name='user')

or create new auth user model:

from authrole.mixins import RoleMixin
from django.contrib.auth.models import AbstractUser
from django.db import models

class MyUser(RoleMixin, AbstractUser):
    pass

In this case remember to set AUTH_USER_MODEL to Your model.

Create tables.

./manage.py migrate

Advanced usage

Own authentication backend

If You need Your own authentication backend, simply extend BaseAuthRoleBackend. fetch_role_permissions function must return a list of auth.Permission objects:

from authrole.auth.backends import BaseAuthRoleBackend
from django.contrib.auth.models import Permission

class MyBackend(BaseAuthRoleBackend):
    def fetch_role_permissions(self, user_obj):
        if user_obj.username == 'admin':
            return Permission.objects.all()
        else:
            return Permission.objects.none()

Extend role

Add OneToOneField to Your model:

from django.db import models

class MyRole(models.Model):
    role = models.OneToOneField('authrole.Role', null=False, blank=False, related_name='myrole')
    extra_field = models.CharField(max_length=10)

And use:

from authrole.models import Role

role = Role.objects.all()[0]

print(role.myrole.extra_field)

Or write Your own role class based on AbstractRole (Django >= 1.5):

from authrole.model import AbstractRole

class MyRole(AbstractRole):
    extra_field = models.CharField(max_length=10)

Point AUTHROLE_ROLE_MODEL to Your new model:

AUTHROLE_ROLE_MODEL = 'app.MyRole'

And use:

from app.models import MyRole

role = MyRole.objects.all()[0]

print(role.extra_field)

Metadata

Release files for django-auth-role 0.4.0

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

Built distribution (wheel)

Table of built distributions (wheels) for django-auth-role 0.4.0
File Interpreter ABI Platform
django_auth_role-0.4.0-py2.py3-none-any.whl Python 3, Python 2 none any Details

Release files / django_auth_role-0.4.0-py2.py3-none-any.whl

Download URL django_auth_role-0.4.0-py2.py3-none-any.whl
Size 12.5 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
1ba022ae540891bd987679d759bb55343f8416c277f5d59646dfef3a572661cc
BLAKE2b-256 checksum
How to use checksums
cd6611677a005d2828f20a5affc8604791cf61ca980d544db370975d1ef08ec3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.4.0 This release

1 release file

0.3.0

1 release file

0.2.0

1 release file

0.1.0

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