Ticket #2630: backends_custom_auth.diff
File backends_custom_auth.diff, 2.1 KB (added by , 18 years ago) |
---|
-
backends.py
1 from django.db import models 1 2 from django.contrib.auth.models import User 2 3 4 from django.conf import settings 5 AUTH_MODULE_HINT = settings.AUTH_MODULE_HINT 6 DEBUG = settings.DEBUG 7 8 9 def get_auth_module(module_name): 10 """ 11 Return a user-defined module for authentication. 12 """ 13 # cls is the last component 14 tmp = module_name.split('.') 15 cls = tmp.pop() 16 if DEBUG: print "Class: ", cls 17 module_name = ".".join(tmp) 18 if DEBUG: print "module name: %s" % module_name 19 mod = __import__(module_name, globals(), locals(), ['']) 20 auth_module = getattr(mod, cls) 21 return auth_module 22 23 3 24 class ModelBackend: 4 25 """ 5 26 Authenticate against django.contrib.auth.models.User … … 7 28 # TODO: Model, login attribute name and password attribute name should be 8 29 # configurable. 9 30 def authenticate(self, username=None, password=None): 31 # get the custom user model 32 auth_module = get_auth_module(AUTH_MODULE_HINT) 10 33 try: 11 user = User.objects.get(username=username) 34 # retrieve the user object 35 user = auth_module.objects.get(username=username) 36 # check_password is a method inherited from User 12 37 if user.check_password(password): 13 38 return user 14 except User.DoesNotExist: 39 except auth_module.DoesNotExist: 40 # fallback on the default method 41 try: 42 user = User.objects.get(username=username) 43 if user.check_password(password): 44 return user 45 except User.DoesNotExist: 46 return None 47 else: 15 48 return None 16 49 17 50 def get_user(self, user_id): 18 51 try: 19 52 return User.objects.get(pk=user_id) 20 53 except User.DoesNotExist: 21 return None 54 return None 55 56