feat: add admin social login bindings
This commit is contained in:
101
db/admin.py
101
db/admin.py
@@ -245,6 +245,107 @@ def update_admin_username(old_username: str, new_username: str) -> bool:
|
||||
return False
|
||||
|
||||
|
||||
# ==================== 管理员聚合登录绑定 ====================
|
||||
|
||||
|
||||
def find_admin_social_login_binding_by_identity(provider: str, social_uid: str):
|
||||
with db_pool.get_db() as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(
|
||||
"SELECT * FROM admin_social_login_bindings WHERE provider = ? AND social_uid = ?",
|
||||
(provider, social_uid),
|
||||
)
|
||||
row = cursor.fetchone()
|
||||
return dict(row) if row else None
|
||||
|
||||
|
||||
def find_admin_social_login_binding(admin_id: int, provider: str):
|
||||
with db_pool.get_db() as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(
|
||||
"SELECT * FROM admin_social_login_bindings WHERE admin_id = ? AND provider = ?",
|
||||
(int(admin_id), provider),
|
||||
)
|
||||
row = cursor.fetchone()
|
||||
return dict(row) if row else None
|
||||
|
||||
|
||||
def upsert_admin_social_login_binding(*, admin_id: int, provider: str, social_uid: str, nickname: str = "", avatar_url: str = ""):
|
||||
with db_pool.get_db() as conn:
|
||||
cursor = conn.cursor()
|
||||
now = get_cst_now_str()
|
||||
try:
|
||||
cursor.execute(
|
||||
"""
|
||||
INSERT INTO admin_social_login_bindings (
|
||||
admin_id, provider, social_uid, nickname, avatar_url, created_at, updated_at, last_login_at
|
||||
)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
||||
ON CONFLICT(admin_id, provider) DO UPDATE SET
|
||||
social_uid = excluded.social_uid,
|
||||
nickname = excluded.nickname,
|
||||
avatar_url = excluded.avatar_url,
|
||||
updated_at = excluded.updated_at,
|
||||
last_login_at = excluded.last_login_at
|
||||
""",
|
||||
(
|
||||
int(admin_id),
|
||||
provider,
|
||||
social_uid,
|
||||
str(nickname or "")[:128],
|
||||
str(avatar_url or "")[:512],
|
||||
now,
|
||||
now,
|
||||
now,
|
||||
),
|
||||
)
|
||||
conn.commit()
|
||||
except sqlite3.IntegrityError:
|
||||
conn.rollback()
|
||||
return None
|
||||
return find_admin_social_login_binding(admin_id, provider)
|
||||
|
||||
|
||||
def update_admin_social_login_binding_profile(binding_id: int, *, nickname: str = "", avatar_url: str = "") -> bool:
|
||||
with db_pool.get_db() as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(
|
||||
"""
|
||||
UPDATE admin_social_login_bindings
|
||||
SET nickname = ?, avatar_url = ?, updated_at = ?, last_login_at = ?
|
||||
WHERE id = ?
|
||||
""",
|
||||
(str(nickname or "")[:128], str(avatar_url or "")[:512], get_cst_now_str(), get_cst_now_str(), int(binding_id)),
|
||||
)
|
||||
conn.commit()
|
||||
return cursor.rowcount > 0
|
||||
|
||||
|
||||
def list_admin_social_login_bindings(admin_id: int) -> list[dict]:
|
||||
with db_pool.get_db() as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(
|
||||
"""
|
||||
SELECT * FROM admin_social_login_bindings
|
||||
WHERE admin_id = ?
|
||||
ORDER BY created_at ASC
|
||||
""",
|
||||
(int(admin_id),),
|
||||
)
|
||||
return [dict(row) for row in cursor.fetchall()]
|
||||
|
||||
|
||||
def delete_admin_social_login_binding(admin_id: int, provider: str) -> bool:
|
||||
with db_pool.get_db() as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(
|
||||
"DELETE FROM admin_social_login_bindings WHERE admin_id = ? AND provider = ?",
|
||||
(int(admin_id), provider),
|
||||
)
|
||||
conn.commit()
|
||||
return cursor.rowcount > 0
|
||||
|
||||
|
||||
def get_system_stats() -> dict:
|
||||
"""获取系统统计信息"""
|
||||
with db_pool.get_db() as conn:
|
||||
|
||||
Reference in New Issue
Block a user