|
|
|
@@ -38,12 +38,12 @@ def admin_accounts(x_admin_token: str = Header(default="", alias="X-Admin-Token"
|
|
|
|
|
"FROM accounts ORDER BY id")]
|
|
|
|
|
for a in rows:
|
|
|
|
|
a["plugins"] = [dict(r)["plugin_id"] for r in conn.execute(
|
|
|
|
|
"SELECT plugin_id FROM account_plugins WHERE account = ?", (a["account"],))]
|
|
|
|
|
"SELECT plugin_id FROM account_plugins WHERE account = %s", (a["account"],))]
|
|
|
|
|
a["grants"] = [dict(r) for r in conn.execute(
|
|
|
|
|
"SELECT plugin_id, can_download, can_run FROM plugin_grants WHERE account = ?",
|
|
|
|
|
"SELECT plugin_id, can_download, can_run FROM account_plugins WHERE account = %s",
|
|
|
|
|
(a["account"],))]
|
|
|
|
|
a["platforms"] = [dict(r)["platform_id"] for r in conn.execute(
|
|
|
|
|
"SELECT platform_id FROM account_platforms WHERE account = ?", (a["account"],))]
|
|
|
|
|
"SELECT platform_id FROM account_platforms WHERE account = %s", (a["account"],))]
|
|
|
|
|
return {"accounts": rows}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -57,13 +57,13 @@ def admin_account_create(a: schemas.AccountCreateIn,
|
|
|
|
|
if not account or not a.password:
|
|
|
|
|
raise HTTPException(400, "account 与 password 必填")
|
|
|
|
|
with _db() as conn:
|
|
|
|
|
exists = conn.execute("SELECT 1 FROM accounts WHERE account = ?", (account,)).fetchone()
|
|
|
|
|
exists = conn.execute("SELECT 1 FROM accounts WHERE account = %s", (account,)).fetchone()
|
|
|
|
|
if exists:
|
|
|
|
|
raise HTTPException(400, f"账号 {account} 已存在")
|
|
|
|
|
_hex, _salt = _hash_password(a.password)
|
|
|
|
|
with _db() as conn:
|
|
|
|
|
conn.execute("INSERT INTO accounts(account, password_hash, salt, is_admin, dept, nickname, "
|
|
|
|
|
"created_at) VALUES (?, ?, ?, ?, ?, ?, ?)",
|
|
|
|
|
"created_at) VALUES (%s, %s, %s, %s, %s, %s, %s)",
|
|
|
|
|
(account, _hex, _salt, int(a.is_admin), (a.dept or "").strip(),
|
|
|
|
|
(a.nickname or "").strip(), _now()))
|
|
|
|
|
conn.commit()
|
|
|
|
@@ -81,26 +81,26 @@ def admin_account_update(a: schemas.AccountUpdateIn,
|
|
|
|
|
if not account:
|
|
|
|
|
raise HTTPException(400, "account 必填")
|
|
|
|
|
with _db() as conn:
|
|
|
|
|
if not conn.execute("SELECT 1 FROM accounts WHERE account = ?", (account,)).fetchone():
|
|
|
|
|
if not conn.execute("SELECT 1 FROM accounts WHERE account = %s", (account,)).fetchone():
|
|
|
|
|
raise HTTPException(400, f"账号 {account} 不存在")
|
|
|
|
|
if a.dept is not None:
|
|
|
|
|
conn.execute("UPDATE accounts SET dept = ? WHERE account = ?",
|
|
|
|
|
conn.execute("UPDATE accounts SET dept = %s WHERE account = %s",
|
|
|
|
|
((a.dept or "").strip(), account))
|
|
|
|
|
if a.nickname is not None:
|
|
|
|
|
conn.execute("UPDATE accounts SET nickname = ? WHERE account = ?",
|
|
|
|
|
conn.execute("UPDATE accounts SET nickname = %s WHERE account = %s",
|
|
|
|
|
((a.nickname or "").strip(), account))
|
|
|
|
|
if a.is_admin is not None:
|
|
|
|
|
# 禁止撤销自己的管理员身份,避免把自己锁在门外
|
|
|
|
|
if a.is_admin:
|
|
|
|
|
conn.execute("UPDATE accounts SET is_admin = 1 WHERE account = ?", (account,))
|
|
|
|
|
conn.execute("UPDATE accounts SET is_admin = 1 WHERE account = %s", (account,))
|
|
|
|
|
else:
|
|
|
|
|
conn.execute("UPDATE accounts SET is_admin = 0 WHERE account = ? AND account <> ?",
|
|
|
|
|
conn.execute("UPDATE accounts SET is_admin = 0 WHERE account = %s AND account <> %s",
|
|
|
|
|
(account, login))
|
|
|
|
|
if a.status is not None:
|
|
|
|
|
# 禁止停用自己,避免把自己锁在门外
|
|
|
|
|
if not a.status and account == login:
|
|
|
|
|
raise HTTPException(400, "不能停用当前登录的账号")
|
|
|
|
|
conn.execute("UPDATE accounts SET status = ? WHERE account = ?",
|
|
|
|
|
conn.execute("UPDATE accounts SET status = %s WHERE account = %s",
|
|
|
|
|
(int(bool(a.status)), account))
|
|
|
|
|
conn.commit()
|
|
|
|
|
return {"ok": True, "account": account}
|
|
|
|
@@ -116,10 +116,10 @@ def admin_account_reset(r: schemas.AccountResetIn,
|
|
|
|
|
if not account or not r.new_password:
|
|
|
|
|
raise HTTPException(400, "account 与新密码必填")
|
|
|
|
|
with _db() as conn:
|
|
|
|
|
if not conn.execute("SELECT 1 FROM accounts WHERE account = ?", (account,)).fetchone():
|
|
|
|
|
if not conn.execute("SELECT 1 FROM accounts WHERE account = %s", (account,)).fetchone():
|
|
|
|
|
raise HTTPException(400, f"账号 {account} 不存在")
|
|
|
|
|
_hex, _salt = _hash_password(r.new_password)
|
|
|
|
|
conn.execute("UPDATE accounts SET password_hash = ?, salt = ? WHERE account = ?",
|
|
|
|
|
conn.execute("UPDATE accounts SET password_hash = %s, salt = %s WHERE account = %s",
|
|
|
|
|
(_hex, _salt, account))
|
|
|
|
|
conn.commit()
|
|
|
|
|
return {"ok": True, "account": account}
|
|
|
|
@@ -137,10 +137,10 @@ def admin_account_delete(d: schemas.AccountDelIn,
|
|
|
|
|
if account == login:
|
|
|
|
|
raise HTTPException(400, "不能删除当前登录的账号")
|
|
|
|
|
with _db() as conn:
|
|
|
|
|
if not conn.execute("SELECT 1 FROM accounts WHERE account = ?", (account,)).fetchone():
|
|
|
|
|
if not conn.execute("SELECT 1 FROM accounts WHERE account = %s", (account,)).fetchone():
|
|
|
|
|
raise HTTPException(400, f"账号 {account} 不存在")
|
|
|
|
|
conn.execute("DELETE FROM accounts WHERE account = ?", (account,))
|
|
|
|
|
conn.execute("DELETE FROM account_plugins WHERE account = ?", (account,))
|
|
|
|
|
# account_plugins / account_platforms 由外键 ON DELETE CASCADE 级联清理
|
|
|
|
|
conn.execute("DELETE FROM accounts WHERE account = %s", (account,))
|
|
|
|
|
conn.commit()
|
|
|
|
|
return {"ok": True, "account": account}
|
|
|
|
|
|
|
|
|
@@ -172,9 +172,9 @@ def admin_avatar_upload(file: UploadFile = File(...),
|
|
|
|
|
if not content or len(content) > 2 * 1024 * 1024:
|
|
|
|
|
raise HTTPException(400, "头像文件为空或超过 2MB")
|
|
|
|
|
with _db() as conn:
|
|
|
|
|
if not conn.execute("SELECT 1 FROM accounts WHERE account = ?", (account,)).fetchone():
|
|
|
|
|
if not conn.execute("SELECT 1 FROM accounts WHERE account = %s", (account,)).fetchone():
|
|
|
|
|
raise HTTPException(400, f"账号 {account} 不存在")
|
|
|
|
|
old = conn.execute("SELECT avatar FROM accounts WHERE account = ?", (account,)).fetchone()
|
|
|
|
|
old = conn.execute("SELECT avatar FROM accounts WHERE account = %s", (account,)).fetchone()
|
|
|
|
|
old_name = (old["avatar"] if old else "") or ""
|
|
|
|
|
config.AVATAR_DIR.mkdir(parents=True, exist_ok=True)
|
|
|
|
|
fname = f"{account}{ext}"
|
|
|
|
@@ -185,7 +185,7 @@ def admin_avatar_upload(file: UploadFile = File(...),
|
|
|
|
|
except OSError:
|
|
|
|
|
pass
|
|
|
|
|
with _db() as conn:
|
|
|
|
|
conn.execute("UPDATE accounts SET avatar = ? WHERE account = ?", (fname, account))
|
|
|
|
|
conn.execute("UPDATE accounts SET avatar = %s WHERE account = %s", (fname, account))
|
|
|
|
|
conn.commit()
|
|
|
|
|
return {"ok": True, "account": account, "avatar": fname}
|
|
|
|
|
|
|
|
|
@@ -194,7 +194,7 @@ def admin_avatar_upload(file: UploadFile = File(...),
|
|
|
|
|
def admin_avatar_get(account: str) -> FileResponse:
|
|
|
|
|
"""读取账号头像(前端 img 直接引用;未设置返回 404)。"""
|
|
|
|
|
with _db() as conn:
|
|
|
|
|
row = conn.execute("SELECT avatar FROM accounts WHERE account = ?", (account,)).fetchone()
|
|
|
|
|
row = conn.execute("SELECT avatar FROM accounts WHERE account = %s", (account,)).fetchone()
|
|
|
|
|
if not row or not row["avatar"]:
|
|
|
|
|
raise HTTPException(404, "未设置头像")
|
|
|
|
|
path = config.AVATAR_DIR / row["avatar"]
|
|
|
|
@@ -208,7 +208,11 @@ def admin_avatar_get(account: str) -> FileResponse:
|
|
|
|
|
def admin_grant(g: schemas.GrantIn,
|
|
|
|
|
x_admin_token: str = Header(default="", alias="X-Admin-Token"),
|
|
|
|
|
x_api_token: str = Header(default="", alias="X-API-Token")) -> dict:
|
|
|
|
|
"""授予/撤销某账号对某插件的权限。granted=True 授,False 撤;同时维护插件级授权表。"""
|
|
|
|
|
"""授予/撤销某账号对某插件的权限。granted=True 授,False 撤。
|
|
|
|
|
|
|
|
|
|
MySQL 版合并表:权限位(can_download/can_run)即存在 account_plugins 本身,
|
|
|
|
|
不再有独立 plugin_grants 表;撤销即删除 account_plugins 行。
|
|
|
|
|
"""
|
|
|
|
|
_require_admin(x_admin_token, x_api_token)
|
|
|
|
|
account = g.account.strip()
|
|
|
|
|
plugin_id = g.plugin_id.strip()
|
|
|
|
@@ -216,21 +220,18 @@ def admin_grant(g: schemas.GrantIn,
|
|
|
|
|
raise HTTPException(400, "account 与 plugin_id 不能为空")
|
|
|
|
|
with _db() as conn:
|
|
|
|
|
if g.granted:
|
|
|
|
|
if not conn.execute("SELECT 1 FROM accounts WHERE account = ?", (account,)).fetchone():
|
|
|
|
|
if not conn.execute("SELECT 1 FROM accounts WHERE account = %s", (account,)).fetchone():
|
|
|
|
|
raise HTTPException(400, f"账号 {account} 不存在,请先建号")
|
|
|
|
|
if not conn.execute("SELECT 1 FROM plugins WHERE id = %s", (plugin_id,)).fetchone():
|
|
|
|
|
raise HTTPException(400, f"插件 {plugin_id} 不存在,请先上传发布")
|
|
|
|
|
conn.execute(
|
|
|
|
|
"INSERT OR IGNORE INTO account_plugins(account, plugin_id) VALUES (?, ?)",
|
|
|
|
|
(account, plugin_id))
|
|
|
|
|
conn.execute(
|
|
|
|
|
"INSERT INTO plugin_grants(account, plugin_id, can_download, can_run) "
|
|
|
|
|
"VALUES (?, ?, ?, ?) "
|
|
|
|
|
"ON CONFLICT(account, plugin_id) DO UPDATE SET "
|
|
|
|
|
"can_download = excluded.can_download, can_run = excluded.can_run",
|
|
|
|
|
"INSERT INTO account_plugins(account, plugin_id, can_download, can_run) "
|
|
|
|
|
"VALUES (%s, %s, %s, %s) "
|
|
|
|
|
"ON DUPLICATE KEY UPDATE can_download = VALUES(can_download), "
|
|
|
|
|
"can_run = VALUES(can_run)",
|
|
|
|
|
(account, plugin_id, int(g.can_download), int(g.can_run)))
|
|
|
|
|
else:
|
|
|
|
|
conn.execute("DELETE FROM account_plugins WHERE account = ? AND plugin_id = ?",
|
|
|
|
|
(account, plugin_id))
|
|
|
|
|
conn.execute("DELETE FROM plugin_grants WHERE account = ? AND plugin_id = ?",
|
|
|
|
|
conn.execute("DELETE FROM account_plugins WHERE account = %s AND plugin_id = %s",
|
|
|
|
|
(account, plugin_id))
|
|
|
|
|
conn.commit()
|
|
|
|
|
_audit(account, "grant" if g.granted else "revoke-grant", plugin_id,
|
|
|
|
@@ -250,13 +251,13 @@ def admin_platform_grant(g: schemas.PlatformGrantIn,
|
|
|
|
|
raise HTTPException(400, "account 与 platform_id 不能为空")
|
|
|
|
|
with _db() as conn:
|
|
|
|
|
if g.granted:
|
|
|
|
|
if not conn.execute("SELECT 1 FROM accounts WHERE account = ?", (account,)).fetchone():
|
|
|
|
|
if not conn.execute("SELECT 1 FROM accounts WHERE account = %s", (account,)).fetchone():
|
|
|
|
|
raise HTTPException(400, f"账号 {account} 不存在,请先建号")
|
|
|
|
|
conn.execute(
|
|
|
|
|
"INSERT OR IGNORE INTO account_platforms(account, platform_id) VALUES (?, ?)",
|
|
|
|
|
"INSERT IGNORE INTO account_platforms(account, platform_id) VALUES (%s, %s)",
|
|
|
|
|
(account, platform_id))
|
|
|
|
|
else:
|
|
|
|
|
conn.execute("DELETE FROM account_platforms WHERE account = ? AND platform_id = ?",
|
|
|
|
|
conn.execute("DELETE FROM account_platforms WHERE account = %s AND platform_id = %s",
|
|
|
|
|
(account, platform_id))
|
|
|
|
|
conn.commit()
|
|
|
|
|
return {"ok": True, "account": account, "platform_id": platform_id, "granted": g.granted}
|
|
|
|
@@ -301,11 +302,11 @@ async def admin_upload_plugin(upload: UploadFile = File(...),
|
|
|
|
|
with _db() as conn:
|
|
|
|
|
conn.execute(
|
|
|
|
|
"INSERT INTO plugins(id, name, latest_version, description, category, icon, manifest) "
|
|
|
|
|
"VALUES (?, ?, ?, ?, ?, ?, ?) ON CONFLICT(id) DO UPDATE SET "
|
|
|
|
|
"name = excluded.name, manifest = excluded.manifest"
|
|
|
|
|
+ (", description = excluded.description" if manifest else "")
|
|
|
|
|
+ (", category = excluded.category" if manifest else "")
|
|
|
|
|
+ (", icon = excluded.icon" if manifest else ""),
|
|
|
|
|
"VALUES (%s, %s, %s, %s, %s, %s, %s) ON DUPLICATE KEY UPDATE "
|
|
|
|
|
"name = VALUES(name), manifest = VALUES(manifest)"
|
|
|
|
|
+ (", description = VALUES(description)" if manifest else "")
|
|
|
|
|
+ (", category = VALUES(category)" if manifest else "")
|
|
|
|
|
+ (", icon = VALUES(icon)" if manifest else ""),
|
|
|
|
|
(plugin_id, name, version,
|
|
|
|
|
(manifest or {}).get("description", ""),
|
|
|
|
|
(manifest or {}).get("category", ""),
|
|
|
|
@@ -313,12 +314,12 @@ async def admin_upload_plugin(upload: UploadFile = File(...),
|
|
|
|
|
manifest_json))
|
|
|
|
|
conn.execute(
|
|
|
|
|
"INSERT INTO plugin_versions(plugin_id, version, filename, uploaded_at, sha256) "
|
|
|
|
|
"VALUES (?, ?, ?, ?, ?) ON CONFLICT(plugin_id, version) DO UPDATE SET "
|
|
|
|
|
"filename = excluded.filename, uploaded_at = excluded.uploaded_at, sha256 = excluded.sha256",
|
|
|
|
|
"VALUES (%s, %s, %s, %s, %s) ON DUPLICATE KEY UPDATE "
|
|
|
|
|
"filename = VALUES(filename), uploaded_at = VALUES(uploaded_at), sha256 = VALUES(sha256)",
|
|
|
|
|
(plugin_id, version, filename, _now(), pkg_sha256))
|
|
|
|
|
cur = conn.execute("SELECT latest_version FROM plugins WHERE id = ?", (plugin_id,))
|
|
|
|
|
cur = conn.execute("SELECT latest_version FROM plugins WHERE id = %s", (plugin_id,))
|
|
|
|
|
if _version_key(cur.fetchone()["latest_version"]) < _version_key(version):
|
|
|
|
|
conn.execute("UPDATE plugins SET latest_version = ? WHERE id = ?", (version, plugin_id))
|
|
|
|
|
conn.execute("UPDATE plugins SET latest_version = %s WHERE id = %s", (version, plugin_id))
|
|
|
|
|
stale_files = _prune_versions(conn, plugin_id)
|
|
|
|
|
conn.commit()
|
|
|
|
|
for fn in stale_files: # 超出保留数的旧版本:清 DB 记录 + 清磁盘包
|
|
|
|
@@ -339,13 +340,14 @@ def admin_plugin_delete(d: schemas.PluginDelIn,
|
|
|
|
|
if not plugin_id:
|
|
|
|
|
raise HTTPException(400, "plugin_id 必填")
|
|
|
|
|
with _db() as conn:
|
|
|
|
|
if not conn.execute("SELECT 1 FROM plugins WHERE id = ?", (plugin_id,)).fetchone():
|
|
|
|
|
if not conn.execute("SELECT 1 FROM plugins WHERE id = %s", (plugin_id,)).fetchone():
|
|
|
|
|
raise HTTPException(400, f"插件 {plugin_id} 不存在")
|
|
|
|
|
files = [dict(r)["filename"] for r in conn.execute(
|
|
|
|
|
"SELECT filename FROM plugin_versions WHERE plugin_id = ?", (plugin_id,))]
|
|
|
|
|
conn.execute("DELETE FROM plugin_versions WHERE plugin_id = ?", (plugin_id,))
|
|
|
|
|
conn.execute("DELETE FROM plugins WHERE id = ?", (plugin_id,))
|
|
|
|
|
conn.execute("DELETE FROM account_plugins WHERE plugin_id = ?", (plugin_id,))
|
|
|
|
|
"SELECT filename FROM plugin_versions WHERE plugin_id = %s", (plugin_id,))]
|
|
|
|
|
# 外键 CASCADE 会连带清理 plugin_versions / account_plugins,显式删除仅作兜底保序
|
|
|
|
|
conn.execute("DELETE FROM account_plugins WHERE plugin_id = %s", (plugin_id,))
|
|
|
|
|
conn.execute("DELETE FROM plugin_versions WHERE plugin_id = %s", (plugin_id,))
|
|
|
|
|
conn.execute("DELETE FROM plugins WHERE id = %s", (plugin_id,))
|
|
|
|
|
conn.commit()
|
|
|
|
|
for fn in files:
|
|
|
|
|
try:
|
|
|
|
@@ -373,19 +375,19 @@ def admin_plugin_patch(d: schemas.PluginPatchIn,
|
|
|
|
|
fields = []
|
|
|
|
|
vals = []
|
|
|
|
|
if d.name is not None and d.name.strip():
|
|
|
|
|
fields.append("name = ?"); vals.append(d.name.strip())
|
|
|
|
|
fields.append("name = %s"); vals.append(d.name.strip())
|
|
|
|
|
if d.description:
|
|
|
|
|
fields.append("description = ?"); vals.append(d.description.strip())
|
|
|
|
|
fields.append("description = %s"); vals.append(d.description.strip())
|
|
|
|
|
if d.category:
|
|
|
|
|
fields.append("category = ?"); vals.append(d.category.strip())
|
|
|
|
|
fields.append("category = %s"); vals.append(d.category.strip())
|
|
|
|
|
if d.icon:
|
|
|
|
|
fields.append("icon = ?"); vals.append(d.icon.strip())
|
|
|
|
|
fields.append("icon = %s"); vals.append(d.icon.strip())
|
|
|
|
|
if d.status in ("published", "disabled", "revoked"):
|
|
|
|
|
fields.append("status = ?"); vals.append(d.status)
|
|
|
|
|
fields.append("status = %s"); vals.append(d.status)
|
|
|
|
|
# 强制更新三态:成对开启 / clear_force 清除 / 仅给其一报错
|
|
|
|
|
if d.force_min_version and d.force_deadline:
|
|
|
|
|
fields.append("force_min_version = ?"); vals.append(d.force_min_version.strip())
|
|
|
|
|
fields.append("force_deadline = ?"); vals.append(d.force_deadline.strip())
|
|
|
|
|
fields.append("force_min_version = %s"); vals.append(d.force_min_version.strip())
|
|
|
|
|
fields.append("force_deadline = %s"); vals.append(d.force_deadline.strip())
|
|
|
|
|
elif (d.force_min_version or d.force_deadline) and not d.clear_force:
|
|
|
|
|
raise HTTPException(400, "强制更新需同时提供 force_min_version 与 force_deadline")
|
|
|
|
|
elif d.clear_force:
|
|
|
|
@@ -395,9 +397,9 @@ def admin_plugin_patch(d: schemas.PluginPatchIn,
|
|
|
|
|
return {"ok": True} # 无更新项
|
|
|
|
|
vals.append(pid)
|
|
|
|
|
with _db() as conn:
|
|
|
|
|
if not conn.execute("SELECT 1 FROM plugins WHERE id = ?", (pid,)).fetchone():
|
|
|
|
|
if not conn.execute("SELECT 1 FROM plugins WHERE id = %s", (pid,)).fetchone():
|
|
|
|
|
raise HTTPException(400, f"插件 {pid} 不存在")
|
|
|
|
|
conn.execute(f"UPDATE plugins SET {', '.join(fields)} WHERE id = ?", vals)
|
|
|
|
|
conn.execute(f"UPDATE plugins SET {', '.join(fields)} WHERE id = %s", vals)
|
|
|
|
|
conn.commit()
|
|
|
|
|
_audit("", "plugin-patch", pid, ", ".join(
|
|
|
|
|
f"{k.split(' ')[0]}={v}" for k, v in zip(fields, vals[:-1])))
|
|
|
|
@@ -424,27 +426,27 @@ def admin_overview(x_admin_token: str = Header(default="", alias="X-Admin-Token"
|
|
|
|
|
p["field_count"] = len(props)
|
|
|
|
|
p["versions"] = [dict(r) for r in conn.execute(
|
|
|
|
|
"SELECT version, filename, sha256, uploaded_at FROM plugin_versions "
|
|
|
|
|
"WHERE plugin_id = ? ORDER BY version", (p["id"],))]
|
|
|
|
|
"WHERE plugin_id = %s ORDER BY version", (p["id"],))]
|
|
|
|
|
p["accounts"] = [dict(r)["account"] for r in conn.execute(
|
|
|
|
|
"SELECT account FROM account_plugins WHERE plugin_id = ?", (p["id"],))]
|
|
|
|
|
"SELECT account FROM account_plugins WHERE plugin_id = %s", (p["id"],))]
|
|
|
|
|
p["grants"] = {dict(r)["account"]: {"canDownload": bool(dict(r)["can_download"]),
|
|
|
|
|
"canRun": bool(dict(r)["can_run"])}
|
|
|
|
|
for r in conn.execute(
|
|
|
|
|
"SELECT account, can_download, can_run FROM plugin_grants "
|
|
|
|
|
"WHERE plugin_id = ?", (p["id"],))}
|
|
|
|
|
"SELECT account, can_download, can_run FROM account_plugins "
|
|
|
|
|
"WHERE plugin_id = %s", (p["id"],))}
|
|
|
|
|
accounts = [dict(r) for r in conn.execute(
|
|
|
|
|
"SELECT id, account, is_admin, dept, nickname, avatar, status, created_at "
|
|
|
|
|
"FROM accounts ORDER BY id")]
|
|
|
|
|
for a in accounts:
|
|
|
|
|
a["plugins"] = [dict(r)["plugin_id"] for r in conn.execute(
|
|
|
|
|
"SELECT plugin_id FROM account_plugins WHERE account = ?", (a["account"],))]
|
|
|
|
|
"SELECT plugin_id FROM account_plugins WHERE account = %s", (a["account"],))]
|
|
|
|
|
a["platforms"] = [dict(r)["platform_id"] for r in conn.execute(
|
|
|
|
|
"SELECT platform_id FROM account_platforms WHERE account = ?", (a["account"],))]
|
|
|
|
|
"SELECT platform_id FROM account_platforms WHERE account = %s", (a["account"],))]
|
|
|
|
|
a["grants"] = {dict(r)["plugin_id"]: {"canDownload": bool(dict(r)["can_download"]),
|
|
|
|
|
"canRun": bool(dict(r)["can_run"])}
|
|
|
|
|
for r in conn.execute(
|
|
|
|
|
"SELECT plugin_id, can_download, can_run FROM plugin_grants "
|
|
|
|
|
"WHERE account = ?", (a["account"],))}
|
|
|
|
|
"SELECT plugin_id, can_download, can_run FROM account_plugins "
|
|
|
|
|
"WHERE account = %s", (a["account"],))}
|
|
|
|
|
admins = [dict(r)["account"] for r in conn.execute(
|
|
|
|
|
"SELECT account FROM accounts WHERE is_admin = 1 ORDER BY account")]
|
|
|
|
|
return {"plugins": plugins, "accounts": accounts, "admins": admins}
|
|
|
|
@@ -475,11 +477,11 @@ def admin_stats(x_admin_token: str = Header(default="", alias="X-Admin-Token"),
|
|
|
|
|
|
|
|
|
|
# 近 14 天运行趋势(按日:触发次数 / 成功 / 失败)
|
|
|
|
|
trend_rows = conn.execute(
|
|
|
|
|
"SELECT substr(at,1,10) AS day, "
|
|
|
|
|
"SELECT LEFT(at,10) AS day, "
|
|
|
|
|
" SUM(CASE WHEN status = 'start' THEN 1 ELSE 0 END) AS runs, "
|
|
|
|
|
" SUM(CASE WHEN status IN ('done','success') THEN 1 ELSE 0 END) AS success, "
|
|
|
|
|
" SUM(CASE WHEN status IN ('error','failed') THEN 1 ELSE 0 END) AS failed "
|
|
|
|
|
"FROM runtime_reports WHERE at >= date('now','-13 days') GROUP BY day ORDER BY day"
|
|
|
|
|
"FROM runtime_reports WHERE at >= CURDATE() - INTERVAL 13 DAY GROUP BY day ORDER BY day"
|
|
|
|
|
).fetchall()
|
|
|
|
|
trend_map = {r["day"]: r for r in trend_rows}
|
|
|
|
|
trend = []
|
|
|
|
@@ -553,7 +555,7 @@ def admin_audit(limit: int = Query(default=200, ge=1, le=1000),
|
|
|
|
|
"SELECT account, nickname FROM accounts")}
|
|
|
|
|
rows = [dict(r) for r in conn.execute(
|
|
|
|
|
"SELECT id, account, action, target, detail, at FROM audit_logs "
|
|
|
|
|
"ORDER BY id DESC LIMIT ?", (limit,))]
|
|
|
|
|
"ORDER BY id DESC LIMIT %s", (limit,))]
|
|
|
|
|
for r in rows:
|
|
|
|
|
r["nickname"] = nick.get(r["account"], "")
|
|
|
|
|
return {"auditlogs": rows}
|
|
|
|
@@ -570,12 +572,26 @@ def admin_reports(limit: int = Query(default=200, ge=1, le=1000),
|
|
|
|
|
"SELECT account, nickname FROM accounts")}
|
|
|
|
|
rows = [dict(r) for r in conn.execute(
|
|
|
|
|
"SELECT id, account, device_id, plugin_id, version, status, msg, duration_sec, at "
|
|
|
|
|
"FROM runtime_reports ORDER BY id DESC LIMIT ?", (limit,))]
|
|
|
|
|
"FROM runtime_reports ORDER BY id DESC LIMIT %s", (limit,))]
|
|
|
|
|
for r in rows:
|
|
|
|
|
r["nickname"] = nick.get(r["account"], "")
|
|
|
|
|
return {"reports": rows}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.get("/api/admin/devices")
|
|
|
|
|
def admin_devices(limit: int = Query(default=200, ge=1, le=1000),
|
|
|
|
|
x_admin_token: str = Header(default="", alias="X-Admin-Token"),
|
|
|
|
|
x_api_token: str = Header(default="", alias="X-API-Token")) -> dict:
|
|
|
|
|
"""执行机设备列表(按最近心跳倒序):环境信息 + Chrome 检测结果。"""
|
|
|
|
|
_require_admin(x_admin_token, x_api_token)
|
|
|
|
|
with _db() as conn:
|
|
|
|
|
rows = [dict(r) for r in conn.execute(
|
|
|
|
|
"SELECT device_id, account, hostname, os, ip, chrome_installed, chrome_version, "
|
|
|
|
|
" chrome_ok, last_seen, status, created_at, updated_at "
|
|
|
|
|
"FROM devices ORDER BY last_seen DESC LIMIT %s", (limit,))]
|
|
|
|
|
return {"devices": rows}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.post("/api/admin/shell-release")
|
|
|
|
|
async def admin_shell_release(upload: UploadFile = File(...),
|
|
|
|
|
version: str = Query(...),
|
|
|
|
|