Skip to content

Instantly share code, notes, and snippets.

@Ciantic
Created June 27, 2010 13:18
Show Gist options
  • Save Ciantic/454898 to your computer and use it in GitHub Desktop.
Save Ciantic/454898 to your computer and use it in GitHub Desktop.
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