feat: 增加管理后台的会话、配置和用户管理
This commit is contained in:
+120
-28
@@ -63,6 +63,7 @@ def login_and_store(
|
||||
password: str,
|
||||
client_factory=EunivClient,
|
||||
manual: bool = False,
|
||||
auto_attempts: int = 3,
|
||||
) -> dict:
|
||||
_purge_pending()
|
||||
account = db.get(Account, employee_id)
|
||||
@@ -74,7 +75,7 @@ def login_and_store(
|
||||
held = False
|
||||
try:
|
||||
try:
|
||||
profile = client.login(manual=manual)
|
||||
profile = client.login(manual=manual, auto_attempts=auto_attempts)
|
||||
except ManualJigsaw as exc:
|
||||
attempt_id = secrets.token_urlsafe(18)
|
||||
_pending[attempt_id] = PendingLogin(client, employee_id, password, time.time())
|
||||
@@ -133,25 +134,32 @@ def fetch_training_detail(db: Session, employee_id: str, path: str, client_facto
|
||||
return _live(db, employee_id, client_factory, lambda client: client.training_detail(path))
|
||||
|
||||
|
||||
def _auto_attempts(db: Session) -> int:
|
||||
from admin.settings import auto_attempt_count
|
||||
|
||||
return auto_attempt_count(db)
|
||||
|
||||
|
||||
def _live(db: Session, employee_id: str, client_factory, read):
|
||||
account = db.get(Account, employee_id)
|
||||
if account is None:
|
||||
raise RuntimeError("请先登录")
|
||||
credential = json.loads(decrypt_secret(account.credential_cipher)) if account.credential_cipher else {}
|
||||
attempts = _auto_attempts(db)
|
||||
client = client_factory(employee_id, decrypt_secret(account.password_cipher))
|
||||
try:
|
||||
if credential.get("yxt_token"):
|
||||
client.restore(credential)
|
||||
else:
|
||||
try:
|
||||
client.authorize(client.login())
|
||||
client.authorize(client.login(manual=False, auto_attempts=attempts))
|
||||
except ManualJigsaw:
|
||||
raise RuntimeError("登录需要重新验证,请重新登录") from None
|
||||
try:
|
||||
data = read(client)
|
||||
except SessionExpired:
|
||||
try:
|
||||
client.authorize(client.login())
|
||||
client.authorize(client.login(manual=False, auto_attempts=attempts))
|
||||
except ManualJigsaw:
|
||||
raise RuntimeError("登录需要重新验证,请重新登录") from None
|
||||
data = read(client)
|
||||
@@ -173,40 +181,124 @@ def revoke_credential(db: Session, employee_id: str) -> None:
|
||||
|
||||
|
||||
def refresh_stored_credentials(client_factory=EunivClient) -> None:
|
||||
"""逐个刷新。先只取出工号,再每人单独打开会话,避免把全部凭证同时放进内存。"""
|
||||
from admin.settings import auto_attempt_count
|
||||
from euniv.db import SessionLocal
|
||||
|
||||
with SessionLocal() as db:
|
||||
accounts = list(db.scalars(select(Account)))
|
||||
for account in accounts:
|
||||
if not account.credential_cipher:
|
||||
continue
|
||||
client = client_factory(account.employee_id, decrypt_secret(account.password_cipher))
|
||||
try:
|
||||
previous = json.loads(decrypt_secret(account.credential_cipher))
|
||||
client.restore(previous)
|
||||
if client.credential_alive():
|
||||
updated = client.export_credential()
|
||||
if _dump(updated) != _dump(previous):
|
||||
account.credential_cipher = encrypt_secret(_dump(updated))
|
||||
db.commit()
|
||||
continue
|
||||
profile = client.login()
|
||||
_persist(db, account.employee_id, decrypt_secret(account.password_cipher), client, profile)
|
||||
except Exception:
|
||||
continue
|
||||
finally:
|
||||
client.close()
|
||||
attempts = auto_attempt_count(db)
|
||||
employee_ids = list(db.scalars(select(Account.employee_id).order_by(Account.employee_id)))
|
||||
for employee_id in employee_ids:
|
||||
_refresh_one(employee_id, attempts, client_factory)
|
||||
|
||||
|
||||
def start_credential_refresh(interval_seconds: int = 1800) -> threading.Event:
|
||||
stop = threading.Event()
|
||||
def _refresh_one(employee_id: str, attempts: int, client_factory) -> None:
|
||||
from admin.users import record_outcome
|
||||
from euniv.db import SessionLocal
|
||||
|
||||
with SessionLocal() as db:
|
||||
account = db.get(Account, employee_id)
|
||||
if account is None or account.refresh_paused or not account.credential_cipher:
|
||||
return
|
||||
client = None
|
||||
valid = False
|
||||
try:
|
||||
password = decrypt_secret(account.password_cipher)
|
||||
previous = json.loads(decrypt_secret(account.credential_cipher))
|
||||
client = client_factory(employee_id, password)
|
||||
client.restore(previous)
|
||||
if client.credential_alive():
|
||||
updated = client.export_credential()
|
||||
if _dump(updated) != _dump(previous):
|
||||
account.credential_cipher = encrypt_secret(_dump(updated))
|
||||
valid = True
|
||||
else:
|
||||
profile = client.login(manual=False, auto_attempts=attempts)
|
||||
_persist(db, employee_id, password, client, profile)
|
||||
valid = True
|
||||
except Exception:
|
||||
db.rollback()
|
||||
valid = False
|
||||
account = db.get(Account, employee_id)
|
||||
if account is None:
|
||||
return
|
||||
finally:
|
||||
if client is not None:
|
||||
try:
|
||||
client.close()
|
||||
except Exception:
|
||||
pass
|
||||
record_outcome(account, valid)
|
||||
db.commit()
|
||||
|
||||
|
||||
class _RefreshGate:
|
||||
"""停止或变更间隔时立刻结束当前等待。"""
|
||||
|
||||
def __init__(self) -> None:
|
||||
self._event = threading.Event()
|
||||
self._lock = threading.Lock()
|
||||
self._reason = ""
|
||||
|
||||
def set(self) -> None:
|
||||
with self._lock:
|
||||
self._reason = "stop"
|
||||
self._event.set()
|
||||
|
||||
def wake(self) -> None:
|
||||
with self._lock:
|
||||
if self._reason != "stop":
|
||||
self._reason = "wake"
|
||||
self._event.set()
|
||||
|
||||
def wait(self, seconds: float) -> str:
|
||||
self._event.wait(timeout=max(0.0, seconds))
|
||||
with self._lock:
|
||||
reason = self._reason
|
||||
if reason == "stop":
|
||||
return "stop"
|
||||
if reason == "wake":
|
||||
self._reason = ""
|
||||
self._event.clear()
|
||||
return "wake"
|
||||
return "timeout"
|
||||
|
||||
|
||||
_gate: _RefreshGate | None = None
|
||||
|
||||
|
||||
def _refresh_interval_seconds() -> int:
|
||||
from admin.settings import refresh_interval_seconds
|
||||
|
||||
return refresh_interval_seconds()
|
||||
|
||||
|
||||
def notify_refresh_interval_changed() -> None:
|
||||
"""保存新间隔后打断等待。这次唤醒只重新计时,不立刻刷新。"""
|
||||
if _gate is not None:
|
||||
_gate.wake()
|
||||
|
||||
|
||||
def start_credential_refresh() -> _RefreshGate:
|
||||
global _gate
|
||||
gate = _RefreshGate()
|
||||
_gate = gate
|
||||
|
||||
def loop() -> None:
|
||||
while not stop.wait(interval_seconds):
|
||||
refresh_stored_credentials()
|
||||
while True:
|
||||
seconds = _refresh_interval_seconds()
|
||||
reason = gate.wait(seconds)
|
||||
if reason == "stop":
|
||||
return
|
||||
if reason == "wake":
|
||||
continue
|
||||
try:
|
||||
refresh_stored_credentials()
|
||||
except Exception:
|
||||
continue
|
||||
|
||||
threading.Thread(target=loop, name="euniv-credential-refresh", daemon=True).start()
|
||||
return stop
|
||||
return gate
|
||||
|
||||
|
||||
def stored_password(account: Account) -> str:
|
||||
|
||||
Reference in New Issue
Block a user