From 53169264da26aa9d1ae7957e0deae3db224d7471 Mon Sep 17 00:00:00 2001 From: what Date: Tue, 21 Jul 2026 18:02:16 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=8E=A5=E5=8F=A3=E5=B1=82=E5=90=8C?= =?UTF-8?q?=E6=AD=A5=20CallWithUser=20=E4=B8=8E=20ResScope=20=E9=87=8D?= =?UTF-8?q?=E5=91=BD=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit HTTPService.Invoke 新增 caller req.User 参数,指定时内部转发请求认证成该身份,配合 req-v2 新增的 Router.CallWithUser。 HttpController.AuthDB()、Orm.Model.SetAuthDB()、OrmController.AuthDB() 的返回值/参数类型从 req.ResAuthDB 同步改成 req.ResScope,跟 req-v2 那边的重命名保持一致。 --- http.go | 49 ++++++------------------------------------------- orm.go | 8 +++++--- 2 files changed, 11 insertions(+), 46 deletions(-) diff --git a/http.go b/http.go index baa6fef..458c874 100644 --- a/http.go +++ b/http.go @@ -5,13 +5,9 @@ import ( "net/http" "git.fsdpf.net/go/req" - "github.com/samber/do/v2" ) -var defaultWsClientGroup WsClientGroup = "__DEFAULT__" -var wsClientID WsClientID = 0 - -type Http interface { +type HTTPService interface { // Start 启动 HTTP 服务。 Start() error @@ -28,11 +24,14 @@ type Http interface { // - path: 请求路径,如 "/api/user/list"。 // - body: 请求体,无请求体时传 nil。 // - headers: 附加请求头键值对,无需额外请求头时传 nil。 + // - caller: 指定认证中间件应认证成的调用者身份,为 nil 时按 headers 里的 + // Authorization/token 正常解析(等同于外部请求)。只有进程内部 + // 发起调用时才可能传非 nil 值,不受外部请求影响。 // // 返回: // - *http.Response: 包含状态码、响应头及响应体的结果。 // - error: 构造请求失败时返回错误。 - Invoke(method, path string, body io.Reader, header http.Header) (*http.Response, error) + Invoke(method, path string, body io.Reader, header http.Header, caller req.User) (*http.Response, error) } type HttpController interface { @@ -63,41 +62,5 @@ type HttpController interface { Call(code string, params map[string]any, category ...req.RouteMatchOption) (req.HttpResponse, error) // AuthDB 返回当前控制器的数据库权限模式。 - AuthDB() req.ResAuthDB -} - -type HttpHandleController struct { - Controller - request *http.Request -} - -func (this HttpHandleController) Request() *http.Request { - return this.request -} - -func (this HttpHandleController) Route() req.Route { - return this.Request().Context().Value(req.RouteCtx{Name: "Route"}).(req.Route) -} - -func (this HttpHandleController) User() req.User { - return this.Request().Context().Value(req.RouteCtx{Name: "User"}).(req.User) -} - -func (this HttpHandleController) Call(code string, params map[string]any, opts ...req.RouteMatchOption) (req.HttpResponse, error) { - return do.MustInvoke[req.Router](this.Container()).Call(this.Request(), code, params, opts...) -} - -func (HttpHandleController) Execute(params req.GlobalParams) any { - return nil -} - -func (HttpHandleController) AuthDB() req.ResAuthDB { - return req.ResAuthOn -} - -func NewHttpController(container do.Injector, request *http.Request) HttpController { - return &HttpHandleController{ - Controller: &BaseController{container}, - request: request, - } + AuthDB() req.ResScope } diff --git a/orm.go b/orm.go index 276617f..5fb6075 100644 --- a/orm.go +++ b/orm.go @@ -30,7 +30,7 @@ const ( OrmRightJoin RelationType = "right" ) -type ModelDecorator func(Model) Model +type ModelDecorator = func(Model) Model type Orm interface { SetGlobalParams(g req.UserAccessor) Orm @@ -83,7 +83,7 @@ type Model interface { SetQueryField(...req.QueryField) // SetRelationResult(code string, items []reflux.R) GetJoinsCode() []string - SetAuthDB(req.ResAuthDB) + SetAuthDB(req.ResScope) SetOrderBy(params ...OrderBy) Model SetGroupBy(params ...GroupBy) Model SetJoin(Join) Model @@ -103,7 +103,7 @@ type Model interface { } type OrmController interface { - AuthDB() req.ResAuthDB + AuthDB() req.ResScope } type OrderBy interface { @@ -119,6 +119,8 @@ type GroupBy interface { type QueryWithRecursive interface { ToQueryBuilder(*db.SelectDataset, Model) (*db.SelectDataset, any) ToTreeData([]reflux.R) reflux.R + // RootCondition 返回锚点条件(pField = root),供外部如 Count() 在不展开递归查询的情况下复用。 + RootCondition() exp.Expression } // @title 创建关联模型