email login, fixing bulk
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
from rules.permissions import ObjectPermissionBackend
|
||||
from rowers.models import User
|
||||
|
||||
from django.contrib.auth import get_user_model # gets the user_model django default or your own custom
|
||||
from django.contrib.auth.backends import ModelBackend
|
||||
from django.db.models import Q
|
||||
|
||||
class MyObjectPermissionBackend(ObjectPermissionBackend):
|
||||
def user_can_authenticate(self, user):
|
||||
return getattr(user, "is_active", True)
|
||||
@@ -11,4 +15,31 @@ class MyObjectPermissionBackend(ObjectPermissionBackend):
|
||||
except User.DoesNotExist:
|
||||
return None
|
||||
return user if self.user_can_authenticate(user) else None
|
||||
|
||||
class EmailLoginBackend(ModelBackend):
|
||||
def user_can_authenticate(self, user):
|
||||
return getattr(user, "is_active", True)
|
||||
|
||||
def authenticate(self, request, username=None, password=None, **kwargs):
|
||||
try:
|
||||
user = User.objects.filter(
|
||||
Q(username__iexact=username) |
|
||||
Q(email__iexact=username)
|
||||
).distinct()
|
||||
except User.DoesNotExist:
|
||||
return None
|
||||
|
||||
if user.exists():
|
||||
user_obj = user.first()
|
||||
if user_obj.check_password(password) and user_obj.is_active:
|
||||
return user_obj
|
||||
return None
|
||||
else:
|
||||
return None
|
||||
|
||||
def get_user(self, user_id):
|
||||
try:
|
||||
return User.objects.get(pk=user_id)
|
||||
except User.DoesNotExist:
|
||||
return None
|
||||
|
||||
|
||||
Reference in New Issue
Block a user