浏览代码

Fix bug of get context user (#17169)

Co-authored-by: 6543 <6543@obermui.de>
tags/v1.16.0-rc1
Lunny Xiao 2 年前
父节点
当前提交
37b29319aa
没有帐户链接到提交者的电子邮件
共有 2 个文件被更改,包括 12 次插入10 次删除
  1. 11
    0
      modules/context/context.go
  2. 1
    10
      routers/web/base.go

+ 11
- 0
modules/context/context.go 查看文件

@@ -547,6 +547,17 @@ func GetContext(req *http.Request) *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
func SignedUserName(req *http.Request) string {
if middleware.IsInternalPath(req) {

+ 1
- 10
routers/web/base.go 查看文件

@@ -14,7 +14,6 @@ import (
"path/filepath"
"strings"

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

var user *models.User
if apiContext := context.GetAPIContext(req); apiContext != nil {
user = apiContext.User
}
if user == nil {
if ctx := context.GetContext(req); ctx != nil {
user = ctx.User
}
}
var user = context.GetContextUser(req)
if user == nil {
// Get user from session if logged in - do not attempt to sign-in
user = auth.SessionUser(sessionStore)

正在加载...
取消
保存