feat(auth): 登录白名单(注册后管理员审批放行 + 全局开关)
用户仍可自助注册,普通账号默认 approved=False(待审批),admin 建号即 True; 存量用户迁移回填 True 避免锁死。系统设置 login_whitelist.enabled 总开关:关= 所有人可登录,开=仅已放行用户(或 admin)可登录。三登录端点建号后、发 token 前 经 enforce_user_approved 拦截,未放行返回 403 PENDING_APPROVAL。管理员在「白名单 管理」页(设置下拉入口)切总开关 + 逐用户放行。取消放行只拦下次登录。 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
f8b40353ef
commit
1d1ab384ef
16 files changed
+508
-5
No files matched your search
+52
@@ -0,0 +1,52 @@
|
||||
"""Add users.approved (登录白名单放行标记).
|
||||
|
||||
Backs the login-whitelist feature: when system setting ``login_whitelist.enabled``
|
||||
is on, only ``approved=True`` users (or admins) may log in; self-registered
|
||||
regular users default to ``approved=False`` (待审批) until an admin approves them.
|
||||
|
||||
Existing rows are backfilled to TRUE (``server_default="1"``) so flipping the
|
||||
switch on for the first time never locks out users who already had accounts.
|
||||
|
||||
Revision ID: 20260625_02
|
||||
Revises: 20260625_01
|
||||
Create Date: 2026-06-25
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
from sqlalchemy import inspect
|
||||
|
||||
|
||||
revision = "20260625_02"
|
||||
down_revision = "20260625_01"
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def _has_table(table_name: str) -> bool:
|
||||
inspector = inspect(op.get_bind())
|
||||
return table_name in inspector.get_table_names()
|
||||
|
||||
|
||||
def _has_column(table_name: str, column_name: str) -> bool:
|
||||
if not _has_table(table_name):
|
||||
return False
|
||||
inspector = inspect(op.get_bind())
|
||||
return any(col["name"] == column_name for col in inspector.get_columns(table_name))
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
# A brand-new DB gets the column from create_all; only patch existing tables.
|
||||
if _has_table("users") and not _has_column("users", "approved"):
|
||||
op.add_column(
|
||||
"users",
|
||||
# server_default="1" backfills existing rows to 已放行 (avoid lockout).
|
||||
sa.Column("approved", sa.Boolean(), nullable=False, server_default="1"),
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
if _has_column("users", "approved"):
|
||||
op.drop_column("users", "approved")
|
||||
Reference in new issue
Block a user