decorator: Add logging data to zulip_login_required.

This fixes an issue that many logged=in pages such as /stats did not
correctly report either the connecting client or the user in server
logs.
This commit is contained in:
Tim Abbott
2017-02-20 11:55:18 -08:00
parent d4206f2f5f
commit 20f9c04ab5
4 changed files with 15 additions and 10 deletions

View File

@@ -299,6 +299,16 @@ def logged_in_and_active(request):
return False return False
return check_subdomain(get_subdomain(request), request.user.realm.subdomain) return check_subdomain(get_subdomain(request), request.user.realm.subdomain)
def add_logging_data(view_func):
# type: (ViewFuncT) -> ViewFuncT
@wraps(view_func)
def _wrapped_view_func(request, *args, **kwargs):
# type: (HttpRequest, *Any, **Any) -> HttpResponse
request._email = request.user.email
process_client(request, request.user, is_json_view=True)
return view_func(request, *args, **kwargs)
return _wrapped_view_func # type: ignore # https://github.com/python/mypy/issues/1927
# Based on Django 1.8's @login_required # Based on Django 1.8's @login_required
def zulip_login_required(function=None, def zulip_login_required(function=None,
redirect_field_name=REDIRECT_FIELD_NAME, redirect_field_name=REDIRECT_FIELD_NAME,
@@ -310,7 +320,8 @@ def zulip_login_required(function=None,
redirect_field_name=redirect_field_name redirect_field_name=redirect_field_name
) )
if function: if function:
return actual_decorator(function) # Add necessary logging data via add_logging_data
return actual_decorator(add_logging_data(function))
return actual_decorator return actual_decorator
def zulip_internal(view_func): def zulip_internal(view_func):
@@ -323,9 +334,7 @@ def zulip_internal(view_func):
if not request.user.is_staff: if not request.user.is_staff:
return HttpResponseRedirect(settings.HOME_NOT_LOGGED_IN) return HttpResponseRedirect(settings.HOME_NOT_LOGGED_IN)
request._email = request.user.email return add_logging_data(view_func)(request, *args, **kwargs)
process_client(request, request.user)
return view_func(request, *args, **kwargs)
return _wrapped_view_func # type: ignore # https://github.com/python/mypy/issues/1927 return _wrapped_view_func # type: ignore # https://github.com/python/mypy/issues/1927
# authenticated_api_view will add the authenticated user's # authenticated_api_view will add the authenticated user's

View File

@@ -107,9 +107,6 @@ def home_real(request):
request.session.modified = True request.session.modified = True
user_profile = request.user user_profile = request.user
request._email = request.user.email
# Process the client as an auth decorator would
process_client(request, user_profile, is_json_view=True)
# If a user hasn't signed the current Terms of Service, send them there # If a user hasn't signed the current Terms of Service, send them there
if settings.TERMS_OF_SERVICE is not None and settings.TOS_VERSION is not None and \ if settings.TERMS_OF_SERVICE is not None and settings.TOS_VERSION is not None and \

View File

@@ -27,8 +27,7 @@ from zerver.forms import RegistrationForm, HomepageForm, RealmCreationForm, \
from zerver.lib.actions import is_inactive from zerver.lib.actions import is_inactive
from django_auth_ldap.backend import LDAPBackend, _LDAPUser from django_auth_ldap.backend import LDAPBackend, _LDAPUser
from zerver.decorator import require_post, has_request_variables, \ from zerver.decorator import require_post, has_request_variables, \
JsonableError, get_user_profile_by_email, REQ, \ JsonableError, get_user_profile_by_email, REQ
zulip_login_required
from zerver.lib.response import json_success from zerver.lib.response import json_success
from zerver.lib.utils import get_subdomain from zerver.lib.utils import get_subdomain
from zproject.backends import password_auth_enabled from zproject.backends import password_auth_enabled

View File

@@ -6,7 +6,7 @@ from django.http import HttpRequest, HttpResponse, HttpResponseForbidden, FileRe
from django.shortcuts import redirect from django.shortcuts import redirect
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
from zerver.decorator import authenticated_json_post_view, zulip_login_required from zerver.decorator import authenticated_json_post_view
from zerver.lib.request import has_request_variables, REQ from zerver.lib.request import has_request_variables, REQ
from zerver.lib.response import json_success, json_error from zerver.lib.response import json_success, json_error
from zerver.lib.upload import upload_message_image_from_request, get_local_file_path, \ from zerver.lib.upload import upload_message_image_from_request, get_local_file_path, \