2017-09-21 23:39:27 +02:00
|
|
|
from django.contrib.auth import get_user_model
|
2021-06-15 16:52:50 +02:00
|
|
|
from django.core.exceptions import PermissionDenied
|
2018-10-06 12:35:49 +02:00
|
|
|
|
2016-08-20 19:35:45 +02:00
|
|
|
from kfet.models import Account, GenericTeamToken
|
2016-08-19 06:20:37 +02:00
|
|
|
|
2017-09-21 23:39:27 +02:00
|
|
|
from .utils import get_kfet_generic_user
|
|
|
|
|
|
|
|
User = get_user_model()
|
|
|
|
|
2017-05-10 12:49:14 +02:00
|
|
|
|
2017-09-23 20:48:28 +02:00
|
|
|
class BaseKFetBackend:
|
|
|
|
def get_user(self, user_id):
|
|
|
|
"""
|
|
|
|
Add extra select related up to Account.
|
|
|
|
"""
|
|
|
|
try:
|
2018-10-06 12:35:49 +02:00
|
|
|
return User.objects.select_related("profile__account_kfet").get(pk=user_id)
|
2017-09-23 20:48:28 +02:00
|
|
|
except User.DoesNotExist:
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2017-09-25 17:16:19 +02:00
|
|
|
class AccountBackend(BaseKFetBackend):
|
|
|
|
def authenticate(self, request, kfet_password=None):
|
2016-08-19 06:20:37 +02:00
|
|
|
try:
|
2017-09-25 17:16:19 +02:00
|
|
|
return Account.objects.get_by_password(kfet_password).user
|
2016-08-19 06:20:37 +02:00
|
|
|
except Account.DoesNotExist:
|
|
|
|
return None
|
|
|
|
|
2016-08-20 19:35:45 +02:00
|
|
|
|
2017-09-25 17:16:19 +02:00
|
|
|
class GenericBackend(BaseKFetBackend):
|
|
|
|
def authenticate(self, request, kfet_token=None):
|
2016-08-20 19:35:45 +02:00
|
|
|
try:
|
2017-09-25 17:16:19 +02:00
|
|
|
team_token = GenericTeamToken.objects.get(token=kfet_token)
|
|
|
|
except GenericTeamToken.DoesNotExist:
|
|
|
|
return
|
|
|
|
|
|
|
|
# No need to keep the token.
|
|
|
|
team_token.delete()
|
|
|
|
|
|
|
|
return get_kfet_generic_user()
|
2021-06-15 16:52:50 +02:00
|
|
|
|
|
|
|
|
|
|
|
class BlockFrozenAccountBackend:
|
|
|
|
def authenticate(self, request, **kwargs):
|
|
|
|
return None
|
|
|
|
|
|
|
|
def get_user(self, user_id):
|
|
|
|
return None
|
|
|
|
|
|
|
|
def has_perm(self, user_obj, perm, obj=None):
|
|
|
|
app_label, _ = perm.split(".")
|
|
|
|
if app_label == "kfet":
|
|
|
|
if (
|
|
|
|
hasattr(user_obj, "profile")
|
|
|
|
and hasattr(user_obj.profile, "account_kfet")
|
|
|
|
and user_obj.profile.account_kfet.is_frozen
|
|
|
|
):
|
|
|
|
raise PermissionDenied
|
|
|
|
|
|
|
|
# Dans le cas général, on se réfère aux autres backends
|
|
|
|
return False
|
|
|
|
|
|
|
|
def has_module_perms(self, user_obj, app_label):
|
|
|
|
if app_label == "kfet":
|
|
|
|
if (
|
|
|
|
hasattr(user_obj, "profile")
|
|
|
|
and hasattr(user_obj.profile, "account_kfet")
|
|
|
|
and user_obj.profile.account_kfet.is_frozen
|
|
|
|
):
|
|
|
|
raise PermissionDenied
|
|
|
|
|
|
|
|
# Dans le cas général, on se réfère aux autres backends
|
|
|
|
return False
|