feat: 管理端用户列表显示姓名和公司

This commit is contained in:
Nixevol
2026-11-18 11:53:05 +08:00
parent 032c5280e7
commit 199960276a
8 changed files with 86 additions and 1 deletions
+2
View File
@@ -75,6 +75,8 @@ def list_users(db: Session) -> list[dict]:
rows.append(
{
"employee_id": account.employee_id,
"user_name": account.user_name or "",
"company": account.company or "",
"has_credential": bool(account.credential_cipher),
"refresh_paused": bool(account.refresh_paused),
"last_refresh_at": _iso_time(account.last_refresh_at),
@@ -0,0 +1,21 @@
"""账号记下首页上的姓名和部门,供管理端用户列表显示。"""
from alembic import op
import sqlalchemy as sa
revision = "0012_account_profile"
down_revision = "0011_drop_proxy_list"
branch_labels = None
depends_on = None
def upgrade() -> None:
with op.batch_alter_table("accounts") as batch:
batch.add_column(sa.Column("user_name", sa.String(length=64), server_default="", nullable=False))
batch.add_column(sa.Column("company", sa.String(length=300), server_default="", nullable=False))
def downgrade() -> None:
with op.batch_alter_table("accounts") as batch:
batch.drop_column("company")
batch.drop_column("user_name")
+2
View File
@@ -56,6 +56,8 @@ class Account(Base):
last_refresh_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
credential_valid: Mapped[bool | None] = mapped_column(Boolean, nullable=True)
month_hours: Mapped[str] = mapped_column(String(32), nullable=False, default="", server_default=text("''"))
user_name: Mapped[str] = mapped_column(String(64), nullable=False, default="", server_default=text("''"))
company: Mapped[str] = mapped_column(String(300), nullable=False, default="", server_default=text("''"))
hours_month: Mapped[str] = mapped_column(String(7), nullable=False, default="", server_default=text("''"))
monthly_target: Mapped[int] = mapped_column(nullable=False, default=600, server_default="600")
proxy_scheme: Mapped[str] = mapped_column(String(16), nullable=False, default="", server_default=text("''"))
+29
View File
@@ -369,6 +369,7 @@ def poll_qr_login(db: Session, attempt_id: str) -> dict:
def fetch_home_summary(db: Session, employee_id: str, client_factory=EunivClient) -> dict:
data = _live(db, employee_id, client_factory, lambda client: _apply_hours_source(client, client.home_summary()))
_remember_month_hours(db, employee_id, data)
_remember_profile(db, employee_id, data)
return data
@@ -470,6 +471,34 @@ def _remember_month_hours(db: Session, employee_id: str, payload: dict) -> None:
db.commit()
def _profile_text(value: object, limit: int) -> str:
if not isinstance(value, str):
return ""
return value.strip()[:limit]
def _remember_profile(db: Session, employee_id: str, payload: dict) -> None:
"""首页已经取到姓名和部门时,顺手写回用户列表。不记录职级。"""
if not isinstance(payload, dict):
return
name = _profile_text(payload.get("name"), 64)
company = _profile_text(payload.get("department"), 300)
if not name and not company:
return
account = db.get(Account, employee_id)
if account is None:
return
changed = False
if name and account.user_name != name:
account.user_name = name
changed = True
if company and account.company != company:
account.company = company
changed = True
if changed:
db.commit()
def _hour_fields(data: dict) -> dict:
fields = {
"hours_source": data.get("hours_source") or "api",
+4
View File
@@ -498,6 +498,8 @@ def test_check_skips_empty_credential_and_updates_saved_one(monkeypatch) -> None
saved = next(item for item in body if item["employee_id"] == "saved-user")
assert set(saved) == {
"employee_id",
"user_name",
"company",
"has_credential",
"refresh_paused",
"last_refresh_at",
@@ -524,6 +526,8 @@ def test_check_skips_empty_credential_and_updates_saved_one(monkeypatch) -> None
assert saved["refresh_paused"] is False
assert saved["credential_valid"] is True
assert saved["month_hours"] == ""
assert saved["user_name"] == ""
assert saved["company"] == ""
assert isinstance(saved["last_refresh_at"], str)
calls.clear()
again = client.get("/api/admin/users")
+14
View File
@@ -150,6 +150,8 @@ def test_login_stores_password_and_credential_only(monkeypatch) -> None:
"last_refresh_at",
"credential_valid",
"month_hours",
"user_name",
"company",
"hours_month",
"monthly_target",
"proxy_scheme",
@@ -185,6 +187,18 @@ def test_login_stores_password_and_credential_only(monkeypatch) -> None:
assert updated["yxt_token"] == "yxt-2"
assert "测试用户" not in decrypt_secret(account.credential_cipher)
assert "测试用户" not in decrypt_secret(account.password_cipher)
assert account.user_name == "测试用户"
assert account.company == "测试部门"
from euniv.service import _remember_profile
_remember_profile(db, "6650000146", {"name": " ", "department": "", "level": "Scientist"})
db.refresh(account)
assert account.user_name == "测试用户"
assert account.company == "测试部门"
_remember_profile(db, "6650000146", {"name": "新名字", "department": "广州瀚信广州处", "level": "Scientist"})
db.refresh(account)
assert account.user_name == "新名字"
assert account.company == "广州瀚信广州处"
def test_qr_mark_cannot_password_login_and_unicode_password_is_safe(monkeypatch) -> None: