feat: 管理端用户列表显示姓名和公司
This commit is contained in:
@@ -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")
|
||||
@@ -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("''"))
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -9,6 +9,8 @@ import { isRecord, textOf } from "../../shared/text";
|
||||
|
||||
interface UserRow {
|
||||
employee_id: string;
|
||||
user_name: string;
|
||||
company: string;
|
||||
has_credential: boolean;
|
||||
refresh_paused: boolean;
|
||||
last_refresh_at: string | null;
|
||||
@@ -164,6 +166,8 @@ function asUser(value: unknown): UserRow | null {
|
||||
if (!isRecord(value) || (typeof value.employee_id !== "string" && typeof value.employee_id !== "number")) return null;
|
||||
return {
|
||||
employee_id: String(value.employee_id),
|
||||
user_name: textOf(value.user_name),
|
||||
company: textOf(value.company),
|
||||
has_credential: value.has_credential === true,
|
||||
refresh_paused: value.refresh_paused === true,
|
||||
last_refresh_at: typeof value.last_refresh_at === "string" && value.last_refresh_at ? value.last_refresh_at : null,
|
||||
@@ -892,6 +896,8 @@ onMounted(() => {
|
||||
<n-checkbox aria-label="选择全部账号" :checked="allPicked" :indeterminate="somePicked" @update:checked="toggleAll" />
|
||||
</th>
|
||||
<th>工号</th>
|
||||
<th>用户名称</th>
|
||||
<th>公司</th>
|
||||
<th>凭据</th>
|
||||
<th>有效</th>
|
||||
<th>本月学时</th>
|
||||
@@ -908,6 +914,8 @@ onMounted(() => {
|
||||
<n-checkbox aria-label="选择该账号" :checked="Boolean(picked[row.employee_id])" @update:checked="(checked: boolean) => (picked = { ...picked, [row.employee_id]: checked })" />
|
||||
</td>
|
||||
<td class="employee">{{ row.employee_id }}</td>
|
||||
<td class="name">{{ row.user_name || "—" }}</td>
|
||||
<td class="company">{{ row.company || "—" }}</td>
|
||||
<td>{{ row.has_credential ? "已保存凭据" : "未保存凭据" }}</td>
|
||||
<td :class="validityClass(validityText(row))">{{ validityText(row) }}</td>
|
||||
<td class="when">{{ monthHoursText(row) }}</td>
|
||||
|
||||
+6
-1
@@ -1569,7 +1569,7 @@ select {
|
||||
}
|
||||
|
||||
.admin-table { width: 100%;
|
||||
min-width: 820px;
|
||||
min-width: 980px;
|
||||
border-collapse: collapse;
|
||||
}
|
||||
|
||||
@@ -1597,11 +1597,16 @@ select {
|
||||
}
|
||||
|
||||
.admin-table .employee,
|
||||
.admin-table .name,
|
||||
.admin-table .when {
|
||||
font-variant-numeric: tabular-nums;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.admin-table .company {
|
||||
max-width: 240px;
|
||||
}
|
||||
|
||||
.admin-table .ok {
|
||||
color: var(--accent);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user