From b752cd49b7b51dbb9377d79f4f005526bd14dc87 Mon Sep 17 00:00:00 2001 From: cflmy Date: Fri, 18 Sep 2026 16:01:40 +0800 Subject: [PATCH] fix(web): keep admin and RBAC desk quiet until opted in MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Skip auto-gating admin_prefix when admin=false, and mount /_rbac/* only when enable_rbac sets desk/管理台 so custom desks are not interfered with. Co-authored-by: Cursor --- doc/design/ext-web-rbac.md | 13 ++++---- ext/web/web.mq.md | 7 +++-- ext/web/网页.mq.md | 7 +++-- plugins/web/internal/app/app.go | 15 ++++++---- plugins/web/internal/app/app_test.go | 44 ++++++++++++++++++++++++++++ plugins/web/internal/httpx/auth.go | 5 +++- plugins/web/plugin.go | 2 +- 7 files changed, 74 insertions(+), 19 deletions(-) diff --git a/doc/design/ext-web-rbac.md b/doc/design/ext-web-rbac.md index ce17984..887dc46 100644 --- a/doc/design/ext-web-rbac.md +++ b/doc/design/ext-web-rbac.md @@ -2,8 +2,8 @@ | | | |---|---| -| 验收 | qdqc:注册登录、评论、desk 内建角色/赋权;`tests/ext/web-rbac-*-smoke.mq.md` | -| 状态 | **Accepted · 已落地核心**(HTTP register + `/_rbac/*` + gate permissions;样站 qdqc 已接线) | +| 验收 | qdqc:门禁 `desk:access` + 评论权限;`tests/ext/web-rbac-*-smoke.mq.md` | +| 状态 | **Accepted · 已落地核心**(gate permissions;`/_rbac/*` 管理台 **opt-in** `管理台=真`) | --- @@ -43,7 +43,7 @@ | 方法 | ABI | 说明 | |------|-----|------| -| `app.rbac` / `权限` | `web_app_rbac` | ensure schema + optional seed catalog table | +| `app.rbac` / `启用权限` | `web_app_rbac` | ensure schema;可选 `目录`;`管理台=真` 才挂 `/_rbac/*` | | `app.gate` | `web_app_gate` | 新增 `permissions`/`权限`(CSV);与 `roles` 可并存:有 permissions 时按权限判定 | | `rbac.can` | `web_rbac_can` | `{ok, allowed}` | | `rbac.assign_role` | `web_rbac_assign_role` | 用户←角色 | @@ -56,9 +56,10 @@ ## 4. 运行时 1. `listen`:若 `auth.rbac=true` 或调用过 `app.rbac`,对 `db` URL `EnsureSchema` + 种子。 -2. 登录成功:解析角色 → DB `user_roles` ∪ GFM 角色展开 → session `permissions`(CSV)+ 兼容 `role`。 -3. `withRBAC`:gate 含 `permissions` 则 `PermissionAllowed`;否则旧 `RoleAllowed`。 -4. 注册:`POST /register` 写入 `web_users`,默认赋 `member` 角色。 +2. 仅当 `auth.rbac_desk=true`(`启用权限 管理台=真`)时挂载 `/_rbac/*` API 与 desk。 +3. 登录成功:解析角色 → DB `user_roles` ∪ GFM 角色展开 → session `permissions`(CSV)+ 兼容 `role`。 +4. `withRBAC`:gate 含 `permissions` 则 `PermissionAllowed`;否则旧 `RoleAllowed`。 +5. 注册:`POST /register` 写入 `web_users`,默认赋 `member` 角色。 --- diff --git a/ext/web/web.mq.md b/ext/web/web.mq.md index 38bd699..4cb76d1 100644 --- a/ext/web/web.mq.md +++ b/ext/web/web.mq.md @@ -707,7 +707,7 @@ Localized submit / cancel copy for rendered forms. + `layout`=None + `asset_version`=None -Construct an app. `admin=True` mounts the built-in CRUD UI under `admin_prefix` (default `/admin`). When `admin=False`, that prefix is not reserved — you may `route` your own pages there. Optional `login_redirect` / `logout_redirect` override post-auth landing. Optional `shell_css` / `layout` / `asset_version` become defaults for pages that do not set their own (see `page.shell_css` / `page.layout` / `page.asset_version`). +Construct an app. `admin=True` mounts the built-in **table browser** (not a CMS) under `admin_prefix` (default `/admin`). When `admin=False`, that prefix is not reserved and is not auto-gated — declare your own `route` + `gate` desk. Optional `login_redirect` / `logout_redirect` override post-auth landing. Optional `shell_css` / `layout` / `asset_version` become defaults for pages that do not set their own (see `page.shell_css` / `page.layout` / `page.asset_version`). > ensure_plugin *> web_app_new page=`page` db=`db` admin=`admin` host=`host` port=`port` admin_prefix=`admin_prefix` login_redirect=`login_redirect` logout_redirect=`logout_redirect` shell_css=`shell_css` layout=`layout` asset_version=`asset_version`* @@ -850,10 +850,11 @@ Require access for `path`. Prefer `permissions` (CSV of `resource:action` codes ## enable_rbac + `catalog`=None + + `desk`=False -Enable database-backed RBAC (role ⊥ permission). On listen, ensures `web_permissions` / `web_roles` / `web_role_permissions` / `web_users` / `web_user_roles` and seeds system roles `superadmin` and `member`. See `doc/design/ext-web-rbac.md`. Management UI: `GET /_rbac/desk` (needs `roles:manage`). +Enable database-backed RBAC (role ⊥ permission). On listen, ensures `web_permissions` / `web_roles` / `web_role_permissions` / `web_users` / `web_user_roles` and seeds system roles `superadmin` and `member`. See `doc/design/ext-web-rbac.md`. By default **no** `/_rbac/*` routes; set `desk=True` to mount the API and `GET /_rbac/desk` (needs `roles:manage`). Permission gates work either way. -*> web_app_rbac app=`self` catalog=`catalog`* +*> web_app_rbac app=`self` catalog=`catalog` desk=`desk`* ## tenant + `mode` diff --git a/ext/web/网页.mq.md b/ext/web/网页.mq.md index a541a48..8a2d397 100644 --- a/ext/web/网页.mq.md +++ b/ext/web/网页.mq.md @@ -680,7 +680,7 @@ SSR 顶栏刊名 Logo、主题切换钮与目录抽屉钮。交互写在 `client + `布局`=None + `资源版本`=None -构造应用。`后台=真` 时在 `后台前缀`(默认 `/admin`)挂载内置 CRUD。`后台=假` 时该前缀不再保留,可自行 `路由` 到 `/admin…`。可选 `登录回跳` / `登出回跳`。可选 `壳样式` / `布局` / `资源版本` 作为未自设页面的默认(见 `页面.壳样式` / `页面.布局` / `页面.资源版本`)。 +构造应用。`后台=真` 时在 `后台前缀`(默认 `/admin`)挂载**内置表浏览器**(不是 CMS)。`后台=假` 时该前缀不保留、不自动门禁,可自行 `路由` + `门禁` 做写作台。可选 `登录回跳` / `登出回跳`。可选 `壳样式` / `布局` / `资源版本` 作为未自设页面的默认(见 `页面.壳样式` / `页面.布局` / `页面.资源版本`)。 > 确保插件 *> web_app_new page=`页面` db=`数据库` admin=`后台` host=`主机` port=`端口` admin_prefix=`后台前缀` login_redirect=`登录回跳` logout_redirect=`登出回跳` shell_css=`壳样式` layout=`布局` asset_version=`资源版本`* @@ -818,10 +818,11 @@ SSR 顶栏刊名 Logo、主题切换钮与目录抽屉钮。交互写在 `client ## 启用权限 + `目录`=None + + `管理台`=假 -启用库表 RBAC(角色 ⊥ 权限)。listen 时确保五表并种子 `superadmin` / `member`。见 `doc/design/ext-web-rbac.md`。管理台:`GET /_rbac/desk`(需 `roles:manage`)。 +启用库表 RBAC(角色 ⊥ 权限)。listen 时确保五表并种子 `superadmin` / `member`。见 `doc/design/ext-web-rbac.md`。默认**不**挂 `/_rbac/*`;`管理台=真` 时才开放 API 与 `GET /_rbac/desk`(需 `roles:manage`)。门禁 `权限=` 在两种模式下均可用。 -*> web_app_rbac app=`self` catalog=`目录`* +*> web_app_rbac app=`self` catalog=`目录` desk=`管理台`* ## 租户 + `模式` diff --git a/plugins/web/internal/app/app.go b/plugins/web/internal/app/app.go index 7fcffb7..84c1c8d 100644 --- a/plugins/web/internal/app/app.go +++ b/plugins/web/internal/app/app.go @@ -220,7 +220,9 @@ func Auth(appBag map[string]any, users any, opts map[string]any) (map[string]any } out["auth"] = authBag gates := gatesOf(out) - if !hasAdminGate(gates, prefix) { + // Auto-gate the built-in admin prefix only when admin=true. Custom desks + // (admin=false) declare their own gates and must not inherit /admin noise. + if boolish(out["admin"]) && !hasAdminGate(gates, prefix) { gates = append(gates, map[string]any{ "path": prefix, "roles": []any{"admin"}, @@ -228,10 +230,8 @@ func Auth(appBag map[string]any, users any, opts map[string]any) (map[string]any "on_deny": "redirect", "exclude": []any{loginPath}, }) - out["gates"] = gates - } else { - out["gates"] = gates } + out["gates"] = gates return out, nil } @@ -337,7 +337,8 @@ func Gate(appBag map[string]any, path string, opts map[string]any) (map[string]a } // Rbac enables DB-backed RBAC on the app (ensure schema at listen). -// opts may include catalog table (unused beyond flag for now). +// opts: catalog/目录 (optional seed table); desk/管理台 (bool, default false) — +// when true, listen mounts /_rbac/* API + desk UI; permission gates work either way. func Rbac(appBag map[string]any, opts map[string]any) (map[string]any, error) { out := clone(appBag) authBag, _ := out["auth"].(map[string]any) @@ -347,10 +348,14 @@ func Rbac(appBag map[string]any, opts map[string]any) (map[string]any, error) { authBag = clone(authBag) } authBag["rbac"] = true + authBag["rbac_desk"] = false if opts != nil { if v, ok := first(opts, "catalog", "目录"); ok && v != nil { authBag["permission_catalog"] = v } + if v, ok := first(opts, "desk", "管理台"); ok { + authBag["rbac_desk"] = boolish(v) + } } out["auth"] = authBag return out, nil diff --git a/plugins/web/internal/app/app_test.go b/plugins/web/internal/app/app_test.go index ea9edfb..e40cb74 100644 --- a/plugins/web/internal/app/app_test.go +++ b/plugins/web/internal/app/app_test.go @@ -114,6 +114,50 @@ func TestAuthDefaultGate(t *testing.T) { } } +func TestAuthNoAutoGateWhenAdminOff(t *testing.T) { + a := app.New(map[string]any{"admin": false}) + out, err := app.Auth(a, []any{}, map[string]any{"login_path": "/login"}) + if err != nil { + t.Fatal(err) + } + if gates, ok := out["gates"].([]any); ok && len(gates) != 0 { + t.Fatalf("expected no auto gate when admin=false, got %v", gates) + } +} + +func TestRbacDeskOptIn(t *testing.T) { + a := app.New(map[string]any{"admin": false}) + out, err := app.Rbac(a, nil) + if err != nil { + t.Fatal(err) + } + authBag := out["auth"].(map[string]any) + if !boolishTest(authBag["rbac"]) || boolishTest(authBag["rbac_desk"]) { + t.Fatalf("default rbac_desk should be false: %v", authBag) + } + out2, err := app.Rbac(a, map[string]any{"管理台": true}) + if err != nil { + t.Fatal(err) + } + if !boolishTest(out2["auth"].(map[string]any)["rbac_desk"]) { + t.Fatal("expected rbac_desk true") + } +} + +func boolishTest(v any) bool { + switch x := v.(type) { + case bool: + return x + case string: + s := strings.ToLower(strings.TrimSpace(x)) + return s == "1" || s == "true" || s == "yes" || s == "真" + case float64: + return x != 0 + default: + return false + } +} + func TestAuthCustomPrefix(t *testing.T) { a := app.New(map[string]any{"admin": true}) out, err := app.Auth(a, []any{}, map[string]any{ diff --git a/plugins/web/internal/httpx/auth.go b/plugins/web/internal/httpx/auth.go index 07ea8a8..f5c3f0c 100644 --- a/plugins/web/internal/httpx/auth.go +++ b/plugins/web/internal/httpx/auth.go @@ -51,6 +51,7 @@ type authConfig struct { logoutRedirect string gates []gate rbac bool + rbacDesk bool register bool registerPath string defaultRole string @@ -86,6 +87,7 @@ func authConfigOf(appBag map[string]any) authConfig { cfg.sessionTTL = n } cfg.rbac = boolish(authBag["rbac"]) + cfg.rbacDesk = boolish(authBag["rbac_desk"]) cfg.register = boolish(authBag["register"]) if p := strOpt(authBag, "register_path", ""); p != "" { cfg.registerPath = p @@ -396,7 +398,8 @@ func (st *state) mountAuthRoutes(mux *http.ServeMux) { } mux.HandleFunc("POST "+rp, st.handleRegisterPost) } - if st.auth.rbac && st.dbURL != "" { + // Opt-in: enable_rbac alone wires gates/schema; /_rbac HTTP surface needs desk=true. + if st.auth.rbac && st.auth.rbacDesk && st.dbURL != "" { mux.HandleFunc("GET /_rbac/roles", st.handleRbacListRoles) mux.HandleFunc("GET /_rbac/permissions", st.handleRbacListPermissions) mux.HandleFunc("POST /_rbac/roles", st.handleRbacCreateRole) diff --git a/plugins/web/plugin.go b/plugins/web/plugin.go index 0f550b1..07764a2 100644 --- a/plugins/web/plugin.go +++ b/plugins/web/plugin.go @@ -224,7 +224,7 @@ static int register_core(void) { if (host_register((char *)"web_app_auth", (char *)"app,users,session_ttl,admin_prefix,login_redirect,logout_redirect,login_path,register,register_path,default_role,session_url", web_app_auth) != 0) return 1; if (host_register((char *)"web_app_oidc", (char *)"app,issuer,client_id,client_secret,redirect_uri,scopes,callback_path,authorize_url,token_url,userinfo_url", web_app_oidc) != 0) return 1; if (host_register((char *)"web_app_gate", (char *)"app,path,roles,permissions,match,on_deny,exclude", web_app_gate) != 0) return 1; - if (host_register((char *)"web_app_rbac", (char *)"app,catalog", web_app_rbac) != 0) return 1; + if (host_register((char *)"web_app_rbac", (char *)"app,catalog,desk", web_app_rbac) != 0) return 1; if (host_register((char *)"web_app_tenant", (char *)"app,mode,param,column,default_scope", web_app_tenant) != 0) return 1; if (host_register((char *)"web_rbac_can", (char *)"permissions,needed", web_rbac_can) != 0) return 1; if (host_register((char *)"web_rbac_ensure", (char *)"url", web_rbac_ensure) != 0) return 1;