middleware: Reorder middleware to clean up LogRequests hasattr checks.

Similar to the previous commit, we should access request.user only
after it has been initialized, rather than having awkward hasattr
checks.

With updates to the settings comments about LogRequests by tabbott.

Signed-off-by: Zixuan James Li <p359101898@gmail.com>
This commit is contained in:
Zixuan James Li 2022-07-14 14:15:40 -04:00 committed by Tim Abbott
parent 37a7d1fe7b
commit 75925fe059
2 changed files with 7 additions and 5 deletions

View File

@ -420,7 +420,7 @@ class LogRequests(MiddlewareMixin):
if requestor_for_logs is None:
# Note that request.user is a Union[RemoteZulipServer, UserProfile, AnonymousUser],
# if it is present.
if hasattr(request, "user") and hasattr(request.user, "format_requestor_for_logs"):
if hasattr(request.user, "format_requestor_for_logs"):
requestor_for_logs = request.user.format_requestor_for_logs()
else:
requestor_for_logs = "unauth@{}".format(get_subdomain(request) or "root")

View File

@ -166,19 +166,21 @@ class TwoFactorLoader(app_directories.Loader):
MIDDLEWARE = (
# With the exception of it's dependencies,
# our logging middleware should be the top middleware item.
"zerver.middleware.TagRequests",
"zerver.middleware.SetRemoteAddrFromRealIpHeader",
"zerver.middleware.RequestContext",
"django.contrib.sessions.middleware.SessionMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
# Important: All middleware before LogRequests should be
# inexpensive, because any time spent in that middleware will not
# be counted in the LogRequests instrumentation of how time was
# spent while processing a request.
"zerver.middleware.LogRequests",
"zerver.middleware.JsonErrorHandler",
"zerver.middleware.RateLimitMiddleware",
"zerver.middleware.FlushDisplayRecipientCache",
"zerver.middleware.ZulipCommonMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"zerver.middleware.LocaleMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"zerver.middleware.HostDomainMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"zerver.middleware.ZulipSCIMAuthCheckMiddleware",