Extend ModelBackend instead of rolling our own

This commit is contained in:
ahmadbilalkhalid 2019-12-19 14:03:25 +05:00
parent 3f012b7514
commit ed22a2261e
1 changed files with 6 additions and 19 deletions

View File

@ -1,26 +1,13 @@
import logging
from membership.models import CustomUser
from django.contrib.auth.backends import ModelBackend
logger = logging.getLogger(__name__)
class MyLDAPBackend(object):
def authenticate(self, email, password):
try:
user = CustomUser.objects.get(email=email)
except CustomUser.DoesNotExist:
# User does not exists in Database
return None
else:
class MyLDAPBackend(ModelBackend):
def authenticate(self, username=None, password=None, **kwargs):
user = super().authenticate(username, password, **kwargs)
if user:
user.create_ldap_account(password)
if user.check_password(password):
return user
else:
return None
def get_user(self, user_id):
try:
return CustomUser.objects.get(pk=user_id)
except CustomUser.DoesNotExist:
return None
return user