dvlyadmin_pro/backend/utils/customBackend.py
2025-03-17 18:06:54 +08:00

37 lines
1.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from django.contrib.auth import get_user_model
from django.contrib.auth.backends import ModelBackend
from django.contrib.auth.hashers import check_password
UserModel = get_user_model()
class CustomBackend(ModelBackend):
"""
优化django原生认证解决查询用户认证时查询无用的django内置权限本系统有自己的权限设计
"""
def authenticate(self, request, username=None, password=None, **kwargs):
try:
if username is None:
username = kwargs.get(UserModel.USERNAME_FIELD)
if username is None or password is None:
return
user = UserModel.objects.get(username=username)
if user.check_password(password) and self.user_can_authenticate(user):
return user
except UserModel.DoesNotExist:
return None
def _get_user_permissions(self, user_obj):
return []
def _get_group_permissions(self, user_obj):
return []
def _get_permissions(self, user_obj, obj, from_name):
return set()
def has_perm(self, user_obj, perm, obj=None):
return False
def has_module_perms(self, user_obj, app_label):
return False