Skip to content

Commit

Permalink
Merge pull request #573 from garrypolley/master
Browse files Browse the repository at this point in the history
Allow Django  AuthenticationBackends to work with Django user

Thanks @garrypolley
  • Loading branch information
rozza committed Oct 1, 2012
2 parents 6a4351e + 4b2ad25 commit 9f8d6b3
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions mongoengine/django/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from mongoengine import *

from django.utils.encoding import smart_str
from django.contrib.auth.models import _user_get_all_permissions
from django.contrib.auth.models import _user_has_perm
from django.contrib.auth.models import AnonymousUser
from django.utils.translation import ugettext_lazy as _

Expand Down Expand Up @@ -104,6 +106,25 @@ def check_password(self, raw_password):
"""
return check_password(raw_password, self.password)

def get_all_permissions(self, obj=None):
return _user_get_all_permissions(self, obj)

def has_perm(self, perm, obj=None):
"""
Returns True if the user has the specified permission. This method
queries all available auth backends, but returns immediately if any
backend returns True. Thus, a user who has permission from a single
auth backend is assumed to have permission in general. If an object is
provided, permissions for this specific object are checked.
"""

# Active superusers have all permissions.
if self.is_active and self.is_superuser:
return True

# Otherwise we need to check the backends.
return _user_has_perm(self, perm, obj)

@classmethod
def create_user(cls, username, password, email=None):
"""Create (and save) a new user with the given username, password and
Expand Down

0 comments on commit 9f8d6b3

Please sign in to comment.