fix(web): keep admin and RBAC desk quiet until opted in
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 <cursoragent@cursor.com>
This commit is contained in:
1 parent
2c91b5cc7b
commit
b752cd49b7
7 files changed
+74
-19
No files matched your search
@@ -2,8 +2,8 @@
|
|||||||
|
|
||||||
| | |
|
| | |
|
||||||
|---|---|
|
|---|---|
|
||||||
| 验收 | qdqc:注册登录、评论、desk 内建角色/赋权;`tests/ext/web-rbac-*-smoke.mq.md` |
|
| 验收 | qdqc:门禁 `desk:access` + 评论权限;`tests/ext/web-rbac-*-smoke.mq.md` |
|
||||||
| 状态 | **Accepted · 已落地核心**(HTTP register + `/_rbac/*` + gate permissions;样站 qdqc 已接线) |
|
| 状态 | **Accepted · 已落地核心**(gate permissions;`/_rbac/*` 管理台 **opt-in** `管理台=真`) |
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -43,7 +43,7 @@
|
|||||||
|
|
||||||
| 方法 | ABI | 说明 |
|
| 方法 | 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 时按权限判定 |
|
| `app.gate` | `web_app_gate` | 新增 `permissions`/`权限`(CSV);与 `roles` 可并存:有 permissions 时按权限判定 |
|
||||||
| `rbac.can` | `web_rbac_can` | `{ok, allowed}` |
|
| `rbac.can` | `web_rbac_can` | `{ok, allowed}` |
|
||||||
| `rbac.assign_role` | `web_rbac_assign_role` | 用户←角色 |
|
| `rbac.assign_role` | `web_rbac_assign_role` | 用户←角色 |
|
||||||
@@ -56,9 +56,10 @@
|
|||||||
## 4. 运行时
|
## 4. 运行时
|
||||||
|
|
||||||
1. `listen`:若 `auth.rbac=true` 或调用过 `app.rbac`,对 `db` URL `EnsureSchema` + 种子。
|
1. `listen`:若 `auth.rbac=true` 或调用过 `app.rbac`,对 `db` URL `EnsureSchema` + 种子。
|
||||||
2. 登录成功:解析角色 → DB `user_roles` ∪ GFM 角色展开 → session `permissions`(CSV)+ 兼容 `role`。
|
2. 仅当 `auth.rbac_desk=true`(`启用权限 管理台=真`)时挂载 `/_rbac/*` API 与 desk。
|
||||||
3. `withRBAC`:gate 含 `permissions` 则 `PermissionAllowed`;否则旧 `RoleAllowed`。
|
3. 登录成功:解析角色 → DB `user_roles` ∪ GFM 角色展开 → session `permissions`(CSV)+ 兼容 `role`。
|
||||||
4. 注册:`POST /register` 写入 `web_users`,默认赋 `member` 角色。
|
4. `withRBAC`:gate 含 `permissions` 则 `PermissionAllowed`;否则旧 `RoleAllowed`。
|
||||||
|
5. 注册:`POST /register` 写入 `web_users`,默认赋 `member` 角色。
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|||||||
+4
-3
@@ -707,7 +707,7 @@ Localized submit / cancel copy for rendered forms.
|
|||||||
+ `layout`=None
|
+ `layout`=None
|
||||||
+ `asset_version`=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
|
> 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`*
|
*> 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
|
## enable_rbac
|
||||||
+ `catalog`=None
|
+ `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
|
## tenant
|
||||||
+ `mode`
|
+ `mode`
|
||||||
|
|||||||
+4
-3
@@ -680,7 +680,7 @@ SSR 顶栏刊名 Logo、主题切换钮与目录抽屉钮。交互写在 `client
|
|||||||
+ `布局`=None
|
+ `布局`=None
|
||||||
+ `资源版本`=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=`资源版本`*
|
*> 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
|
+ `目录`=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=`管理台`*
|
||||||
|
|
||||||
## 租户
|
## 租户
|
||||||
+ `模式`
|
+ `模式`
|
||||||
|
|||||||
@@ -220,7 +220,9 @@ func Auth(appBag map[string]any, users any, opts map[string]any) (map[string]any
|
|||||||
}
|
}
|
||||||
out["auth"] = authBag
|
out["auth"] = authBag
|
||||||
gates := gatesOf(out)
|
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{
|
gates = append(gates, map[string]any{
|
||||||
"path": prefix,
|
"path": prefix,
|
||||||
"roles": []any{"admin"},
|
"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",
|
"on_deny": "redirect",
|
||||||
"exclude": []any{loginPath},
|
"exclude": []any{loginPath},
|
||||||
})
|
})
|
||||||
out["gates"] = gates
|
|
||||||
} else {
|
|
||||||
out["gates"] = gates
|
|
||||||
}
|
}
|
||||||
|
out["gates"] = gates
|
||||||
return out, nil
|
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).
|
// 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) {
|
func Rbac(appBag map[string]any, opts map[string]any) (map[string]any, error) {
|
||||||
out := clone(appBag)
|
out := clone(appBag)
|
||||||
authBag, _ := out["auth"].(map[string]any)
|
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 = clone(authBag)
|
||||||
}
|
}
|
||||||
authBag["rbac"] = true
|
authBag["rbac"] = true
|
||||||
|
authBag["rbac_desk"] = false
|
||||||
if opts != nil {
|
if opts != nil {
|
||||||
if v, ok := first(opts, "catalog", "目录"); ok && v != nil {
|
if v, ok := first(opts, "catalog", "目录"); ok && v != nil {
|
||||||
authBag["permission_catalog"] = v
|
authBag["permission_catalog"] = v
|
||||||
}
|
}
|
||||||
|
if v, ok := first(opts, "desk", "管理台"); ok {
|
||||||
|
authBag["rbac_desk"] = boolish(v)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
out["auth"] = authBag
|
out["auth"] = authBag
|
||||||
return out, nil
|
return out, nil
|
||||||
|
|||||||
@@ -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) {
|
func TestAuthCustomPrefix(t *testing.T) {
|
||||||
a := app.New(map[string]any{"admin": true})
|
a := app.New(map[string]any{"admin": true})
|
||||||
out, err := app.Auth(a, []any{}, map[string]any{
|
out, err := app.Auth(a, []any{}, map[string]any{
|
||||||
|
|||||||
@@ -51,6 +51,7 @@ type authConfig struct {
|
|||||||
logoutRedirect string
|
logoutRedirect string
|
||||||
gates []gate
|
gates []gate
|
||||||
rbac bool
|
rbac bool
|
||||||
|
rbacDesk bool
|
||||||
register bool
|
register bool
|
||||||
registerPath string
|
registerPath string
|
||||||
defaultRole string
|
defaultRole string
|
||||||
@@ -86,6 +87,7 @@ func authConfigOf(appBag map[string]any) authConfig {
|
|||||||
cfg.sessionTTL = n
|
cfg.sessionTTL = n
|
||||||
}
|
}
|
||||||
cfg.rbac = boolish(authBag["rbac"])
|
cfg.rbac = boolish(authBag["rbac"])
|
||||||
|
cfg.rbacDesk = boolish(authBag["rbac_desk"])
|
||||||
cfg.register = boolish(authBag["register"])
|
cfg.register = boolish(authBag["register"])
|
||||||
if p := strOpt(authBag, "register_path", ""); p != "" {
|
if p := strOpt(authBag, "register_path", ""); p != "" {
|
||||||
cfg.registerPath = p
|
cfg.registerPath = p
|
||||||
@@ -396,7 +398,8 @@ func (st *state) mountAuthRoutes(mux *http.ServeMux) {
|
|||||||
}
|
}
|
||||||
mux.HandleFunc("POST "+rp, st.handleRegisterPost)
|
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/roles", st.handleRbacListRoles)
|
||||||
mux.HandleFunc("GET /_rbac/permissions", st.handleRbacListPermissions)
|
mux.HandleFunc("GET /_rbac/permissions", st.handleRbacListPermissions)
|
||||||
mux.HandleFunc("POST /_rbac/roles", st.handleRbacCreateRole)
|
mux.HandleFunc("POST /_rbac/roles", st.handleRbacCreateRole)
|
||||||
|
|||||||
@@ -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_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_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_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_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_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;
|
if (host_register((char *)"web_rbac_ensure", (char *)"url", web_rbac_ensure) != 0) return 1;
|
||||||
|
|||||||
Reference in new issue
Block a user