2016-09-01 00:45:44 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2017-09-21 23:39:27 +02:00
|
|
|
from django.contrib.auth import get_user_model
|
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
|
|
|
|
2016-08-19 06:20:37 +02:00
|
|
|
class KFetBackend(object):
|
|
|
|
def authenticate(self, request):
|
2016-08-20 23:31:30 +02:00
|
|
|
password = request.POST.get('KFETPASSWORD', '')
|
|
|
|
password = request.META.get('HTTP_KFETPASSWORD', password)
|
2016-08-19 06:20:37 +02:00
|
|
|
if not password:
|
|
|
|
return None
|
|
|
|
|
|
|
|
try:
|
2017-09-22 23:31:46 +02:00
|
|
|
return Account.objects.get_by_password(password).user
|
2016-08-19 06:20:37 +02:00
|
|
|
except Account.DoesNotExist:
|
|
|
|
return None
|
|
|
|
|
2016-08-20 19:35:45 +02:00
|
|
|
|
|
|
|
class GenericTeamBackend(object):
|
|
|
|
def authenticate(self, username=None, token=None):
|
|
|
|
valid_token = GenericTeamToken.objects.get(token=token)
|
|
|
|
if username == 'kfet_genericteam' and valid_token:
|
2017-09-21 23:39:27 +02:00
|
|
|
return get_kfet_generic_user()
|
2016-08-20 19:35:45 +02:00
|
|
|
return None
|
|
|
|
|
|
|
|
def get_user(self, user_id):
|
|
|
|
try:
|
2017-04-06 18:23:27 +02:00
|
|
|
return (
|
|
|
|
User.objects
|
2017-05-10 12:49:14 +02:00
|
|
|
.select_related('profile__account_kfet')
|
2017-04-06 18:23:27 +02:00
|
|
|
.get(pk=user_id)
|
|
|
|
)
|
2016-08-20 19:35:45 +02:00
|
|
|
except User.DoesNotExist:
|
|
|
|
return None
|