Skip to content

Commit 766272b

Browse files
lunny6543
andauthored
Fix bug of get context user (#17169) (#17172)
Co-authored-by: 6543 <6543@obermui.de>
1 parent 4707d4b commit 766272b

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

modules/context/context.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,17 @@ func GetContext(req *http.Request) *Context {
587587
return req.Context().Value(contextKey).(*Context)
588588
}
589589

590+
// GetContextUser returns context user
591+
func GetContextUser(req *http.Request) *models.User {
592+
if apiContext, ok := req.Context().Value(apiContextKey).(*APIContext); ok {
593+
return apiContext.User
594+
}
595+
if ctx, ok := req.Context().Value(contextKey).(*Context); ok {
596+
return ctx.User
597+
}
598+
return nil
599+
}
600+
590601
// SignedUserName returns signed user's name via context
591602
func SignedUserName(req *http.Request) string {
592603
if middleware.IsInternalPath(req) {

routers/web/base.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"path/filepath"
1515
"strings"
1616

17-
"code.gitea.io/gitea/models"
1817
"code.gitea.io/gitea/modules/context"
1918
"code.gitea.io/gitea/modules/httpcache"
2019
"code.gitea.io/gitea/modules/log"
@@ -147,15 +146,7 @@ func Recovery() func(next http.Handler) http.Handler {
147146
"i18n": lc,
148147
}
149148

150-
var user *models.User
151-
if apiContext := context.GetAPIContext(req); apiContext != nil {
152-
user = apiContext.User
153-
}
154-
if user == nil {
155-
if ctx := context.GetContext(req); ctx != nil {
156-
user = ctx.User
157-
}
158-
}
149+
var user = context.GetContextUser(req)
159150
if user == nil {
160151
// Get user from session if logged in - do not attempt to sign-in
161152
user = auth.SessionUser(sessionStore)

0 commit comments

Comments
 (0)