feat(web): RBAC, tenant scope, and WS room hooks

Add configurable role⊥permission (five tables, gate permissions, register,
/_rbac desk), multi-tenant path/header resolution with JSON tenant_scope,
and route_ws on_message/presence/dynamic room_key — with author surfaces,
demos, and smokes.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
cflmyandCursor committed 2026-09-17 20:54:02 +08:00
1 parent 27991a7822
commit 5cf84fe9b7
27 files changed
+2372 -80

No files matched your search

+73 -6
View File
@@ -733,10 +733,13 @@ Serve `/`, routed pages, `/_part/{id}` (home) and `{path}/_part/{id}` (routes),
+ `path`
+ `echo`=True
+ `mode`=None
+ `room_key`=None
+ `on_message`=None
+ `presence`=False
Register a WebSocket endpoint at `path` (e.g. `/live`). `mode` is `echo` (default), `broadcast` (fan-out text to all sockets on this path), or `drain`. Legacy `echo=False` maps to `drain`. Connect from a client with `web.ws.connect`.
Register a WebSocket endpoint at `path` (e.g. `/live` or `/chat/{id}`). `mode` is `echo` (default), `broadcast` (fan-out text to all sockets on this path), `room` (named room via `room_key`, supports `{id}` from the path), or `drain`. Legacy `echo=False` maps to `drain`. `on_message` is an optional `lib.member` hook (G-WS2): called before fan-out with `message`/`room`/`user`; return `{skip:true}` to drop or `{message:…}` to rewrite. `presence=True` also joins `room.presence` and emits join/leave JSON. Connect from a client with `web.ws.connect`.
*> web_app_route_ws app=`self` path=`path` echo=`echo` mode=`mode`*
*> web_app_route_ws app=`self` path=`path` echo=`echo` mode=`mode` room_key=`room_key` on_message=`on_message` presence=`presence`*
## auth
+ `users`
@@ -745,21 +748,42 @@ Register a WebSocket endpoint at `path` (e.g. `/live`). `mode` is `echo` (defaul
+ `login_redirect`=None
+ `logout_redirect`=None
+ `login_path`=None
+ `register`=False
+ `register_path`=/register
+ `default_role`="member"
Keep the app's `admin=True`, and gate `{admin_prefix}` (segment-boundary prefix) behind a login page (default `/admin`). Unauthenticated requests redirect to `login_path` (default `{admin_prefix}/login`, auto-excluded). Does not match `/admin-publish`-style siblings.
Keep the app's `admin=True`, and gate `{admin_prefix}` (segment-boundary prefix) behind a login page (default `/admin`). Unauthenticated requests redirect to `login_path` (default `{admin_prefix}/login`, auto-excluded). Does not match `/admin-publish`-style siblings. With `register=True` and `enable_rbac`, exposes `register_path` (writes `web_users` + default role).
*> web_app_auth app=`self` users=`users` session_ttl=`session_ttl` admin_prefix=`admin_prefix` login_redirect=`login_redirect` logout_redirect=`logout_redirect` login_path=`login_path`*
*> web_app_auth app=`self` users=`users` session_ttl=`session_ttl` admin_prefix=`admin_prefix` login_redirect=`login_redirect` logout_redirect=`logout_redirect` login_path=`login_path` register=`register` register_path=`register_path` default_role=`default_role`*
## gate
+ `path`
+ `roles`=admin
+ `permissions`=""
+ `match`=prefix
+ `on_deny`=forbid
+ `exclude`=None
Require one of `roles` (CSV) for `path`. `match=prefix` uses segment boundaries; `exact` is equality. Trailing `*` on `path` means prefix. `on_deny=redirect` sends visitors to `login_path` (with `?next=`); `forbid` returns 403. `exclude` is CSV or list of open paths.
Require access for `path`. Prefer `permissions` (CSV of `resource:action` codes such as `desk:access`); when non-empty, authorization uses the session permission set. Otherwise require one of `roles` (legacy). `match=prefix` uses segment boundaries; `exact` is equality. Trailing `*` on `path` means prefix. `on_deny=redirect` sends visitors to `login_path` (with `?next=`); `forbid` returns 403. `exclude` is CSV or list of open paths.
*> web_app_gate app=`self` path=`path` roles=`roles` match=`match` on_deny=`on_deny` exclude=`exclude`*
*> web_app_gate app=`self` path=`path` roles=`roles` permissions=`permissions` match=`match` on_deny=`on_deny` exclude=`exclude`*
## enable_rbac
+ `catalog`=None
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`).
*> web_app_rbac app=`self` catalog=`catalog`*
## tenant
+ `mode`
+ `param`=None
+ `column`=tenant_id
+ `default_scope`=False
Enable multi-tenant resolution: `mode` is `path` (query/`/t/{id}/…`), `subdomain`, or `header`. `param` is the path/query key or header name. When `default_scope=True`, JSON routes and form inserts auto-filter/stamp `column` (default `tenant_id`). Per-route override: JSON table column `tenant_scope` / `租户作用域`.
*> web_app_tenant app=`self` mode=`mode` param=`param` column=`column` default_scope=`default_scope`*
## gallery
+ `path`=/gallery
@@ -843,6 +867,49 @@ Mount `GET path` to stream an object. Path must capture `key` (use `{*key}` for
*> web_app_download app=`self` path=`path` storage=`storage` disposition=`disposition`*
# rbac
Offline helpers for role ⊥ permission. Prefer `app.enable_rbac` + gates for HTTP; use these for scripts/tests.
## can
+ `permissions`
+ `needed`
Return `{ok, allowed}` — whether held permission CSV includes any of `needed` (CSV). Held `*` allows all.
> ensure_plugin
*> web_rbac_can permissions=`permissions` needed=`needed`*
## assign_role
+ `url`
+ `username`
+ `role`
Bind `username` (in `web_users`) to role name.
> ensure_plugin
*> web_rbac_assign_role url=`url` username=`username` role=`role`*
## create_role
+ `url`
+ `name`
Create a non-system role; returns `{ok, id, name}`.
> ensure_plugin
*> web_rbac_create_role url=`url` name=`name`*
## set_role_permissions
+ `url`
+ `role`
+ `permissions`
+ `grantable`=""
Replace permissions on a non-system role (CSV). `grantable` restricts grants (anti-escalation); empty = full catalog.
> ensure_plugin
*> web_rbac_set_role_permissions url=`url` role=`role` permissions=`permissions` grantable=`grantable`*
# auth
+ `users`
+ `session_ttl`=3600
+73 -6
View File
@@ -699,10 +699,13 @@ SEO / OpenGraph 元数据表(`|键|值|`)。如 `title`、`description`、`c
+ `路径`
+ `回显`=真
+ `模式`=None
+ `房间`=None
+ `消息钩子`=None
+ `在场`=假
在 `路径`(如 `/live`)注册 WebSocket 端点。`模式` 为 `echo`(默认)、`broadcast`(同路径全体广播)或 `drain`。旧参数 `回显=假` 等价于 `drain`。客户端用 `网页.实时` 的 `连接` 接入。
在 `路径`(如 `/live` 或 `/chat/{id}`)注册 WebSocket。`模式` 为 `echo`(默认)、`broadcast`、`room`(需 `房间` 键,可含 `{id}`)或 `drain`。旧参数 `回显=假` 等价于 `drain`。`消息钩子` 为可选 `库.成员`(G-WS2):广播前调用,入参含 `message`/`room`/`user`;返回 `{skip:真}` 丢弃或 `{message:…}` 改写。`在场=真` 时另进 `房间.presence` 并推送 join/leave。客户端用 `网页.实时` 的 `连接` 接入。
*> web_app_route_ws app=`self` path=`路径` echo=`回显` mode=`模式`*
*> web_app_route_ws app=`self` path=`路径` echo=`回显` mode=`模式` room_key=`房间` on_message=`消息钩子` presence=`在场`*
## 鉴权
+ `用户表`
@@ -711,21 +714,42 @@ SEO / OpenGraph 元数据表(`|键|值|`)。如 `title`、`description`、`c
+ `登录回跳`=None
+ `登出回跳`=None
+ `登录路径`=None
+ `注册`=假
+ `注册路径`=/register
+ `默认角色`="member"
在保持 `后台=真` 的同时,为 `{后台前缀}` 加段边界前缀门禁(默认 `/admin`)。未登录 重定向 到 `登录路径`(默认 `{前缀}/login`,自动排除)。不会匹配 `/admin-publish` 这类旁系路径。
在保持 `后台=真` 的同时,为 `{后台前缀}` 加段边界前缀门禁(默认 `/admin`)。未登录 重定向 到 `登录路径`(默认 `{前缀}/login`,自动排除)。不会匹配 `/admin-publish` 这类旁系路径。`注册=真` 且已 `启用权限` 时开放 `注册路径`(写入 `web_users` 并赋默认角色)。
*> web_app_auth app=`self` users=`用户表` session_ttl=`会话时长` admin_prefix=`后台前缀` login_redirect=`登录回跳` logout_redirect=`登出回跳` login_path=`登录路径`*
*> web_app_auth app=`self` users=`用户表` session_ttl=`会话时长` admin_prefix=`后台前缀` login_redirect=`登录回跳` logout_redirect=`登出回跳` login_path=`登录路径` register=`注册` register_path=`注册路径` default_role=`默认角色`*
## 门禁
+ `路径`
+ `角色`=admin
+ `权限`=""
+ `匹配`=prefix
+ `拒绝`=forbid
+ `排除`=None
要求会话角色落在 `角色`(CSV)之一。`匹配=prefix` 为段边界前缀;`exact` 为精确路径。路径末尾 `*` 亦表示前缀。`拒绝=redirect` 时访客跳转登录页(带 `?next=`);`forbid` 返回 403。`排除` 为 CSV/列表(如登录页)。
为 `路径` 要求访问权。优先使用 `权限`(CSV,如 `desk:access`);非空时按会话权限集判定。否则要求会话角色落在 `角色`(CSV)之一。`匹配=prefix` 为段边界前缀;`exact` 为精确路径。路径末尾 `*` 亦表示前缀。`拒绝=redirect` 时访客跳转登录页(带 `?next=`);`forbid` 返回 403。`排除` 为 CSV/列表(如登录页)。
*> web_app_gate app=`self` path=`路径` roles=`角色` match=`匹配` on_deny=`拒绝` exclude=`排除`*
*> web_app_gate app=`self` path=`路径` roles=`角色` permissions=`权限` match=`匹配` on_deny=`拒绝` exclude=`排除`*
## 启用权限
+ `目录`=None
启用库表 RBAC(角色 ⊥ 权限)。listen 时确保五表并种子 `superadmin` / `member`。见 `doc/design/ext-web-rbac.md`。管理台:`GET /_rbac/desk`(需 `roles:manage`)。
*> web_app_rbac app=`self` catalog=`目录`*
## 租户
+ `模式`
+ `参数`=None
+ `列`=tenant_id
+ `默认作用域`=假
启用多租户解析:`模式` 为 `path`(查询参数或 `/t/{id}/…`)、`subdomain` 或 `header`。`参数` 为路径/查询键或请求头名。`默认作用域=真` 时 JSON 路由与表单插入自动按 `列`(默认 `tenant_id`)过滤/盖戳。单路由可用表列 `租户作用域` 覆盖。
*> web_app_tenant app=`self` mode=`模式` param=`参数` column=`列` default_scope=`默认作用域`*
## 相册
+ `路径`=/gallery
@@ -809,6 +833,49 @@ SEO / OpenGraph 元数据表(`|键|值|`)。如 `title`、`description`、`c
*> web_app_download app=`self` path=`路径` storage=`存储` disposition=`处置`*
# 权限
角色 ⊥ 权限的离线助手。HTTP 侧优先 `应用.启用权限` + 门禁;脚本/测试可用本类。
## 可否
+ `权限`
+ `需要`
返回 `{ok, allowed}`:持有权限 CSV 是否包含 `需要`(CSV)中任一码。持有 `*` 则全允许。
> 确保插件
*> web_rbac_can permissions=`权限` needed=`需要`*
## 赋角色
+ `数据库`
+ `用户名`
+ `角色`
将 `用户名`(须已在 `web_users`)绑定到角色名。
> 确保插件
*> web_rbac_assign_role url=`数据库` username=`用户名` role=`角色`*
## 建角色
+ `数据库`
+ `名称`
新建非系统角色;返回 `{ok, id, name}`。
> 确保插件
*> web_rbac_create_role url=`数据库` name=`名称`*
## 设角色权限
+ `数据库`
+ `角色`
+ `权限`
+ `可授`=""
覆盖非系统角色的权限(CSV)。`可授` 限制可授予码(防提权);空则允许整份目录。
> 确保插件
*> web_rbac_set_role_permissions url=`数据库` role=`角色` permissions=`权限` grantable=`可授`*
# 鉴权
+ `用户表`
+ `会话时长`=3600