Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions modules/context/access_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package context

import (
"bytes"
"context"
"html/template"
"net/http"
"time"
Expand All @@ -22,18 +23,19 @@ type routerLoggerOptions struct {
Ctx map[string]interface{}
}

var signedUserNameStringPointerKey interface{} = "signedUserNameStringPointerKey"

// AccessLogger returns a middleware to log access logger
func AccessLogger() func(http.Handler) http.Handler {
logger := log.GetLogger("access")
logTemplate, _ := template.New("log").Parse(setting.AccessLogTemplate)
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
start := time.Now()
next.ServeHTTP(w, req)
identity := "-"
if val := SignedUserName(req); val != "" {
identity = val
}
r := req.WithContext(context.WithValue(req.Context(), signedUserNameStringPointerKey, &identity))

next.ServeHTTP(w, r)
rw := w.(ResponseWriter)

buf := bytes.NewBuffer([]byte{})
Expand Down
11 changes: 11 additions & 0 deletions modules/context/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
})
}
}
Expand Down
11 changes: 11 additions & 0 deletions modules/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,17 @@ func Contexter() func(next http.Handler) http.Handler {
}

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
}
}
})
}
}