Package: djinn_auth
djinn_authRole based auth module for Djinn. The package is also usable as
standalone module, since it has no relation to other djinn packages.This module builds upon the django.contrib.auth module, and extends it
with role based authorisation, both global roles and local roles (or:
roles on a specific model instance). Roles can be assigned to a user
and a group.For (un)assignments you can use the API specified in
djinn_auth.utils. This provides functions to assign roles to
users/groups globally and locally, and to check on global and local
roles.  Specific checks enable you to check whether a user has a role,
either directly or through one of it’s groups.Note that while this module is agnostic of the specific User model
(since this is a swappable model), it will assume at least the
existence of the user_permissions relation.Global permissionsGlobal permissions provide a means of granting permissions to a user
or group globally. This means that a check on a local permission on a
given model instance, will also return the global permissions.You can give a user a permission by:give the user a permission through the User.user_permissions attributegive the user a role that has this permissionadd the user to a group that has a role that has this permissionadd the user to a group that has the permissionLocal permissionsYou can enable permissions for a given user or group on a specific
model instance by:giving the user a local role on the object with that permissiongiving a group the user is part of a local role with that permissiongiving the permission to the user on the objectgiving the permission to a group the user is part of on the objectTo prevent acquisition of the global permissions on an instance,
implement theacquire_global_rolesproperty and return False. This
enables the scenario where users have a global ‘allow’, but some local
‘forbiddens’. Please note that this only disallows global _roles_, not
direct permissions on the user or it’s groups itself.If you need to add some sort of hierarchy, you may do so using theacquire_fromattribute on your instance, that must return a list of
instances it wishes to obtain roles/permissions from.InstallationInstall the usual way using _pip_ or _easy_install_. Add djinn_auth
to your INSTALLED_APPS. Add the djinn_auth backend to the
AUTHENTICATION_BACKENDS setting:AUTHENTICATION_BACKENDS = (‘django.contrib.auth.backends.ModelBackend’,
‘djinn_auth.authbackend.AuthBackend’
)UsageBasic use of the djinn_auth module is not different from using the
builtin Django authorisation. You can use the same decorators or
calls, since djinn_auth adds it’s own backend to the autorisation
chain. Following, are some simpe cases. Check the utils.py file for
the full API. Yes, you will need to read Python code..!Create a role and add permissions:from djinn_auth.models import Role
from django.contrib.auth.models import Permissionowner_role = Role.objects.create(name=”owner”)do_something = Permission.objects.get(codename=”myapp.do_something”)owner_role.add_permission(do_something)To assign a global role for a user or group (assuming you have a user
_bobdobalina_):>> from djinn_auth.utils import assign_global_role, has_global_role
>> assign_global_role(bobdobalina, owner_role)
>> has_global_role(bobdobalina, owner_role)
True
>>This should give us the permission granted to the role (please note that
the backend is normally called by the Django auth machinery using the
_User.has_perm_ call:>> from djinn_auth.authbackend import AuthBackend
>> backend = AuthBackend()
>> backend.has_perm(bobdobalina, “myapp.do_something”)
TrueRevoke it:>> from djinn_auth.utils import unassign_global_role
>> unassign_global_role(bobdobalina, owner_role)
>> has_global_role(bobdobalina, owner_role)
False
>> backend.has_perm(bobdobalina, “myapp.do_something”)
FalseAssign a local role:>> from djinn_auth.utils import assign_local_role, has_local_role
>> instance = MyContentType.objects.get(pk=666)
>> assign_local_role(bobdobalina, instance, owner_role)
>> has_local_role(bobdobaline, instance, owner_role)
True
>> backend.has_perm(bobdobalina, “myapp.do_something”)
False
>> backend.has_perm(bobdobalina, “myapp.do_something”, obj=instance)
TrueViewsTo protect class based views, djinn_auth provides a mixin class that
adds a permission check: djinn_auth.views.base.PermissionProtectedMixin.Use like so:from django.views.generic.base import View
from djinn_auth.views.base import PermissionProtectedMixinclass MyView(PermissionProtectedMixin, View):permission = “myapp.view”or even:class MyView(PermissionProtectedMixin, View):permission = {‘GET’: ‘myapp.view’, ‘POST’: ‘myapp.edit’}1.0.7import fixes. Some libraries were updated1.0.6django 1.8+ and backward compatibility1.0.5convenience on get_local_rolesadmin enhancement1.0.4Added group base implementationMade auth backend ‘acquisition’ enabled1.0.3Some more support for roles as string repr in utils methods1.0.2Added admin.py for management uiMade has_*_role functions in utils accept role names as well as objectsfix for swapped user modelAdded protected view mixin1.0.1Added unit test for global rolesMore to READMEFixed repr issue in LocalRole1.0.0Initial version