Files
AgentLab/frontend/src/store/auth.ts
T
ThearchyHelios 941723b17c 端到端验收修复与指标出具:
- 身份与工具:登录页建第一个管理员(认人接口的 401 说明能不能建);搭建者有只读的人和组名录,选人给候选并核对名字;授权三行带标题;端点请求头名校验(空名、非法名存不进、发不出时说清原因);写入能力在工具页试运行一律演练、不发送,HTTP 状态照实说成败;写记忆不算只读、查询能力只能选同一端点上的只读能力;审批与遮罩的说法和实际规则一致;开登录时不要署名、导入和发布记登录的人;使用者不显示推演等只给搭建者的操作,403 按是否开登录说话;登录页只认一次人
- 流程与实例:图里用到的岗位(派给谁、超时升级给谁、发送确认派给谁)全部派生、没绑人的发布时报错;写外部系统的参数按能力声明的类型收拢后再算哈希和发送,收不成就不发;发起人的实例页只看这一步的状态,处理人才给待办卡;会签进度带登录核实和时间,升级后照实写规则;实例状态写流程的结论(已退回、未确认、等待核实写入结果),核实生效后不再挂失败原因;流程应用首页不派生最近封存运行的结果块,看板没数据时是安静的空态;样例⑩的金额带类型和下限、待办标题带金额,表单上下限前后端一起生效
- 看板与证据:定时任务模板加 ISO 周、上一 ISO 周、本月、上月;出处链穿过原样解析查询结果的整形节点,追不到原始数据的照实说停在哪里,样例⑨改用 cell();应用首页和版本历史都能做成应用;指标块单位不重复;版本列表带退役说明;定时任务页头跟着刷新;删视图要确认、新视图能起名;编辑器属性用中文名、术语带解释
- 指标出具(用户 2026-10-15 拍板):契约只写「指标来自」、上游没有报告撰写节点、成果里没有模型写的话时按指标出具——必需指标齐、指标输入追到原始数据、成果字段里的数回指得上才完整出具;受管级要求每个指标静态地追到原始数据(governed.metric_untraced);出具横幅和应用印章写明按指标
- 受管工作流降为已发布只给管理员(Q8 顺延,服务端同样拦);工作流接口带当前发布版的级别,画布上改过之后也认得出受管工作流
2026-10-15 07:15:37 +08:00

