Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Django object permission backend, that passesthrough
from django.conf import settings
class ObjectPermBackend(object):
"""Simple object permission backend that passesthrough the has_perm calls
to the object.
"""
supports_object_permissions = True
supports_anonymous_user = True
def authenticate(self, username, password): #@UnusedVariable
"""Stub authenticate, do nothing."""
return None
def has_perm(self, user, perm, obj=None):
"""Passes the has_perm call to object.
:param user: User object
:param perm: Permission string
:param obj: Object
"""
if hasattr(obj, "has_perm"):
return obj.has_perm(user, perm)
return False
@Ciantic
Copy link
Author

Ciantic commented Jun 27, 2010

Usage in settings.py:

AUTHENTICATION_BACKENDS = (
    'django.contrib.auth.backends.ModelBackend',
    'objectperms.ObjectPermBackend',
)

Notice that this does not affect the normal ModelBackend when used like this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment