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

View file

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