Fix bug of get context user (#17169) (#17172)

Co-authored-by: 6543 <6543@obermui.de>
release/v1.15
Lunny Xiao 2021-09-28 21:42:43 +08:00 committed by GitHub
parent 4707d4b8a9
commit 766272b154
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 10 deletions

View File

@ -587,6 +587,17 @@ func GetContext(req *http.Request) *Context {
return req.Context().Value(contextKey).(*Context) return req.Context().Value(contextKey).(*Context)
} }
// GetContextUser returns context user
func GetContextUser(req *http.Request) *models.User {
if apiContext, ok := req.Context().Value(apiContextKey).(*APIContext); ok {
return apiContext.User
}
if ctx, ok := req.Context().Value(contextKey).(*Context); ok {
return ctx.User
}
return nil
}
// SignedUserName returns signed user's name via context // SignedUserName returns signed user's name via context
func SignedUserName(req *http.Request) string { func SignedUserName(req *http.Request) string {
if middleware.IsInternalPath(req) { if middleware.IsInternalPath(req) {

View File

@ -14,7 +14,6 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/httpcache" "code.gitea.io/gitea/modules/httpcache"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
@ -147,15 +146,7 @@ func Recovery() func(next http.Handler) http.Handler {
"i18n": lc, "i18n": lc,
} }
var user *models.User var user = context.GetContextUser(req)
if apiContext := context.GetAPIContext(req); apiContext != nil {
user = apiContext.User
}
if user == nil {
if ctx := context.GetContext(req); ctx != nil {
user = ctx.User
}
}
if user == nil { if user == nil {
// Get user from session if logged in - do not attempt to sign-in // Get user from session if logged in - do not attempt to sign-in
user = auth.SessionUser(sessionStore) user = auth.SessionUser(sessionStore)