2016-09-01 00:45:44 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2017-04-06 14:15:03 +02:00
|
|
|
from django.contrib.auth.models import User
|
2016-09-01 00:45:44 +02:00
|
|
|
|
2016-08-19 06:20:37 +02:00
|
|
|
from kfet.backends import KFetBackend
|
2017-04-06 14:15:03 +02:00
|
|
|
|
2016-08-19 06:20:37 +02:00
|
|
|
|
|
|
|
class KFetAuthenticationMiddleware(object):
|
2017-04-06 14:15:03 +02:00
|
|
|
"""Authenticate another user for this request if KFetBackend succeeds.
|
|
|
|
|
|
|
|
By the way, if a user is authenticated, we refresh its from db to add
|
|
|
|
values from CofProfile and Account of this user.
|
|
|
|
|
|
|
|
"""
|
2016-08-19 06:20:37 +02:00
|
|
|
def process_request(self, request):
|
2017-04-06 14:15:03 +02:00
|
|
|
if request.user.is_authenticated():
|
|
|
|
# avoid multiple db accesses in views and templates
|
|
|
|
user_pk = request.user.pk
|
|
|
|
request.user = (
|
|
|
|
User.objects
|
|
|
|
.select_related('profile__account_kfet')
|
|
|
|
.get(pk=user_pk)
|
|
|
|
)
|
|
|
|
|
2016-08-19 06:20:37 +02:00
|
|
|
kfet_backend = KFetBackend()
|
|
|
|
temp_request_user = kfet_backend.authenticate(request)
|
|
|
|
if temp_request_user:
|
2016-08-20 23:31:30 +02:00
|
|
|
request.real_user = request.user
|
2016-08-19 06:20:37 +02:00
|
|
|
request.user = temp_request_user
|