academon/core/backends/reportmonster.py

37 lines
1.1 KiB
Python
Raw Normal View History

2022-08-08 09:43:31 +00:00
from django.contrib.auth.backends import ModelBackend
from django.contrib.auth import get_user_model
from django.contrib import messages
from django.conf import settings
2022-09-16 12:51:56 +00:00
from reportmonster.classes.vessel import Vessel
2022-08-08 09:43:31 +00:00
from bcrypt import checkpw
2022-09-16 15:40:15 +00:00
from ..models.auth import Profile
2022-08-08 09:43:31 +00:00
class ReportMonsterBackend(ModelBackend):
2022-09-16 12:51:56 +00:00
def authenticate(self, request, username: str = None, password: str = None):
2022-08-08 09:43:31 +00:00
self.request = request
2022-08-08 15:18:39 +00:00
monster = settings.MONSTERCONFIG
2022-09-16 12:51:56 +00:00
loginvessel = list(
filter(lambda x: x.name == settings.LOGIN_VESSEL, monster.vessels)
)[0]
2022-08-08 09:43:31 +00:00
2022-09-16 15:40:15 +00:00
try:
userdata = list(loginvessel.getUsers(username=username).values())[0]
except:
return
2022-08-08 09:43:31 +00:00
2022-09-16 12:51:56 +00:00
if checkpw(password.encode(), userdata["password"].encode()):
2022-08-08 09:43:31 +00:00
user, _ = get_user_model().objects.get_or_create(email=username)
user.save()
2022-09-16 15:40:15 +00:00
profile, _ = Profile.objects.get_or_create(user=user)
profile.first_name = userdata["firstname"]
profile.last_name = userdata["lastname"]
profile.save()
2022-08-08 09:43:31 +00:00
return user
2022-09-16 15:40:15 +00:00
return