class EmpBackend(ModelBackend):
Emp = get_user_model()
supports_object_permissions = True
supports_anonymous_user = False
supports_inactive_user = False
def authenticate(self, request, email=None, password=None, **kwargs):
if email is None or password is None:
return
try:
username = kwargs.get('Emp.USERNAME_FIELD')
user = Emp.objects.get(email=username)
if user.check_password(password) and self.user_can_authenticate(user):
return user
except Emp.DoesNotExist:
return None
return None
def user_can_authenticate(self, user):
is_active = getattr(user, 'is_active', None)
return is_active or is_active is None
def get_user(self, user_id):
try:
return Emp.objects.get(pk=user_id)
except Emp.DoesNotExist:
return None
def has_perm(self, perm, obj=None):
return True
def has_module_perms(self, app_label):
return True
Emp = get_user_model()
supports_object_permissions = True
supports_anonymous_user = False
supports_inactive_user = False
def authenticate(self, request, email=None, password=None, **kwargs):
if email is None or password is None:
return
try:
username = kwargs.get('Emp.USERNAME_FIELD')
user = Emp.objects.get(email=username)
if user.check_password(password) and self.user_can_authenticate(user):
return user
except Emp.DoesNotExist:
return None
return None
def user_can_authenticate(self, user):
is_active = getattr(user, 'is_active', None)
return is_active or is_active is None
def get_user(self, user_id):
try:
return Emp.objects.get(pk=user_id)
except Emp.DoesNotExist:
return None
def has_perm(self, perm, obj=None):
return True
def has_module_perms(self, app_label):
return True
On Monday, July 5, 2021 at 12:09:17 PM UTC-5 David Crandell wrote:
This is the message I get when I try to login "Please enter a correct email and password. Note that both fields may be case-sensitive."I've changed my code a little in an effort to try to remedy my situation.I'm really stuck on this. From what I can tell, it's passing the values to the authenticate() method in my EmpBackend but it keeps saying password is not valid. How do I pass the correct password value? Am I overlooking something?SETTINGS.PY# Login model override
AUTH_USER_MODEL = 'employees.Emp'
LOGIN_URL = 'employees:login'
LOGIN_REDIRECT_URL = 'employees:emp-home'
AUTHENTICATION_BACKENDS = [
'django.contrib.auth.backends.ModelBackend',
'employees.backends.EmpBackend',
]FORMS.PYclass LoginForm(AuthenticationForm):
def __init__(self, *args, **kwargs):
super(LoginForm, self).__init__(*args, **kwargs)
username = forms.EmailField(widget=forms.TextInput(
attrs={
'class': 'form-control',
'placeholder': '',
'id': 'username',
}
))
password = forms.CharField(widget=forms.PasswordInput(
attrs={
'class': 'form-control',
'placeholder': '',
'id': 'password',
}
))
template_name = 'employees/login.html'VIEWS.PYclass MyLogin(LoginView):
template_name = 'employees/login.html'
form_class = LoginFormsuccess_url = 'employees:emp-home'
def get_success_url(self):
return super(MyLogin, self).get_success_url()
def form_valid(self, form):
user = self.request.POST.get('username')
password = self.request.POST.get('password')
EmpBackend.authenticate(Emp, self.request, user, password)
return HttpResponseRedirect(self.get_success_url())MODELS.PYclass Emp(AbstractBaseUser):
first_name = models.CharField(max_length=100, default=None)
last_name = models.CharField(max_length=100, default=None)
email = models.EmailField(max_length=255, default=None, unique=True)
username = models.CharField(max_length=100, default=None, null=True)
phone = models.CharField(max_length=20, default=None, null=True, blank=True)
password = models.CharField(max_length=100)
address1 = models.CharField(max_length=100, default=None, null=True, blank=True)
address2 = models.CharField(max_length=100, default=None, null=True, blank=True)
city = models.CharField(max_length=100, default=None, null=True, blank=True)
state = models.CharField(max_length=100, default=None, null=True, blank=True)
zip = models.CharField(max_length=10, default=None, null=True, blank=True)
position = models.CharField(max_length=50, default=None)
date_hired = models.DateTimeField(auto_now_add=True)
date_updated = models.DateTimeField(auto_now_add=True)
date_terminated = models.DateTimeField(default=None, null=True, blank=True)
is_active = models.BooleanField(default=True)
status = models.SmallIntegerField(default=0)
emp_is_salary = models.BooleanField(default=False)
emp_pto_rate = models.DecimalField(max_digits=8, decimal_places=4, default=0.0)
emp_user_level = models.SmallIntegerField(default=1)
emerg_contact1 = models.CharField(max_length=100, default=None, null=True, blank=True)
emerg_contact1_phone = models.CharField(max_length=20, default=None, null=True, blank=True)
emerg_contact1_address = models.CharField(max_length=200, default=None, null=True, blank=True)
emerg_contact1_city_st = models.CharField(max_length=200, default=None, null=True, blank=True)
emerg_contact2 = models.CharField(max_length=100, default=None, null=True, blank=True)
emerg_contact2_phone = models.CharField(max_length=20, default=None, null=True, blank=True)
emerg_contact2_address = models.CharField(max_length=200, default=None, null=True, blank=True)
emerg_contact2_city_st = models.CharField(max_length=200, default=None, null=True, blank=True)
emp_note = models.TextField(default=None, null=True, blank=True)
emp_hourly_rate = models.DecimalField(max_digits=8, decimal_places=4, default=0.0)
emp_net_pto = models.DecimalField(max_digits=8, decimal_places=4, default=0.0)
emp_pto_prev = models.DecimalField(max_digits=8, decimal_places=4, default=0.0)
emp_image = models.ImageField(upload_to='profile-pics/', default='profile-pics/default.png', null=True, blank=True)
last_login = models.DateTimeField(default=None, null=True)
is_staff = models.BooleanField(default=True, blank=True)
date_joined = models.DateTimeField(auto_now_add=True, blank=True)
is_superuser = models.BooleanField(default=False, blank=True)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
def get_full_name(self):
return f'{self.first_name} {self.last_name}'
def get_email(self):
return self.email
def __str__(self):
return self.email@property
def is_staff(self):
return self.staff
@property
def is_admin(self):
return self.admin
def save(self, *args, **kwargs):
super(Emp, self).save(*args, **kwargs)
img = Image.open(self.emp_image.path)
if img.height > 300 or img.width > 300:
output_size = (300, 300)
img.thumbnail(output_size)
img.save(self.emp_image.image)
self.last_login = timezone.utc
def get_absolute_url(self):
return reverse('employees:emp-detail', args=[self.id])
objects = UserManager()On Sunday, July 4, 2021 at 9:14:21 PM UTC-5 ANi wrote:What does the error message say?guitard...@gmail.com 在 2021年7月4日 星期日下午1:48:58 [UTC+8] 的信中寫道:Hello, I am using a custom auth backend and everything displays properly, except I cannot login with the credentials in my model. I have spent 5 hours on this and cannot figure it out. Please help.views.pyclass MyLogin(LoginView):
template_name = 'employees/login.html'
form_class = LoginForm
def form_valid(self, form):
user = form.get_user()
employee = Emp.objects.get(email=user)
if employee.is_active:
EmpBackend.authenticate(self.request, username=user, password=form.password)
return HttpResponseRedirect(self.get_success_url())backends.pyfrom django.contrib.auth.backends import ModelBackend
from .models import Emp
class EmpBackend(ModelBackend):
def authenticate(self, request, username=None, password=None, **kwargs):
try:
user = Emp.objects.get(email=username)
if user.check_password(password):
return user
except Emp.DoesNotExist:
return None
return None
def get_user(self, user_id):
try:
return Emp.objects.get(pk=user_id)
except Emp.DoesNotExist:
return Nonemodels.pyclass Emp(AbstractBaseUser):
first_name = models.CharField(max_length=100, default=None)
last_name = models.CharField(max_length=100, default=None)
email = models.EmailField(max_length=255, default=None, unique=True)
username = models.CharField(max_length=100, default=None, null=True)
phone = models.CharField(max_length=20, default=None, null=True, blank=True)
password = models.CharField(max_length=100)
address1 = models.CharField(max_length=100, default=None, null=True, blank=True)
address2 = models.CharField(max_length=100, default=None, null=True, blank=True)
city = models.CharField(max_length=100, default=None, null=True, blank=True)
state = models.CharField(max_length=100, default=None, null=True, blank=True)
zip = models.CharField(max_length=10, default=None, null=True, blank=True)
position = models.CharField(max_length=50, default=None)
date_hired = models.DateTimeField(auto_now_add=True)
date_updated = models.DateTimeField(auto_now_add=True)
date_terminated = models.DateTimeField(default=None, null=True, blank=True)
is_active = models.BooleanField(default=True)
status = models.SmallIntegerField(default=0)
emp_is_salary = models.BooleanField(default=False)
emp_pto_rate = models.DecimalField(max_digits=8, decimal_places=4, default=0.0)
emp_user_level = models.SmallIntegerField(default=1)
emerg_contact1 = models.CharField(max_length=100, default=None, null=True, blank=True)
emerg_contact1_phone = models.CharField(max_length=20, default=None, null=True, blank=True)
emerg_contact1_address = models.CharField(max_length=200, default=None, null=True, blank=True)
emerg_contact1_city_st = models.CharField(max_length=200, default=None, null=True, blank=True)
emerg_contact2 = models.CharField(max_length=100, default=None, null=True, blank=True)
emerg_contact2_phone = models.CharField(max_length=20, default=None, null=True, blank=True)
emerg_contact2_address = models.CharField(max_length=200, default=None, null=True, blank=True)
emerg_contact2_city_st = models.CharField(max_length=200, default=None, null=True, blank=True)
emp_note = models.TextField(default=None, null=True, blank=True)
emp_hourly_rate = models.DecimalField(max_digits=8, decimal_places=4, default=0.0)
emp_net_pto = models.DecimalField(max_digits=8, decimal_places=4, default=0.0)
emp_pto_prev = models.DecimalField(max_digits=8, decimal_places=4, default=0.0)
emp_image = models.ImageField(upload_to='profile-pics/', default='profile-pics/default.png', null=True, blank=True)
last_login = models.DateTimeField(default=None, null=True)
is_staff = models.BooleanField(default=True, blank=True)
date_joined = models.DateTimeField(auto_now_add=True, blank=True)
is_superuser = models.BooleanField(default=False, blank=True)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
def get_full_name(self):
return f'{self.first_name} {self.last_name}'
def get_email(self):
return self.email
def __str__(self):
return self.email
def has_perm(self, perm, obj=None):
return True
def has_module_perms(self, app_label):
return True
@property
def is_staff(self):
return self.staff
@property
def is_admin(self):
return self.admin
def save(self, *args, **kwargs):
super(Emp, self).save(*args, **kwargs)
img = Image.open(self.emp_image.path)
if img.height > 300 or img.width > 300:
output_size = (300, 300)
img.thumbnail(output_size)
img.save(self.emp_image.image)
self.last_login = timezone.utc
def get_absolute_url(self):
return reverse('employees:emp-detail', args=[self.id])
objects = UserManager()
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/783df27c-8e79-482a-a3e2-2c886a3954e9n%40googlegroups.com.
No comments:
Post a Comment