gestioCOF/kfet/middleware.py

30 lines
935 B
Python
Raw Normal View History

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
from kfet.backends import KFetBackend
2017-04-06 14:15:03 +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.
"""
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)
)
kfet_backend = KFetBackend()
temp_request_user = kfet_backend.authenticate(request)
if temp_request_user:
request.real_user = request.user
request.user = temp_request_user