mirror of
https://github.com/django/django.git
synced 2025-07-04 17:59:13 +00:00
multi-auth: Added a couple of auth backends.
git-svn-id: http://code.djangoproject.com/svn/django/branches/multi-auth@2883 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
078e2f8d73
commit
0990cce754
51
django/contrib/auth/backends.py
Normal file
51
django/contrib/auth/backends.py
Normal file
@ -0,0 +1,51 @@
|
||||
from django.conf import settings
|
||||
from django.contrib.auth.models import User, check_password
|
||||
|
||||
class SettingsBackend:
|
||||
"""
|
||||
Authenticate against vars in settings.py Use the login name, and a hash
|
||||
of the password.
|
||||
|
||||
ADMIN_LOGIN = 'admin'
|
||||
ADMIN_PASSWORD = 'sha1$4e987$afbcf42e21bd417fb71db8c66b321e9fc33051de'
|
||||
"""
|
||||
def authenticate(self, username=None, password=None):
|
||||
login_valid = (settings.ADMIN_LOGIN == username)
|
||||
pwd_valid = check_password(password, settings.ADMIN_PASSWORD)
|
||||
if login_valid and pwd_valid:
|
||||
# TODO: This should be abstracted out someplace else.
|
||||
try:
|
||||
user = User.objects.get(username=username)
|
||||
except User.DoesNotExist:
|
||||
user = User(username=username, password='')
|
||||
user.is_staff = True
|
||||
user.is_superuser = True
|
||||
user.save()
|
||||
return user
|
||||
return None
|
||||
|
||||
def get_user(self, user_id):
|
||||
try:
|
||||
return User.objects.get(pk=user_id)
|
||||
except User.DoesNotExist:
|
||||
return None
|
||||
|
||||
class ModelBackend:
|
||||
"""
|
||||
Authenticate against django.contrib.auth.models.User
|
||||
"""
|
||||
# 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
|
||||
|
||||
def get_user(self, user_id):
|
||||
try:
|
||||
return User.objects.get(pk=user_id)
|
||||
except User.DoesNotExist:
|
||||
return None
|
Loading…
x
Reference in New Issue
Block a user