147 lines
6.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { create } from 'zustand'
import { ApiError, api } from '../api/client'
import { localActor, useLocalActor } from '../lib/actor'
import type { AuthMode, Principal, Role } from '../typesApp'
/**
* 当前的人(GET /api/auth/me):登录方式、用户名、角色、组。外壳按它收起导航、换掉署名入口,待办卡按它写「以谁的
* 名义处理」,表单的 JSON 控件按它决定给不给填。
*
* 关登录(off,默认)时 /me 恒为 mode='off'、role='admin':所有人等同管理员,和加这一层之前的界面一模一样。
* 服务端没有这个接口(老服务端回 404 / 405)也按 off 算:新前端连老服务端,不能因为一个身份接口把整站拦在门外。
* 网络失败不下结论(me 留空),外壳照旧走离线那一套,恢复后再取。
*/
export const OFF_PRINCIPAL: Principal = {
mode: 'off', username: null, display_name: null, role: 'admin', groups: [], verified: false,
}
interface AuthState {
me: Principal | null
/** 取过一次(成功或按 off 兜底)。网络失败时保持 false */
ready: boolean
/**
* 还没登录、而且服务端说能建第一个管理员(账号密码登录的新部署,库里还没有能登录的管理员:/me 的 401 带 can_bootstrap)。
* 登录页据此给「建第一个管理员」的表单
*/
canBootstrap: boolean
/** 取 /me;返回取到的身份,网络失败返回 null。同一时刻只发一个请求,后来的共用它 */
load: () => Promise<Principal | null>
set: (me: Principal) => void
logout: () => Promise<void>
}
/** 在路上的那一次认人:启动时的认人、开发模式下 effect 执行两次、恢复连接时的补取撞在一起,共用这一个请求 */
let loading: Promise<Principal | null> | null = null
export const useAuth = create<AuthState>((set) => ({
me: null,
ready: false,
canBootstrap: false,
load: () => {
loading ??= (async (): Promise<Principal | null> => {
try {
const me = await api.auth.me({ timeoutMs: 15_000 })
set({ me, ready: true, canBootstrap: false })
return me
} catch (e) {
if (e instanceof ApiError && (e.status === 404 || e.status === 405) && !e.code) {
set({ me: OFF_PRINCIPAL, ready: true, canBootstrap: false })
return OFF_PRINCIPAL
}
// 开了登录、还没登录:服务端回 401 并说明登录方式(登录页据此给账号密码表单还是单点登录按钮),新部署还说能不能
// 建第一个管理员
if (e instanceof ApiError && e.status === 401) {
const body = e.body as { mode?: unknown; can_bootstrap?: unknown } | undefined
const mode: AuthMode = body?.mode === 'oidc' ? 'oidc' : 'local'
const me: Principal = { ...OFF_PRINCIPAL, mode, role: 'user', verified: false }
set({ me, ready: true, canBootstrap: mode === 'local' && body?.can_bootstrap === true })
return me
}
return null
}
})().finally(() => { loading = null })
return loading
},
set: (me) => set({ me, ready: true, canBootstrap: false }),
logout: async () => {
await api.auth.logout()
set({ me: null, ready: false })
},
}))
const RANK: Record<Role, number> = { user: 1, builder: 2, admin: 3 }
/** 开没开登录:明确是 local / oidc 才算;off 和没取到都算没开(界面照今天的样子) */
export const loginOn = (me: Principal | null | undefined): boolean => me?.mode === 'local' || me?.mode === 'oidc'
/** 至少是这个角色。关登录时所有人等同管理员 */
export function atLeast(me: Principal | null | undefined, role: Role): boolean {
if (!loginOn(me)) return true
return RANK[me!.role] >= RANK[role]
}
/** 开了登录、还没登录(/me 回了未认证的身份):要去登录页 */
export const needsLogin = (me: Principal | null | undefined): boolean => loginOn(me) && !me!.verified
/** 订阅版 */
export const usePrincipal = (): Principal | null => useAuth((s) => s.me)
export const useLoginOn = (): boolean => useAuth((s) => loginOn(s.me))
export const useAtLeast = (role: Role): boolean => useAuth((s) => atLeast(s.me, role))
/** 给人看的名字:显示名(用户名);关登录时是本机署名,由调用方自己取 */
export function principalName(me: Principal | null | undefined): string {
if (!me) return ''
const name = me.display_name?.trim()
const user = me.username?.trim() ?? ''
return name && user && name !== user ? `${name}(${user})` : name || user
}
/**
* 发布、发起、导入确认这些留痕记在谁的名下:开了登录是登录的人(verified,没有署名框——服务端开登录时只认登录的人,
* api/deps.actor_of、signer_of);关登录是本机署名(自报、未认证,可能还没填,name 为空串)
*/
export interface Signer { verified: boolean; name: string }
export function signerOf(me: Principal | null | undefined, actor: string | null): Signer {
return loginOn(me) ? { verified: true, name: principalName(me) } : { verified: false, name: actor ?? '' }
}
/** 订阅版:登录身份、本机署名变了跟着变 */
export function useSigner(): Signer {
const me = useAuth((s) => s.me)
return signerOf(me, useLocalActor())
}
/** 不在组件里(确认框的文字、请求体):取此刻的 */
export const signerNow = (): Signer => signerOf(useAuth.getState().me, localActor())
/** 登录页的地址,带上回来的路:/login?next=/apps/x */
export function loginUrl(next?: string): string {
const back = next ?? `${window.location.pathname}${window.location.search}`
return back && back !== '/' && !back.startsWith('/login') ? `/login?next=${encodeURIComponent(back)}` : '/login'
}
/**
* 等身份认定(/auth/me 回来,成功或按 off 兜底)。启动时设置、主题这类请求和认人同时发出,回来时要先知道是谁才能决定
* 用不用它的结果。最多等 timeoutMs,到点还没认定按没认定(null)交回
*/
export function authSettled(timeoutMs = 15_000): Promise<Principal | null> {
const now = useAuth.getState()
if (now.ready) return Promise.resolve(now.me)
return new Promise((resolve) => {
const stop = useAuth.subscribe((s) => {
if (!s.ready) return
stop()
clearTimeout(timer)
resolve(s.me)
})
const timer = setTimeout(() => { stop(); resolve(null) }, timeoutMs)
})
}
/**
* 界面偏好(主题)只存本机、不写服务端(D-s):开了登录、又不是管理员时。服务端设置里的 ui 一组是全站共用的,改它要
* 管理员;搭建者、使用者换主题只在这台设备上生效,也不拿服务端的值翻回去
*/
export const prefsLocalOnly = (me: Principal | null | undefined): boolean => loginOn(me) && !atLeast(me, 'admin')