Skip to content

Commit f784e85

Browse files
committed
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 <[email protected]>
1 parent 1fc7d6d commit f784e85

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

modules/context/access_log.go

Lines changed: 6 additions & 4 deletions
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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,13 @@ 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+
username := ctx.Data["SignedUserName"].(string)
281+
identityPtr := ctx.Req.Context().Value(signedUserNameStringPointerKey).(*string)
282+
if identityPtr != nil && username != "" {
283+
*identityPtr = username
284+
}
278285
})
279286
}
280287
}

modules/context/context.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -770,6 +770,14 @@ func Contexter() func(next http.Handler) http.Handler {
770770
}
771771

772772
next.ServeHTTP(ctx.Resp, ctx.Req)
773+
774+
// Handle adding signedUserName to the context for the AccessLogger
775+
username := ctx.Data["SignedUserName"].(string)
776+
identityPtr := ctx.Req.Context().Value(signedUserNameStringPointerKey).(*string)
777+
if identityPtr != nil && username != "" {
778+
*identityPtr = username
779+
}
780+
773781
})
774782
}
775783
}

0 commit comments

Comments
 (0)