Extend ModelBackend instead of rolling our own
This commit is contained in:
parent
3f012b7514
commit
ed22a2261e
1 changed files with 6 additions and 19 deletions
|
@ -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
|
|
||||||
|
|
Loading…
Reference in a new issue