|
22 | 22 | from django.conf import settings |
23 | 23 | from django.contrib import auth |
24 | 24 | from django.contrib.auth import login as django_login |
| 25 | +from django.contrib.auth.models import User |
25 | 26 | from django.utils.deprecation import MiddlewareMixin |
26 | 27 | from django.utils.functional import SimpleLazyObject |
27 | 28 |
|
@@ -62,8 +63,25 @@ def get_user(request): |
62 | 63 | if not request.session.get('has_demo_data'): |
63 | 64 | request.session['has_demo_data'] = False |
64 | 65 |
|
| 66 | + # if auth proxy header is setup, then create the user |
| 67 | + # as authentication has already happened. |
| 68 | + auth_proxy_header = settings.WGER_SETTINGS.get("AUTH_PROXY_HEADER") |
| 69 | + if auth_proxy_header: |
| 70 | + auth_proxy_header_django = "HTTP_" + auth_proxy_header.replace("-", "_").upper() |
| 71 | + username = request.META.get(auth_proxy_header_django) |
| 72 | + logger.debug(f'using auth_proxy_header "{auth_proxy_header}" got username "{username}"') |
| 73 | + |
| 74 | + if username: |
| 75 | + user_query = User.objects.filter(username=username) |
| 76 | + if user_query.exists(): |
| 77 | + user = user_query.first() |
| 78 | + else: |
| 79 | + user = User.objects.create_user(username) |
| 80 | + user.save() |
| 81 | + |
| 82 | + django_login(request, user, backend='django.contrib.auth.backends.ModelBackend') |
65 | 83 | # Django didn't find a user, so create one now |
66 | | - if ( |
| 84 | + elif ( |
67 | 85 | settings.WGER_SETTINGS['ALLOW_GUEST_USERS'] |
68 | 86 | and request.method == 'GET' |
69 | 87 | and create_user |
|
0 commit comments