2016-08-19 06:20:37 +02:00
|
|
|
import hashlib
|
|
|
|
|
2016-08-20 19:35:45 +02:00
|
|
|
from django.contrib.auth.models import User, Permission
|
|
|
|
from gestioncof.models import CofProfile
|
|
|
|
from kfet.models import Account, GenericTeamToken
|
2016-08-19 06:20:37 +02:00
|
|
|
|
|
|
|
class KFetBackend(object):
|
|
|
|
def authenticate(self, request):
|
|
|
|
password = request.META.get('HTTP_KFETPASSWORD')
|
|
|
|
if not password:
|
|
|
|
return None
|
|
|
|
|
|
|
|
try:
|
|
|
|
password_sha1 = hashlib.sha1(password.encode()).hexdigest()
|
|
|
|
account = Account.objects.get(password=password_sha1)
|
|
|
|
user = account.cofprofile.user
|
|
|
|
except Account.DoesNotExist:
|
|
|
|
return None
|
|
|
|
|
|
|
|
return user
|
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:
|
|
|
|
user, created = User.objects.get_or_create(username='kfet_genericteam')
|
|
|
|
perm_is_team = Permission.objects.get(codename='is_team')
|
|
|
|
user.user_permissions.add(perm_is_team)
|
|
|
|
return user
|
|
|
|
return None
|
|
|
|
|
|
|
|
def get_user(self, user_id):
|
|
|
|
try:
|
|
|
|
return User.objects.get(pk=user_id)
|
|
|
|
except User.DoesNotExist:
|
|
|
|
return None
|