Skip to content

Commit 37b2931

Browse files
lunny6543
andauthored
Fix bug of get context user (#17169)
Co-authored-by: 6543 <[email protected]>
1 parent a82c799 commit 37b2931

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
@@ -547,6 +547,17 @@ func GetContext(req *http.Request) *Context {
547547
return req.Context().Value(contextKey).(*Context)
548548
}
549549

550+
// GetContextUser returns context user
551+
func GetContextUser(req *http.Request) *models.User {
552+
if apiContext, ok := req.Context().Value(apiContextKey).(*APIContext); ok {
553+
return apiContext.User
554+
}
555+
if ctx, ok := req.Context().Value(contextKey).(*Context); ok {
556+
return ctx.User
557+
}
558+
return nil
559+
}
560+
550561
// SignedUserName returns signed user's name via context
551562
func SignedUserName(req *http.Request) string {
552563
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)