Commit 64dd05d8b0b0496b5701144e2a41b6aad112e11d

Authored by Carlos Coêlho
Committed by Sergio Oliveira
1 parent d0ef3ba4

Setting HTTP header from middleware

Created middleware that sets HTTP header with user data

Signed-off-by: Carlos Oliveira <carlospecter@gmail.com>
Signed-off-by: Alexandre Barbosa <alexandreab@gmail.com>
Showing 1 changed file with 33 additions and 0 deletions   Show diff stats
colab/accounts/middleware.py 0 → 100644
... ... @@ -0,0 +1,33 @@
  1 +from json import dumps as json_dumps
  2 +
  3 +from colab.accounts.models import User
  4 +from colab.proxy.utils.views import ColabProxyView
  5 +
  6 +
  7 +class RemoteUserMiddleware(object):
  8 +
  9 + def process_view(self, request, view_func, view_args, view_kwargs):
  10 + if not request.user.is_authenticated():
  11 + return
  12 +
  13 + if not hasattr(view_func, 'im_class'):
  14 + return
  15 +
  16 + if not issubclass(view_func.im_class, ColabProxyView):
  17 + return
  18 +
  19 + user = User.objects.get(
  20 + username=request.user.get_username()
  21 + )
  22 +
  23 + remote_user_data = {}
  24 +
  25 + remote_user_data['EMAIL'] = user.email
  26 + remote_user_data['NAME'] = user.username
  27 +
  28 + request.META['REMOTE_USER_DATA'] = json_dumps(
  29 + remote_user_data,
  30 + sort_keys=True,
  31 + )
  32 +
  33 + return None
... ...