From 7c4172ef71a0805f16b8bd89188bb1b4d3e33f9b Mon Sep 17 00:00:00 2001 From: zeripath Date: Wed, 4 Aug 2021 18:26:30 +0100 Subject: Pass down SignedUserName down to AccessLogger context (#16605) * Pass down SignedUserName down to AccessLogger context Unfortunately when the AccessLogger was moved back before the contexters the SignedUserName reporting was lost. This is due to Request.WithContext leading to a shallow copy of the Request and the modules/context/Context being within that request. This PR adds a new context variable of a string pointer which is set and handled in the contexters. Fix #16600 Signed-off-by: Andrew Thornton * handle nil ptr issue Signed-off-by: Andrew Thornton Co-authored-by: Lunny Xiao --- modules/context/api.go | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'modules/context/api.go') diff --git a/modules/context/api.go b/modules/context/api.go index a21e0d7402..8f1ed3f2ce 100644 --- a/modules/context/api.go +++ b/modules/context/api.go @@ -275,6 +275,17 @@ func APIContexter() func(http.Handler) http.Handler { ctx.Data["CsrfToken"] = html.EscapeString(ctx.csrf.GetToken()) next.ServeHTTP(ctx.Resp, ctx.Req) + + // Handle adding signedUserName to the context for the AccessLogger + usernameInterface := ctx.Data["SignedUserName"] + identityPtrInterface := ctx.Req.Context().Value(signedUserNameStringPointerKey) + if usernameInterface != nil && identityPtrInterface != nil { + username := usernameInterface.(string) + identityPtr := identityPtrInterface.(*string) + if identityPtr != nil && username != "" { + *identityPtr = username + } + } }) } } -- cgit v1.2.3