File tree Expand file tree Collapse file tree 3 files changed +28
-4
lines changed Expand file tree Collapse file tree 3 files changed +28
-4
lines changed Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ package context
6
6
7
7
import (
8
8
"bytes"
9
+ "context"
9
10
"html/template"
10
11
"net/http"
11
12
"time"
@@ -22,18 +23,19 @@ type routerLoggerOptions struct {
22
23
Ctx map [string ]interface {}
23
24
}
24
25
26
+ var signedUserNameStringPointerKey interface {} = "signedUserNameStringPointerKey"
27
+
25
28
// AccessLogger returns a middleware to log access logger
26
29
func AccessLogger () func (http.Handler ) http.Handler {
27
30
logger := log .GetLogger ("access" )
28
31
logTemplate , _ := template .New ("log" ).Parse (setting .AccessLogTemplate )
29
32
return func (next http.Handler ) http.Handler {
30
33
return http .HandlerFunc (func (w http.ResponseWriter , req * http.Request ) {
31
34
start := time .Now ()
32
- next .ServeHTTP (w , req )
33
35
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 )
37
39
rw := w .(ResponseWriter )
38
40
39
41
buf := bytes .NewBuffer ([]byte {})
Original file line number Diff line number Diff line change @@ -275,6 +275,17 @@ func APIContexter() func(http.Handler) http.Handler {
275
275
ctx .Data ["CsrfToken" ] = html .EscapeString (ctx .csrf .GetToken ())
276
276
277
277
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
+ }
278
289
})
279
290
}
280
291
}
Original file line number Diff line number Diff line change @@ -734,6 +734,17 @@ func Contexter() func(next http.Handler) http.Handler {
734
734
}
735
735
736
736
next .ServeHTTP (ctx .Resp , ctx .Req )
737
+
738
+ // Handle adding signedUserName to the context for the AccessLogger
739
+ usernameInterface := ctx .Data ["SignedUserName" ]
740
+ identityPtrInterface := ctx .Req .Context ().Value (signedUserNameStringPointerKey )
741
+ if usernameInterface != nil && identityPtrInterface != nil {
742
+ username := usernameInterface .(string )
743
+ identityPtr := identityPtrInterface .(* string )
744
+ if identityPtr != nil && username != "" {
745
+ * identityPtr = username
746
+ }
747
+ }
737
748
})
738
749
}
739
750
}
You can’t perform that action at this time.
0 commit comments