Files
biliup/app/lib/use-streamers.ts
Noah d343386fbd feat: 用户管理与分级权限(超级管理员 / 操作员 / 只读观察者) (#1718)
* feat(web): multi-user accounts with fixed admin/operator/viewer roles

- migration 6 adds web_users and copies the existing single admin with
  the same id and password hash, so upgrades need no action and existing
  sessions stay logged in
- table-driven permission middleware on every business route (default
  deny), 403 for missing permission, 401 when logged out
- hooks/override and secrets redacted for non-admins; operators may only
  pick registered bilibili accounts for templates
- /v1/web-users management API, /v1/me and self password change
- login rate limit: 5 consecutive failures lock the username for 30s
- revoked sessions cut long-lived websocket/stream responses

Refs #1717

* feat(cli): add `biliup user list` and `biliup user reset-password`

Refs #1717

* feat(web-ui): user management page, account menu and editable login name

- /users page (super admin only): create users, change role, reset
  password, disable, force logout, delete; table on desktop, cards on
  mobile, with loading/empty/error states
- sidebar filters menu entries by the permissions from /v1/me and shows
  a no-access state for pages the role cannot open
- account menu in the sidebar footer with change password and logout,
  hidden when --auth is off
- 403 responses show a warning toast instead of redirecting
- login page username is editable (default biliup)

Refs #1717

* feat(web-ui): hide actions the current role cannot perform

Streamer add/edit/delete/pause, hook and override editing, template and
upload actions, bilibili account management and config saving are only
rendered for roles that have the matching permission. Account and type
tree requests are skipped for roles that would get 403.

Refs #1717

* fix(web-ui): wait for permissions on restricted pages, surface pause errors

- pages that need a permission not every role has render a spinner until
  /v1/me returns, so roles without it never fire requests doomed to 403
- user management is hidden when --auth is off, with an explanation if
  the page is opened directly
- the pause button goes through the shared response handler, so a 403
  shows the permission toast instead of failing silently
- permission errors sent as plain text keep their message

Refs #1717

* fix(web-ui): keep user actions reachable on tablet and narrow desktop

Refs #1717

* fix(py): handle the user subcommand in the stream-gears entry point

Refs #1717
2026-09-23 14:59:58 +00:00

77 lines
2.0 KiB
TypeScript

import useSWR from "swr";
import {
BiliType,
fetcher,
LiveStreamerEntity,
User
} from "./api-streamer";
import {useEffect, useState} from "react";
import {useMe} from "./use-me";
export default function useStreamers() {
const { data, error, isLoading } = useSWR<LiveStreamerEntity[]>("/v1/streamers", fetcher);
return {
isLoading,
streamers: data,
};
}
const NO_USERS: any[] = [];
export function useBiliUsers() {
// 账号列表归「编辑投稿模板」权限;没有这项权限的角色(只读观察者)不去请求,免得满屏 403
const {can} = useMe();
const {data, error, isLoading} = useSWR<User[]>(can("template.edit") ? "/v1/users" : null, fetcher);
const [list, setList] = useState<any[]>(NO_USERS);
useEffect(() => {
if (!data) return;
const updateList = async (item: User) => {
try {
const res = await fetcher(`/v1/users/${item.id}`, undefined);
return {
...item,
name: res.data.name,
face: res.data?.face || "/noface.jpg",
};
} catch (error) {
console.error(error);
return {
...item,
name: "Cookie已失效",
face: "/noface.jpg",
};
}
};
// data 为空数组时这里解析为 [],顺带把上一批账号的补全结果清掉,
// 之后再有账号时不会先闪一下旧数据
Promise.all(data.map(updateList)).then(setList);
}, [data])
return {
isLoading,
isError: error,
// 没有账号时直接派生为空列表,不必等 effect 里再 setState 一轮
biliUsers: !data || data.length === 0 ? NO_USERS : list,
};
}
export function useTypeTree() {
const {can} = useMe();
const { data: archivePre, error, isLoading } = useSWR(can("upload.submit") ? "/bili/archive/pre" : null, fetcher);
const treeData = archivePre?.data?.typelist.map((type: BiliType)=> {
return {
label: type.name,
value: type.id,
children: type.children
};
});
return {
isLoading,
isError: error,
typeTree: treeData,
};
}