Skip to content

Commit 719e2f2

Browse files
zeripathlunny
andauthored
Pass down SignedUserName down to AccessLogger context (#16605) (#16616)
Backport #16605 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 <art27@cantab.net> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
1 parent 40687a2 commit 719e2f2

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

modules/context/access_log.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package context
66

77
import (
88
"bytes"
9+
"context"
910
"html/template"
1011
"net/http"
1112
"time"
@@ -22,18 +23,19 @@ type routerLoggerOptions struct {
2223
Ctx map[string]interface{}
2324
}
2425

26+
var signedUserNameStringPointerKey interface{} = "signedUserNameStringPointerKey"
27+
2528
// AccessLogger returns a middleware to log access logger
2629
func AccessLogger() func(http.Handler) http.Handler {
2730
logger := log.GetLogger("access")
2831
logTemplate, _ := template.New("log").Parse(setting.AccessLogTemplate)
2932
return func(next http.Handler) http.Handler {
3033
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
3134
start := time.Now()
32-
next.ServeHTTP(w, req)
3335
identity := "-"
34-
if val := SignedUserName(req); val != "" {
35-
identity = val
36-
}
36+
r := req.WithContext(context.WithValue(req.Context(), signedUserNameStringPointerKey, &identity))
37+
38+
next.ServeHTTP(w, r)
3739
rw := w.(ResponseWriter)
3840

3941
buf := bytes.NewBuffer([]byte{})

modules/context/api.go

+11
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,17 @@ func APIContexter() func(http.Handler) http.Handler {
275275
ctx.Data["CsrfToken"] = html.EscapeString(ctx.csrf.GetToken())
276276

277277
next.ServeHTTP(ctx.Resp, ctx.Req)
278+
279+
// Handle adding signedUserName to the context for the AccessLogger
280+
usernameInterface := ctx.Data["SignedUserName"]
281+
identityPtrInterface := ctx.Req.Context().Value(signedUserNameStringPointerKey)
282+
if usernameInterface != nil && identityPtrInterface != nil {
283+
username := usernameInterface.(string)
284+
identityPtr := identityPtrInterface.(*string)
285+
if identityPtr != nil && username != "" {
286+
*identityPtr = username
287+
}
288+
}
278289
})
279290
}
280291
}

modules/context/context.go

+11
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,17 @@ func Contexter() func(next http.Handler) http.Handler {
774774
}
775775

776776
next.ServeHTTP(ctx.Resp, ctx.Req)
777+
778+
// Handle adding signedUserName to the context for the AccessLogger
779+
usernameInterface := ctx.Data["SignedUserName"]
780+
identityPtrInterface := ctx.Req.Context().Value(signedUserNameStringPointerKey)
781+
if usernameInterface != nil && identityPtrInterface != nil {
782+
username := usernameInterface.(string)
783+
identityPtr := identityPtrInterface.(*string)
784+
if identityPtr != nil && username != "" {
785+
*identityPtr = username
786+
}
787+
}
777788
})
778789
}
779790
}

0 commit comments

Comments
 (0)