2012-06-08 00:08:47 +08:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2010-02-23 20:02:41 +08:00
|
|
|
from django.contrib.auth.models import User, Permission
|
2006-06-29 00:37:02 +08:00
|
|
|
|
2008-07-02 13:00:09 +08:00
|
|
|
|
2008-03-08 11:09:29 +08:00
|
|
|
class ModelBackend(object):
|
2006-06-29 00:37:02 +08:00
|
|
|
"""
|
2008-07-02 13:00:09 +08:00
|
|
|
Authenticates against django.contrib.auth.models.User.
|
2006-06-29 00:37:02 +08:00
|
|
|
"""
|
2009-12-10 09:05:35 +08:00
|
|
|
|
2006-06-29 00:37:02 +08:00
|
|
|
# TODO: Model, login attribute name and password attribute name should be
|
|
|
|
# configurable.
|
|
|
|
def authenticate(self, username=None, password=None):
|
|
|
|
try:
|
|
|
|
user = User.objects.get(username=username)
|
|
|
|
if user.check_password(password):
|
|
|
|
return user
|
|
|
|
except User.DoesNotExist:
|
|
|
|
return None
|
|
|
|
|
2011-09-11 05:00:32 +08:00
|
|
|
def get_group_permissions(self, user_obj, obj=None):
|
2008-07-02 13:00:09 +08:00
|
|
|
"""
|
|
|
|
Returns a set of permission strings that this user has through his/her
|
|
|
|
groups.
|
|
|
|
"""
|
2011-09-11 05:00:32 +08:00
|
|
|
if user_obj.is_anonymous() or obj is not None:
|
|
|
|
return set()
|
2007-09-20 00:50:30 +08:00
|
|
|
if not hasattr(user_obj, '_group_perm_cache'):
|
2010-12-04 13:59:56 +08:00
|
|
|
if user_obj.is_superuser:
|
|
|
|
perms = Permission.objects.all()
|
|
|
|
else:
|
|
|
|
perms = Permission.objects.filter(group__user=user_obj)
|
|
|
|
perms = perms.values_list('content_type__app_label', 'codename').order_by()
|
2010-02-23 20:02:41 +08:00
|
|
|
user_obj._group_perm_cache = set(["%s.%s" % (ct, name) for ct, name in perms])
|
2007-09-20 00:50:30 +08:00
|
|
|
return user_obj._group_perm_cache
|
2008-07-02 13:00:09 +08:00
|
|
|
|
2011-09-11 05:00:32 +08:00
|
|
|
def get_all_permissions(self, user_obj, obj=None):
|
|
|
|
if user_obj.is_anonymous() or obj is not None:
|
2010-01-28 09:47:23 +08:00
|
|
|
return set()
|
2007-09-20 00:50:30 +08:00
|
|
|
if not hasattr(user_obj, '_perm_cache'):
|
2012-06-08 00:08:47 +08:00
|
|
|
user_obj._perm_cache = set(["%s.%s" % (p.content_type.app_label, p.codename) for p in user_obj.user_permissions.select_related()])
|
2007-09-20 00:50:30 +08:00
|
|
|
user_obj._perm_cache.update(self.get_group_permissions(user_obj))
|
|
|
|
return user_obj._perm_cache
|
|
|
|
|
2011-09-11 05:00:32 +08:00
|
|
|
def has_perm(self, user_obj, perm, obj=None):
|
2010-12-22 03:18:12 +08:00
|
|
|
if not user_obj.is_active:
|
|
|
|
return False
|
2011-09-11 05:00:32 +08:00
|
|
|
return perm in self.get_all_permissions(user_obj, obj)
|
2007-09-20 00:50:30 +08:00
|
|
|
|
|
|
|
def has_module_perms(self, user_obj, app_label):
|
2008-07-02 13:00:09 +08:00
|
|
|
"""
|
|
|
|
Returns True if user_obj has any permissions in the given app_label.
|
|
|
|
"""
|
2010-12-22 03:18:12 +08:00
|
|
|
if not user_obj.is_active:
|
|
|
|
return False
|
2008-07-02 13:05:50 +08:00
|
|
|
for perm in self.get_all_permissions(user_obj):
|
|
|
|
if perm[:perm.index('.')] == app_label:
|
|
|
|
return True
|
|
|
|
return False
|
2007-09-20 00:50:30 +08:00
|
|
|
|
2006-06-29 00:37:02 +08:00
|
|
|
def get_user(self, user_id):
|
|
|
|
try:
|
|
|
|
return User.objects.get(pk=user_id)
|
|
|
|
except User.DoesNotExist:
|
|
|
|
return None
|
2009-03-15 13:54:28 +08:00
|
|
|
|
|
|
|
|
|
|
|
class RemoteUserBackend(ModelBackend):
|
|
|
|
"""
|
|
|
|
This backend is to be used in conjunction with the ``RemoteUserMiddleware``
|
|
|
|
found in the middleware module of this package, and is used when the server
|
|
|
|
is handling authentication outside of Django.
|
|
|
|
|
|
|
|
By default, the ``authenticate`` method creates ``User`` objects for
|
|
|
|
usernames that don't already exist in the database. Subclasses can disable
|
|
|
|
this behavior by setting the ``create_unknown_user`` attribute to
|
|
|
|
``False``.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Create a User object if not already in the database?
|
|
|
|
create_unknown_user = True
|
|
|
|
|
|
|
|
def authenticate(self, remote_user):
|
|
|
|
"""
|
|
|
|
The username passed as ``remote_user`` is considered trusted. This
|
|
|
|
method simply returns the ``User`` object with the given username,
|
|
|
|
creating a new ``User`` object if ``create_unknown_user`` is ``True``.
|
|
|
|
|
|
|
|
Returns None if ``create_unknown_user`` is ``False`` and a ``User``
|
|
|
|
object with the given username is not found in the database.
|
|
|
|
"""
|
|
|
|
if not remote_user:
|
|
|
|
return
|
|
|
|
user = None
|
|
|
|
username = self.clean_username(remote_user)
|
|
|
|
|
|
|
|
# Note that this could be accomplished in one try-except clause, but
|
|
|
|
# instead we use get_or_create when creating unknown users since it has
|
|
|
|
# built-in safeguards for multiple threads.
|
|
|
|
if self.create_unknown_user:
|
|
|
|
user, created = User.objects.get_or_create(username=username)
|
|
|
|
if created:
|
|
|
|
user = self.configure_user(user)
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
user = User.objects.get(username=username)
|
|
|
|
except User.DoesNotExist:
|
|
|
|
pass
|
|
|
|
return user
|
|
|
|
|
|
|
|
def clean_username(self, username):
|
|
|
|
"""
|
|
|
|
Performs any cleaning on the "username" prior to using it to get or
|
|
|
|
create the user object. Returns the cleaned username.
|
|
|
|
|
|
|
|
By default, returns the username unchanged.
|
|
|
|
"""
|
|
|
|
return username
|
|
|
|
|
|
|
|
def configure_user(self, user):
|
|
|
|
"""
|
|
|
|
Configures a user after creation and returns the updated user.
|
|
|
|
|
|
|
|
By default, returns the user unmodified.
|
|
|
|
"""
|
|
|
|
return user
|