refactor(后端): 清掉 Django 遗留的死列与手工级联,角色字符串收成一份
三条迁移,一次部署(0008/0009 含 DROP COLUMN,需要 OJ2_ALLOW_DESTRUCTIVE=1): - 0008 删 IP 相关:比赛 IP 白名单(前端本来就没有输入框,detail.vue 无条件置空)、 submission.ip(前端从未显示过)、以及一次都没被调用过的 IP 限流桶。 judge_server.ip 是运维数据,保留。 - 0009 删九个只有 Django 时代写过、OJ2 一次都没读过的列:user 的 auth_token / open_api / open_api_appkey / session_keys,user_profile 的 blog / github / school / major / language。open_api 后台连开关都没有,那段「已经开着就不重置 appkey」的逻辑从上线起没进过 if。判据是「全仓零读取」而不是「看着没用」—— raw_password 同样刺眼却是在用的,别一起清掉。 - 0010 给 17 条外键补上删除动作,不再是 Django 留下的一律 NO ACTION。父行消失后 必然无意义、且不构成学生留痕的走 CASCADE(中间表、题单/教程/成就的组成部分、 user_profile 与 user_stat);需要人看见的继续拦着——submission.problem_id、 以及 user 的绝大多数外键,删用户撞外键会被 handler 翻译成「请改为禁用账号」, 这是有意的:全 CASCADE 会静默抹掉成就与进度,而 submission.user_id 压根没有 外键,结果是一半删一半留。六处手工级联随之删掉。 角色字符串收进 packages/contract/src/roles.ts:原先 ADMIN_ROLES / TEACHER_ROLES 在两个文件各抄一份、学生口径在四个文件各写一遍、前端 USER_TYPE 是第三份副本。 AuthUser.adminType 与 drizzle 的列都收窄成联合类型,二十多处 `=== "Super Admin"` 从此受编译器管着($type 是纯 TS 层的,generate 确认不产生任何 SQL 变更)。 顺带删掉 db/relations.ts —— drizzle-kit pull 的产物,全仓零引用。 一处行为变化:后台用户列表传非法的 ?type= 回 400,不再静默返回空列表;界面上的 下拉只有合法值,打不到这条。 验证:tsc / vue-tsc / vite build / check:routes 全过;三条迁移在 dev 库执行, 并逐条建 fixture 走 HTTP 接口验过删除连坐与拦截(题单五张子表连坐、user_badge 二级连坐、删有提交的题目仍 409、删有表情的用户仍 409)。 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AeJoYc2t2d7cThVqMBYrBF
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import { ADMIN_ROLES, TEACHER_ROLES } from "@oj2/contract"
|
||||
import type { Context, MiddlewareHandler } from "hono"
|
||||
|
||||
import { failure } from "../http"
|
||||
@@ -56,9 +57,6 @@ function requireRole(
|
||||
}
|
||||
}
|
||||
|
||||
const ADMIN_ROLES = ["Student Admin", "Teacher Admin", "Super Admin"]
|
||||
const TEACHER_ROLES = ["Teacher Admin", "Super Admin"]
|
||||
|
||||
/** 旧 `@admin_role_required` */
|
||||
export const requireAdmin = requireRole((user) => ADMIN_ROLES.includes(user.adminType))
|
||||
|
||||
|
||||
@@ -1,5 +1,12 @@
|
||||
import { randomBytes } from "node:crypto"
|
||||
|
||||
import {
|
||||
toAdminType,
|
||||
toProblemPermission,
|
||||
type AdminType,
|
||||
type ProblemPermission,
|
||||
} from "@oj2/contract"
|
||||
|
||||
import { eq } from "drizzle-orm"
|
||||
import type { Context } from "hono"
|
||||
import { deleteCookie, getCookie, setCookie } from "hono/cookie"
|
||||
@@ -17,12 +24,17 @@ interface StoredSession {
|
||||
contestPasswords: Record<string, string>
|
||||
}
|
||||
|
||||
/**
|
||||
* 会话里的用户。`adminType` / `problemPermission` 是**联合类型而不是 string** ——
|
||||
* 全仓二十多处 `user.adminType === "Super Admin"` 靠它兜底,拼错一个字母就编译不过。
|
||||
* 收窄发生在下面读库那一处,是整个后端唯一一个把裸字符串变成角色的地方。
|
||||
*/
|
||||
export interface AuthUser {
|
||||
id: number
|
||||
username: string
|
||||
email: string | null
|
||||
adminType: string
|
||||
problemPermission: string
|
||||
adminType: AdminType
|
||||
problemPermission: ProblemPermission
|
||||
isDisabled: boolean
|
||||
className: string | null
|
||||
}
|
||||
@@ -126,7 +138,14 @@ async function getUserByToken(token: string | undefined): Promise<SessionResult>
|
||||
}
|
||||
|
||||
await redis.expire(sessionKey(token), config.sessionTtlSeconds)
|
||||
return { user }
|
||||
// 唯一的收窄点。库里是 text 列,认不出来的值降成最低权限,见 toAdminType 的注释。
|
||||
return {
|
||||
user: {
|
||||
...user,
|
||||
adminType: toAdminType(user.adminType),
|
||||
problemPermission: toProblemPermission(user.problemPermission),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/** 要区分「未登录」和「已被禁用」的用这个 —— 目前只有鉴权中间件需要 */
|
||||
|
||||
Reference in New Issue
Block a user