From ed22a2261e4a2ed9cad2e27115254ad1e5a893d4 Mon Sep 17 00:00:00 2001 From: meow Date: Thu, 19 Dec 2019 14:03:25 +0500 Subject: [PATCH] Extend ModelBackend instead of rolling our own --- utils/backend.py | 25 ++++++------------------- 1 file changed, 6 insertions(+), 19 deletions(-) diff --git a/utils/backend.py b/utils/backend.py index cbf38d6c..2b5c86e5 100644 --- a/utils/backend.py +++ b/utils/backend.py @@ -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