2024-11-24 07:45:02 +00:00
|
|
|
from django.contrib.auth import get_user_model
|
|
|
|
from django.contrib.auth.backends import ModelBackend
|
2024-12-07 13:22:12 +00:00
|
|
|
from django.contrib.auth.base_user import BaseUserManager
|
2024-11-24 07:45:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
class EmailBackend(ModelBackend):
|
|
|
|
def authenticate(self, request, username=None, password=None, **kwargs):
|
|
|
|
UserModel = get_user_model()
|
|
|
|
try:
|
|
|
|
user = UserModel.objects.get(email=username)
|
|
|
|
except UserModel.DoesNotExist:
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
if user.check_password(password): # type: ignore
|
|
|
|
return user
|
|
|
|
return None
|
2024-12-07 13:22:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
class UserProfileManager(BaseUserManager):
|
|
|
|
""" Manager for user profiles """
|
|
|
|
|
|
|
|
def create_user(self, email, name, password=None):
|
|
|
|
""" Create a new user profile """
|
|
|
|
if not email:
|
|
|
|
raise ValueError('User must have an email address')
|
|
|
|
|
|
|
|
email = self.normalize_email(email)
|
|
|
|
user = self.model(email=email, name=name)
|
|
|
|
|
|
|
|
user.set_password(password)
|
|
|
|
user.save(using=self._db)
|
|
|
|
|
|
|
|
return user
|
|
|
|
|
|
|
|
def create_superuser(self, email, name, password):
|
|
|
|
""" Create a new superuser profile """
|
|
|
|
user = self.create_user(email, name, password)
|
|
|
|
user.is_superuser = True
|
|
|
|
user.is_staff = True
|
|
|
|
|
|
|
|
user.save(using=self._db)
|
|
|
|
|
|
|
|
return user
|