From e5c003becc20f8532f8dc25d452ef620da7cede5 Mon Sep 17 00:00:00 2001 From: margrop <1731827+margrop@users.noreply.github.com> Date: Fri, 4 Dec 2026 20:36:47 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=AE=A1=E7=90=86=E5=91=98=E7=99=BB?= =?UTF-8?q?=E5=BD=95=E5=90=8E=E9=BB=98=E8=AE=A4=E8=BF=9B=E5=85=A5=E7=8B=AC?= =?UTF-8?q?=E7=AB=8B=E7=AE=A1=E7=90=86=E5=90=8E=E5=8F=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 登录成功后按会话角色分流:管理员进入 /admin,学生仍进入学习首页 - 同步更新 11 个管理员登录 E2E 的落地页断言,并把用户管理删除定位限定在设置对话框内 - README 中英文补充管理员默认落地页说明 --- README.en.md | 2 +- README.md | 2 +- e2e/admin-settings.spec.ts | 2 +- e2e/auth-flow.spec.ts | 14 +++++++------- e2e/banks.spec.ts | 2 +- e2e/error-detail.spec.ts | 2 +- e2e/i18n-consistency.spec.ts | 2 +- e2e/notebook-management.spec.ts | 2 +- e2e/question-grade-settings.spec.ts | 2 +- e2e/question-numbering.spec.ts | 2 +- e2e/review.spec.ts | 2 +- e2e/setup-wizard.spec.ts | 2 +- e2e/stats-and-print.spec.ts | 2 +- src/app/login/page.tsx | 6 ++++-- 14 files changed, 23 insertions(+), 21 deletions(-) diff --git a/README.en.md b/README.en.md index 7e0c5036..487b5d22 100644 --- a/README.en.md +++ b/README.en.md @@ -193,7 +193,7 @@ Practice statistics show total practice, accuracy (correct / attempted) and acti - **🔑 One-time setup link**: fresh installs no longer ship an admin with a fixed password. On first start the server generates a setup link that works only once; open it to choose the admin email and password. The desktop and standalone Android apps open it automatically; on fnOS you enter the admin email and password in the install wizard instead; Docker, npm and source runs print it in the startup log (the token is also in `config/setup-token` in the data directory). Instances upgraded from earlier versions keep their accounts. - **🛡️ AI data sharing consent**: before first use, each account sees a notice listing the data sent to the third-party AI provider and the current provider; nothing is sent until the user agrees. If the user declines, AI features are unavailable and everything else keeps working. Consent can be withdrawn any time under **Settings → Account**. - **🧭 Setup wizard**: on first sign-in the admin goes through a wizard: change the default password (only for the default admin of earlier versions) → set up an AI provider (free MiniMax Code recommended) → create the first notebook. The last two steps can be skipped, and the wizard can be run again from **Settings → User Management**. Instances already in use are not interrupted after upgrading. -- **🔐 Multiple users and admin console**: sign-up and sign-in, with data isolated per user. New sign-ups are off by default (the sign-in page then hides the sign-up link and asks visitors to contact the administrator) and can be turned on under **Settings → User Management**, where admins can also cap daily AI use per user and see today's AI calls and token usage. Administrators open the admin console from **Settings → User Management** to view usage statistics and enable, disable or delete users. On a user's detail page, **View this user's data** lets an admin open that user's notebooks, question details, practice, answers and printing; a banner shows the current identity, all changes and AI usage are recorded under that account, and **Exit view** returns to the administrator's own view. Administrators can delete their own account under **Settings → Danger** by entering their password, which permanently deletes the account and all of its data; student accounts cannot self-delete and are removed by an administrator in User Management. Sign-in and sign-up limit repeated failures to block password guessing. +- **🔐 Multiple users and admin console**: sign-up and sign-in, with data isolated per user. New sign-ups are off by default (the sign-in page then hides the sign-up link and asks visitors to contact the administrator) and can be turned on under **Settings → User Management**, where admins can also cap daily AI use per user and see today's AI calls and token usage. Administrators land in the admin console (`/admin`) right after signing in, while regular users still land on the study home page; the console can also be opened from **Settings → User Management** to view usage statistics and enable, disable or delete users. On a user's detail page, **View this user's data** lets an admin open that user's notebooks, question details, practice, answers and printing; a banner shows the current identity, all changes and AI usage are recorded under that account, and **Exit view** returns to the administrator's own view. Administrators can delete their own account under **Settings → Danger** by entering their password, which permanently deletes the account and all of its data; student accounts cannot self-delete and are removed by an administrator in User Management. Sign-in and sign-up limit repeated failures to block password guessing. - **📈 Anonymous telemetry and administrator agreement**: the administrator accepts the [agreement](doc/EULA.en.md) during initialization (on fnOS, in the installer wizard). The instance then sends a batch of anonymous usage statistics and error fingerprints to the Wrong Notebook collection service about every 6 hours, without user or instance identifiers, IP addresses, question content, AI conversations or error messages. Instances upgraded from older versions do not forward by default; administrators see a banner where they can review the agreement and turn it on. Under "Settings → User Management → Privacy & telemetry" an administrator can turn forwarding off, enable local recording (kept on this server for troubleshooting), point to a self-hosted collection service and see what the instance collected; `TELEMETRY_DISABLED=true` forces everything off. When the agreement changes, administrators must accept again and forwarding pauses meanwhile; ordinary users are not affected. See [Privacy](doc/PRIVACY.en.md) and the [design](doc/TELEMETRY_DESIGN.md). - **🐞 Debug logs**: when something goes wrong, an admin turns on debug mode under "Settings → User Management", reproduces the problem, then exports the debug log (JSON) and sends it to the author. Debug mode records every server log, including `debug` level, plus frontend errors; API keys, passwords and tokens are removed on export. The log lives in memory only, debug mode turns off after 24 hours, and a restart clears it. - **🔌 Openclaw integration**: an external Openclaw agent can upload question images in bulk via `POST /api/openclaw/batch-upload`; see [Environment variables](#-environment-variables). diff --git a/README.md b/README.md index ae4f53c0..12d48c56 100644 --- a/README.md +++ b/README.md @@ -193,7 +193,7 @@ - **🔑 一次性初始化链接**:全新安装不再预置固定密码的管理员。服务首次启动时生成只能使用一次的初始化链接,打开后自行设置管理员邮箱和密码:桌面版、安卓单机版自动打开;飞牛OS 直接在安装向导中填写管理员邮箱和密码;Docker、npm、源码运行打印在启动日志中(也可在数据目录的 `config/setup-token` 中找到令牌)。从旧版本升级的实例保留原有账号。 - **🛡️ AI 数据授权**:每个账号首次使用前会看到授权说明,列出发送给第三方 AI 服务商的数据和当前服务商,同意后才会发送;不同意时 AI 功能不可用,其他功能照常使用,可在「设置 → 账户」中随时撤回。 - **🧭 安装向导**:管理员首次登录时进入向导:修改默认密码(仅旧版本的默认管理员需要)→ 配置 AI 渠道(推荐免费的 MiniMax Code)→ 创建第一个错题本,后两步可跳过;可在「设置 → 用户管理」中重新运行。已在使用的实例升级后不会自动弹出。 -- **🔐 多用户与管理后台**:支持注册、登录,数据按用户隔离;新用户注册默认关闭(关闭时登录页不显示注册入口,改为提示联系管理员),管理员可在「设置 → 用户管理」中开启,并可限制每人每天的 AI 使用次数、查看今日调用次数和 token 用量;管理员可在「设置 → 用户管理」进入管理后台,查看使用统计,启用/禁用或删除用户;在用户详情页点「查看该用户数据」后,管理员可进入该用户的错题本、错题详情、练习、作答和打印,顶部提示条标明当前身份,所有改动和 AI 用量记在该账号下,点「退出查看」恢复管理员视角;管理员可在「设置 → 危险」中输入密码注销自己的账号,账号及全部数据随之永久删除;学生账号不能自助注销,由管理员在用户管理中删除。登录和注册带失败次数限制,防止暴力破解密码。 +- **🔐 多用户与管理后台**:支持注册、登录,数据按用户隔离;新用户注册默认关闭(关闭时登录页不显示注册入口,改为提示联系管理员),管理员可在「设置 → 用户管理」中开启,并可限制每人每天的 AI 使用次数、查看今日调用次数和 token 用量;管理员登录后默认进入管理后台(`/admin`),普通用户仍落在学习首页;管理员也可从「设置 → 用户管理」打开管理后台,查看使用统计,启用/禁用或删除用户;在用户详情页点「查看该用户数据」后,管理员可进入该用户的错题本、错题详情、练习、作答和打印,顶部提示条标明当前身份,所有改动和 AI 用量记在该账号下,点「退出查看」恢复管理员视角;管理员可在「设置 → 危险」中输入密码注销自己的账号,账号及全部数据随之永久删除;学生账号不能自助注销,由管理员在用户管理中删除。登录和注册带失败次数限制,防止暴力破解密码。 - **📈 匿名遥测与使用协议**:管理员在初始化时同意[使用协议](doc/EULA.md)(飞牛OS 在安装向导中勾选),实例默认每 6 小时向错题本收集服务发送一批匿名的功能使用统计和错误指纹,不含用户或实例标识、IP、题目内容、AI 对话或错误原文;存量实例升级后默认不外发,管理员登录时可从提示横幅查看协议后开启。「设置 → 用户管理 → 隐私与遥测」可关闭外发、开启本地记录(只保存在本机,供排查问题)、改为自建收集服务,并查看本实例收集了什么;设置 `TELEMETRY_DISABLED=true` 强制全部关闭。协议升级后管理员需重新同意,期间暂停外发,普通用户不受影响。详见[隐私说明](doc/PRIVACY.md)与[设计](doc/TELEMETRY_DESIGN.md)。 - **🐞 调试日志**:遇到问题时,管理员在「设置 → 用户管理」中打开调试模式,复现问题后导出调试日志(JSON)发给作者。调试模式下记录包括 debug 级别在内的全部服务端日志和前端错误,导出时自动去除 API Key、密码和令牌;日志只保存在内存中,24 小时后自动关闭,服务重启后清空。 - **🔌 Openclaw 集成**:外部 Openclaw agent 可通过 `POST /api/openclaw/batch-upload` 批量上传题目图片,配置见[环境变量](#-环境变量)。 diff --git a/e2e/admin-settings.spec.ts b/e2e/admin-settings.spec.ts index fa423832..8f7a62c7 100644 --- a/e2e/admin-settings.spec.ts +++ b/e2e/admin-settings.spec.ts @@ -12,7 +12,7 @@ test('Admin can configure OpenAI settings with multi-instance support', async ({ await page.locator('button[type="submit"]').click(); // Wait for login to complete - await page.waitForURL('**/', { timeout: 15000 }); + await page.waitForURL('**/admin', { timeout: 15000 }); // 2. Open Settings await page.getByRole('button', { name: '设置' }).click(); diff --git a/e2e/auth-flow.spec.ts b/e2e/auth-flow.spec.ts index f7947ca0..3f26e33a 100644 --- a/e2e/auth-flow.spec.ts +++ b/e2e/auth-flow.spec.ts @@ -100,23 +100,24 @@ test.describe('Authentication Flow', () => { await page.locator('button[type="submit"]').click(); // Verify Admin Login and Home Page - await page.waitForURL('**/'); + await page.waitForURL('**/admin'); // --- Go to Settings > User Management --- // Open Settings (Button with gear icon, sr-only text "Settings" or "设置") await page.getByRole('button').filter({ has: page.locator('svg.lucide-settings') }).click(); // Wait for Dialog - await expect(page.getByRole('dialog')).toBeVisible({ timeout: 5000 }); + const adminSettings = page.getByRole('dialog'); + await expect(adminSettings).toBeVisible({ timeout: 5000 }); // Click "User Management" / "用户管理" Tab - await page.getByRole('tab', { name: /User Management|用户管理/ }).click(); + await adminSettings.getByRole('tab', { name: /User Management|用户管理/ }).click(); // 等待用户列表加载 // --- Delete User 'jason' --- - // Find row with 'jason@qq.com' - const userRow = page.locator('tr').filter({ hasText: user.email }); + // 管理后台首页也有用户表,只在设置对话框内定位目标行,避免读到未刷新的旧行 + const userRow = adminSettings.locator('tr').filter({ hasText: user.email }); // 使用更长的超时时间等待用户行出现 await expect(userRow).toBeVisible({ timeout: 10000 }); @@ -129,10 +130,9 @@ test.describe('Authentication Flow', () => { await acceptConfirmDialog(page); // Verify user is gone - await expect(page.locator('tr').filter({ hasText: user.email })).not.toBeVisible({ timeout: 5000 }); + await expect(adminSettings.locator('tr').filter({ hasText: user.email })).not.toBeVisible({ timeout: 5000 }); // --- Logout Admin --- - const adminSettings = page.getByRole('dialog'); await adminSettings.getByRole('tab', { name: /^(Account|账户)$/ }).click(); await adminSettings.getByRole('button', { name: /^(Logout|退出登录)$/ }).click(); await acceptConfirmDialog(page); diff --git a/e2e/banks.spec.ts b/e2e/banks.spec.ts index d0dff269..95f431b2 100644 --- a/e2e/banks.spec.ts +++ b/e2e/banks.spec.ts @@ -70,7 +70,7 @@ test('[E2E-BK01] 管理员导入与重命名真题库,普通用户搜索并加 await page.locator('input[name="email"]').fill('admin@localhost'); await page.locator('input[name="password"]').fill(ADMIN_PASSWORD); await page.locator('button[type="submit"]').click(); - await page.waitForURL('**/', { timeout: 15000 }); + await page.waitForURL('**/admin', { timeout: 15000 }); await page.goto('/banks'); await expect(page.getByRole('heading', { name: '真题库', exact: true }).first()).toBeVisible(); diff --git a/e2e/error-detail.spec.ts b/e2e/error-detail.spec.ts index 33cb58c2..95d231ff 100644 --- a/e2e/error-detail.spec.ts +++ b/e2e/error-detail.spec.ts @@ -10,7 +10,7 @@ async function login(page: Page) { await page.getByLabel(/邮箱|Email/).fill('admin@localhost'); await page.getByLabel(/^密码$|^Password$/).fill(ADMIN_PASSWORD); await page.getByRole('button', { name: /登录|Login/ }).click(); - await page.waitForURL('**/', { timeout: 15000 }); + await page.waitForURL('**/admin', { timeout: 15000 }); } /** 详情页中标题为 title 的卡片 */ diff --git a/e2e/i18n-consistency.spec.ts b/e2e/i18n-consistency.spec.ts index 82f5ac96..97c34aeb 100644 --- a/e2e/i18n-consistency.spec.ts +++ b/e2e/i18n-consistency.spec.ts @@ -8,7 +8,7 @@ async function login(page: Page) { await page.getByLabel(/邮箱|Email/).fill('admin@localhost'); await page.getByLabel(/^密码$|^Password$/).fill(ADMIN_PASSWORD); await page.getByRole('button', { name: /登录|Login/ }).click(); - await page.waitForURL('**/', { timeout: 15000 }); + await page.waitForURL('**/admin', { timeout: 15000 }); } /** 详情页中标题为 title 的卡片 */ diff --git a/e2e/notebook-management.spec.ts b/e2e/notebook-management.spec.ts index 10b99ed7..682fe257 100644 --- a/e2e/notebook-management.spec.ts +++ b/e2e/notebook-management.spec.ts @@ -6,7 +6,7 @@ async function login(page: Page) { await page.getByLabel(/邮箱|Email/).fill('admin@localhost'); await page.getByLabel(/^密码$|^Password$/).fill(ADMIN_PASSWORD); await page.getByRole('button', { name: /登录|Login/ }).click(); - await page.waitForURL('**/', { timeout: 15000 }); + await page.waitForURL('**/admin', { timeout: 15000 }); } test('新建错题本、搜索和批量删除错题,清空后删除错题本', async ({ page }) => { diff --git a/e2e/question-grade-settings.spec.ts b/e2e/question-grade-settings.spec.ts index b1d2af07..eb2ef421 100644 --- a/e2e/question-grade-settings.spec.ts +++ b/e2e/question-grade-settings.spec.ts @@ -14,7 +14,7 @@ for (const viewport of [{ name: '桌面', width: 1280, height: 900 }, { name: ' await page.getByLabel(/邮箱|Email/).fill('admin@localhost'); await page.getByLabel(/^密码$|^Password$/).fill(ADMIN_PASSWORD); await page.getByRole('button', { name: /登录|Login/ }).click(); - await page.waitForURL('**/'); + await page.waitForURL('**/admin'); const original = await (await page.request.get('/api/user')).json(); const ids: string[] = []; diff --git a/e2e/question-numbering.spec.ts b/e2e/question-numbering.spec.ts index 0c89b05b..e4208a46 100644 --- a/e2e/question-numbering.spec.ts +++ b/e2e/question-numbering.spec.ts @@ -8,7 +8,7 @@ async function login(page: Page) { await page.getByLabel(/邮箱|Email/).fill('admin@localhost'); await page.getByLabel(/^密码$|^Password$/).fill(ADMIN_PASSWORD); await page.getByRole('button', { name: /登录|Login/ }).click(); - await page.waitForURL('**/', { timeout: 15000 }); + await page.waitForURL('**/admin', { timeout: 15000 }); } test('题目编号默认显示 6 位 ID,管理员切换为流水号后详情页显示 #编号,两种编号都能定位', async ({ page }) => { diff --git a/e2e/review.spec.ts b/e2e/review.spec.ts index 9c11e742..20bc99f4 100644 --- a/e2e/review.spec.ts +++ b/e2e/review.spec.ts @@ -6,7 +6,7 @@ async function login(page: Page) { await page.getByLabel(/邮箱|Email/).fill('admin@localhost'); await page.getByLabel(/^密码$|^Password$/).fill(ADMIN_PASSWORD); await page.getByRole('button', { name: /登录|Login/ }).click(); - await page.waitForURL('**/', { timeout: 15000 }); + await page.waitForURL('**/admin', { timeout: 15000 }); } test('做错的题按计划出现在今日复习中,首页显示待复习数量', async ({ page }) => { diff --git a/e2e/setup-wizard.spec.ts b/e2e/setup-wizard.spec.ts index a9decb7a..e14fe968 100644 --- a/e2e/setup-wizard.spec.ts +++ b/e2e/setup-wizard.spec.ts @@ -8,7 +8,7 @@ test('管理员可从设置重新打开安装向导,跳过各步骤后回到 await page.locator('input[name="email"]').fill('admin@localhost'); await page.locator('input[name="password"]').fill(ADMIN_PASSWORD); await page.locator('button[type="submit"]').click(); - await page.waitForURL('**/', { timeout: 15000 }); + await page.waitForURL('**/admin', { timeout: 15000 }); // 设置 → 用户管理 → 重新运行安装向导 await page.getByRole('button', { name: /^(设置|Settings)$/ }).click(); diff --git a/e2e/stats-and-print.spec.ts b/e2e/stats-and-print.spec.ts index 02812a96..4288c03a 100644 --- a/e2e/stats-and-print.spec.ts +++ b/e2e/stats-and-print.spec.ts @@ -6,7 +6,7 @@ async function login(page: Page) { await page.getByLabel(/邮箱|Email/).fill('admin@localhost'); await page.getByLabel(/^密码$|^Password$/).fill(ADMIN_PASSWORD); await page.getByRole('button', { name: /登录|Login/ }).click(); - await page.waitForURL('**/', { timeout: 15000 }); + await page.waitForURL('**/admin', { timeout: 15000 }); } test('知识点页签按标签计数,打印预览显示所选错题本的题目和答案', async ({ page }) => { diff --git a/src/app/login/page.tsx b/src/app/login/page.tsx index 6579d9d3..4aa7dff5 100644 --- a/src/app/login/page.tsx +++ b/src/app/login/page.tsx @@ -1,7 +1,7 @@ "use client"; import { useCallback, useEffect, useState } from "react"; -import { signIn } from "next-auth/react"; +import { getSession, signIn } from "next-auth/react"; import { useRouter } from "next/navigation"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; @@ -60,7 +60,9 @@ export default function LoginPage() { } else if (result?.error) { setError(t.auth?.login?.failed || 'Login failed'); } else { - router.push("/"); + // 管理员登录后直接进入管理后台;学生仍落在学习首页 + const session = await getSession(); + router.push(session?.user?.role === "admin" ? "/admin" : "/"); router.refresh(); } } catch {