huge update
This commit is contained in:
123 files changed
+25119
-4984
No files matched your search
+34
-24
@@ -1,6 +1,11 @@
|
||||
from django.contrib.auth.models import User
|
||||
# Core Django imports
|
||||
from django.contrib.auth.backends import ModelBackend
|
||||
from .models import Staff, Student
|
||||
from django.contrib.auth.models import Group, User
|
||||
from django.db.models import Q
|
||||
|
||||
# Local app imports
|
||||
from app.models import Parent, Staff, Student
|
||||
|
||||
|
||||
class MultiModelBackend(ModelBackend):
|
||||
def authenticate(self, request, username=None, password=None, **kwargs):
|
||||
@@ -9,30 +14,36 @@ class MultiModelBackend(ModelBackend):
|
||||
"""
|
||||
if username is None or password is None:
|
||||
return None
|
||||
|
||||
# Try authenticating as Staff by phone
|
||||
# Use filter().first() to avoid MultipleObjectsReturned error
|
||||
|
||||
# Try authenticating as Staff by phone first
|
||||
staff = Staff.objects.filter(phone=username).first()
|
||||
if staff and staff.check_password(password):
|
||||
return staff
|
||||
if staff:
|
||||
if staff.check_password(password):
|
||||
return staff
|
||||
|
||||
# Try authenticating as Staff by email
|
||||
# Use filter().first() to avoid MultipleObjectsReturned error
|
||||
staff = Staff.objects.filter(email=username).first()
|
||||
if staff and staff.check_password(password):
|
||||
return staff
|
||||
if staff:
|
||||
if staff.check_password(password):
|
||||
return staff
|
||||
|
||||
# Try authenticating as Parent by phone
|
||||
parent = Parent.objects.filter(phone=username).first()
|
||||
if parent:
|
||||
if parent.check_password(password):
|
||||
return parent
|
||||
|
||||
# Try authenticating as Student by phone
|
||||
# Use filter().first() to avoid MultipleObjectsReturned error
|
||||
student = Student.objects.filter(phone=username).first()
|
||||
if student and student.check_password(password):
|
||||
return student
|
||||
if student:
|
||||
if student.check_password(password):
|
||||
return student
|
||||
|
||||
# Try authenticating as Student by email
|
||||
# Use filter().first() to handle multiple students with same email
|
||||
student = Student.objects.filter(email=username).first()
|
||||
if student and student.check_password(password):
|
||||
return student
|
||||
if student:
|
||||
if student.check_password(password):
|
||||
return student
|
||||
|
||||
# Return None if no match
|
||||
return None
|
||||
@@ -41,7 +52,7 @@ class MultiModelBackend(ModelBackend):
|
||||
"""
|
||||
Retrieve a user instance using their ID.
|
||||
"""
|
||||
for model in [Staff, Student]:
|
||||
for model in [Staff, Student, Parent]:
|
||||
try:
|
||||
return model.objects.get(pk=user_id)
|
||||
except model.DoesNotExist:
|
||||
@@ -54,16 +65,15 @@ class MultiModelBackend(ModelBackend):
|
||||
"""
|
||||
if not user_obj.is_active or user_obj.is_anonymous or obj is not None:
|
||||
return set()
|
||||
|
||||
|
||||
if isinstance(user_obj, (Staff, Student)):
|
||||
if not hasattr(user_obj, '_group_perm_cache'):
|
||||
if not hasattr(user_obj, "_group_perm_cache"):
|
||||
perms = set()
|
||||
for group in user_obj.groups.all():
|
||||
perms.update(
|
||||
"%s.%s" % (ct, name)
|
||||
"%s.%s" % (ct, name)
|
||||
for ct, name in group.permissions.values_list(
|
||||
'content_type__app_label',
|
||||
'codename'
|
||||
"content_type__app_label", "codename"
|
||||
)
|
||||
)
|
||||
user_obj._group_perm_cache = perms
|
||||
@@ -74,7 +84,7 @@ class MultiModelBackend(ModelBackend):
|
||||
def get_all_permissions(self, user_obj, obj=None):
|
||||
if not user_obj.is_active or user_obj.is_anonymous or obj is not None:
|
||||
return set()
|
||||
|
||||
|
||||
if isinstance(user_obj, (Staff, Student)):
|
||||
return self.get_group_permissions(user_obj, obj)
|
||||
else:
|
||||
@@ -83,7 +93,7 @@ class MultiModelBackend(ModelBackend):
|
||||
def has_perm(self, user_obj, perm, obj=None):
|
||||
if not user_obj.is_active:
|
||||
return False
|
||||
|
||||
|
||||
if isinstance(user_obj, (Staff, Student)):
|
||||
return perm in self.get_all_permissions(user_obj, obj)
|
||||
else:
|
||||
|
||||
Reference in new issue
Block a user